Skip to content

Instantly share code, notes, and snippets.

@warrenbuckley
Created February 20, 2020 14:22
Show Gist options
  • Save warrenbuckley/c88b8c5c9cf91f9857ceff8b07c2ac43 to your computer and use it in GitHub Desktop.
Save warrenbuckley/c88b8c5c9cf91f9857ceff8b07c2ac43 to your computer and use it in GitHub Desktop.
Trying to get autocompletion on a Razor .cshtml view with Roslyn's CompletionService
[HttpPost]
public string FetchCompletions(RazorCode model)
{
var host = new System.Web.Razor.RazorEngineHost(new CSharpRazorCodeLanguage());
var templateEngine = new System.Web.Razor.RazorTemplateEngine(host);
var compiledCode = templateEngine.GenerateCode(new StringReader(model.Code));
var parsed = templateEngine.ParseTemplate(new StringReader(model.Code));
// Cannot convert from 'System.Web.Razor.Parser.SyntaxTree.Block' to 'Microsoft.CodeAnalysis.Document'
var completionService = Microsoft.CodeAnalysis.Completion.CompletionService.GetService(compiledCode.Document);
var completions = completionService.GetCompletionsAsync(compiledCode.Document, 1);
return "hello warren";
}
@warrenbuckley
Copy link
Author

Note

The above code is NOT working & this is what I need some help & advice on.

@muratalalmis
Copy link

Hi Warren, Did you find the solution?

@warrenbuckley
Copy link
Author

@muratalalmis no not from memory - do you have an answer to the problem?

@muratalalmis
Copy link

I couldn't find a solution with this way but I saw a solution when I'm inspecting the vscode's C# extension. It was using the omnisharp (http://prntscr.com/vk5aw0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment