【问题标题】:VS Code php tag autocompleteVS Code php 标签自动完成
【发布时间】:2019-09-08 06:19:27
【问题描述】:

有点琐碎的问题,但它就在这里。我无法弄清楚为什么 VS Code 正确地自动完成(使用选项卡)所有 html 标签,而 php 标签却没有。当我输入“php”并点击选项卡时,它会创建:

<php></php>

这是没用的,我不知道它为什么在那里。我想让它做普通的php标签

<?php ?>

我已关闭建议,因为它们分散了我的注意力。无论如何要编辑默认/顶部“选项卡自动完成”项目?

【问题讨论】:

    标签: php autocomplete visual-studio-code vscode-settings


    【解决方案1】:

    这个技巧对我有用:转到“首选项:打开用户片段”,然后将此 sn-p 粘贴到 html(是的,那是 html)用户 sn-ps:

    "php": {
        "prefix": "php",
        "body": [
            "<?php $1 ?>"
        ],
        "description": "php tag"
    }
    

    【讨论】:

    • 公平地说,我不知道为什么这不是一个内置的 sn-p。您想要添加&lt;php&gt; 标签的可能性有多大?有人可以向开发团队提出这个建议吗?
    • 这在报价内有效吗?在我的情况下不起作用...任何解决方法?
    • 非常感谢sn-p。不知道为什么 VS Code 没有内置它。
    • 为我工作。谢谢! (和@norixxx 有同样的问题。根据here 修复,在here 的帮助下)
    【解决方案2】:

    我将通过将由 $0 表示的最终光标放在标签之间的新行中来详细说明 Lane 的答案。在 sn-p 中,“body”数组中的每个逗号代表一个新行。

    所以在 Visual Sutiio Code 中转到 File > Preferences > User Snippets 搜索“html.json” 并通过粘贴来编辑它:

    {
      // Place your snippets for html here. 
      "php": {
        "prefix": "php",
        "body": ["<?php", "$0", "?>"],
        "description": "php tag"
      }
    }
    

    【讨论】:

      【解决方案3】:

      在 Visual Studio Code 中,转到 File &gt; Preferences &gt; User Snippets &gt; html.json 文件,然后粘贴以下代码:

      "php": {
          "prefix": "php",
          "body": [
              "<?php $1 ; ?>",
              "$2"
          ],
          "description": "php tag"
      }
      

      【讨论】:

        【解决方案4】:

        File > Preferences下有User Snippets,使用html添加php sn-ps。

        {
          "Snippet name": {
          "prefix": ["keywords", "for", "finding"],
          "body": [
            "<?php ${1:placeholder} ?>"
          ],
          "description": "A php tag."
          }
        }
        

        $1、$2 等是输入 sn-p 时将聚焦的变量。您还可以使用我的示例中介绍的占位符。

        对于多行正文,以逗号结束当前行并在“ ”中的新行添加内容。您可以在此处找到有关在 VS Code 中创建自己的 sn-ps 的更多信息:

        https://code.visualstudio.com/docs/editor/userdefinedsnippets

        【讨论】:

          【解决方案5】:

          进入 File > Preferences > User Snippets 现在写入 bar php 并按下回车键。

          vs 代码生成文件名为 php.code-sn-ps ,现在在这里粘贴:

            { 
          
              // Place your GLOBAL snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
              // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
              // is left empty or omitted, the snippet gets applied to all languages. 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:
              "php": {
                      
                  "prefix": "php",
                  "body": [
                      "<?php //put here code ?>"
                  ],
                  "description": "php tag"
              } 
          }
          

          【讨论】:

            【解决方案6】:

            据我了解,您没有添加外部 PHP IntelliSense 扩展并使用 VS Code 提供的默认 PHP IntelliSense,如果是这种情况,您需要将扩展​​添加到您的Visual Studio 代码编辑器。

            要添加扩展,请使用键盘并按 Ctrl+Shift+p 您将获得命令面板并输入以下命令 Extensions: Install Extension 然后在编辑器的左侧面板中您将获得搜索扩展的选项,您可以搜索PHP IntelliSense 并安装它。编辑器将要求重新启动编辑器以应用扩展。

            我建议通过在编辑器的设置 json 配置中添加以下属性 "php.suggest.basic":false 来禁用 VS Code 的内置 PHP IntelliSense(键盘按 Ctrl+, 您将获得设置 json 到编辑的权利)以避免重复的建议。

            要使 PHP IntelliSense 正常工作,您至少需要安装 PHP 7,您可以将其添加到 PATH 或在设置 json 中设置 "php.executablePath":"php physical path"(键盘按 Ctrl+, 您将获得设置 json)

            【讨论】:

            • 感谢您的宝贵时间,但我确实有 PHP IntelliSense 分机。 (包括 PHP7)安装,甚至在听从你的建议并禁用内置的,我希望能做到这一点,它做同样的事情。
            • 我没有遇到多少人对使用 VS Code for PHP 感到满意。
            【解决方案7】:

            我更喜欢在 keybindings.json 中添加自定义键盘快捷键 - 这可以让您在智能感知无法正常工作的地方添加 php 标签,例如在 html 属性的引号内。您还可以选择一些文本(也许您从其他地方复制了一些没有完整 php 标签的 php)并自动将其包装在 php 标签中。这是我使用的:

            {
              "key": "cmd+alt+ctrl+p",
              "command": "editor.action.insertSnippet",
              "when": "editorTextFocus",
              "args": {
                "snippet": "<?php $1$TM_SELECTED_TEXT$0 ?>"
              }
            }
            

            【讨论】:

              【解决方案8】:

              也许你应该安装 PHP Awesome Snippetts 扩展。

              【讨论】:

                猜你喜欢
                • 2016-09-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-10-30
                • 1970-01-01
                • 2019-07-28
                • 1970-01-01
                相关资源
                最近更新 更多