codemirror-languageserver
Advanced tools
| import type { Text, ChangeSet } from '@codemirror/state'; | ||
| import type * as LSP from 'vscode-languageserver-protocol'; | ||
| export declare function changeSetToEvents(doc: Text, changes: ChangeSet): LSP.TextDocumentContentChangeEvent[]; |
+3
-2
@@ -1,5 +0,5 @@ | ||
| export { LanguageServerClient, languageServerPlugin } from './plugin'; | ||
| export { LanguageServerClient, languageServerPlugin, SynchronizationMethod, } from './plugin'; | ||
| export { jumpToDefinition, jumpToDefinitionPos, jumpToDefinitionKeymap, } from './definition'; | ||
| export { PyrightInitializationOptions, RustAnalyzerInitializationOptions, TypeScriptInitializationOptions, ESLintInitializationOptions, ClangdInitializationOptions, GoplsInitializationOptions, } from './initialization'; | ||
| import { LanguageServerClient } from './plugin'; | ||
| import { LanguageServerClient, SynchronizationMethod } from './plugin'; | ||
| import type { LanguageServerClientOptions, LanguageServerBaseOptions } from './plugin'; | ||
@@ -9,2 +9,3 @@ interface LanguageServerOptions<InitializationOptions = unknown> extends LanguageServerClientOptions<InitializationOptions> { | ||
| allowHTMLContent?: boolean; | ||
| synchronizationMethod?: SynchronizationMethod; | ||
| } | ||
@@ -11,0 +12,0 @@ interface LanguageServerWebsocketOptions<InitializationOptions = unknown> extends LanguageServerBaseOptions { |
+45
-18
@@ -27,4 +27,24 @@ import { insertCompletionText, autocompletion as autocompletion$1 } from '@codemirror/autocomplete'; | ||
| function changeSetToEvents(doc, changes) { | ||
| const events = []; | ||
| changes.iterChanges((fromA, toA, fromB, toB, inserted) => { | ||
| events.push({ | ||
| range: { | ||
| start: offsetToPos(doc, fromA), | ||
| end: offsetToPos(doc, toA), | ||
| }, | ||
| text: inserted.toString(), | ||
| }); | ||
| }); | ||
| events.sort((a, b) => { | ||
| if (!('range' in a)) | ||
| return 1; | ||
| if (!('range' in b)) | ||
| return -1; | ||
| return (posToOffset(doc, b.range.start) - posToOffset(doc, a.range.start)); | ||
| }); | ||
| return events; | ||
| } | ||
| const timeout = 10000; | ||
| const changesDelay = 500; | ||
| const CompletionItemKindMap = Object.fromEntries(Object.entries(CompletionItemKind).map(([key, value]) => [value, key])); | ||
@@ -190,4 +210,9 @@ const useLast = (values) => values.reduce((_, v) => v, ''); | ||
| } | ||
| var SynchronizationMethod; | ||
| (function (SynchronizationMethod) { | ||
| SynchronizationMethod["Full"] = "full"; | ||
| SynchronizationMethod["Incremental"] = "incremental"; | ||
| })(SynchronizationMethod || (SynchronizationMethod = {})); | ||
| class LanguageServerPlugin { | ||
| constructor(view, { client, documentUri, languageId, allowHTMLContent, }) { | ||
| constructor(view, { client, documentUri, languageId, allowHTMLContent, synchronizationMethod, }) { | ||
| this.view = view; | ||
@@ -198,4 +223,4 @@ this.client = client; | ||
| this.allowHTMLContent = allowHTMLContent; | ||
| this.synchronizationMethod = synchronizationMethod; | ||
| this.documentVersion = 0; | ||
| this.changesTimeout = 0; | ||
| this.client.attachPlugin(this); | ||
@@ -206,14 +231,18 @@ this.initialize({ | ||
| } | ||
| update({ docChanged }) { | ||
| update({ state, changes, startState, docChanged }) { | ||
| if (!docChanged) { | ||
| return; | ||
| } | ||
| if (this.changesTimeout) { | ||
| clearTimeout(this.changesTimeout); | ||
| switch (this.synchronizationMethod) { | ||
| case SynchronizationMethod.Full: | ||
| this.sendChanges([ | ||
| { | ||
| text: this.view.state.doc.toString(), | ||
| }, | ||
| ]); | ||
| break; | ||
| case SynchronizationMethod.Incremental: | ||
| this.sendChanges(changeSetToEvents(startState.doc, changes)); | ||
| break; | ||
| } | ||
| this.changesTimeout = self.setTimeout(() => { | ||
| this.sendChange({ | ||
| documentText: this.view.state.doc.toString(), | ||
| }); | ||
| }, changesDelay); | ||
| } | ||
@@ -236,3 +265,3 @@ destroy() { | ||
| } | ||
| async sendChange({ documentText }) { | ||
| async sendChanges(contentChanges) { | ||
| if (!this.client.ready) { | ||
@@ -247,3 +276,3 @@ return; | ||
| }, | ||
| contentChanges: [{ text: documentText }], | ||
| contentChanges, | ||
| }); | ||
@@ -256,3 +285,3 @@ } | ||
| requestDiagnostics(view) { | ||
| this.sendChange({ documentText: view.state.doc.toString() }); | ||
| this.sendChanges([{ text: view.state.doc.toString() }]); | ||
| } | ||
@@ -300,5 +329,2 @@ async requestHoverTooltip(view, { line, character }) { | ||
| } | ||
| this.sendChange({ | ||
| documentText: context.state.doc.toString(), | ||
| }); | ||
| const result = await this.client.textDocumentCompletion({ | ||
@@ -632,2 +658,3 @@ textDocument: { uri: this.documentUri }, | ||
| allowHTMLContent: options.allowHTMLContent, | ||
| synchronizationMethod: options.synchronizationMethod, | ||
| }), | ||
@@ -641,2 +668,2 @@ hoverTooltip(), | ||
| export { LanguageServerClient, jumpToDefinition, jumpToDefinitionKeymap, jumpToDefinitionPos, languageServer, languageServerPlugin, languageServerWithTransport }; | ||
| export { LanguageServerClient, SynchronizationMethod, jumpToDefinition, jumpToDefinitionKeymap, jumpToDefinitionPos, languageServer, languageServerPlugin, languageServerWithTransport }; |
+10
-6
@@ -80,2 +80,6 @@ import { EditorView, Tooltip, ViewPlugin } from '@codemirror/view'; | ||
| } | ||
| export declare enum SynchronizationMethod { | ||
| Full = "full", | ||
| Incremental = "incremental" | ||
| } | ||
| export declare class LanguageServerPlugin implements PluginValue { | ||
@@ -88,4 +92,4 @@ private view; | ||
| private allowHTMLContent; | ||
| private changesTimeout; | ||
| constructor(view: EditorView, { client, documentUri, languageId, allowHTMLContent, }: { | ||
| private synchronizationMethod; | ||
| constructor(view: EditorView, { client, documentUri, languageId, allowHTMLContent, synchronizationMethod, }: { | ||
| client: LanguageServerClient; | ||
@@ -95,4 +99,5 @@ documentUri: string; | ||
| allowHTMLContent: boolean; | ||
| synchronizationMethod: SynchronizationMethod; | ||
| }); | ||
| update({ docChanged }: ViewUpdate): void; | ||
| update({ state, changes, startState, docChanged }: ViewUpdate): void; | ||
| destroy(): void; | ||
@@ -102,5 +107,3 @@ initialize({ documentText }: { | ||
| }): Promise<void>; | ||
| sendChange({ documentText }: { | ||
| documentText: string; | ||
| }): Promise<void>; | ||
| sendChanges(contentChanges: LSP.TextDocumentContentChangeEvent[]): Promise<void>; | ||
| requestDiagnostics(view: EditorView): void; | ||
@@ -154,2 +157,3 @@ requestHoverTooltip(view: EditorView, { line, character }: { | ||
| allowHTMLContent: boolean; | ||
| synchronizationMethod: SynchronizationMethod; | ||
| }>; | ||
@@ -156,0 +160,0 @@ export interface LanguageServerBaseOptions { |
+1
-1
| { | ||
| "name": "codemirror-languageserver", | ||
| "version": "1.17.1", | ||
| "version": "1.18.0", | ||
| "description": "Language Server Plugin for CodeMirror 6", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
1419492
0.11%20
5.26%39468
0.09%