Visual Studio代码用户代码段不起作用

时间:2017-06-02 04:49:09

标签: visual-studio-code

我已启用默认" log" VS Code中的代码片段。我还添加了另一个片段。我输入前缀时都不显示。我已将editor.tabCompletion设置为true。

知道为什么这些不起作用?

21 个答案:

答案 0 :(得分:34)

有几种方法可以解决这个问题。问题是Intellisense正在阻碍或默认将您的片段放在列表的底部。

首先我使用了建议延迟,但后来我决定让Intellisense将我的代码段放在使用snippetSuggestions用户首选项的建议列表的顶部:

{
  "editor.tabCompletion": true,
  "editor.snippetSuggestions": "top"
}

答案 1 :(得分:13)

有时候,首先被忽视的显而易见的东西。 在启动VSCode并创建新文档(Ctrl + N)时,这些片段不起作用可能会令人惊讶。 默认情况下,检查VSCode编辑器的右下角,将显示“纯文本”文件。使用摘要将文件类型更改为您喜欢的语言。

这可以克服一些不必要的麻烦;-)

答案 2 :(得分:8)

如果您尝试插入PHP代码段,值得注意的是您必须手动包含

<?php

在片段开始工作之前。

答案 3 :(得分:6)

我实际发现的是,当你拥有一个php intelliSense并且你想要一个 php 标签标签来打开和关闭时,我也将扩展名添加到一个html文件类型中。

   {
      "Open php tag": {
         "scope": "php, html",
         "prefix": "php",
         "body": [
           "<?php",
             "  $1",
           "?>"
        ],
        "description": "Opens php tags"
      }
   }

这对我有用:)

答案 4 :(得分:4)

  1. 打开一个常见的html文件。
  2. 在右下角使用插件(例如:Django HTML),然后单击它
  3. vscode顶部将打开一个窗口
  4. 单击“为...配置文件关联。'html'...”
  5. 搜索“ HTML”(需要使用大写锁定)
  6. 选择第一个选项

    https://i.stack.imgur.com/E5FyH.png https://i.stack.imgur.com/VhJ7n.png

答案 5 :(得分:4)

要与打字稿反应,我必须在范围内专门添加“ typescriptreact”。

 "scope": "javascript, typescript, typescriptreact"

答案 6 :(得分:3)

选择范围时,扩展很重要。我最近已从使用.js扩展名进行反应,改为使用.tsx扩展名来进行打字稿反应。我的代码段说“ javascript,打字稿”,所以我以为我被覆盖了,但是tsx文件实际上被认为是“ typescriptreact”。我更改为以下内容,它开始在我的tsx文件上起作用:

{   
    "Styled Component React Arrow Function With Default Export": {
    "scope": "javascript, javascriptreact, typescript, typescriptreact",
    "prefix": "scraf",
    "body": [
        "import React from 'react'",
        "import styled from 'styled-components/macro'",
        "",
        "const Container = styled.div`",
        "  display: flex;",
        "`",
        "",
        "const ${TM_FILENAME_BASE} = () => {",
        "  return (",
        "    <Container>",
        "     $1",
        "    </Container>",
        "  )",
        "}",
        "export default ${TM_FILENAME_BASE}",
    ],
    "description": "Styled Component React Arrow Function With Default Export"
}

答案 7 :(得分:2)

对于那些正在寻找覆盖默认代码段(日志)的用户

在用户摘录(至少对于打字稿)中,覆盖log.json文件摘录对我不起作用。

解决方案: 我创建了一个新的全局代码段(“文件”>“首选项”>“用户代码段”>“新的全局代码段”文件),并使用了不同的名称“ clog”(全名:“ clog.code-snippets”)和前缀。

答案 8 :(得分:2)

当我创建第一个代码段时,我也遇到了这个问题。我添加了片段,没有任何反应。

重新启动VS Code并开始工作。

答案 9 :(得分:1)

我的问题是某些片段有效,而其他片段则不然。 (例如:JavaScript Snippets效果很好)

当我安装了一个添加了相应的特定文件类型关联的插件(我的情况:* .blade.php)时,我的问题就解决了

在blade.json上创建UserSnippets时,它工作正常。

  1. 检查文件扩展名,看看是否有适合您文件类型的插件。
  2. 安装并尝试重新添加代码段。

答案 10 :(得分:1)

我试图设置一个片段来自动将php打开和关闭标签添加到我的文件中,因为Robert Cooper注意到它只在我已经在代码特定类型块内写入时才起作用。

每当我在HTML块中使用它时,我都会!p加载<?php$1?>。该片段已设置为html.json文件。

答案 11 :(得分:1)

只需重新启动VScode 。没关系。

答案 12 :(得分:0)

此问题的原因是另一种语言在 VS Code 中处于活动状态。检查左下角 VS Code 将语言更改为所需的语言。截图如下。 enter image description here

点击C++,顶部会出现一个输入框。输入适当的语言,例如 HTML,问题将得到解决。

enter image description here

答案 13 :(得分:0)

有用的 Python 代码片段

{
    "if": {
        "prefix": "if",
        "body": ["if ${1:expression}:", "\t${2:pass}"],
        "description": "Code snippet for an if statement"
    },
    "if/else": {
        "prefix": "if/else",
        "body": ["if ${1:condition}:", "\t${2:pass}", "else:", "\t${3:pass}"],
        "description": "Code snippet for an if statement with else"
    },
    "elif": {
        "prefix": "elif",
        "body": ["elif ${1:expression}:", "\t${2:pass}"],
        "description": "Code snippet for an elif"
    },
    "else": {
        "prefix": "else",
        "body": ["else:", "\t${1:pass}"],
        "description": "Code snippet for an else"
    },
    "while": {
        "prefix": "while",
        "body": ["while ${1:expression}:", "\t${2:pass}"],
        "description": "Code snippet for a while loop"
    },
    "while/else": {
        "prefix": "while/else",
        "body": ["while ${1:expression}:", "\t${2:pass}", "else:", "\t${3:pass}"],
        "description": "Code snippet for a while loop with else"
    },
    "for": {
        "prefix": "for",
        "body": ["for ${1:target_list} in ${2:expression_list}:", "\t${3:pass}"],
        "description": "Code snippet for a for loop"
    },
    "for/else": {
        "prefix": "for/else",
        "body": ["for ${1:target_list} in ${2:expression_list}:", "\t${3:pass}", "else:", "\t${4:pass}"],
        "description": "Code snippet for a for loop with else"
    },
    "try/except": {
        "prefix": "try/except",
        "body": ["try:", "\t${1:pass}", "except ${2:expression} as ${3:identifier}:", "\t${4:pass}"],
        "description": "Code snippet for a try/except statement"
    },
    "try/finally": {
        "prefix": "try/finally",
        "body": ["try:", "\t${1:pass}", "finally:", "\t${2:pass}"],
        "description": "Code snippet for a try/finally statement"
    },
    "try/except/else": {
        "prefix": "try/except/else",
        "body": [
            "try:",
            "\t${1:pass}",
            "except ${2:expression} as ${3:identifier}:",
            "\t${4:pass}",
            "else:",
            "\t${5:pass}"
        ],
        "description": "Code snippet for a try/except/else statement"
    },
    "try/except/finally": {
        "prefix": "try/except/finally",
        "body": [
            "try:",
            "\t${1:pass}",
            "except ${2:expression} as ${3:identifier}:",
            "\t${4:pass}",
            "finally:",
            "\t${5:pass}"
        ],
        "description": "Code snippet for a try/except/finally statement"
    },
    "try/except/else/finally": {
        "prefix": "try/except/else/finally",
        "body": [
            "try:",
            "\t${1:pass}",
            "except ${2:expression} as ${3:identifier}:",
            "\t${4:pass}",
            "else:",
            "\t${5:pass}",
            "finally:",
            "\t${6:pass}"
        ],
        "description": "Code snippet for a try/except/else/finally statement"
    },
    "with": {
        "prefix": "with",
        "body": ["with ${1:expression} as ${2:target}:", "\t${3:pass}"],
        "description": "Code snippet for a with statement"
    },
    "def": {
        "prefix": "def",
        "body": ["def ${1:funcname}(${2:parameter_list}):", "\t\"\"\"", "\t${3:docstring}", "\t\"\"\"","\t${4:pass}"],
        "description": "Code snippet for a function definition"
    },
    "def(class method)": {
        "prefix": "def(class method)",
        "body": ["def ${1:funcname}(self, ${2:parameter_list}):", "\t\"\"\"", "\t${3:docstring}", "\t\"\"\"", "\t${4:pass}"],
        "description": "Code snippet for a class method"
    },
    "def(static class method)": {
        "prefix": "def(static class method)",
        "body": ["@staticmethod", "def ${1:funcname}(${2:parameter_list}):", "\t\"\"\"", "\t${3:docstring}", "\t\"\"\"", "\t${4:pass}"],
        "description": "Code snippet for a static class method"
    },
    "def(abstract class method)": {
        "prefix": "def(abstract class method)",
        "body": ["def ${1:funcname}(self, ${2:parameter_list}):", "\t\"\"\"", "\t${3:docstring}", "\t\"\"\"", "\traise NotImplementedError"],
        "description": "Code snippet for an abstract class method"
    },
    "class": {
        "prefix": "class",
        "body": ["class ${1:classname}(${2:object}):", "\t\"\"\"", "\t${3:docstring}", "\t\"\"\"", "\t${4:pass}"],
        "description": "Code snippet for a class definition"
    },
    "lambda": {
        "prefix": "lambda",
        "body": ["lambda ${1:parameter_list}: ${2:expression}"],
        "description": "Code snippet for a lambda statement"
    },
    "if(main)": {
        "prefix": "__main__",
        "body": ["if __name__ == \"__main__\":", "    ${1:pass}"],
        "description": "Code snippet for a `if __name__ == \"__main__\": ...` block"
    },
    "async/def": {
        "prefix": "async/def",
        "body": ["async def ${1:funcname}(${2:parameter_list}):", "\t${3:pass}"],
        "description": "Code snippet for an async statement"
    },
    "async/for": {
        "prefix": "async/for",
        "body": ["async for ${1:target} in ${2:iter}:", "\t${3:block}"],
        "description": "Code snippet for an async for statement"
    },
    "async/for/else": {
        "prefix": "async/for/else",
        "body": ["async for ${1:target} in ${2:iter}:", "\t${3:block}", "else:", "\t${4:block}"],
        "description": "Code snippet for an async for statement with else"
    },
    "async/with": {
        "prefix": "async/with",
        "body": ["async with ${1:expr} as ${2:var}:", "\t${3:block}"],
        "description": "Code snippet for an async with statement"
    },
    "ipdb": {
        "prefix": "ipdb",
        "body": "import ipdb; ipdb.set_trace()",
        "description": "Code snippet for ipdb debug"
    },
    "pdb": {
        "prefix": "pdb",
        "body": "import pdb; pdb.set_trace()",
        "description": "Code snippet for pdb debug"
    },
    "pudb": {
        "prefix": "pudb",
        "body": "import pudb; pudb.set_trace()",
        "description": "Code snippet for pudb debug"
    },
    "add/new/cell": {
        "prefix": "add/new/cell",
        "body": "# %%",
        "description": "Code snippet to add a new cell"
    },
    "mark/markdown": {
        "prefix": "mark/markdown",
        "body": "# %% [markdown]",
        "description": "Code snippet to add a new markdown cell"
    }
}

答案 14 :(得分:0)

我不得不:

  1. 转到设置(Windows/Linux:文件>首选项>设置,macOS:代码>首选项>设置)并输入快速< /em> 在搜索框中,然后单击在 settings.json 中编辑enter image description here

  2. 添加

"editor.quickSuggestions": true

在相应的部分下 文件类型(在我的情况下markdown):

答案 15 :(得分:0)

万一其他人遇到此问题,我正在使用本机响应,而我的JavaScript代码段则无法使用。我通过转到设置来检查settings.json,搜索快速建议,然后在settings.json中进行编辑。

False

否则,您可以为javascriptreact创建代码段。

答案 16 :(得分:0)

对于面对此问题的其他任何人,请确保还检查摘要中定义的范围。尽管文件类型匹配,但片段没有显示给我,从范围内删除语言对我来说解决了这个问题。

答案 17 :(得分:0)

从vscode扩展市场安装CodeLLDB平台软件包。应该可以解决您的问题

答案 18 :(得分:0)

"editor.tabCompletion":"on"更改为Part_No对我有用。

答案 19 :(得分:0)

不要忘记在全局代码段中指定语言。就我而言,它已修复。

@ContextConfiguration(classes = ExecutableMain.class, initializers = ConfigFileApplicationContextInitializer.class)

答案 20 :(得分:0)

在我的情况下,由于代码段名称中有一个空格,因此无法正常工作

{
  ...,
  "snippet name": { // it should be snippet_name. Notice the _
    ...
  }
}

因此请确保:

  1. 您在{name}.code-snippets文件夹下有一个.vscode文件
  2. 您的代码段名称不包含spaces
  3. 您将语言包括在范围内。我必须同时添加typescripttypescriptreact