use our own input method in vscode

The lean one was annoying for \{...}
This commit is contained in:
2025-04-05 10:31:32 -07:00
parent 8c983dd571
commit 590f344516
4 changed files with 52 additions and 11 deletions

10
newt-vscode/src/abbrev.ts Normal file
View File

@@ -0,0 +1,10 @@
export const ABBREV: Record<string, string> = {
"\\x": "×",
"\\r": "→",
"\\all": "∀",
"\\\\": "\\",
"\\==": "≡",
"\\circ": "∘",
"\\1": "₁",
"\\2": "₂",
};

View File

@@ -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.

1
playground/src/abbrev.ts Symbolic link
View File

@@ -0,0 +1 @@
../../newt-vscode/src/abbrev.ts

View File

@@ -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<string, string> = {
'\\x': '×',
'\\r': '→',
'\\all': '∀',
'\\\\': '\\',
'\\==': '≡',
'\\circ': '∘',
'\\1': '₁',
'\\2': '₂',
}
function Editor({ initialValue }: EditorProps) {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {