From 716ad938fce66dd5f7c10e8eae67b51bc3a3e679 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Thu, 21 Sep 2023 16:13:22 -0700 Subject: [PATCH] stop gap for large files making editor slow (#690) Signed-off-by: Jess Frazelle --- package.json | 2 ++ src/editor/lsp/plugin.ts | 46 +++++++++++++++++++++++++++------------- yarn.lock | 10 +++++++++ 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 3fe2a5389..010604335 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@uiw/react-codemirror": "^4.21.13", "@xstate/react": "^3.2.2", "crypto-js": "^4.1.1", + "debounce-promise": "^3.1.2", "formik": "^2.4.3", "fuse.js": "^6.6.2", "http-server": "^14.1.1", @@ -102,6 +103,7 @@ "@tauri-apps/cli": "^1.3.1", "@types/crypto-js": "^4.1.1", "@types/debounce": "^1.2.1", + "@types/debounce-promise": "^3.1.6", "@types/isomorphic-fetch": "^0.0.36", "@types/react-modal": "^3.16.0", "@types/uuid": "^9.0.1", diff --git a/src/editor/lsp/plugin.ts b/src/editor/lsp/plugin.ts index 38ea82f57..6dcb6013b 100644 --- a/src/editor/lsp/plugin.ts +++ b/src/editor/lsp/plugin.ts @@ -13,6 +13,7 @@ import { CompletionItemKind, CompletionTriggerKind, } from 'vscode-languageserver-protocol' +import debounce from 'debounce-promise' import type { Completion, @@ -53,14 +54,11 @@ export class LanguageServerPlugin implements PluginValue { private languageId: string private documentVersion: number - private changesTimeout: number - constructor(private view: EditorView, private allowHTMLContent: boolean) { this.client = this.view.state.facet(client) this.documentUri = this.view.state.facet(documentUri) this.languageId = this.view.state.facet(languageId) this.documentVersion = 0 - this.changesTimeout = 0 this.client.attachPlugin(this) @@ -71,12 +69,10 @@ export class LanguageServerPlugin implements PluginValue { update({ docChanged }: ViewUpdate) { if (!docChanged) return - if (this.changesTimeout) clearTimeout(this.changesTimeout) - this.changesTimeout = window.setTimeout(() => { - this.sendChange({ - documentText: this.view.state.doc.toString(), - }) - }, changesDelay) + + this.sendChange({ + documentText: this.view.state.doc.toString(), + }) } destroy() { @@ -99,14 +95,34 @@ export class LanguageServerPlugin implements PluginValue { async sendChange({ documentText }: { documentText: string }) { if (!this.client.ready) return + + console.log(documentText.length) + + if (documentText.length > 5000) { + // Clear out the text it thinks we have, large documents will throw a stack error. + // This is obviously not a good fix but it works for now til we figure + // out the stack limits in wasm and also rewrite the parser. + // Since this is only for hover and completions it will be fine, + // completions will still work for stdlib but hover will not. + // That seems like a fine trade-off for a working editor for the time + // being. + documentText = '' + } + try { - await this.client.textDocumentDidChange({ - textDocument: { - uri: this.documentUri, - version: this.documentVersion++, + debounce( + () => { + return this.client.textDocumentDidChange({ + textDocument: { + uri: this.documentUri, + version: this.documentVersion++, + }, + contentChanges: [{ text: documentText }], + }) }, - contentChanges: [{ text: documentText }], - }) + changesDelay, + { leading: true } + ) } catch (e) { console.error(e) } diff --git a/yarn.lock b/yarn.lock index 3f484b51e..2d0339d0f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1883,6 +1883,11 @@ resolved "https://registry.yarnpkg.com/@types/crypto-js/-/crypto-js-4.1.1.tgz#602859584cecc91894eb23a4892f38cfa927890d" integrity sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA== +"@types/debounce-promise@^3.1.6": + version "3.1.6" + resolved "https://registry.yarnpkg.com/@types/debounce-promise/-/debounce-promise-3.1.6.tgz#873e838574011095ed0debf73eed3538e1261d75" + integrity sha512-DowqK95aku+OxMCeG2EQSeXeGeE8OCwLpMsUfIbP7hMF8Otj8eQXnzpwdtIKV+UqQBtkMcF6vbi4Otbh8P/wmg== + "@types/debounce@^1.2.1": version "1.2.1" resolved "https://registry.yarnpkg.com/@types/debounce/-/debounce-1.2.1.tgz#79b65710bc8b6d44094d286aecf38e44f9627852" @@ -2806,6 +2811,11 @@ data-uri-to-buffer@^4.0.0: resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e" integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A== +debounce-promise@^3.1.2: + version "3.1.2" + resolved "https://registry.yarnpkg.com/debounce-promise/-/debounce-promise-3.1.2.tgz#320fb8c7d15a344455cd33cee5ab63530b6dc7c5" + integrity sha512-rZHcgBkbYavBeD9ej6sP56XfG53d51CD4dnaw989YX/nZ/ZJfgRx/9ePKmTNiUiyQvh4mtrMoS3OAWW+yoYtpg== + debug@^3.2.7: version "3.2.7" resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a"