I try to get code completion, but when I try to get completions async by CompletionService
I get NullReferenceException
.
Here is a piece of code:
public async Task<IList<ICompletionData>> GetCompletionData(String code, int offset)
{
IList<ICompletionData> completionData = null;
if (_workspace != null)
{
_workspace.Dispose();
}
_workspace = new AdhocWorkspace();
_project = _workspace.AddProject("Test", LanguageNames.CSharp);
var sourceText = SourceText.From(code);
var document = _workspace.AddDocument(_project.Id, "Compilation0", sourceText);
var completionService = CompletionService.GetService(document);
var charCompletion = GetCompletionTrigger('.');
var data = await completionService.GetCompletionsAsync(_document, offset,charCompletion,).ConfigureAwait(false);
if (data == null || data.Items.Any() == false)
return new List<ICompletionData>();
completionData = data.Items.Select(item => new RoslynCodeCompletion(_document, item)).ToList<ICompletionData>();
return completionData;
}
Error at line:
var data = await completionService.GetCompletionsAsync(_document, offset,charCompletion,).ConfigureAwait(false);//NullReferenseException.
Script code codepletion triggers by '.' (dot) symbol:
var str="";
str. // here calls `GetCompletionData`
Full code snippet is on pastebin
This became a lot easier after Roslyn PR #14921 was merged. Before you had to a lot of manual MEF configuration, but now it should be enough to have the following nuget packages installed:
And Roslyn will do the required configuration.