如何使用Visual Studio自动大括号完成来包围现有块? [英] How to use Visual Studio automatic brace completion to surround an existing block?

查看:942
本文介绍了如何使用Visual Studio自动大括号完成来包围现有块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Visual Studio自动大括号完成的问题在于它忽略了我正在尝试包围整个块。出于这个原因,我习惯性地禁用它,但是他们已经将这个默认为'on'多年,所以我想我会试着找出我是否遗漏了一些关于如何有效使用Visual Studio的内容......不会这是我多年来第一次不知道一些基本的编辑功能。

The problem I have with Visual Studio automatic brace completion is that it neglects to understand that I'm trying to surround an entire block. For this reason I habitually disable it, but they've defaulted this to 'on' for years, so I thought I'd try to find out if I'm missing something about how to use Visual Studio efficiently... wouldn't be the first time I went for years without knowing some basic editing feature.

主要的问题是当我尝试环绕文本时,从<$ c $开始c>围绕命令列表锁定(myMutex){:

The main problematic case is when I try to surround text, starting with something like lock (myMutex) { around a list of commands:

// Imagine I just typed the following line, where ^ is the cursor
lock (myMutex) {^ }  // <---  it just auto-added " }" to the end.
command1();
command2();
command3();
// where I plan to put the "}"

我必须按删除正确的次数撤消自动格式化和错误放置的大括号命令,以便继续向下搜索光标以完成周围。

I then have to press delete the correct number of times to undo the auto-formatting and the wrongly placed braces command, in order to proceed with scooting the cursor down to complete the surrounding.

这似乎是到目前为止是常见的情况,使支架自动完成非常烦人。难道我做错了什么?是否有一些我可以输入的键盘序列,这将导致我想要的东西,我只是不知道?或者我是否正确每次重新安装VS时都禁用它?

This seems to be by far the common case, making the braces autocompletion very annoying. Am I doing something wrong? Is there some keyboard sequence I can enter that would sanely result in what I want that I should be using, that I just don't know about? Or am I correct to just disable this every time I reinstall VS?

推荐答案

据我所知,内置任何内容都没有在Visual Studio中执行此任务。有一些工具(不是免费的)可以极大地改善Visual Studio编辑体验。但正如我所说,它们不是免费的,有时它们也会减慢Visual Studio的基本操作。

As far as I know there is nothing built-in in Visual Studio for this task. There are some tools (not free) that greatly improve the Visual Studio editing experience. But as I have said they are not free and sometimes they tend to slow down also the basic operation of Visual Studio.

但是你可以创建一个 SurroundsWith 片段。一个小的自动化向导,您可以在Visual Studio 2015中检索并执行,右键单击要用大括号包围的所选代码块。

However you could create a SurroundsWith snippet. A little automation wizard that you could retrieve and execute also in Visual Studio 2015 right clicking on the selected block of code that you want to be surrounded by braces.


  • 首先,使用工具菜单中的
    扩展和更新下载代码段设计器

  • 然后打开菜单FILE - > NEW - > FILE并选择Snippet Designer
    然后选择Code Snippet。

  • 在Snippet编辑器中添加以下行

  • First thing is to download the Snippet Designer using the Extensions and Updates from the TOOLS menu.
  • Then open the menu FILE -> NEW -> FILE and choose Snippet Designer then Code Snippet.
  • In the Snippet editor add these lines

{
    $selected$ $end$ 
}


  • 然后在代码段框中输入一个名称并选择C#
    作为语言。

  • Then give a name entering some text in the Snippet box and select C# as language.

    现在使用您选择的任何文本编辑器打开代码段,并将
    更改为< SnippetType> with

    Now open the Snippet with any text editor of your choice and change the value <SnippetType> with

    <SnippetType>SurroundsWith</SnippetType>
    


  • 保存文件。

  • Save the file.

    通常,此时,Visual Studio应该能够立即识别新的代码片段,但无论如何,您可以关闭并重新打开IDE。

    Usually, at this point, Visual Studio should be able to recognize immediately the new snippet but in any case you could close and reopen the IDE.

    现在,如果您选择一个代码块,请右键单击并选择 Surrounds With Ctrl + K S ), MyCodeSnippets 然后您的命名代码段将缩进代码

    Now if you select a block of code, right click and choose Surrounds With (Ctrl+KS), MyCodeSnippets and then your named snippet you will have your code indented

    如果您有兴趣,可以在MSDN上的此页面上找到有关Snippets功能的更多详细信息: 创建代码片段

    If you are interested you could find a lot more details about the Snippets functionality at this page on MSDN: Creating Code Snippets

    这篇关于如何使用Visual Studio自动大括号完成来包围现有块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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