While working on my Java project in VS Code, I stumbled upon some really helpful code snippets:
Once I type in a variable name and add .sysout
, .cast
, or similar, the snippet suggestion appears. Upon insertion, it translates to:
res.sysout => System.out.println(res);
res.cast => (()(res))
...
I got curious about how this feature is implemented in VS Code. Unfortunately, my search at redhat-developer/vscode-java didn't yield any useful information.
That's when I thought of adapting these snippets to work with JS/TS and other languages.
After going through VS Code's documentation on snippets, I found some potentially relevant details:
The following variables can be used:
TM_SELECTED_TEXT
: The currently selected text or an empty stringTM_CURRENT_LINE
: The contents of the current line- ...
However, when attempting to create my own snippet, I struggled with replacing the variable name. In other words, even though I could retrieve the variable name using TM_CURRENT_LINE
and insert a snippet, the previous variable name would still remain. For example,
res.log => resconsole.log(res);