Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codemirror deferrers #3006

Merged
merged 9 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -30,7 +30,7 @@
import { LanguageServerClient } from '../client'
import { CompletionItemKindMap } from './autocomplete'
import { addToken, SemanticToken } from './semantic-tokens'
import { deferExecution, posToOffset, formatMarkdownContents } from './util'

Check warning on line 33 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / build-test-apps (ubuntu-latest)

'deferExecution' is defined but never used

Check warning on line 33 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / build-test-apps (windows-latest)

'deferExecution' is defined but never used

Check warning on line 33 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / playwright-ubuntu

'deferExecution' is defined but never used

Check warning on line 33 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / build-test-apps (macos-14)

'deferExecution' is defined but never used

Check warning on line 33 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / playwright-macos

'deferExecution' is defined but never used
import lspAutocompleteExt from './autocomplete'
import lspHoverExt from './hover'
import lspFormatExt from './format'
Expand Down Expand Up @@ -93,23 +93,10 @@
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 @@
}

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 @@
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 @@
)
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 @@
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 @@
)
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 @@
)
return null

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

const result = await this.client.textDocumentCompletion({
textDocument: { uri: this.getDocUri() },
Expand Down Expand Up @@ -525,7 +525,7 @@
processDiagnostics(params: PublishDiagnosticsParams) {
if (params.uri !== this.getDocUri()) return

const diagnostics = params.diagnostics

Check warning on line 528 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / build-test-apps (ubuntu-latest)

'diagnostics' is assigned a value but never used

Check warning on line 528 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / build-test-apps (windows-latest)

'diagnostics' is assigned a value but never used

Check warning on line 528 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / playwright-ubuntu

'diagnostics' is assigned a value but never used

Check warning on line 528 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / build-test-apps (macos-14)

'diagnostics' is assigned a value but never used

Check warning on line 528 in packages/codemirror-lsp-client/src/plugin/lsp.ts

View workflow job for this annotation

GitHub Actions / playwright-macos

'diagnostics' is assigned a value but never used
.map(({ range, message, severity }) => ({
from: posToOffset(this.view.state.doc, range.start)!,
to: posToOffset(this.view.state.doc, range.end)!,
Expand Down
Loading
Loading