use our own input method in vscode
The lean one was annoying for \{...}
This commit is contained in:
10
newt-vscode/src/abbrev.ts
Normal file
10
newt-vscode/src/abbrev.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
export const ABBREV: Record<string, string> = {
|
||||||
|
"\\x": "×",
|
||||||
|
"\\r": "→",
|
||||||
|
"\\all": "∀",
|
||||||
|
"\\\\": "\\",
|
||||||
|
"\\==": "≡",
|
||||||
|
"\\circ": "∘",
|
||||||
|
"\\1": "₁",
|
||||||
|
"\\2": "₂",
|
||||||
|
};
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { exec } from "child_process";
|
import { exec } from "child_process";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import { ABBREV } from "./abbrev";
|
||||||
|
|
||||||
interface FC {
|
interface FC {
|
||||||
file: string;
|
file: string;
|
||||||
@@ -22,6 +23,45 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
const diagnosticCollection =
|
const diagnosticCollection =
|
||||||
vscode.languages.createDiagnosticCollection("newt");
|
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) {
|
function checkDocument(document: vscode.TextDocument) {
|
||||||
const fileName = document.fileName;
|
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.
|
// 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
1
playground/src/abbrev.ts
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../../newt-vscode/src/abbrev.ts
|
||||||
@@ -6,6 +6,7 @@ import { h, render } from "preact";
|
|||||||
import { ChangeEvent } from "preact/compat";
|
import { ChangeEvent } from "preact/compat";
|
||||||
import { archive, preload } from "./preload.ts";
|
import { archive, preload } from "./preload.ts";
|
||||||
import { CompileReq, CompileRes, Message } from "./types.ts";
|
import { CompileReq, CompileRes, Message } from "./types.ts";
|
||||||
|
import { ABBREV } from "./abbrev.ts";
|
||||||
|
|
||||||
// editor.(createModel / setModel / getModels) to switch files
|
// editor.(createModel / setModel / getModels) to switch files
|
||||||
|
|
||||||
@@ -221,17 +222,6 @@ interface EditorProps {
|
|||||||
initialValue: string;
|
initialValue: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ABBREV: Record<string, string> = {
|
|
||||||
'\\x': '×',
|
|
||||||
'\\r': '→',
|
|
||||||
'\\all': '∀',
|
|
||||||
'\\\\': '\\',
|
|
||||||
'\\==': '≡',
|
|
||||||
'\\circ': '∘',
|
|
||||||
'\\1': '₁',
|
|
||||||
'\\2': '₂',
|
|
||||||
}
|
|
||||||
|
|
||||||
function Editor({ initialValue }: EditorProps) {
|
function Editor({ initialValue }: EditorProps) {
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user