Integrate a Language Server Protocol compatible language service into CodeMirror
See the codeIntegrate a Language Server Protocol compatible language service into CodeMirror.
This package has peer dependencies on the following packages:
Since you will probably import these directly yourself, it’s recommended to install all of them explicitly.
npm install \
@codemirror/autocomplete \
@codemirror/language \
@codemirror/lint \
@codemirror/state \
@codemirror/view \
codemirror-languageservice
EditorView as usual.textDocument() extension
to your editor. It’s recommended to pass a file URI. If none is given, a URI is generated that
uses the inmemory:// protocol.languageId for text
documents. If this isn’t available, the plaintext language is used.hast-util-to-dom,
mdast-util-from-markdown, and
mdast-util-to-hast.import { autocompletion } from '@codemirror/autocomplete'
import { json } from '@codemirror/lang-json'
import { linter } from '@codemirror/lint'
import { EditorView, hoverTooltip } from '@codemirror/view'
import {
createCompletionSource,
createHoverTooltipSource,
createLintSource,
textDocument
} from 'codemirror-languageservice'
import { toDom } from 'hast-util-to-dom'
import { fromMarkdown } from 'mdast-util-from-markdown'
import { toHast } from 'mdast-util-to-hast'
import { doComplete, doDiagnostics, doHover } from './my-language-service.js'
function markdownToDom(markdown) {
const mdast = fromMarkdown(markdown)
const hast = toHast(mdast)
return toDom(hast, { fragment: true })
}
const view = new EditorView({
doc: '',
parent: document.getElementById('my-editor'),
extensions: [
json(),
textDocument('file:///example.txt'),
autocompletion({
override: [createCompletionSource({ doComplete, markdownToDom })]
}),
hoverTooltip(createHoverTooltipSource({ doHover, markdownToDom })),
linter(createLintSource({ doDiagnostics }))
]
})
createCompletionSource(options)Create an LSP based completion source.
doComplete (Function) — Provide LSP completions items.markdownToDom (Function) — Convert a markdown string to DOM.fromCompletionItemKind (Function, optional) — Convert an LSP completion item kind to a
CodeMirror completion type.section (string, optional) — The section to use for completions.triggerCharacters (string) — Only trigger completions automatically when one of these
characters is typed.A CodeMirror completion source that uses LSP based completions.
(CompletionSource)
createHoverTooltipSource(options)Create an LSP based hover tooltip provider.
doHover (Function) — Provide LSP hover infomarkdownToDom (Function) — Convert a markdown string to DOM.A CodeMirror hover tooltip source that uses LSP based hover information.
(HoverTooltipSource)
createLintSource(options)Create an LSP based lint source.
By default CodeMirror provides styling for the cm-lintRange-hint, cm-lintRange-info,
cm-lintRange-warning, and cm-lintRange-error classes. This extension also uses the
cm-lintRange-deprecated and cm-lintRange-unnecessary classes which you may want to style. For
example:
.cm-lintRange-deprecated {
background-image: none !important;
text-decoration: line-through;
}
.cm-lintRange-unnecessary {
background-repeat: no-repeat !important;
opacity: 0.4;
}
doDiagnostics (Function) — Provide LSP diagnosticsformatSource (Function, optional) — Format the source of a diagnostic.markClass (string, optional) — An additional class for all diagnostics provided by this
validation.A CodeMirror lint source that uses LSP based diagnostics.
(LintSource)
dispatchTextEdits(view, edits)Apply LSP text edits to an CodeMirror EditorView.
view (EditorView) — The view to dispatch
the changes to.edits
(TextEdit[])
— The edits that should be applied.getTextDocument(state)Get the text document for a CodeMirror editor state.
state (EditorState) — The editor state
to get the text document for.The text document.
(TextDocument)
textDocument(uri?)Assign a text document to an editor state.
This text document is used by other extensions provided by codemirror-languageservice.
The language ID is determined from the name of the language
used. If this isn’t found, the language ID defaults to plaintext.
uri (string) —The URI to use for the text document. If this is left unspecified, an
auto-incremented inmemory:// URI is used.A CodeMirror extension. (Extension)
There’s an example available in the
example directory.
This project is compatible with evergreen browsers.
This project provides LSP based integrations for CodeMirror. However, not all LSP features map well to CodeMirror. The goal is only to provide integrations that make sense for CodeMirror. If you have a pragmatic idea to integrate another LSP method, feel free to open a new issue.
On top of that, see my general contributing guidelines.
codemirror-languageservice was developed as part of the Transloadit JSON editor.Known language services that you could use this for:
vscode-css-languageservice — CSS,
LESS & SCSS language service extracted from VSCode to be reused.vscode-html-languageservice —
Language services for HTML.vscode-json-languageservice — JSON
language service extracted from VSCode to be reused.vscode-markdown-languageservice
— The language service that powers VS Code’s Markdown support.yaml-language-server — Language
Server for YAML Files.@tailwindcss/language-service —
About Intelligent Tailwind CSS tooling.@volar/language-service — The Embedded Language Tooling Framework.41 commits
TypeScript
95.9%
HTML
3.3%
Integrate a Language Server Protocol compatible language service into CodeMirror
See the codeIntegrate a Language Server Protocol compatible language service into CodeMirror.
This package has peer dependencies on the following packages:
Since you will probably import these directly yourself, it’s recommended to install all of them explicitly.
npm install \
@codemirror/autocomplete \
@codemirror/language \
@codemirror/lint \
@codemirror/state \
@codemirror/view \
codemirror-languageservice
EditorView as usual.textDocument() extension
to your editor. It’s recommended to pass a file URI. If none is given, a URI is generated that
uses the inmemory:// protocol.languageId for text
documents. If this isn’t available, the plaintext language is used.hast-util-to-dom,
mdast-util-from-markdown, and
mdast-util-to-hast.import { autocompletion } from '@codemirror/autocomplete'
import { json } from '@codemirror/lang-json'
import { linter } from '@codemirror/lint'
import { EditorView, hoverTooltip } from '@codemirror/view'
import {
createCompletionSource,
createHoverTooltipSource,
createLintSource,
textDocument
} from 'codemirror-languageservice'
import { toDom } from 'hast-util-to-dom'
import { fromMarkdown } from 'mdast-util-from-markdown'
import { toHast } from 'mdast-util-to-hast'
import { doComplete, doDiagnostics, doHover } from './my-language-service.js'
function markdownToDom(markdown) {
const mdast = fromMarkdown(markdown)
const hast = toHast(mdast)
return toDom(hast, { fragment: true })
}
const view = new EditorView({
doc: '',
parent: document.getElementById('my-editor'),
extensions: [
json(),
textDocument('file:///example.txt'),
autocompletion({
override: [createCompletionSource({ doComplete, markdownToDom })]
}),
hoverTooltip(createHoverTooltipSource({ doHover, markdownToDom })),
linter(createLintSource({ doDiagnostics }))
]
})
createCompletionSource(options)Create an LSP based completion source.
doComplete (Function) — Provide LSP completions items.markdownToDom (Function) — Convert a markdown string to DOM.fromCompletionItemKind (Function, optional) — Convert an LSP completion item kind to a
CodeMirror completion type.section (string, optional) — The section to use for completions.triggerCharacters (string) — Only trigger completions automatically when one of these
characters is typed.A CodeMirror completion source that uses LSP based completions.
(CompletionSource)
createHoverTooltipSource(options)Create an LSP based hover tooltip provider.
doHover (Function) — Provide LSP hover infomarkdownToDom (Function) — Convert a markdown string to DOM.A CodeMirror hover tooltip source that uses LSP based hover information.
(HoverTooltipSource)
createLintSource(options)Create an LSP based lint source.
By default CodeMirror provides styling for the cm-lintRange-hint, cm-lintRange-info,
cm-lintRange-warning, and cm-lintRange-error classes. This extension also uses the
cm-lintRange-deprecated and cm-lintRange-unnecessary classes which you may want to style. For
example:
.cm-lintRange-deprecated {
background-image: none !important;
text-decoration: line-through;
}
.cm-lintRange-unnecessary {
background-repeat: no-repeat !important;
opacity: 0.4;
}
doDiagnostics (Function) — Provide LSP diagnosticsformatSource (Function, optional) — Format the source of a diagnostic.markClass (string, optional) — An additional class for all diagnostics provided by this
validation.A CodeMirror lint source that uses LSP based diagnostics.
(LintSource)
dispatchTextEdits(view, edits)Apply LSP text edits to an CodeMirror EditorView.
view (EditorView) — The view to dispatch
the changes to.edits
(TextEdit[])
— The edits that should be applied.getTextDocument(state)Get the text document for a CodeMirror editor state.
state (EditorState) — The editor state
to get the text document for.The text document.
(TextDocument)
textDocument(uri?)Assign a text document to an editor state.
This text document is used by other extensions provided by codemirror-languageservice.
The language ID is determined from the name of the language
used. If this isn’t found, the language ID defaults to plaintext.
uri (string) —The URI to use for the text document. If this is left unspecified, an
auto-incremented inmemory:// URI is used.A CodeMirror extension. (Extension)
There’s an example available in the
example directory.
This project is compatible with evergreen browsers.
This project provides LSP based integrations for CodeMirror. However, not all LSP features map well to CodeMirror. The goal is only to provide integrations that make sense for CodeMirror. If you have a pragmatic idea to integrate another LSP method, feel free to open a new issue.
On top of that, see my general contributing guidelines.
codemirror-languageservice was developed as part of the Transloadit JSON editor.Known language services that you could use this for:
vscode-css-languageservice — CSS,
LESS & SCSS language service extracted from VSCode to be reused.vscode-html-languageservice —
Language services for HTML.vscode-json-languageservice — JSON
language service extracted from VSCode to be reused.vscode-markdown-languageservice
— The language service that powers VS Code’s Markdown support.yaml-language-server — Language
Server for YAML Files.@tailwindcss/language-service —
About Intelligent Tailwind CSS tooling.@volar/language-service — The Embedded Language Tooling Framework.41 commits
TypeScript
95.9%
HTML
3.3%