From 87f6d0472008db0c186fa2f6a78e3dabca41fe1b Mon Sep 17 00:00:00 2001 From: Okiki Ojo Date: Fri, 13 Oct 2023 07:24:43 +0000 Subject: [PATCH] chore: (wip) hmm... autocomplete is rather finicky --- src/components/Editor.tsx | 1 - src/lib/codemirror/codemirror.ts | 6 ++- src/lib/lsp/lsp.ts | 82 ++++++++++++++++++++++---------- 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index b4da4b8..ce26d90 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -6,7 +6,6 @@ import { languageServer } from '@/lib/lsp/lsp'; import JSONServerWorker from "@/lib/lsp/json-server?worker"; const JSONServer = new JSONServerWorker(); export const ls = languageServer({ - // json-server serverWorker: JSONServer, rootUri: 'file:///', documentUri: `file:///tsconfig.json`, // tsconfig.json diff --git a/src/lib/codemirror/codemirror.ts b/src/lib/codemirror/codemirror.ts index bafbbe4..c1d7ea3 100644 --- a/src/lib/codemirror/codemirror.ts +++ b/src/lib/codemirror/codemirror.ts @@ -7,7 +7,8 @@ import { THEME, HIGHTLIGHT_STYLE } from "@/lib/codemirror/editor-theme"; import { Compartment, EditorState, type Extension } from "@codemirror/state"; import { indentUnit, syntaxHighlighting } from "@codemirror/language"; -import { json, jsonLanguage } from "@codemirror/lang-json"; +import { json, jsonLanguage, jsonParseLinter } from "@codemirror/lang-json"; +import { linter } from "@codemirror/lint"; const initalText = `\ { @@ -17,7 +18,7 @@ const initalText = `\ "strict": true, "esModuleInterop": true, "skipLibCheck": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, } } `; @@ -40,6 +41,7 @@ export default (parentEl: HTMLElement, extentions?: Extension, doc = initalText) // typescript: true // }), json(), + linter(jsonParseLinter()), (extentions ? extentions : []) ] }), diff --git a/src/lib/lsp/lsp.ts b/src/lib/lsp/lsp.ts index 7e72654..a5054c3 100644 --- a/src/lib/lsp/lsp.ts +++ b/src/lib/lsp/lsp.ts @@ -1,12 +1,15 @@ -import { autocompletion } from '@codemirror/autocomplete'; +import { autocompletion, startCompletion } from '@codemirror/autocomplete'; import { setDiagnostics } from '@codemirror/lint'; import { Facet } from '@codemirror/state'; -import { EditorView, ViewPlugin, hoverTooltip } from '@codemirror/view'; +import { EditorView, ViewPlugin, hoverTooltip, keymap } from '@codemirror/view'; + +// https://www.npmjs.com/package/@open-rpc/client-js import { RequestManager, Client } from '@/lib/open-rpc-client-js'; import { PostMessageWorkerTransport } from "@/lib/open-rpc-client-js/transports/PostMessageWorkerTransport"; + import { DiagnosticSeverity, CompletionItemKind, @@ -410,36 +413,63 @@ export function languageServer(options: LanguageServerOptions) { if (plugin == null) return null; const { state, pos, explicit } = context; - const line = state.doc.lineAt(pos); - let trigKind: CompletionTriggerKind = - CompletionTriggerKind.Invoked; - let trigChar: string | undefined; - if ( - !explicit && - plugin.capabilities?.completionProvider?.triggerCharacters?.includes( - line.text[pos - line.from - 1] + try { + const line = state.doc.lineAt(pos); + let trigKind: CompletionTriggerKind = + CompletionTriggerKind.Invoked; + let trigChar: string | undefined; + if ( + !explicit && + plugin.capabilities?.completionProvider?.triggerCharacters?.includes( + line.text[pos - line.from - 1] + ) + ) { + trigKind = CompletionTriggerKind.TriggerCharacter; + trigChar = line.text[pos - line.from - 1]; + } + + // if ( + // trigKind === CompletionTriggerKind.Invoked && + // !context.matchBefore(/\w+$/) + // ) { + // return null; + // } + + const result = await plugin.requestCompletion( + context, + offsetToPos(state.doc, pos), + { + triggerKind: trigKind, + triggerCharacter: trigChar, + } ) - ) { - trigKind = CompletionTriggerKind.TriggerCharacter; - trigChar = line.text[pos - line.from - 1]; - } - if ( - trigKind === CompletionTriggerKind.Invoked && - !context.matchBefore(/\w+$/) - ) { + console.log({ + triggerChars: plugin.capabilities?.completionProvider?.triggerCharacters, + explicit, + trigKind, + context, + matchBefore: context.matchBefore(/\w+$/), + result + }) + return result; + + } catch (e) { + console.log("Unable to get completions", { pos, error: e }); return null; } - return await plugin.requestCompletion( - context, - offsetToPos(state.doc, pos), - { - triggerKind: trigKind, - triggerCharacter: trigChar, - } - ); }, ], }), + keymap.of([ + { + key: ",", + run: (x) => { + // TODO: fix this hack + setTimeout(() => startCompletion(x), 1); + return false; + }, + }, + ]), baseTheme, ]; } -- knotmirror 0.0.1