Plug-In for code section highlighting

I am investigating writing a ReSharper Plug-In to highlight code using different background colours based on various criteria.  For example, class constructors, public/internal/private members, attributes (Tests, etc), etc.

I have searched the ReSharper Plug-In SDK and examples but have been unable to find anything that seems appropriate.  Either I'm missing something or what I'm trying to achieve is not possible with ReSharper.

Could somebody tell me whether I'm on the right track or whether I'll need to look into writing a VS extension instead?

I appreciate any advice.

Thanks for reading.

Regards
Tony

10 comments
Comment actions Permalink

Hi Tony. I'm not sure that ReSharper can do this. Let me investigate, and I'll get back to you.

Just to clarify - are you wanting to highlight the whole code block, or just member names - e.g. for a constructor, do you want to highlight the name of the method, or the name declaration and all code between the { ... } braces?

Thanks
Matt

0
Comment actions Permalink

Yes - I'd like to highlight the entire code block.

0
Comment actions Permalink

Hi Tony. Sorry for the delay in the reply. I think you should be able to accomplish what you're after by implementing a daemon stage processor that adds a highlighting instance to the section of code that you're interested it. This page has some more information: http://confluence.jetbrains.net/display/ReSharper/2.4+Daemons+and+Daemon+Stages+%28R7%29 and while it describes highlighters as the squiggly lines for errors, highlighters are also used to provide the syntax colouring for identifiers (e.g. when you turn on "Color Identifiers" in the options, resharper uses highlighters to change the colour of mutable variables, interfaces and so on).

You will need to register the class that implements the highlighting information with StaticSeverityHighlightingAttribute. I think that if you leave it like that, ReSharper will provide a default style to display the highlight, usually a squiggly underline with a different colour based on the severity. Or you can provide your own styles, as squiggly or solid underlines, dotted or solid surrounding outlines or just plain text, where you can specify a different background colour, for example. ReSharper does this to colour the identifiers, as mentioned above, by using the RegisterHighlightAttribute. This also allows hooking into the Fonts and Colours part of the Visual Studio options dialog. You can also just implement ICustomStyleHighlighting and provide a style with each highlight.

Usual caveat applies - I haven't tried this, but this looks like a good place to start. Let me know if you hit problems!

Hope it helps
Matt

--

Matt Ellis

Technical Evangelist

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

0
Comment actions Permalink

That's great.  Thanks very much Matt, I appreciate your assistance.

I'll give it a go and we'll see what I can do...

Tony

0
Comment actions Permalink

Hello again.

I have (finally) had opportunity to spend some time experimenting with the SDK and so far the closest I've managed to get is to put a border around a section of code. Attempts to change the background colour were unsuccessful.

The second-best outcome was only effective on the text itself. The white-space in-between and to the right of each line of text remained un-effected.

Unfortunately I seem to have broken my project and can't get the highlighting working at all now. I used the demo DaemonStage code from the SamplePlugin project in the SDK folder. I copied and customised the UseOfInt16MaxValueLiteralHighlighting class and altered the UseInt16MaxValueLiteralDaemonProcess class to use my Highlighting class. Playing with my Highlighting class I was able to get the previously mentioned results.

Any suggestions how I can best proceed?

I'm not asking for someone to show me how to do this (although if someone want's to write it for me I won't object ;)), but I confess that I'm finding it quite confusing and I'm resorting to a lot of guesswork.

Thanks again.

Tony.

0
Comment actions Permalink

Hi Tony. Sorry you're having problems. It's hard to say what's going wrong without seeing the code. Do you fancy attaching it or posting it to GitHub or somewhere so we can take a look?

Thanks
Matt

0
Comment actions Permalink

Ok, I've uploaded it to GitHub. Hope it works - I've never done that before.

https://github.com/TechnoTone/ReSharper-plugin-research

0
Comment actions Permalink

Upload seems to have worked just fine. Looking at the code, it's kinda working. Ish.

For some reason, Visual Studio doesn't want to draw custom highlighting styles. If you hover the mouse over a "Tony", it will display the tooltip correctly, alt+enter will display the configuration options, and a warning appears in the error stripe on the right side of the document. And if you view the code in the preview panel of, say, the find usages window, the highlight is correctly displayed. It's just not displaying in Visual Studio.

I don't know why yet, but I'll dig deeper and get back to you with anything I find.

Matt

0
Comment actions Permalink

I've just come across the SyntaxHighlighting class. Seems this may be of use but I can't find any documentation relating to it.

I'll keep digging/experimenting...

0
Comment actions Permalink

The SyntaxHighlighting class is used by the code preview panel in various windows such as find usages, etc. The normal editor windows use Visual Studio's language services to provide basic syntax highlighting, and ReSharper paints additional highlights on top. Those language services aren't available to the preview window, so the SyntaxHighlight class provides this basic syntax highlighting.

It's actually looking like there may be a technical limitation with custom highlights in a plugin. When ReSharper is first installed, it registers its own highlights with VS's Fonts and Colours system, and plugins installed later aren't a part of this. This is my understanding at the moment - I'm currently talking to the dev team about it, and I need to look into further. I'll let you know when I've got more info.

0

Please sign in to leave a comment.