1

Consider the following VSCode code snippet taken from here:

 "var set get f m_":{
    "prefix": "varsetgetfm",
    "body":[
        "${1:int} ${2:VARNM};",
        "$1 ${2/^(?:f|m)_?([A-Za-z])(.*)|([A-Z].*)$/${1:/downcase}$2${3:/downcase}/}(){return $2;}",
        "void ${2/^(?:f|m)_?([A-Za-z])(.*)|([A-Z].*)$/${1:/downcase}$2${3:/downcase}/}($1 val){$2 = val;}"
    ]
  }

Is there a way to convert this and other snippets from VSCode to a format usable in Visual Studio IDE automatically without having to manually figure out the equivalent? Visual Studio IDE seems to have xml based snippets and not json based snippets like VSCode does.

Tryer
  • 3,580
  • 1
  • 26
  • 49
  • does VS allow field transformation – rioV8 Sep 26 '21 at 13:43
  • @rioV8 Your snippet was powerful. I do not know the answer to your question. I am using snippets only recently. Since VS has been around longer and is actually an IDE, I would hope that one can do stuff in it that one can do in VSCode as well. – Tryer Sep 26 '21 at 14:03
  • @rioV8 FWIW, see https://learn.microsoft.com/en-us/answers/questions/566576/visual-studio-ide-code-snippet-to-convert-field-fr.html MS VSIDE folks seem to suggest that it is not a feature possible in VS IDE yet. :-( – Tryer Sep 27 '21 at 10:38

1 Answers1

1

There’s no existing way. There’s however a way and it involves parsing the json and creating the xml needed. You can use the schema reference link to see json <-> xml, snippet, equivalent.

In other words, make it yourself. Should be fairly easy given that all the details are already documented by Microsoft.


Next day edit:

Take a look at this gif I made demo.gif. It replicates partially replicates the vscode snippet inside visual studio.

Also, the snippet xml code:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>varsetget</Title>
            <Shortcut>varsetget</Shortcut>
            <Description>Code snippet testing</Description>
            <Author>Dorian</Author>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>the var type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>VARNM</ID>
                    <ToolTip>the var name</ToolTip>
                    <Default>VARNM</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[
                $selected$$type$$end$ $VARNM$;
                $type$ $VARNM$(){return $VARNM$;}
                void $VARNM$($type$ val){$VARNM$ = val;}
            ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

To be able to use it, create a *.snippet file wherever you want on your machine with the code above, then:

  1. enter image description here

  2. enter image description here

  3. enter image description here

  4. enter image description here

  5. Navigate and open the *.snippet file you created

  6. enter image description here

  7. enter image description here

  8. enter image description here

  9. You can now test out the snippet

NOTE: In Visual Studio 2019 the default of inserting a code snippet or moving to the next Declaration Literal is pressing Tab twice as opposed to Visual Studio Code, where only one Tab is needed.


Everything I wrote is from the msdocs or deducted from the examples there.


My last edit:

It looks like you're not out of luck. But I do not have the time to investigate/test further, sadly. https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.package.expansionfunction?view=visualstudiosdk-2019

Also, according to this SO post you basically would have to create your own language, which I personally doubt.

PS: How come the guys at MS q&a didn't suggest this I do not understand...

Dorian
  • 79
  • 6
  • I doubt that link is sufficient. For instance, in the VSCode snippet in the OP, there is a conversion automatically from upper case to lower case. How would one do that in a Visual Studio IDE code snippet? The link certainly offers no help or hint I can easily find. – Tryer Sep 26 '21 at 13:11
  • You now ask for editor behavior, not literally replicating a snippet. Different things imo. Also no1 besides a human at ms working/ed in the project can tell you such detail (assuming it exists and undocumented). The only thing you/we have is that documentation. – Dorian Sep 27 '21 at 07:57
  • Thank you for the details provided. However, your gif file does not do what @rioV8 's snippet does in VSCode. Please see https://imgur.com/LuBzSDF Note that `VariableName` get converted to full lowercase `variablename` in the snippet code provided by @rioV8. In your case, `f_testASs` is the variable name. The function names continue to be the same. I want them to be `f_testass`. That is the essence of what I would like the snippet to achieve. – Tryer Sep 27 '21 at 10:32
  • FWIW, see https://learn.microsoft.com/en-us/answers/questions/566576/visual-studio-ide-code-snippet-to-convert-field-fr.html MS VSIDE folks seem to suggest that it is not a feature possible in VS IDE yet. :-( – Tryer Sep 27 '21 at 10:38
  • Oh, I see now. Ty for the gif. I wouldn't have noticed. Well, would it bother you if you add three `Literals`? Type, fname and vname? Else I'm sorry but its ide limit like I said. – Dorian Sep 27 '21 at 10:53
  • Actually, there is a bunch of other stuff in my actual workflow, where I don't have just 3 lines, but close to 8 lines that need to alternate between the actual UPPERCASE and lowercase stuff. So, being able to automate it without tabbing through 8 times and manually changing stuff from UPPER to lower is essential. – Tryer Sep 27 '21 at 10:57
  • You may have a chance using the `Function` https://learn.microsoft.com/en-us/visualstudio/ide/code-snippet-functions?view=vs-2019 abilities of the xml snipetting. I'll attempt it myself. You're welcomed to try it yourself in the mean time. Race you to it :D in theory (currently) you can pass the `fname` to the function and internally convert the param to lowercase or prety much neededCase. – Dorian Sep 27 '21 at 11:03
  • this snippet does not replicate the OP snippet because it does not remove the `f_` in the function names – rioV8 Sep 27 '21 at 11:47
  • Thanks for digging through and unearthing these internals. It certainly seems quite complicated to do this. – Tryer Sep 27 '21 at 14:59