我正在尝试创建一个 VS 代码用户片段 useState()
目前我有
"use state": {
"prefix": "us",
"body": [
"const [$1, set${1/(.*)/${1:/upcase}/}] = useState($2);",
"$3"
],
"description": "creates use state"
},
Run Code Online (Sandbox Code Playgroud)
当我在 $1(位置 1)输入 'foo' 时,我得到:
const [foo, setFOO] as useState()
Run Code Online (Sandbox Code Playgroud)
但是我想得到:
const [foo, setFoo] as useState()
Run Code Online (Sandbox Code Playgroud)
如何更改我的代码段以这种方式工作?
我是 VSCode 新手。考虑到代码片段,我四处寻找一种在代码片段中编写脚本的方法。我的意思是做的不仅仅是填充或转换变量。例如...
这是一个简单的片段。我将输入rci类初始值设定项。当我输入方法参数时,我希望发生作业和文档以及其他一些事情。
rci<tab>然后def initialize(a, b))导致这样的结果......
attr_reader :a
attr_reader :b
# @param a [...] ...
# @param b [...] ...
def initialize(a, b)
@a = a
@b = b
end
Run Code Online (Sandbox Code Playgroud)
是否可以?如何才能实现呢?可能有任意数量的争论。每个参数都会触发类初始值设定项的另一行。
假设存在以下代码。
sample text
Run Code Online (Sandbox Code Playgroud)
当用户双击text,然后按{或 时(,它只会在保留文本的同时换行。
sample {text}
sample (text)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在 VS Code 设置中应用此规则$。我期望的是
sample $text$
Run Code Online (Sandbox Code Playgroud)
VS Code 中的哪个设置与此功能相关?
我使用vscode。
我想使用 costum 片段,但{TM_FILENAME}有一个扩展名。
如何删除扩展名{TM_FILENAME}?
像这样:`
MyModule.js:转变: ${TM_FILENAME/(\w+)\.js/\1/g}
MyModule
我正在尝试在Visual Studio代码中创建一个代码段.这有效,但缺少缩进:
我的片段:
"HTML structure": {
"prefix": "html",
"body": [
"<!DOCTYPE html>",
"<html lang='fr'>",
"<head>",
"<meta charset='UTF-8'>",
"<meta name='viewport' content='width=device-width, initial-scale=1.0'>",
"<meta http-equiv='X-UA-Compatible' content='ie=edge'>",
"<title>$1</title>",
"</head>",
"<body>",
"$2",
"</body>",
"</html>"
],
"description": "Base template for html file"
}
Run Code Online (Sandbox Code Playgroud)
你看到什么了 :
<!DOCTYPE html>
<html lang='fr'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
<title>test</title>
</head>
<body>
test
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想要的是什么:
<!DOCTYPE html>
<html lang='fr'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
<title></title>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个片段,该片段根据文件路径创建类名。如果文件名为index.js,我希望类名采用文件夹名称。否则,使用文件名。
index.js如果文件被命名(它正确插入文件夹名称),我有一个当前正在工作的转换(如下所示) 。
我将如何扩展这一点(假设有可能)也适用于第二种情况?
我确实从VSCode 文档中注意到,您可以使用一些基本的 if/else 格式,它们仅在捕获组存在时插入给定的文本。我已经能够让那些人通过一些简单的例子来工作。但不确定这些是否可以以某种方式用来完成我想做的事情。
当前片段:
{
"mySnippet": {
"prefix": "cls",
"body": [
"class ${TM_FILEPATH/[\\/\\w$]+\\/(?!index)|\\/index.js//g} {}",
"export default ${TM_FILEPATH/[\\/\\w$]+\\/(?!index)|\\/index.js//g};"
]
},
}
Run Code Online (Sandbox Code Playgroud) javascript regex code-snippets visual-studio-code vscode-snippets
在 Visual Studio 2019 中,我可以使用Alt++多行编辑“虚拟”空白,然后按任意键使所有行都用空格填充到所选列,我经常使用它来使初始化代码更易于阅读Shift。(Arrow Keys)然而,当我切换到 Visual Studio Code 时,我找不到等效的东西。我能找到的最接近的东西是Ctrl+ Alt+ (Arrow Keys)。这并不是我所需要的,因为它只将每行光标放在末尾,而不是前面示例中的“虚拟”空白。
这是 Visual Studio 2019 中的可视化示例(我不知道如何制作 GIF):
VSCode 中有类似的东西吗?还是我现在没有它?
VSCode 设置"javascript.preferences.quoteStyle": "single"对我来说不起作用,我的 JavaScript 代码片段中仍然出现双引号,如图所示:
有没有办法让 VSCode 使用单引号,而不必使用像 Prettier 这样的任何外部扩展?
编辑 \xe2\x80\x94 这对于 coc-snippets是不可能的。Ultisnips 可以实现这一点。
\n我有以下用于 React 的 vim 片段(带有 coc-snippets):
\nsnippet STATE_HOOK "useState hook" b\nconst [${1:state}, set${1:`!v expand(\'%:t:r\')`}] = useState($2)\nendsnippet\nRun Code Online (Sandbox Code Playgroud)\n这可用于快速创建以下(不正确的)代码:
\nconst [color, setcolor] = useState("green");\nRun Code Online (Sandbox Code Playgroud)\n问题是setcolor需要采用驼峰式,如下所示:setColor
如何编写此代码片段以使扩展输入大写?
\n在 Visual Studio 中,我想更新 fullprop 片段以根据属性名称自动转换字段名称:(因此只需输入“属性名称”和“类型”)
public string MyTest
{
get => _myTest;
set
{
if (_myTest == value) return;
_myTest = value;
RaisePropertyChanged();
}
}
private string _myTest;
Run Code Online (Sandbox Code Playgroud)
我找不到转换的MyProperty方法myProperty。我发现这个主题似乎适用于 VSCode,但在 Visual Studio 中不起作用。VSCode 中的代码片段似乎与 Visual Studio 不同,因为它$1$可以在 Visual Studio 中工作,但${1}不起作用。那么 Visual Studio 中有等效的方法吗?
感谢您的帮助 !
vscode-snippets ×10
javascript ×3
regex ×2
c++ ×1
formatting ×1
react-hooks ×1
reactjs ×1
ultisnips ×1
vim ×1