Skip to content

Commit

Permalink
Codemirror deferrers (#3006)
Browse files Browse the repository at this point in the history
* Force document update requests when necessary

* fix up codemirror deferrers

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* lock

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fixups kcl/index

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fix copilot

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* docs

Signed-off-by: Jess Frazelle <github@jessfraz.com>

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
Co-authored-by: Marijn Haverbeke <marijn@haverbeke.berlin>
  • Loading branch information
jessfraz and marijnh authored Jul 11, 2024
1 parent 2dd1f0f commit 88d9cdc
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 124 deletions.
2 changes: 1 addition & 1 deletion docs/kcl/patternTransform.md

Large diffs are not rendered by default.

90 changes: 45 additions & 45 deletions packages/codemirror-lsp-client/src/plugin/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,10 @@ export class LanguageServerPlugin implements PluginValue {
private doSemanticTokens: boolean = false
private doFoldingRanges: boolean = false

private _defferer = deferExecution((code: string) => {
try {
// Update the state (not the editor) with the new code.
this.client.textDocumentDidChange({
textDocument: {
uri: this.getDocUri(),
version: this.documentVersion++,
},
contentChanges: [{ text: code }],
})

this.requestSemanticTokens()
this.updateFoldingRanges()
} catch (e) {
console.error(e)
}
}, this.changesDelay)
// When a doc update needs to be sent to the server, this holds the
// timeout handle for it. When null, the server has the up-to-date
// document.
private sendScheduled: number | null = null

constructor(options: LanguageServerOptions, private view: EditorView) {
this.client = options.client
Expand Down Expand Up @@ -152,14 +139,9 @@ export class LanguageServerPlugin implements PluginValue {
}

update(viewUpdate: ViewUpdate) {
// If the doc didn't change we can return early.
if (!viewUpdate.docChanged) {
return
if (viewUpdate.docChanged) {
this.scheduleSendDoc()
}

this.sendChange({
documentText: viewUpdate.state.doc.toString(),
})
}

destroy() {
Expand All @@ -184,16 +166,6 @@ export class LanguageServerPlugin implements PluginValue {
this.updateFoldingRanges()
}

async sendChange({ documentText }: { documentText: string }) {
if (!this.client.ready) return

this._defferer(documentText)
}

requestDiagnostics() {
this.sendChange({ documentText: this.getDocText() })
}

async requestHoverTooltip(
view: EditorView,
{ line, character }: { line: number; character: number }
Expand All @@ -204,7 +176,7 @@ export class LanguageServerPlugin implements PluginValue {
)
return null

this.sendChange({ documentText: this.getDocText() })
this.ensureDocSent()
const result = await this.client.textDocumentHover({
textDocument: { uri: this.getDocUri() },
position: { line, character },
Expand All @@ -227,6 +199,42 @@ export class LanguageServerPlugin implements PluginValue {
return { pos, end, create: (view) => ({ dom }), above: true }
}

scheduleSendDoc() {
if (this.sendScheduled != null) window.clearTimeout(this.sendScheduled)
this.sendScheduled = window.setTimeout(
() => this.sendDoc(),
this.changesDelay
)
}

sendDoc() {
if (this.sendScheduled != null) {
window.clearTimeout(this.sendScheduled)
this.sendScheduled = null
}

if (!this.client.ready) return
try {
// Update the state (not the editor) with the new code.
this.client.textDocumentDidChange({
textDocument: {
uri: this.getDocUri(),
version: this.documentVersion++,
},
contentChanges: [{ text: this.view.state.doc.toString() }],
})

this.requestSemanticTokens()
this.updateFoldingRanges()
} catch (e) {
console.error(e)
}
}

ensureDocSent() {
if (this.sendScheduled != null) this.sendDoc()
}

async getFoldingRanges(): Promise<LSP.FoldingRange[] | null> {
if (
!this.doFoldingRanges ||
Expand Down Expand Up @@ -284,13 +292,7 @@ export class LanguageServerPlugin implements PluginValue {
)
return null

this.client.textDocumentDidChange({
textDocument: {
uri: this.getDocUri(),
version: this.documentVersion++,
},
contentChanges: [{ text: this.getDocText() }],
})
this.ensureDocSent()

const result = await this.client.textDocumentFormatting({
textDocument: { uri: this.getDocUri() },
Expand Down Expand Up @@ -330,9 +332,7 @@ export class LanguageServerPlugin implements PluginValue {
)
return null

this.sendChange({
documentText: context.state.doc.toString(),
})
this.ensureDocSent()

const result = await this.client.textDocumentCompletion({
textDocument: { uri: this.getDocUri() },
Expand Down
Loading

0 comments on commit 88d9cdc

Please sign in to comment.