# Adding / controlling code snippets

**URL:** https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77
**Category:** AlterNET Studio Support
**Tags:** editor
**Created:** [May 28, 2020, 12:00am UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77 "2020-05-28T00:00:00Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![smurphy](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/s/606060/32.png) [@smurphy](https://forum.alternetsoft.com/u/smurphy)
#### Post date: [May 28, 2020, 12:00am UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/1 "2020-05-28T00:00:00Z")

</div>

Hi,  
Not sure if this is possible, do you have an example on how to insert custom code snippets to the dropdown invoked by CTRL+K+X ?  
  
Thanks

---

<div class="post-metadata">

### Author: ![andrew.medvedev](https://forum.alternetsoft.com/user_avatar/forum.alternetsoft.com/andrew.medvedev/32/28_2.png) [@andrew.medvedev](https://forum.alternetsoft.com/u/andrew.medvedev)
#### Post date: [May 28, 2020, 12:00am UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/2 "2020-05-28T00:00:00Z")

</div>

Hi,  
There are two ways to customize code snippets.  
1. Add code snippet from code like this:  
 CodeSnippet snippet = new Alternet.Syntax.CodeCompletion.CodeSnippet();  
 snippet.Code = new CodeSnippetCode();  
 snippet.Header = new CodeSnippetHeader();  
 snippet.Header.Author = "Your";  
 snippet.Header.Description = "My custom class snippet";  
 snippet.Header.Title = "my custom class";  
 ICodeSnippetType type1 = new CodeSnippetType();  
 type1.SnippetType = SnippetType.Expansion;  
 snippet.Header.Types.Add(type1);  
 ICodeSnippetType type2 = new CodeSnippetType();  
 type2.SnippetType = SnippetType.SurroundsWith;  
 snippet.Header.Types.Add(type2);  
 snippet.ImageIndex = 64;  
 ICodeSnippetDeclaration decl = new CodeSnippetDeclaration();  
 ICodeSnippetLiteral literal = new CodeSnippetLiteral();  
 literal.ID = "name";  
 literal.ToolTip = "Class name";  
 literal.Default = "MyCustomClass";  
 decl.Literals.Add(literal);  
 snippet.Declarations.Add(decl);  
 snippet.Code.Delimiter = "$";  
 snippet.Code.Code = @"class $name$  
{  
 private int customField;  
$selected$$end$  
}";  
 csParser1.CodeSnippets.Add(snippet);  
2. Load code snippets from xml file like this:  
 csParser1.CodeSnippets.LoadFile(@"C:\Test\CSharpSnippets.xml");  
I've uploaded CSharpSnippets.xml here:  
[https://drive.google.com/file/d/1fZgOQ-fmIy77zJCJrqRPgwL-vJYUJwi-/view?usp=sharing](https://drive.google.com/file/d/1fZgOQ-fmIy77zJCJrqRPgwL-vJYUJwi-/view?usp=sharing)  
Kind regards,  
Andrew Medvedev

---

<div class="post-metadata">

### Author: ![smurphy](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/s/606060/32.png) [@smurphy](https://forum.alternetsoft.com/u/smurphy)
#### Post date: [May 29, 2020, 12:00am UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/3 "2020-05-29T00:00:00Z")

</div>

Huge thanks! Great support

---

<div class="post-metadata">

### Author: ![Felix](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/f/606060/32.png) [@Felix](https://forum.alternetsoft.com/u/Felix)
#### Post date: [October 27, 2021, 7:54am UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/4 "2021-10-27T07:54:10Z")

</div>

Hi,  
I’m trying to load custom code snippets from file using:

```auto
vbParser.CodeSnippets.LoadFile("path/to/file.xml");

```

The method returns true, indicating that the import worked but the code snippets do not show up in vbParser.CodeSnippets. Am i doing something wrong?

Kind regards,  
Felix

---

<div class="post-metadata">

### Author: ![andrew.medvedev](https://forum.alternetsoft.com/user_avatar/forum.alternetsoft.com/andrew.medvedev/32/28_2.png) [@andrew.medvedev](https://forum.alternetsoft.com/u/andrew.medvedev)
#### Post date: [October 27, 2021, 1:30pm UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/5 "2021-10-27T13:30:19Z")

</div>

Hi Felix,

I’ve uploaded a demo project showing how to load snippets into the Visual Basic (advanced) parser. These snippets are displayed if you press Ctrl + K + X in the editor.  
Could you also clarify what VbParser you are using, Advanced or Roslyn? If you send us your snippet file, we will be able to look further into it.  
[https://drive.google.com/file/d/1r2rcBKqt0Vf2nGBMFgWJAlvA6-oQgXZ0/view?usp=sharing](https://drive.google.com/file/d/1r2rcBKqt0Vf2nGBMFgWJAlvA6-oQgXZ0/view?usp=sharing)

Kind regards,  
Andrew

---

<div class="post-metadata">

### Author: ![Felix](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/f/606060/32.png) [@Felix](https://forum.alternetsoft.com/u/Felix)
#### Post date: [October 27, 2021, 2:08pm UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/6 "2021-10-27T14:08:23Z")

</div>

Hi Andrew,  
I have tried to load my snippet file with the example you provided and it works, so the problem is not the xml file.

I am using the Roslyn version of the VbParser in a DebugCodeEdit in WPF. If I switch your example to the Roslyn version I encounter the same problem. There are a bunch of default snippets loaded but my custom ones don’t show up.

Best,  
Felix

---

<div class="post-metadata">

### Author: ![andrew.medvedev](https://forum.alternetsoft.com/user_avatar/forum.alternetsoft.com/andrew.medvedev/32/28_2.png) [@andrew.medvedev](https://forum.alternetsoft.com/u/andrew.medvedev)
#### Post date: [October 28, 2021, 7:40am UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/7 "2021-10-28T07:40:29Z")

</div>

Hi Felix,

There seems to be a problem with custom snippets in Roslyn parsers, which will be fixed in the coming update.  
For now you can use the following workaround:

```auto
    public class CustomVbParser
        : VbParser
    {
        public override ICodeCompletionRepository CreateRepository()
        {
            return new CustomVbRepository(this, RoslynSolution, false);
        }
    }

    public class CustomVbRepository : Alternet.Syntax.Parsers.Roslyn.CodeCompletion.VbRepository
    {
        private System.Collections.Hashtable codeSnippets;

        public CustomVbRepository(ISyntaxParser owner, IRoslynSolution roslynSolution, bool caseSensitive)
         : base(owner, roslynSolution, caseSensitive)
        {
            codeSnippets = new System.Collections.Hashtable();
        }

        public override ICodeSnippetsProvider GetCodeSnippets(string language)
        {
            object obj = codeSnippets[language];
            if (obj != null)
                return (ICodeSnippetsProvider)obj;
            ICodeSnippetsProvider provider;
            if (HasSnippetMembers(language))
                provider = new CodeSnippetMembers();
            else
                provider = new CodeSnippets();
            provider.Images = DisplayScaledImages.Images;
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(RoslynParser));
            if (language == "vb")
                GetResourceTextFile(provider, "VB.xml");
            else
            {
                obj = resources.GetObject("CodeSnippets." + language);
                if (obj != null)
                    provider.LoadStream(new StringReader((string)obj));
            }

            codeSnippets[language] = provider;
            return provider;
        }

        private void GetResourceTextFile(ICodeSnippetsProvider provider, string filename)
        {
            using (Stream stream = this.GetType().Assembly.GetManifestResourceStream("Alternet.Syntax.Parsers.Roslyn.Classes." + filename))
            {
                if (stream != null)
                    provider.LoadStream(new StreamReader(stream));
            }
        }
    }

```

Kind regards,  
Andrew

---

<div class="post-metadata">

### Author: ![Felix](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/f/606060/32.png) [@Felix](https://forum.alternetsoft.com/u/Felix)
#### Post date: [October 28, 2021, 8:03am UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/8 "2021-10-28T08:03:25Z")

</div>

Hi Andrew,  
thank you for the reply. Your workaround works well enough for now and I am looking forward to the next update.

Best,  
Felix

---

<div class="post-metadata">

### Author: ![Felix](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/f/606060/32.png) [@Felix](https://forum.alternetsoft.com/u/Felix)
#### Post date: [May 31, 2023, 6:22am UTC](https://forum.alternetsoft.com/t/adding-controlling-code-snippets/77/9 "2023-05-31T06:22:15Z")

</div>

Hi again,  
I know this is like 2 years later but I’d like to inform you that while you have fixed the issue for VisualBasic snippets in Roslyn parsers, the same issue is still present for C# snippets.

Best,  
Felix
