unnamed/slim 个片段 vscode
unnamed/slim snippets in vscode
我正在使用 vscode 编辑乳胶(使用乳胶工作室插件),并且
我最近开始创建自己的片段并且非常喜欢这个功能。但是,我发现创建 "small" 仅缩写频繁单词序列的片段的语法有点繁重。特别是,我觉得必须给每个片段一个 'name' 很麻烦。
是否有 "slim" snippets/aliases 的机制,例如将一个文件作为输入,其中每个片段都是一行——第一个单词是缩写,其余的缩写是什么?
你有几个选择。一种是编写一个可以执行此操作的扩展 - 我将展示像可以正常工作的扩展一样工作的代码 - 它看起来很复杂但使用起来非常简单。
其次,您可以使用 Hyper Snips 扩展名,您的代码片段文件(例如 latex.hsnips
)可能像这样:
snippet dategreeting "Gives you the current date!"
Hello from your hsnip on ``rv = new Date().toDateString()``!
endsnippet
snippet // "Fraction simple" A
\frac{}{}[=10=]
endsnippet
snippet stte A
some text to expand
endsnippet
“”中的描述不是必需的,我在最后一段中删除了它。 A
标志将立即插入您的替换文本,没有它您将 Tab 插入替换文本。如此处的示例所示,您可以根据需要在代码段中使用 javascript。
gif 显示得不是很好这里是 auto-expansion 与 Hyper Snips 的演示:
或者,下载扩展 macro-commander。它允许您在设置中的宏中使用 vscode 扩展命令。该宏将进入您的 settings.json
文件:
"macros": {
"slimSnippetsInsertion" : [
{
"javascript": [
"const editor = vscode.window.activeTextEditor;",
"const document = editor.document;",
"const we = new vscode.WorkspaceEdit();",
"const cursorPosition = editor.selection.active;", // use whether an actual selection or not, returns a Position
"let keyWordRange = document.getWordRangeAtPosition(cursorPosition);", // returns a Range of start/end Positions or undefined
"if (keyWordRange === undefined) {",
"await window.showInformationMessage(`cursor must be in or immediately after word to be replaced`);",
"return;",
"}",
"let wordAtCursor = document.getText(keyWordRange);", // this is the key word to find in slimSnippets.txt
"const thisWorkspace = vscode.workspace.workspaceFolders[0].uri.toString();",
// file:///c:/Users/Mark/OneDrive/Test Bed
"const snippetFileContent = await vscode.workspace.fs.readFile(vscode.Uri.parse(`${thisWorkspace}/.vscode/slimSnippets.txt`));",
"const snippets = snippetFileContent.toString();",
// ignore leading spaces/tabs before keys
// using a named capturing group for the replacement text
"const regex = new RegExp(`\r?(?<=\n|^)[\t ]*(?<key>${wordAtCursor})[\t ]+?(?<replacementText>.*?)(?=\r?\n|$)`);",
"let found = snippets.match(regex);", // returns null if no matches
// matched a key but only spaces as replacement text, so do nothing and exit
"if (found && found.groups.replacementText.trimStart().length === 0) {",
"await window.showInformationMessage(`replacement text is only spaces, not replacing`);",
"return;",
"}",
"if (found) {", // found at least a matching key
"if (found.groups.replacementText) {", // found non-space replacement text
// replace `\n` and `\t` with unicode values for newline and tab
"let replace = found.groups.replacementText.replace(/\\n/g, '\u000A').replace(/\\t/g, '\u0009');",
"let snippet = new vscode.SnippetString(replace)",
"if (editor.selections.length === 1) editor.insertSnippet(snippet, keyWordRange);", // if zero or one selection"
// if multiple selections, uses first key and replacement text"
"else editor.insertSnippet(snippet);",
"}",
"else await window.showInformationMessage(`matching key found but with no replacement text in slimSnippets.txt`);",
"}",
"else await window.showInformationMessage(`no matching key found in slimSnippets.txt`);",
]
}
您可以看到我在哪里读取位于工作区 .vscode
文件夹中的 simpleSnippets.txt
文件 - 但您可以更改位置,只要您更改命令:vscode.workspace.fs.readFile
以上。
slimSnippets.txt
文件只是一个简单的文本文件,其中每行的第一个单词是 key
,该行的其余部分是替换词。
howdy1 first sentence with tabstops
howdy1 this won't be used, duplicate key above
howdy2 second sentence with variable $TM_FILENAME
key3 videos 111111 // one space necessary between key and replacement text
// it will be removed, others retained
key1 222222
stte some text to expand
mt2e more text to expand
[replacement text can have placeholders, tabstops and choices just like regular snippets]
[join multiple-lines snippets into one string with newlines as below]
[some text\nsome more text] [\t can be used for tabs]
key5 line 1\n\tline 2\n\t\tline 3
键是单个单词,如果没有替换文本(或文件中键后只有空格),则不会发生任何事情 - 键不会被替换。
实际插入的文本可以是纯文本或使用 vscode 的代码片段格式 - 请参阅上面的示例文本文件。
光标必须紧跟在词的后面或词中,可以选择或不选择该词。它必须是正则表达式意义上的单词 - 不是在它之前或之后与单词相邻的连续文本 - 只是一个独立的单词,它可以在线的任何位置。
如果您有重复的密钥,将使用第一个。 key/replacement行之间可以有空格,也可以没有。
您不会获得按键的智能感知。我可能会解决这个问题。
最后,您需要一个键绑定来触发此宏(在 keybindings.json
中):
{
"key": "ctrl+;", // whatever keybinding you wish
"command": "macros.slimSnippetsInsertion"
},
我正在使用 vscode 编辑乳胶(使用乳胶工作室插件),并且 我最近开始创建自己的片段并且非常喜欢这个功能。但是,我发现创建 "small" 仅缩写频繁单词序列的片段的语法有点繁重。特别是,我觉得必须给每个片段一个 'name' 很麻烦。
是否有 "slim" snippets/aliases 的机制,例如将一个文件作为输入,其中每个片段都是一行——第一个单词是缩写,其余的缩写是什么?
你有几个选择。一种是编写一个可以执行此操作的扩展 - 我将展示像可以正常工作的扩展一样工作的代码 - 它看起来很复杂但使用起来非常简单。
其次,您可以使用 Hyper Snips 扩展名,您的代码片段文件(例如 latex.hsnips
)可能像这样:
snippet dategreeting "Gives you the current date!"
Hello from your hsnip on ``rv = new Date().toDateString()``!
endsnippet
snippet // "Fraction simple" A
\frac{}{}[=10=]
endsnippet
snippet stte A
some text to expand
endsnippet
“”中的描述不是必需的,我在最后一段中删除了它。 A
标志将立即插入您的替换文本,没有它您将 Tab 插入替换文本。如此处的示例所示,您可以根据需要在代码段中使用 javascript。
gif 显示得不是很好这里是 auto-expansion 与 Hyper Snips 的演示:
或者,下载扩展 macro-commander。它允许您在设置中的宏中使用 vscode 扩展命令。该宏将进入您的 settings.json
文件:
"macros": {
"slimSnippetsInsertion" : [
{
"javascript": [
"const editor = vscode.window.activeTextEditor;",
"const document = editor.document;",
"const we = new vscode.WorkspaceEdit();",
"const cursorPosition = editor.selection.active;", // use whether an actual selection or not, returns a Position
"let keyWordRange = document.getWordRangeAtPosition(cursorPosition);", // returns a Range of start/end Positions or undefined
"if (keyWordRange === undefined) {",
"await window.showInformationMessage(`cursor must be in or immediately after word to be replaced`);",
"return;",
"}",
"let wordAtCursor = document.getText(keyWordRange);", // this is the key word to find in slimSnippets.txt
"const thisWorkspace = vscode.workspace.workspaceFolders[0].uri.toString();",
// file:///c:/Users/Mark/OneDrive/Test Bed
"const snippetFileContent = await vscode.workspace.fs.readFile(vscode.Uri.parse(`${thisWorkspace}/.vscode/slimSnippets.txt`));",
"const snippets = snippetFileContent.toString();",
// ignore leading spaces/tabs before keys
// using a named capturing group for the replacement text
"const regex = new RegExp(`\r?(?<=\n|^)[\t ]*(?<key>${wordAtCursor})[\t ]+?(?<replacementText>.*?)(?=\r?\n|$)`);",
"let found = snippets.match(regex);", // returns null if no matches
// matched a key but only spaces as replacement text, so do nothing and exit
"if (found && found.groups.replacementText.trimStart().length === 0) {",
"await window.showInformationMessage(`replacement text is only spaces, not replacing`);",
"return;",
"}",
"if (found) {", // found at least a matching key
"if (found.groups.replacementText) {", // found non-space replacement text
// replace `\n` and `\t` with unicode values for newline and tab
"let replace = found.groups.replacementText.replace(/\\n/g, '\u000A').replace(/\\t/g, '\u0009');",
"let snippet = new vscode.SnippetString(replace)",
"if (editor.selections.length === 1) editor.insertSnippet(snippet, keyWordRange);", // if zero or one selection"
// if multiple selections, uses first key and replacement text"
"else editor.insertSnippet(snippet);",
"}",
"else await window.showInformationMessage(`matching key found but with no replacement text in slimSnippets.txt`);",
"}",
"else await window.showInformationMessage(`no matching key found in slimSnippets.txt`);",
]
}
您可以看到我在哪里读取位于工作区 .vscode
文件夹中的 simpleSnippets.txt
文件 - 但您可以更改位置,只要您更改命令:vscode.workspace.fs.readFile
以上。
slimSnippets.txt
文件只是一个简单的文本文件,其中每行的第一个单词是 key
,该行的其余部分是替换词。
howdy1 first sentence with tabstops
howdy1 this won't be used, duplicate key above
howdy2 second sentence with variable $TM_FILENAME
key3 videos 111111 // one space necessary between key and replacement text
// it will be removed, others retained
key1 222222
stte some text to expand
mt2e more text to expand
[replacement text can have placeholders, tabstops and choices just like regular snippets]
[join multiple-lines snippets into one string with newlines as below]
[some text\nsome more text] [\t can be used for tabs]
key5 line 1\n\tline 2\n\t\tline 3
键是单个单词,如果没有替换文本(或文件中键后只有空格),则不会发生任何事情 - 键不会被替换。
实际插入的文本可以是纯文本或使用 vscode 的代码片段格式 - 请参阅上面的示例文本文件。
光标必须紧跟在词的后面或词中,可以选择或不选择该词。它必须是正则表达式意义上的单词 - 不是在它之前或之后与单词相邻的连续文本 - 只是一个独立的单词,它可以在线的任何位置。
如果您有重复的密钥,将使用第一个。 key/replacement行之间可以有空格,也可以没有。
您不会获得按键的智能感知。我可能会解决这个问题。
最后,您需要一个键绑定来触发此宏(在 keybindings.json
中):
{
"key": "ctrl+;", // whatever keybinding you wish
"command": "macros.slimSnippetsInsertion"
},