Monaco Language Server Types Converter
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
-
TypeError: Cannot read properties of undefined (reading 'Uri')
cause Attempting to use a conversion function like `toMarkerData` that requires `monaco.Uri` without providing the `monaco` object or `Uri` utility.fixEnsure you are passing the `monaco` global object (or `monaco.Uri`) as an argument to conversion functions that expect it, especially after the removal of `setMonaco()` in v0.3.0. For example: `toMonaco.toMarkerData(lspDiagnostic, monaco.Uri.parse('file:///main.ts'))`. -
Error: named export 'toCodeAction' not found. (hint: from 'monaco-languageserver-types')
cause Incorrect import path for specific conversion utilities. The library uses submodules for 'toMonaco' and 'fromMonaco' functions.fixAdjust your import statement to target the correct submodule. For example, change `import { toCodeAction } from 'monaco-languageserver-types'` to `import { toCodeAction } from 'monaco-languageserver-types/lib/toMonaco'`.
Warnings
- breaking The `setMonaco()` function was removed in v0.3.0. Previously, it was used to configure the Monaco editor module globally. Now, the `monaco` object (or relevant `Uri` utilities) must be passed directly to conversion functions where needed.
- breaking The deprecated singular `selectionRange` APIs were removed in v0.4.0. Functions handling single selection ranges are no longer available.
- breaking The `defaultSeverity` options for `toCodeAction` and `toMarkerData` were removed in v0.4.0. Additionally, the default severity for markers changed to `error` if no explicit severity is provided.
- gotcha When converting LSP `Position` and `Range` types to Monaco types, be mindful of the 0-based vs 1-based indexing. LSP uses 0-based for both lines and characters, while Monaco uses 1-based for lines and 1-based for columns. This library handles the conversion automatically, but incorrect manual manipulation can lead to off-by-one errors.
Install
-
npm install monaco-languageserver-types -
yarn add monaco-languageserver-types -
pnpm add monaco-languageserver-types
Imports
- toMonaco
import { toMonaco } from 'monaco-languageserver-types'import * as toMonaco from 'monaco-languageserver-types/lib/toMonaco'
- fromMonaco
import fromMonaco from 'monaco-languageserver-types'
import * as fromMonaco from 'monaco-languageserver-types/lib/fromMonaco'
- toCodeAction
import { toCodeAction } from 'monaco-languageserver-types'import { toCodeAction } from 'monaco-languageserver-types/lib/toMonaco'
Quickstart
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 }
*/