public ITagger <T> CreateTagger <T>(ITextBuffer buffer) where T : ITag { HighlightTag.InitTags(ClassificationTypeRegistry); var classifier = new LuaClassifier(buffer, ClassificationTypeRegistry); return(classifier as ITagger <T>); }
public IEnumerable <ITagSpan <ClassificationTag> > GetTags(NormalizedSnapshotSpanCollection spans) { if (spans.Count == 0) { yield break; } var snapShot = spans[0].Snapshot; foreach (var span in spans) { var startLine = span.Start.GetContainingLine(); var endLine = span.End.GetContainingLine(); var startLineNumber = startLine.LineNumber; var endLineNumber = endLine.LineNumber; #region Tag for (int i = startLineNumber; i <= endLineNumber; i++) { var line = spans[0].Snapshot.GetLineFromLineNumber(i); _parser.Scanner.VsSetSource(line.GetText(), 0); int state = 0; _lineStates.TryGetValue(i, out state); Irony.Parsing.Token token = _parser.Scanner.VsReadToken(ref state); while (token != null) { if (token.Category == Irony.Parsing.TokenCategory.Content) { if (token.EditorInfo != null) { ClassificationTag tag; if (TryGetTag(token, out tag)) { if (line.Start.Position + token.Location.Position + token.Length <= line.End.Position) { var location = new SnapshotSpan(snapShot, line.Start.Position + token.Location.Position, token.Length); yield return(new TagSpan <ClassificationTag>(location, tag)); } } } } else if (token.Category == Irony.Parsing.TokenCategory.Comment) { if (line.Start.Position + token.Location.Position + token.Length <= line.End.Position) { var location = new SnapshotSpan(snapShot, line.Start.Position + token.Location.Position, token.Length); yield return(new TagSpan <ClassificationTag>(location, HighlightTag.GetTagWithTokenType(TokenType.Comment))); } } token = _parser.Scanner.VsReadToken(ref state); } int oldState = 0; _lineStates.TryGetValue(i + 1, out oldState); _lineStates[i + 1] = state; //We're going into overtime, process new tags and send the event that these spans need updating! if (oldState != state) { var lineNumber = endLineNumber; while (oldState != state && lineNumber < snapShot.LineCount) { lineNumber++; var dummyToken = _parser.Scanner.VsReadToken(ref state); while (dummyToken != null) { dummyToken = _parser.Scanner.VsReadToken(ref state); } _lineStates.TryGetValue(lineNumber + 1, out oldState); _lineStates[lineNumber + 1] = state; } if (lineNumber >= snapShot.LineCount) { lineNumber = snapShot.LineCount - 1; } var lastLine = snapShot.GetLineFromLineNumber(lineNumber); if (lastLine != null && this.TagsChanged != null) { int length = lastLine.End.Position - endLine.End.Position; var snapShotSpan = new SnapshotSpan(snapShot, endLine.End.Position, length); this.TagsChanged(this, new SnapshotSpanEventArgs(snapShotSpan)); } } } #endregion } }
private bool TryGetTag(Irony.Parsing.Token token, out ClassificationTag tag) { tag = HighlightTag.GetTag(token); return(tag != null); }