ojsheikh / unicode-latex

A visual studio code extension to insert Unicode symbols given LaTeX symbol names
MIT License
28 stars 25 forks source link

Add way to show how to input a given character #17

Open Blaisorblade opened 4 years ago

Blaisorblade commented 4 years ago

Use case: I'm using some Unicode API and want to know how to type the Unicode symbol they're using.

The best way, right now, is to copy the symbol and search it through https://github.com/ojsheikh/unicode-latex/blob/master/src/latex.ts. It'd be convenient to automate that.

holm-xie commented 4 years ago

I add a command showLatexCode in a naive way to insert the code just behind the selection(if it is a valid LaTex name in the latex.ts). but it works for me, here is the patch, hope it helps:

diff -ur ./package.json "..\\unicode-latex-master2020-01-17_1445\\unicode-latex-master/package.json"
--- ./package.json  2020-01-17 17:53:53.815184600 +0800
+++ "..\\unicode-latex-master2020-01-17_1445\\unicode-latex-master/package.json"    2019-06-23 05:44:28.000000000 +0800
@@ -17,7 +17,6 @@
   "activationEvents": [
     "onCommand:unicode-latex.insertMathSymbol",
     "onCommand:unicode-latex.replaceLatexNames",
-    "onCommand:unicode-latex.showLatexCode",
     "onLanguage:plaintext"
   ],
   "main": "./out/src/extension",
@@ -30,10 +29,6 @@
       {
         "command": "unicode-latex.replaceLatexNames",
         "title": "Unicode: Replace LaTeX"
-      },
-      {
-        "command": "unicode-latex.showLatexCode",
-        "title": "Unicode: Show LaTex Code"
       }
     ]
   },
diff -ur ./src/extension.ts "..\\unicode-latex-master2020-01-17_1445\\unicode-latex-master/src/extension.ts"
--- ./src/extension.ts  2020-01-17 17:37:23.673565300 +0800
+++ "..\\unicode-latex-master2020-01-17_1445\\unicode-latex-master/src/extension.ts"    2019-06-23 05:44:28.000000000 +0800
@@ -8,7 +8,6 @@
 let pickOptions: vscode.QuickPickOptions = {
     matchOnDescription: true,
 };
-var latexSymbols2: { [name:string]: string} = {};

 export function activate(context: vscode.ExtensionContext) {

@@ -18,7 +17,6 @@
             description: name,
             label: latexSymbols[name],
         });
-        latexSymbols2[latexSymbols[name]] = name;
     }

     let insertion = vscode.commands.registerCommand('unicode-latex.insertMathSymbol', () => {
@@ -27,9 +25,6 @@
     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);
@@ -37,7 +32,6 @@

     context.subscriptions.push(insertion);
     context.subscriptions.push(replacement);
-    context.subscriptions.push(showLatex);
     context.subscriptions.push(completionSub);
 }

@@ -84,24 +78,5 @@
     });
 }

-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() {}
Blaisorblade commented 4 years ago

@holm-xie Thanks! Can you please format that as code (add a line with three backticks before and one after)? It's hard to read otherwise...

holm-xie commented 4 years ago

Please check it out again, I did not aware the markdown render make the patch mess in my previous post. Since the patch is made in a reverse order, it should be applied like patch -R -p 1 < showLatexCode.patch if you use patch command to merge it to your copy of code.

Blaisorblade commented 4 years ago

Thanks @holm-xie! That works, I've opened a PR, with appropriate credit: I've not tagged you in the commit because that's annoying (it sends notifications at each rebase), but I can if you want.

As far as I can see, one must select the character, run the vscode command, and the latex command appears in the editor.

holm-xie commented 4 years ago

@Blaisorblade, a PR is fine, please. You're right, the selected character is for looking-up. If needed, without the hint of selection, we can also check the character around the cursor or even more. But I think the existing implemention is enough for my requirement.

holm-xie commented 4 years ago

@Blaisorblade, a PR is fine, please. BTW, change the line editBuilder.insert(editor.selection.end, latexSymbols2[text]); as following will make the command output more sensible:

editBuilder.insert(editor.selection.end, "("+latexSymbols2[text]+")");
let.wordRange = editor.document.getWordRangeAtPosition(editor.selection.start);
editor.document.getText(wordRange);