Monaco Language Server Types Converter

0.4.0 · active · verified Sun Apr 19

monaco-languageserver-types provides a utility layer for converting data structures between the Language Server Protocol (LSP) types and the Monaco Editor's native types. This is crucial for integrating language servers, which typically communicate using LSP, with the Monaco Editor frontend. The current stable version is 0.4.0, with a release cadence that appears to be driven by feature additions and bug fixes, often addressing compatibility with newer LSP specifications or Monaco Editor versions. Its key differentiator is providing a comprehensive, idiomatic TypeScript API for bidirectional type mapping, simplifying the development of language features within Monaco by abstracting away manual type transformations and reducing boilerplate code for common LSP operations like diagnostics, completion, and hover information.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates converting an LSP Diagnostic to a Monaco Editor Marker and a Monaco Position back to an LSP Position using the library's utility functions.

import * as monaco from 'monaco-editor';
import type * as lsp from 'vscode-languageserver-types';
import { toMonaco, fromMonaco } from 'monaco-languageserver-types';

// Assume 'monaco' is globally available or correctly imported if using ESM
// This example simulates converting LSP Diagnostics to Monaco Markers

const lspDiagnostic: lsp.Diagnostic = {
  range: {
    start: { line: 0, character: 7 },
    end: { line: 0, character: 10 }
  },
  message: "'foo' is undefined",
  severity: 1, // lsp.DiagnosticSeverity.Error
  code: "TS2304",
  source: "typescript"
};

const monacoMarker: monaco.editor.IMarkerData = toMonaco.toMarkerData(lspDiagnostic, monaco.Uri.parse('file:///main.ts'));

console.log('LSP Diagnostic:', lspDiagnostic);
console.log('Monaco Marker:', monacoMarker);

// Example of converting Monaco Position back to LSP Position
const monacoPosition: monaco.Position = new monaco.Position(1, 5);
const lspPosition: lsp.Position = fromMonaco.fromPosition(monacoPosition);

console.log('Monaco Position:', monacoPosition.toString());
console.log('LSP Position:', lspPosition);

/* Output will be similar to:
LSP Diagnostic: { range: { start: [Object], end: [Object] }, message: "'foo' is undefined", severity: 1, code: "TS2304", source: "typescript" }
Monaco Marker: { startLineNumber: 1, startColumn: 8, endLineNumber: 1, endColumn: 11, message: "'foo' is undefined", severity: 8, code: "TS2304", source: "typescript" }
Monaco Position: (1,5)
LSP Position: { line: 0, character: 4 }
*/

view raw JSON →