From 0fff368adfc1bb8440631fcefea668cea9bc70d4 Mon Sep 17 00:00:00 2001 From: "Paolo G. Giarrusso" Date: Mon, 20 Jan 2020 05:57:31 +0100 Subject: [PATCH] Add way to show how to input a given character (fix #17) I take no credit; this is the patch from https://github.com/ojsheikh/unicode-latex/issues/17#issuecomment-575558713 (not yet tested). --- package.json | 5 +++++ src/extension.ts | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/package.json b/package.json index 923680d..23c1b39 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "activationEvents": [ "onCommand:unicode-latex.insertMathSymbol", "onCommand:unicode-latex.replaceLatexNames", + "onCommand:unicode-latex.showLatexCode", "onLanguage:plaintext" ], "main": "./out/src/extension", @@ -29,6 +30,10 @@ { "command": "unicode-latex.replaceLatexNames", "title": "Unicode: Replace LaTeX" + }, + { + "command": "unicode-latex.showLatexCode", + "title": "Unicode: Show LaTex Code" } ] }, diff --git a/src/extension.ts b/src/extension.ts index 643dc24..43588ed 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -8,6 +8,7 @@ let latexItems: vscode.QuickPickItem[] = []; let pickOptions: vscode.QuickPickOptions = { matchOnDescription: true, }; +var latexSymbols2: { [name:string]: string} = {}; export function activate(context: vscode.ExtensionContext) { @@ -17,6 +18,7 @@ export function activate(context: vscode.ExtensionContext) { description: name, label: latexSymbols[name], }); + latexSymbols2[latexSymbols[name]] = name; } let insertion = vscode.commands.registerCommand('unicode-latex.insertMathSymbol', () => { @@ -25,6 +27,9 @@ export function activate(context: vscode.ExtensionContext) { let replacement = vscode.commands.registerCommand('unicode-latex.replaceLatexNames', () => { replaceWithUnicode(vscode.window.activeTextEditor); }); + let showLatex = vscode.commands.registerCommand('unicode-latex.showLatexCode', () => { + appendLatexOfUnicode(vscode.window.activeTextEditor); + }); const selector: vscode.DocumentSelector = ['plaintext', 'markdown', 'coq']; const provider = new LatexCompletionItemProvider(latexSymbols); @@ -32,6 +37,7 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(insertion); context.subscriptions.push(replacement); + context.subscriptions.push(showLatex); context.subscriptions.push(completionSub); } @@ -78,5 +84,24 @@ function replaceWithUnicode(editor: vscode.TextEditor) { }); } +function appendLatexOfUnicode(editor: vscode.TextEditor) { + if (!editor) { return; } + + // If nothing is selected, do nothing + let selection = (() => { + if (editor.selection.start.isBefore(editor.selection.end)) { + return editor.selection; + } else { + return; + } + })(); + + let text = editor.document.getText(selection); + if (latexSymbols2.hasOwnProperty(text)){ + editor.edit((editBuilder) => { + editBuilder.insert(editor.selection.end, latexSymbols2[text]); + }); + } +} // this method is called when your extension is deactivated export function deactivate() {}