在Visual Studio中使用自动代码完成功能时,是否可以修改IntelliSense输入的代码? [英] Is there a way to modify the code entered by IntelliSense when using auto code completion in Visual Studio?

查看:45
本文介绍了在Visual Studio中使用自动代码完成功能时,是否可以修改IntelliSense输入的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改当我按下 tab tab tab 时Visual Studio/Intellisense输入的自动填充代码.

我已经尝试过在在线论坛和Microsoft文档中进行搜索,但没有找到解决方案.

例如,当我键入"MessageBox.s"并按tab时,它将自动完成

  MessageBox.Show 

我想对其进行更改,以使其自动完成至

  MessageBox.Show("); 

通过这种方式,我不必输入总是要使用的括号,引号和分号,从而节省了时间.在Visual Studio中有什么方法可以做到这一点?我正在使用Visual Studio Community2017.或者是否可以使用第三方添加自定义行为?

解决方案

简短的回答:是的,不是.

据我了解,Visual Studio中有两种不同的自动填充代码类别:

  • 代码段.存储在以.snippet结尾的文件中的代码块.
  • 类及其方法

代码片段在VS文件中的不同位置(我的位置与施耐德的位置完全不同).这些就像 If 语句一样,可以打开和编辑文件.通过转到工具>代码片段管理器> CSharp> Visual C#,可以在VS中找到文件夹/文件位置.对我来说是C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ Community \ VC#\ Snippets \ 1033 \ Visual C#.如果双击其中一个文件,则可以在VS中将其打开并编辑代码(全部为XML).如果要编辑填充的代码,请使用"CDATA"查找代码的一部分:

 <代码语言="csharp"><![CDATA [foreach($ collection $中的$ type $ $ identifier $){$ selected $ $ end $}]]> 

您可以在Microsoft文档中进一步阅读语法的详细信息.但是,如果您想修改另一种自动填充的东西-内置库中的类及其方法-尽我所能,就没有办法做到;但是,有一种变通方法是使用快捷方式"创建自己的自定义代码段.我发现

当弹出新文件窗口时,选择XML文件.

摆脱默认的代码行,然后粘贴以下代码:

 <?xml version ="1.0" encoding ="utf-8"?>< CodeSnippets xmlns ="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">< CodeSnippet Format ="1.0.0"><标题>< Title> MessageBox.Show</Title><快捷方式&mes;</快捷方式></Header><片段><参考文献><参考>< Assembly> System.Windows.Forms.dll</Assembly></参考></参考文献><导入><导入><命名空间> System.Windows.Forms</命名空间></导入></导入><代码语言="CSharp"><![CDATA [MessageBox.Show(");]]></代码></Snippet></CodeSnippet></CodeSnippets> 

快捷方式标记是您将在代码项目中键入的内容,以触发此代码段作为建议.在CDATA之后的内括号之间将是为您自动填充的代码.

将文件保存到文档"文件夹(这是VS稍后将在其中查看的默认文件夹).将文件命名为MessageBoxShow.snippet

现在进入工具">代码片段管理器"

选择您的语言(Csharp),然后单击导入"

导航到刚才保存的MessageBox.Snippet文件,然后双击它.将打开导入代码段"对话框,要求您从右侧窗格的选择中选择将代码段添加到何处.选择之一应该是我的代码片段".选择它,然后单击完成

单击确定"关闭代码段管理器.

您现在应该可以输入"mess"并填写您的自定义代码段.否则,您可以尝试关闭并重新打开项目或VS.微软的教程说您可以稍后添加快捷方式标签,但是我尝试过这种方式,而VS却没有选择它,因此我不得不使用快捷方式标签 then import重新创建文件.它成功了!

当然,您基本上可以使用相同的方法来创建代码段&您想要的任何东西的捷径.您只需要更改标题,快捷方式和代码(CDATA)标记中的文本,然后将其保存在其他文件名下即可.

感谢Peter Schneider和Gil Sand的贡献.虽然您没有为我提供直接解决问题的方法,但是您确实帮助我找到了我想要的东西.

I want to modify the auto-fill code that Visual Studio/Intellisense enters in when I press tab or tab tab.

I have already tried searching in online forums and in the Microsoft documentation and I have not found a solution to what I am trying to achieve.

For example, when I type in "MessageBox.s" and press tab, it will auto complete with

MessageBox.Show

I would like to change it so that it will instead auto complete to

MessageBox.Show("");

This way I can save time by not having to type in the parenthesis, quotation marks and semicolon, which I am always going to use. Is there any way to do this in Visual Studio? I am using Visual Studio Community 2017. Or is there a third party add in I can use to customize the behavior?

解决方案

The short answer: Yes... and no.

As I understand it, there are two different categories of auto-fill code in Visual Studio:

  • Code snippets. Blocks of code that are stored in files ending in .snippet.
  • Classes and their methods

The code snippets are in various places in the VS files (mine are in a quite different place than Schneider's). These are things like If statements, and the files can be opened and edited. You can find the folder/file locations in VS by going to Tools >Code Snippets Manager >CSharp >Visual C#. For me it was C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC#\Snippets\1033\Visual C#. If you double click on one of the files you can open it in VS and edit the code, which is all XML. If you want to edit the code that fills in you look for a the part of the code with "CDATA":

<Code Language="csharp"><![CDATA[foreach ($type$ $identifier$ in $collection$)
{
    $selected$ $end$
}]]>

You can read more into the details of the syntax in the Microsoft Documentation. But if you want to modify the other type of auto-fill stuff - classes and their methods from the built in libraries - as best as I can gather there is no way to do it; however, there is a workaround in creating your own custom snippets with "shortcuts". I found this page from Microsoft to be helpful in figuring out how to do it. There were a couple of things I had to do differently though compared to the documentation, so I will provide an explanation here using the MessageBox.Show example in C#.

Go to File >New >File

When the new file window pops up, select XML file.

Get rid of the default line of code and paste in the following code:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MessageBox.Show</Title>
      <Shortcut>mess</Shortcut>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>System.Windows.Forms.dll</Assembly>
        </Reference>
      </References>
      <Imports>
        <Import>
          <Namespace>System.Windows.Forms</Namespace>
        </Import>
      </Imports>
      <Code Language="CSharp">
        <![CDATA[MessageBox.Show("");]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

The shortcut tag is what you will type in in your code project to trigger this snippet to come up as a suggestion. And what's in between the inner brackets after CDATA will be the code that auto-fills for you.

Save the file into your Documents folder (this is the default folder where VS will look later). Name the file MessageBoxShow.snippet

Now go into Tools >Code Snippets Manager

Select your language (Csharp), then click import

Navigate to the MessageBox.Snippet file you just saved a moment ago and double click it. The Import Code Snippet dialog opens, asking you to choose where to add the snippet from the choices in the right pane. One of the choices should be My Code Snippets. Select it and click Finish

Click OK to close the code snippets manager.

You should now be able to type in "mess" and have your custom snippet code fill in. If not, you may try closing and reopening your project or VS. The Microsoft tutorial said you can add in the shortcut tag later, but I tried it that way and VS didn't pick it up, so I had to recreate the file with the shortcut tag in place then import it and it worked!

Of course you can essentially use this same method to create a snippet & shortcut for whatever you want. You just need to change the text in the title, shortcut, and code (CDATA) tags and save it under a different file name.

Thank you Peter Schneider and Gil Sand for your contributions. While you did not provide me with a direct solution to my question, you did help me to find what I was looking for.

这篇关于在Visual Studio中使用自动代码完成功能时,是否可以修改IntelliSense输入的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆