简体   繁体   English

VS 2017 - 需要修复 `cw` 代码片段输出

[英]VS 2017 - need to fix `cw` code snippet output

At some point in VS 2017 (most probably after an update to version 15.8.7) the cw code snippet which usually produced Console.WriteLine();在 VS 2017 中的某个时候(很可能在更新到版本 15.8.7 之后)通常生成Console.WriteLine();cw代码片段Console.WriteLine(); has began to produce System.Console.WriteLine();已经开始产生System.Console.WriteLine(); . .

Surprisingly this behavior is not constant - it works as expected in old projects with the same VS version.令人惊讶的是,这种行为并不是恒定的——它在具有相同 VS 版本的旧项目中按预期工作。

I've tried to remove .vs folder and it didn't help.我试图删除.vs文件夹,但没有帮助。

Here is the content of cw.snippet file from `C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\VC#\\Snippets\\1033\\Visual C#\\cw.snippet这里是cw.snippet文件从`C含量:\\程序文件(x86)\\微软的Visual Studio \\ 2017年\\企业\\ VC#\\片段\\ 1033 \\ Visual C#中\\ cw.snippet

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>cw</Title>
            <Shortcut>cw</Shortcut>
            <Description>Code snippet for Console.WriteLine</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false">
                    <ID>SystemConsole</ID>
                    <Function>SimpleTypeName(global::System.Console)</Function>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[$SystemConsole$.WriteLine($end$);]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Is there a way to fix cw snippet so it always produces Console.WriteLine();有没有办法修复cw片段,以便它始终生成Console.WriteLine(); ? ?

You can tell the snippet if the using statement for system is not already on the page to add it.如果页面上还没有用于 system 的 using 语句,您可以告诉代码片段以添加它。 Include the following after the open tag <snippet> and before opening <declarations> tag.包括开放标记之后以下<snippet>开口<declarations>标记。

<Imports>
    <Import>
        <Namespace>System</Namespace>
    </Import>
</Imports>

For Reference to add more (in this case it isn't needed but you may in the future) imports you will need to make separate tag pairs (as below).对于引用添加更多(在这种情况下不需要,但您可能在将来)导入,您需要制作单独的标签对(如下所示)。

<Import>
    <Namespace></Namespace>
</Import>

For me, I ended up just editing the snippet itself.对我来说,我最终只是编辑了代码片段本身。 I'm sure I will need to do this every time there is an update and I'm not sure of a better permanent fix outside of creating a user-defined snippet.我确定每次有更新时我都需要这样做,而且我不确定除了创建用户定义的代码段之外还有更好的永久性修复。

My quick and dirty fix was to edit the file...我快速而肮脏的修复方法是编辑文件...

%USERPROFILE%.vscode\\extensions\\ms-dotnettools.csharp-1.23.8\\snippets\\csharp.json %USERPROFILE%.vscode\\extensions\\ms-dotnettools.csharp-1.23.8\\snippets\\csharp.json

Then look for the following section (found mine around line 53)...然后查找以下部分(在第 53 行附近找到我的部分)...

"Console.WriteLine": {
    "prefix": "cw",
    "body": [
        "System.Console.WriteLine($0);"
    ],
    "description": "Console.WriteLine"
},

Then just change...那就换个...

"System.Console.WriteLine($0);"

To...到...

"Console.WriteLine($0);"

Save, then restart VSCode and test the cw snippet.保存,然后重新启动 VSCode 并测试cw代码段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM