private void AddRegion(List <Region> list, VSSnapshot snapshot, SyntaxTree tree, int startModifier, SyntaxNode startNode, SyntaxNode endNode) { int start = startNode.Span.GetSpan(snapshot).End + startModifier; int end = endNode.Span.GetSpan(snapshot).End; SourceLine startLine = snapshot.GetLineFromPosition(startNode.Span.GetSpan(snapshot).End); SourceLine endLine = snapshot.GetLineFromPosition(end); if (startLine.LineNumber == endLine.LineNumber) { return; } string text; if (endLine.LineNumber - startLine.LineNumber > 10) { int textStart = snapshot.GetLineFromLineNumber(startLine.LineNumber).Span.Start; int textEnd = snapshot.GetLineFromLineNumber(startLine.LineNumber + 10).Span.End; text = snapshot.GetText(textStart, textEnd - textStart); } else { text = snapshot.GetText(startLine.Span.Start, endLine.Span.End - startLine.Span.Start).Trim('\r', '\n'); } GLSL.Text.Span span = GLSL.Text.Span.Create(start, end); list.Add(new Region(snapshot.CreateTrackingSpan(span), text)); }
public IEnumerable <ITagSpan <IOutliningRegionTag> > GetTags(NormalizedSnapshotSpanCollection spans) { if (spans.Count == 0) { yield break; } VSSnapshot snapshot = this.source.CurrentSnapshot as VSSnapshot; SyntaxTree tree = this.source.Tree; List <Region> regionList; lock (this.lockObject) { regionList = this.regions; } for (int j = 0; j < regionList.Count; j++) { if (regionList[j].Span.GetSpan(snapshot).End >= snapshot.Length) { continue; } yield return(new TagSpan <IOutliningRegionTag>(new SnapshotSpan(snapshot.TextSnapshot, regionList[j].Span.GetSpan(snapshot).ToVSSpan()), new OutliningRegionTag(false, true, "...", regionList[j].Text))); } }
protected override bool Run(VSConstants.VSStd97CmdID commandId) { SyntaxTree tree = this.source.Tree; if (tree == null) { return(false); } VSSnapshot snapshot = this.source.CurrentSnapshot as VSSnapshot; int position = this.TextView.Caret.Position.BufferPosition.Position; IdentifierSyntax identifier = tree.GetNodeFromPosition(snapshot, position) as IdentifierSyntax; Span span = identifier?.Definition.DefinitionSpan?.GetSpan(snapshot); if (span == null) { return(false); } this.TextView.Caret.MoveTo(new Microsoft.VisualStudio.Text.SnapshotPoint(snapshot.TextSnapshot, span.Start)); return(true); }
public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> quickInfoContent, out ITrackingSpan applicableToSpan) { applicableToSpan = null; if (session == null) { throw new ArgumentNullException(nameof(session)); } if (quickInfoContent == null) { throw new ArgumentNullException(nameof(quickInfoContent)); } VSSnapshot snapshot = this.source.CurrentSnapshot as VSSnapshot; if (snapshot == null) { return; } int triggerPosition = session.GetTriggerPoint(this.textBuffer).GetPosition(snapshot.TextSnapshot); SyntaxTree tree = this.source.Tree; IdentifierSyntax identifier = tree.GetNodeFromPosition(snapshot, triggerPosition) as IdentifierSyntax; IClassificationFormatMap formatMap = this.provider.FormatMap.GetClassificationFormatMap("text"); if (identifier?.Definition == null) { return; } quickInfoContent.Add(new QuickTipPanel(identifier.Definition.GetIcon(this.provider.GlyphService), identifier.Definition.ToTextBlock(formatMap, this.provider.TypeRegistry), null)); applicableToSpan = (identifier.Span as VSTrackingSpan).TrackingSpan; }
public static int GetPosition(this ITrackingPoint point, Snapshot snapshot) { VSSnapshot vs = snapshot as VSSnapshot; if (vs == null) { throw new ArgumentException($"{nameof(snapshot)} must be a VSSnapshot", nameof(snapshot)); } return(point.GetPosition(vs.TextSnapshot)); }
public static ITextSnapshot ToITextSnapshot(this Snapshot snapshot) { VSSnapshot vs = snapshot as VSSnapshot; if (vs == null) { throw new ArgumentException($"{nameof(snapshot)} must be a VSSnapshot and must not be null"); } return(vs.TextSnapshot); }
public static SnapshotSpan GetSnapshotSpan(this Snapshot snapshot, GLSL.Text.Span span) { VSSnapshot vs = snapshot as VSSnapshot; if (vs == null) { throw new ArgumentException("snapshot must be a VSSnapshot"); } return(new SnapshotSpan(vs.TextSnapshot, span.ToVSSpan())); }
private void UpdateRegions(VSSnapshot snapshot) { SyntaxTree tree = this.source.Tree; if (tree == null || snapshot == null) { return; } List <Region> newRegions = new List <Region>(); foreach (SyntaxNode node in tree.Root.Descendants) { switch (node.SyntaxType) { case SyntaxType.FunctionDefinition: FunctionDefinitionSyntax functionDefinition = node as FunctionDefinitionSyntax; if (functionDefinition.FunctionHeader?.RightParentheses != null && functionDefinition.Block?.RightBrace != null) { this.AddRegion(newRegions, snapshot, tree, 1, functionDefinition.FunctionHeader.RightParentheses, functionDefinition.Block.RightBrace); } break; case SyntaxType.InterfaceBlock: InterfaceBlockSyntax interfaceBlock = node as InterfaceBlockSyntax; if (interfaceBlock.Identifier != null && interfaceBlock.RightBrace != null) { this.AddRegion(newRegions, snapshot, tree, 1, interfaceBlock.Identifier, interfaceBlock.RightBrace); } break; case SyntaxType.StructSpecifier: StructSpecifierSyntax structSpecifier = node as StructSpecifierSyntax; if (structSpecifier.RightBrace != null) { if (structSpecifier.TypeName != null) { this.AddRegion(newRegions, snapshot, tree, 0, structSpecifier.TypeName, structSpecifier.RightBrace); } else if (structSpecifier.StructKeyword != null) { this.AddRegion(newRegions, snapshot, tree, 1, structSpecifier.StructKeyword, structSpecifier.RightBrace); } } break; case SyntaxType.IfDefinedPreprocessor: IfDefinedPreprocessorSyntax ifDefined = node as IfDefinedPreprocessorSyntax; if (ifDefined.EndIfKeyword != null) { this.AddRegion(newRegions, snapshot, tree, 1, ifDefined.IfDefinedKeyword, ifDefined.EndIfKeyword); } break; case SyntaxType.IfPreprocessor: IfPreprocessorSyntax ifPreprocessor = node as IfPreprocessorSyntax; if (ifPreprocessor.EndIfKeyword != null) { this.AddRegion(newRegions, snapshot, tree, 1, ifPreprocessor.IfKeyword, ifPreprocessor.EndIfKeyword); } break; case SyntaxType.IfNotDefinedPreprocessor: IfNotDefinedPreprocessorSyntax ifNotDefined = node as IfNotDefinedPreprocessorSyntax; if (ifNotDefined.EndIfKeyword != null) { this.AddRegion(newRegions, snapshot, tree, 1, ifNotDefined.IfNotDefinedKeyword, ifNotDefined.EndIfKeyword); } break; } } foreach (IfPreprocessor preprocessor in this.source.Settings.Preprocessors) { if (preprocessor.EndIf != null) { this.AddRegion(newRegions, snapshot, tree, 1, preprocessor.Keyword, preprocessor.EndIf.EndIfKeyword); } for (int i = 0; i < preprocessor.ElsePreprocessors.Count; i++) { SyntaxToken next; if (i < preprocessor.ElsePreprocessors.Count - 1) { ElseIfPreprocessorSyntax elseIf = preprocessor.ElsePreprocessors[i].Keyword.Parent as ElseIfPreprocessorSyntax; if (elseIf != null) { next = elseIf.ExcludedCode.Code.Last() as SyntaxToken; } else { next = preprocessor.ElsePreprocessors[i].Keyword; } } else { next = preprocessor.EndIf.EndIfKeyword; } this.AddRegion(newRegions, snapshot, tree, 1, preprocessor.ElsePreprocessors[i].Keyword, next); } } lock (this.lockObject) { this.regions = newRegions; } this.TagsChanged?.Invoke(this, new SnapshotSpanEventArgs(new SnapshotSpan(snapshot.TextSnapshot, snapshot.Span.ToVSSpan()))); }
private void ColorComments(SnapshotSpan span, List <ClassificationSpan> spans, VSSnapshot snapshot, IReadOnlyList <GLSL.Text.TrackingSpan> commentSpans) { for (int i = 0; i < commentSpans.Count; i++) { Span commentSpan = commentSpans[i].GetSpan(snapshot).ToVSSpan(); if (span.IntersectsWith(commentSpan)) { spans.Add(new ClassificationSpan(new SnapshotSpan(span.Snapshot, commentSpan), this.classificationTypeRegistryService.GetClassificationType(GLSLConstants.Comment))); } } IReadOnlyList <GLSL.Text.TrackingSpan> sourceSpans = this.source.CommentSpans; for (int i = 0; i < sourceSpans.Count; i++) { if (!commentSpans.Contains(s => s.GetSpan(snapshot).Overlaps(sourceSpans[i].GetSpan(snapshot)))) { spans.Add(new ClassificationSpan(new SnapshotSpan(span.Snapshot, sourceSpans[i].GetSpan(snapshot).ToVSSpan()), this.classificationTypeRegistryService.GetClassificationType(GLSLConstants.Comment))); } } }
public IList <ClassificationSpan> GetClassificationSpans(SnapshotSpan span) { VSSnapshot currentSnapshot = new VSSnapshot(this.source, span.Snapshot); LinkedList <Token> tokens = this.lexer.Run(currentSnapshot, span.Start.GetContainingLine().ExtentIncludingLineBreak.Span.ToGLSLSpan()); List <ClassificationSpan> spans = new List <ClassificationSpan>(); SyntaxTree tree = this.source.Tree; if (tree == null) { return(spans); } foreach (Token token in tokens) { if (!span.OverlapsWith(token.Span.ToVSSpan())) { continue; } SyntaxToken syntaxToken = tree?.GetNodeFromPosition(this.source.CurrentSnapshot, token.Span.Start) as SyntaxToken; string classificationName = string.Empty; if (syntaxToken != null) { classificationName = syntaxToken.GetClassificationName(); } else { if (token.SyntaxType.IsPreprocessor()) { classificationName = GLSLConstants.PreprocessorKeyword; } else if (token.SyntaxType.IsPunctuation()) { classificationName = GLSLConstants.Punctuation; } else if (token.SyntaxType.IsKeyword()) { classificationName = GLSLConstants.Keyword; } else if (token.SyntaxType.IsNumber()) { classificationName = GLSLConstants.Number; } else if (token.SyntaxType == SyntaxType.IdentifierToken) { classificationName = GLSLConstants.Identifier; } } if (!string.IsNullOrEmpty(classificationName)) { SnapshotSpan snapshotSpan = new SnapshotSpan(span.Snapshot, token.Span.ToVSSpan()); spans.Add(new ClassificationSpan(snapshotSpan, this.classificationTypeRegistryService.GetClassificationType(classificationName))); } } this.ColorComments(span, spans, currentSnapshot, this.lexer.CommentSpans); return(spans); }