Adding / controlling code snippets

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
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
Kind regards,
Andrew Medvedev
Huge thanks! Great support

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

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

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

Kind regards,
Andrew

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

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:

    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

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