From 590f34451683a61a1c43fc65ad7c1567f257d65a Mon Sep 17 00:00:00 2001 From: Steve Dunham Date: Sat, 5 Apr 2025 10:31:32 -0700 Subject: [PATCH] use our own input method in vscode The lean one was annoying for \{...} --- newt-vscode/src/abbrev.ts | 10 +++++++++ newt-vscode/src/extension.ts | 40 ++++++++++++++++++++++++++++++++++++ playground/src/abbrev.ts | 1 + playground/src/main.ts | 12 +---------- 4 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 newt-vscode/src/abbrev.ts create mode 120000 playground/src/abbrev.ts diff --git a/newt-vscode/src/abbrev.ts b/newt-vscode/src/abbrev.ts new file mode 100644 index 0000000..a2e22ae --- /dev/null +++ b/newt-vscode/src/abbrev.ts @@ -0,0 +1,10 @@ +export const ABBREV: Record = { + "\\x": "×", + "\\r": "→", + "\\all": "∀", + "\\\\": "\\", + "\\==": "≡", + "\\circ": "∘", + "\\1": "₁", + "\\2": "₂", +}; diff --git a/newt-vscode/src/extension.ts b/newt-vscode/src/extension.ts index 5af64cf..bd48f86 100644 --- a/newt-vscode/src/extension.ts +++ b/newt-vscode/src/extension.ts @@ -1,6 +1,7 @@ import * as vscode from "vscode"; import { exec } from "child_process"; import path from "path"; +import { ABBREV } from "./abbrev"; interface FC { file: string; @@ -22,6 +23,45 @@ export function activate(context: vscode.ExtensionContext) { const diagnosticCollection = vscode.languages.createDiagnosticCollection("newt"); + vscode.workspace.onDidChangeTextDocument((event) => { + const editor = vscode.window.activeTextEditor; + if (!editor || event.document !== editor.document) return; + + const changes = event.contentChanges; + if (changes.length === 0) return; + + const lastChange = changes[changes.length - 1]; + const text = lastChange.text; + + // Check if the last change is a potential shortcut trigger + if (!text || !" ')\\".includes(text)) return; + + const document = editor.document; + const position = lastChange.range.end; + const lineText = document.lineAt(position.line).text; + const start = Math.max(0, position.character - 10); + const snippet = lineText.slice(start, position.character); + console.log(`change '${text}' snippet ${snippet}`) + const m = snippet.match(/(\\[^ ]+)$/); + if (m) { + const cand = m[0]; + console.log('cand', cand); + const replacement = ABBREV[cand]; + console.log('repl', replacement); + if (replacement) { + const range = new vscode.Range( + position.line, + position.character - cand.length, + position.line, + position.character + ); + editor.edit((editBuilder) => { + editBuilder.replace(range, replacement); + }); + } + } + }); + function checkDocument(document: vscode.TextDocument) { const fileName = document.fileName; // Is there a better way to do this? It will get fussy with quoting and all plus it's not visible to the user. diff --git a/playground/src/abbrev.ts b/playground/src/abbrev.ts new file mode 120000 index 0000000..2189ee0 --- /dev/null +++ b/playground/src/abbrev.ts @@ -0,0 +1 @@ +../../newt-vscode/src/abbrev.ts \ No newline at end of file diff --git a/playground/src/main.ts b/playground/src/main.ts index 753569f..a827627 100644 --- a/playground/src/main.ts +++ b/playground/src/main.ts @@ -6,6 +6,7 @@ import { h, render } from "preact"; import { ChangeEvent } from "preact/compat"; import { archive, preload } from "./preload.ts"; import { CompileReq, CompileRes, Message } from "./types.ts"; +import { ABBREV } from "./abbrev.ts"; // editor.(createModel / setModel / getModels) to switch files @@ -221,17 +222,6 @@ interface EditorProps { initialValue: string; } -const ABBREV: Record = { - '\\x': '×', - '\\r': '→', - '\\all': '∀', - '\\\\': '\\', - '\\==': '≡', - '\\circ': '∘', - '\\1': '₁', - '\\2': '₂', - } - function Editor({ initialValue }: EditorProps) { const ref = useRef(null); useEffect(() => {