Search code examples
c#.netwpfroslyncode-editor

How to set up code completion in RoslynPad wpf editor control?


I am a beginner level programmer, trying to create a code editor for C# with RoslynPad. I've added the editor control to my WPF window, and highlighting also works. I would also like to enable code completion. I would appreciate if someone could point me in the right direction as to how to go about this.

This is what I have now;

{
    var roslynEditor = new CodeTextEditor();
    var roslynHost = new RoslynHost(additionalAssemblies: new[]
    {
        Assembly.Load("RoslynPad.Roslyn.Windows"),
        Assembly.Load("RoslynPad.Editor.Windows"),
    });
    roslynEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#");

}

From the few resources I have been able to gather, I believe that enabling code completion has to do with creating a Roslyn document and syntax tree for the project and using that to create a Completion Service. Please do tell me the steps to do so as well as how to add my own assemblies to the code completion. Also does the "additionalAssemblies" parameter in RoslynHost constructor have anything to do with that ?


Solution

  • I believe I have it;

    Just did it like this;

     var roslynEditor = new RoslynCodeEditor();               
     var workingDirectory = Directory.GetCurrentDirectory();
     var roslynHost = new RoslynHost(additionalAssemblies: new[]
        {
           Assembly.Load("RoslynPad.Roslyn.Windows"),
           Assembly.Load("RoslynPad.Editor.Windows"),
        },
        references: 
        RoslynHostReferences.NamespaceDefault.With(typeNamespaceImports: 
        new[] { typeof(object), typeof(myassemblyType) }));
                
    
     roslynEditor.Initialize(roslynHost, new ClassificationHighlightColors(), workingDirectory, "");
     roslynEditor.SyntaxHighlighting =    
     HighlightingManager.Instance.GetDefinition("C#");