Ivan Zaruba
Ivan Zaruba

Reputation: 4504

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(); has began to produce System.Console.WriteLine();.

Surprisingly this behavior is not constant - it works as expected in old projects with the same VS version.

I've tried to remove .vs folder and it didn't help.

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

<?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();?

Upvotes: 2

Views: 1462

Answers (2)

Arvo Bowen
Arvo Bowen

Reputation: 4929

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

Then look for the following section (found mine around line 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.

Upvotes: 4

Royal Kitsune
Royal Kitsune

Reputation: 33

You can tell the snippet if the using statement for system is not already on the page to add it. Include the following after the open tag <snippet> and before opening <declarations> tag.

<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>

Upvotes: 3

Related Questions