简体   繁体   中英

Start Roslyn analyzer by button?

I have a Roslyn code analyzer that takes a lot of time to run, so running it either at compilation start or as a whole tree analysis is not appropriate.

Instead I would like to have the analysis trigger when the user clicks on a button in the menu (or toolbar/keyboard shortcut).

Is this possible? If not, is there a way for a traditional MEF extension to access the Roslyn tree of an opened document and to add errors/warnings to the Error List and corresponding squigglies under the syntax nodes in the document?

Roslyn analyzers can only be triggered by Roslyn; you can't do that.

You can access Roslyn from traditional VS extensions by injecting VisualStudioWorkspace in any MEF component. There are a bunch of extension methods in Microsoft.CodeAnalysis.EditorFeatures.Text to map between Roslyn nodes / documents and VS buffers / snapshots.

You can report errors using the standard VS error APIs; you may also want to look at the code in Roslyn that reports Roslyn errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM