VSCode - Make custom user snippets overwriting default suggestion - visual-studio-code

To match my company's coding standards, I'm making custom snippets like so:
// php.json
{
"If block": {
"prefix": ["if"],
"body": ["if ( ${1:condition} )", "{", "\t$0", "}"],
"description": "if block",
},
// ...
}
Then, when I type if + Tab, I have two suggestions:
I'm forced to press enter to actually select the first one, which is my custom made snippet ; the second being PHP's default snippet.
Is there a way to actually ignore PHP's default snippets in case I have the exact same prefix?

Related

Golang VS Code Snippet: Mimic "built-in" `variable.print!` snippet

Context: The Golang VS Code extension has built-in snippets/macros for creating a fmt.PrintX statement from a given variable:
Note how the variable name is filled in for me.
I frequently write methods, and the Golang extension's meth snippet, in my opinion, is slow, since there are 5 (!) tab stops:
So far, I've written a snippet that mimics the meth snippet:
"Struct Method": {
"prefix": ".meth",
"body": [
"func ($1 $2) $3($4) $5 {",
"\t$6",
"}"
],
"description": "some description"
}
And I would like this snippet to mimic the print! macro/snippet, where it fills in the variable name; for the method snippet, instead of filling a variable, it would automatically fill in the method receiver ($1 and $2) with the first letter of the struct name, and the full struct name in the second.
So, if I have the following struct:
type SomeStruct struct {}
and type:
SomeStruct.meth
then activate the snippet, it should output:
func (s SomeStruct) .(.) . {
.
}
where each . is a tab stop.
Is this sort of snippet possible? If so, how can I write it so it does this?
One thing you can do is to make a snippet in a keybinding that can parse the current line. Put this into your keybindings.json:
{
"key": "alt+w",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "\n\nfunc (${TM_CURRENT_LINE/\\s*type\\s*(.)(.*)\\s* struct\\s*{}/${1:/downcase} $1$2)/} $1($2) $3 {\n\t$4\n}"
},
// "when": "langId == golang"
}
The workflow is different than what you mentioned but is also easier - you don't have to type SomeStruct.meth. Just trigger the keybinding at the end of the type SomeStruct struct {} line as in the demo.
If you want to be anywhere in the document and trigger a snippet completion, then the HyperSnips extension may be the way to go.

Variable does not expand in transformation

I am working on a VS code extension for a custom language. This contains a snippet that allows the user to create a function with parameters and start to set up some documentation inside the function. The way I am achieving this is to use variable transforms on the parameter variable (${2}) using some matching tricks to extract each individual parameter and data type. In this simpler example, each parameter name will be on its own line inside a block comment.
"function": {
"prefix": "func",
"body": [
"function ${1:functionName}(${2:params})",
"${2/([^,]+), *|([^,]+$)/\t${BLOCK_COMMENT_START} ${1}${2} ${BLOCK_COMMENT_END}\n/g}",
"end"
]
}, ...
The expected output is this:
function functionName(hello,world)
{ hello }
{ world }
end
The actual output is this:
function functionName(hello,world)
${BLOCK_COMMENT_START} hello ${BLOCK_COMMENT_END}
${BLOCK_COMMENT_START} world ${BLOCK_COMMENT_END}
end
The issue is that ${BLOCK_COMMENT_START} and _END do not expand inside the transform. I can easily get around this issue by hardcoding the curly braces in the snippet, but I would prefer to find a way where I do not have to do that.
"body": [
"function ${1:functionName}(${2:params})",
"${2/([^,]+), *|([^,]+$)/\t{ ${1}${2} }\n/g}",
"end"
]
Is there a way that I can have a variable expand in the transform?

How to use partial match on vscode-icons

I trying to add custom icons on vscode-icons.
Unfortunately my project file names have not extension naming rule.
They just end with '*Style.tsx' like 'ButtonStyle.tsx' or 'BoxStyle.tsx'
// this not work
{
"icon": "styled-components",
"format": "png",
"filenamesGlob": ["*Style", "*style"],
"extensionsGlob": ["tsx", "ts"],
"filename": true,
}
I noticed that vscode-icons not support regex.
Is there any other way without renaming files?
Thanks

How can I select a part of TM_DIRECTORY variable?

I want to select a part of the TM_DIRECTORY variable on VScode snippet. I mean I want to select Tests\Setup of the d:\Projects\Hakhsin\hakhsin\tests\Setup in code-snippet file. Look at this:
// On snippet file
"PHP Class": {
"scope": "",
"prefix": ["phpClass"],
"body": [
"<?php\n\nnamespace ${TM_DIRECTORY/(?<=(?:[\w:\\]hakhsin\\)).+(?=\\)//};\n\nclass ${TM_FILENAME_BASE} {\n\t$2\n}"
],
"description": "New PHP Class"
},
And I want to get this result:
namespace Tests\Setup;
class StorageFactory {
}
But I get this result:
<?php
namespace d:\Projects\Hakhsin\hakhsin\tests\Setup;
class StorageFactory {
}
It doesn't appear you can use variables inside of other variables in a snippet transform.
You can try this code which is more "dynamic" than yours but not perfect:
"${TM_DIRECTORY/([^\\\\]*\\\\){4}(.*)/${2:/capitalize}/}",
The {4} in that is if you have 4 segments in the directory structure before the part you want, like d:\Projects\Hakhsin\hakhsin\tests\Setup The segments are d:\, Projects\, Hakhsin\ and hakhsin\. If that number of segments is known and stable than the snippet I showed works well.
I doubt the number of those segments would vary within a project but might very well between projects - you would just have to change the {x} item for each project if so.
I solved it but it is not dynamic.
namespace ${TM_DIRECTORY/(.*hakhsin\\\\)(.*)/${2:/capitalize}/};
Is there a better way? anything like this:
namespace ${TM_DIRECTORY/(.*${WORKSPACE_NAME}\\\\)(.*)/${2:/capitalize}/};
This is the result:
<?php
namespace Tests\Setup;
class StorageFactory {
}

How to auto capitalize in custom snippet for user input

In my framework we are having a code structure like:
varname => ${VARNAME}
I am trying to use VSCode user snippet to make it working but unable to capitalize given varname!
https://code.visualstudio.com/updates/v1_25#_snippet-placeholder-transformations
Tried this exact code but not working!!
Also tried already solved solutions also. But none of them working.
VS Code: How to convert snippet placeholder to uppercase or lowercase?
When using variable transformations like TM_FILENAME, capitalize working.
(https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables)
"Insert_const_variable": {
"prefix": "const",
"body": "${1:varname} -> ${1/(.*)/${1:/upcase}/}",
"description": "Declare const"
}
expected : const -> CONST
actual : const -> const
Thanks in advance.