2

I'm working on a semantic highlighting plugin for VS. Here you can see a web Example.

The goal: Acquiring all variables and creating different Classifications for every one of them.

The problem: Getting the variables from the code without writing a C# lexer.

My current approach uses an ITagger. I use an ITagAggregator to get the tags of all the spans that get passed to the ITagger. Then I filter those and get only spans with the "identifier" classification which includes varibles, methods names, class names, usings and properties.

public class Classifier : ITagger<ClassificationTag> {

    public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans) {

        ITextSnapshot snapshot = spans[0].Snapshot;

        var tags = _aggregator.GetTags(spans).Where((span) => span.Tag.ClassificationType.Classification.Equals("identifier")).ToArray();

        foreach(var classifiedSpan in tags) {

            foreach(SnapshotSpan span in classifiedSpan.Span.GetSpans(snapshot)) {
                //generate classification based on variable name 
                yield return new TagSpan<ClassificationTag>(span, new ClassificationTag(_classification));
            }
        }
    }
}

It would be a lot easier to use the builtin C# Lexer to get a list of all variables bundled to a bunch of meta data. Is this data available for plugin development? Is there an alternative way I could acquire it, if not?

Jordan Parker
  • 239
  • 3
  • 8

3 Answers3

4

The problem: Getting the variables from the code without writing a C# lexer.

Roslyn can do this: https://roslyn.codeplex.com/

There's even a Syntax Visualizer sample that might interest you. I also found an example using Roslyn to create a Syntax Highlighter.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
3

Visual Studio exposes that information as a code model.

Here is an example how you can access class, and then find attribute on the class, and parse attribute arguments:

Accessing attribute info from DTE

Here is more information about code models: http://msdn.microsoft.com/en-us/library/ms228763.aspx

Here's also automation object model chart what I've been using quite few times: http://msdn.microsoft.com/en-us/library/za2b25t3.aspx

Also, as said, Roslyn is indeed also a possible option. Here is an example for VS2015 using roslyn: https://github.com/tomasr/roslyn-colorizer/blob/master/RoslynColorizer/RoslynColorizer.cs

Community
  • 1
  • 1
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • Interesting. This does indeed mean that I don't need to update to VS 2014 for this to get it's full functionality. Do you have any additional helpful examples? – Jordan Parker Jan 06 '15 at 22:15
  • 1
    @Jedy; not particularly, what exactly would you like to know? I added "Automation Object Model Chart" to my answer which has been very useful to me, and is not too simple to find. It helps to navigate. Particularily, see "COde elements". You have access to `CodeVariable` which you probably want. Ps, in your *specific* case, it might be even wiser to use "underlying C# classifier" in this case, if all you want to know is identifiers. – Erti-Chris Eelmaa Jan 12 '15 at 10:13
1

For building language tools if may be better to use a parser generator for C#. The GOLD parsing system is one such toolkit which can handle LALR grammars. It has a .NET component based engine that you can use in your project and it can be used to integrate with any IDE. You can also find the grammars for various programming languages including C#.

codelion
  • 1,056
  • 1
  • 11
  • 17