Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Writing Your First Visual Studio Language Service

Rate me:
Please Sign up or sign in to vote.
4.95/5 (61 votes)
11 Dec 2009CPOL8 min read 233K   4K   157  
A guide to writing a language service for Visual Studio using Irony.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Irony.Compiler {
  // Initial implementation of syntax highlighting in editor. 
  // TokenColor, TokenTriggers and TokenType are copied from the Visual studio integration assemblies. 
  //  Each terminal/token would have its TokenEditorInfo that can be used either by VS integration package 
  //   or any editor for syntax highligting.

  public class TokenEditorInfo {
    public readonly TokenType Type;
    public readonly TokenColor Color;
    public readonly TokenTriggers Triggers;
    public TokenEditorInfo(TokenType type, TokenColor color, TokenTriggers triggers) {
      Type = type;
      Color = color;
      Triggers = triggers;
    }

  }//class

  public enum TokenColor {
    Text = 0,
    Keyword = 1,
    Comment = 2,
    Identifier = 3,
    String = 4,
    Number = 5,
  }

  // (Comments are coming from visual studio integration package)
  //     Specifies a set of triggers that can be fired from an Microsoft.VisualStudio.Package.IScanner
  //     language parser.
  [Flags]
  public enum TokenTriggers {
    // Summary:
    //     Used when no triggers are set. This is the default.
    None = 0,
    //
    // Summary:
    //     A character that indicates that the start of a member selection has been
    //     parsed. In C#, this could be a period following a class name. In XML, this
    //     could be a < (the member select is a list of possible tags).
    MemberSelect = 1,
    //
    // Summary:
    //     The opening or closing part of a language pair has been parsed. For example,
    //     in C#, a { or } has been parsed. In XML, a < or > has been parsed.
    MatchBraces = 2,
    //
    // Summary:
    //     A character that marks the start of a parameter list has been parsed. For
    //     example, in C#, this could be an open parenthesis, "(".
    ParameterStart = 16,
    //
    // Summary:
    //     A character that separates parameters in a list has been parsed. For example,
    //     in C#, this could be a comma, ",".
    ParameterNext = 32,
    //
    // Summary:
    //     A character that marks the end of a parameter list has been parsed. For example,
    //     in C#, this could be a close parenthesis, ")".
    ParameterEnd = 64,
    //
    // Summary:
    //     A parameter in a method's parameter list has been parsed.
    Parameter = 128,
    //
    // Summary:
    //     This is a mask for the flags used to govern the IntelliSense Method Tip operation.
    //     This mask is used to isolate the values Microsoft.VisualStudio.Package.TokenTriggers.Parameter,
    //     Microsoft.VisualStudio.Package.TokenTriggers.ParameterStart, Microsoft.VisualStudio.Package.TokenTriggers.ParameterNext,
    //     and Microsoft.VisualStudio.Package.TokenTriggers.ParameterEnd.
    MethodTip = 240,
  }

  public enum TokenType {
    Unknown = 0,
    Text = 1,
    Keyword = 2,
    Identifier = 3,
    String = 4,
    Literal = 5,
    Operator = 6,
    Delimiter = 7,
    WhiteSpace = 8,
    LineComment = 9,
    Comment = 10,
  }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions