How to display snippets using Regex matching code in VS Code - visual-studio-code

I tried to read the documentation to write a extensions, but the information I got was very limited. I wanted to match the code dynamically and output a snippet of code via RE, but there seems to be no way
{
"quick show": {
"prefix": "/.*[\\/](.*[\\/].*)/",
"body": "$1",
"description": "Code snippet"
}
}
Is there any good way to do that

Related

How to manually add snippets to VSCode

There are a slew of questions/answers on this, and I went through them without success - here's my setup...
Extensions:
snippets.json file location -- C:\Users\USERNAME.vscode\snippets.json
Suggestion Settings:
Added Emmet Path (redacted):
Snippets.json
{
"Less": {
"scope": "php html",
"prefix": "!less",
"body": [
"<script src='https://cdn.jsdelivr.net/npm/less'></script>"
],
"description":"Adds the Less.CSS CDN"
},
"MooTools": {
"prefix": "!cdnmt",
"body": [
"<script src='https: //cdnjs.cloudflare.com/ajax/libs/mootools/1.6.0/mootools-core.min.js' integrity='sha512-P6QjhxYLbTLOHQpilU3lsmhwLfgVh/zN5rVzcGbmiTWhMcpkHVk8eqWk2vqvM0Z6OhJfrdbYoTzJtGQYUj8ibw==' crossorigin='anonymous' referrerpolicy='no-referrer'></script>"
]
}
}
Right now, neither quicktype works. I've tried !less - there's no conflicting shortcut and hitting tab or enter after it doesn't do anything other than tab or enter.
Same goes for MooTools...
Some reviewed links:
here-are-the-default-emmet-settings-in-visual-studio-code
visual-studio-code-user-snippets-not-working
I've gone through the myriad of answers and tried just about everything. Nothing seems to work. I also am unable to locate/edit the defaultSettings.json (opens as read only in vs).
Additional Information (1)
Tried updating included files:
Still not working (FYI - I did restart VSCode to make sure that wasn't blocking it from taking over).
The simplest way to add snippets is to use the interface, either the "File/ Preferences/ Configure User Snippets" menu entry or the similarly named element in the gear icon you can find on the side bar:
You can then choose to add global snippets (to be used in any file type) or per-language snippets:
The program takes care of creating a file with the appropriate name and extension in the correct location. It also adds some boilerplate with documentation. This way you don't need to do prior research about your environment.
{
// Place your snippets for sql here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}

How do I visually replace an expression in VS Code with a symbol?

I am using Visual Studio Code for Python development.
A very common expression in python is lambda x: x which can be a little verbose when used 40+ times in a module.
Does VS code allow for this to be replaced with a symbol? eg. replacing lambda with λ?
So code like df.apply(lambda x: x**2) would appear as df.apply(λx: x**2)
To clarify, I DONT want to modify the source code, I just want it to be displayed on my code editor using the shorthand.
Very late to the party, but I recommend using this extension: prettify-symbols-mode
Adding following config to settings.json worked for me:
"prettifySymbolsMode.substitutions": [{
"language": "python",
"revealOn": "active-line",
"substitutions": [
{ "ugly": "lambda", "pretty": "λ", "post": "\\s*(?:\\w|_).*?\\s*:" }
]
}]

Snippet with function name

I want to add user code snippet and I want to have the function name in it, something like this:
{
"Print to console": {
"prefix": "clog",
"body": [
"console.log('CONSOLE', '$FUNC_NAME$', $1);"
],
"description": "Log to console"
}
}
I can't find how to get the function name in VSCode snippets.
Any help?
You'll need to write an extension to do this. VS Code's user snippets cannot access program structure information, such as class or function names.
Check out the completions extension sample to get started.

How do I add a snippet programmatically?

We have some custom snippets we provide as part of our VS Code extension via key bindings and a snippets json file:
{
"key": "ctrl+shift+i",
"mac": "cmd+shift+i",
"command": "editor.action.insertSnippet"
},
...
"snippets": [
{
"language": "xml",
"path": "./snippets/xml.json"
}
]
We would like a button to add one particular snippet to the editor at the current cursor position.
How do I programmatically I invoke the part of "editor.action.insertSnippet" after the user has selected the snippet?
I posted this issue on the vscode repo.
jrieken responded with the following reply:
The insertSnippet-command accepts an argument which is either the name of a snippet or a snippet itself. So, either { snippet: "console.log($1)$0"} for an inline snippet or { langId: "csharp", name: "myFavSnippet" } referencing an existing snippet.
You can run any registered command via vscode.commands.executeCommand. See also the vscode namespace API.

Cannot auto-complete c++ in vs code

Hey I recently started using vs code and am having a really weird issue. where my code won't auto complete. I have installed the c/c++ extension and have the "c_cpp_properties.json" file containning the following
{
"configurations": [
{
"name": "Mac",
"includePath": ["/usr/include"]
},
{
"name": "Linux",
"includePath": ["/usr/include"]
},
{
"name": "Win32",
"includePath": ["c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include"]
}
]
}
Before you ask I have copied proper include path to "/usr/include" I just did a really simple test I wrote 1 line
"VkInstance instance;"
It compiled and ran fine but whenever I start to type VkInstance It never comes up as auto complete. Moreover weirdly some stuff auto completes fine and other stuff doesn't for example "vkCreateInstance" shows up as i'm typing it. Perhaps I'm missing something or doing something wrong would like some feedback
Thanks!
#include <vulkan/vulkan.h>
int main()
{
VkInstance instance;
return 0;
}
This seems to be a limitation (or bug?) of VSCode's code completion feature. The Vulkan header wraps the typdefs for all handles (like VkInstance, VkFence, etc.) in a Macro and it looks like VSCode can't handle this.
E.g. this:
VK_DEFINE_HANDLE(VkInstance)
Won't autocomplete, but if you replace the macro by hand to get this:
typedef struct VkInstance_T* VkInstance;
Auto-Completion works.
If this is a bug (just had a quick look at this), it may be worth reporting over at https://github.com/Microsoft/vscode-cpptools/issues