///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> public override IEnumerable <TagSnapshotRange <ColorPreviewTag> > GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { if (snapshotRanges != null) { // Loop through the snapshot ranges foreach (TextSnapshotRange snapshotRange in snapshotRanges) { // Get the text of the snapshot range string text = snapshotRange.Text; // Look for a regex pattern match MatchCollection matches = Regex.Matches(text, this.Pattern, RegexOptions.IgnoreCase); if (matches.Count > 0) { // Loop through the matches foreach (Match match in matches) { // Create a tag ColorPreviewTag tag = new ColorPreviewTag(); tag.Color = UIColor.FromWebColor(match.Value).ToColor(); // Ensure full alpha if (tag.Color.A < 255) { tag.Color = Color.FromArgb(255, tag.Color.R, tag.Color.G, tag.Color.B); } // Yield the tag yield return(new TagSnapshotRange <ColorPreviewTag>( TextSnapshotRange.FromSpan(snapshotRange.Snapshot, snapshotRange.StartOffset + match.Index, match.Length), tag)); } } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> public override IEnumerable <TagSnapshotRange <CustomTag> > GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { if (snapshotRanges != null) { // Loop through the snapshot ranges foreach (TextSnapshotRange snapshotRange in snapshotRanges) { // Get the text of the snapshot range string text = snapshotRange.Text; // Look for a regex pattern match MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase); if (matches.Count > 0) { // Loop through the matches foreach (Match match in matches) { // Create a tag CustomTag tag = new CustomTag(); // Yield the tag yield return(new TagSnapshotRange <CustomTag>( TextSnapshotRange.FromSpan(snapshotRange.Snapshot, snapshotRange.StartOffset + match.Index, match.Length), tag)); } } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> public override IEnumerable <TagSnapshotRange <IClassificationTag> > GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { if (String.IsNullOrEmpty(currentWord)) { yield break; } // Get a regex of the current word Regex search = new Regex(String.Format(@"\b{0}\b", currentWord), RegexOptions.Singleline); // Loop through the requested snapshot ranges... foreach (TextSnapshotRange snapshotRange in snapshotRanges) { // If the snapshot range is not zero-length... if (!snapshotRange.IsZeroLength) { // Look for current word matches foreach (Match match in search.Matches(snapshotRange.Text)) { // Add a highlighted range yield return(new TagSnapshotRange <IClassificationTag>( new TextSnapshotRange(snapshotRange.Snapshot, TextRange.FromSpan(snapshotRange.StartOffset + match.Index, match.Length)), new ClassificationTag(wordHighlightClassificationType) )); } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> public override IEnumerable <TagSnapshotRange <IClassificationTag> > GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { // Loop through the requested snapshot ranges... foreach (TextSnapshotRange snapshotRange in snapshotRanges) { // If the snapshot range is not zero-length... if (!snapshotRange.IsZeroLength) { // Get a snapshot reader ITextSnapshotReader reader = snapshotRange.Snapshot.GetReader(snapshotRange.StartOffset); // If not already at the start of a line, back up to the start if (!reader.IsAtSnapshotLineStart) { reader.GoToCurrentSnapshotLineStart(); } // Read through the snapshot until the end of the target range is reached while ((!reader.IsAtSnapshotEnd) && (reader.Offset < snapshotRange.EndOffset)) { // Save the start of the line offset int lineStartOffset = reader.Offset; // Get the line start text (we need at most 6 chars for this sample) string lineStartText = reader.PeekText(6); // Go to the end of the line reader.GoToCurrentSnapshotLineEnd(); // Add a range for the line if it starts with one of the defined strings... // The StyleRegistryClassificationTag is a special ClassificationTag that allows you to indicate // an alternate IHighlightingStyleRegistry to use for syntax highlighting... if using the // normal AmbientHighlightingStyleRegistry, you'd just use a regular ClassificationTag instead if (lineStartText.StartsWith("---")) { // Apply green to lines that start with "---" yield return(new TagSnapshotRange <IClassificationTag>( new TextSnapshotRange(snapshotRange.Snapshot, new TextRange(lineStartOffset, reader.Offset)), new StyleRegistryClassificationTag(commentCT, styleRegistry) )); } else if (lineStartText.StartsWith("Error:")) { // Apply maroon to lines that start with "Error:" yield return(new TagSnapshotRange <IClassificationTag>( new TextSnapshotRange(snapshotRange.Snapshot, new TextRange(lineStartOffset, reader.Offset)), new StyleRegistryClassificationTag(errorCT, styleRegistry) )); } // Consume the newline reader.GoToNextSnapshotLineStart(); } } } }
public override IEnumerable <TagSnapshotRange <IClassificationTag> > GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { // Loop through the requested snapshot ranges... foreach (TextSnapshotRange snapshotRange in snapshotRanges) { // If the snapshot range is not zero-length... if (!snapshotRange.IsZeroLength) { IEnumerable <TagSnapshotRange <ITokenTag> > tokenTagRanges = tokenTagAggregator.GetTags(snapshotRange); if (tokenTagRanges != null) { foreach (TagSnapshotRange <ITokenTag> tokenTagRange in tokenTagRanges) { if (tokenTagRange.Tag.Token != null) { switch (tokenTagRange.Tag.Token.Key) { case "Identifier": { // Get the text of the token string text = tokenTagRange.SnapshotRange.Text; if (tokens.Contains(text)) { // Add a highlighted range yield return(new TagSnapshotRange <IClassificationTag>( new TextSnapshotRange(snapshotRange.Snapshot, tokenTagRange.SnapshotRange.TextRange), new ClassificationTag(ClassificationTypes.OtherError) )); } break; } } } } } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> public override IEnumerable <TagSnapshotRange <IUnusedRegionTag> > GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { var parseData = this.Document.ParseData as CustomDotNetParseData; if ((parseData != null) && (parseData.UnusedRanges != null)) { var count = parseData.UnusedRanges.Count; if (count > 0) { // Return the intersecting snapshot ranges specified in the parse data foreach (var snapshotRange in snapshotRanges) { var index = parseData.UnusedRanges.BinarySearch(new TextSnapshotOffset(snapshotRange.Snapshot, snapshotRange.StartOffset)); if (index < 0) { index = ~index; } while (index < count) { var unusedSnapshotRange = parseData.UnusedRanges[index]; if (snapshotRange.OverlapsWith(unusedSnapshotRange)) { yield return(new TagSnapshotRange <IUnusedRegionTag>(unusedSnapshotRange, new UnusedRegionTag())); } else if (unusedSnapshotRange.StartOffset >= snapshotRange.EndOffset) { break; } index++; } } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> public override IEnumerable <TagSnapshotRange <IClassificationTag> > GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { // Loop through the requested snapshot ranges... foreach (TextSnapshotRange snapshotRange in snapshotRanges) { // If the snapshot range is not zero-length... if (!snapshotRange.IsZeroLength) { IEnumerable <TagSnapshotRange <ITokenTag> > tokenTagRanges = tokenTagAggregator.GetTags(snapshotRange); if (tokenTagRanges != null) { foreach (TagSnapshotRange <ITokenTag> tokenTagRange in tokenTagRanges) { if (tokenTagRange.Tag.Token != null) { switch (tokenTagRange.Tag.Token.Key) { case "XmlCommentText": { if (highlightDocumentationComments) { // Get the text of the token string text = tokenTagRange.SnapshotRange.Text; // Look for the text "Actipro" int index = text.IndexOf("Actipro"); while (index != -1) { // Add a highlighted range yield return(new TagSnapshotRange <IClassificationTag>( new TextSnapshotRange(snapshotRange.Snapshot, TextRange.FromSpan(tokenTagRange.SnapshotRange.StartOffset + index, 7)), new ClassificationTag(ClassificationTypes.SyntaxError) )); // Look for another match index = text.IndexOf("Actipro", index + 7); } } break; } case "Identifier": { if (highlightIdentifiers) { // Get the text of the token string text = tokenTagRange.SnapshotRange.Text; // If the text is "Actipro"... if (text == "Actipro") { // Add a highlighted range yield return(new TagSnapshotRange <IClassificationTag>( new TextSnapshotRange(snapshotRange.Snapshot, tokenTagRange.SnapshotRange.TextRange), new ClassificationTag(ClassificationTypes.SyntaxError) )); } } break; } } } } } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> public override IEnumerable <TagSnapshotRange <IIntraLineSpacerTag> > GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { if (snapshotRanges != null) { foreach (var snapshotRange in snapshotRanges) { var index = this.BinarySearchDeclarations(snapshotRange); if (index < 0) { index = ~index; } while (index < cachedDeclarations.Count) { var declaration = cachedDeclarations[index++]; var startOffset = declaration.VersionRange.Translate(snapshotRange.Snapshot).StartOffset; if (snapshotRange.Contains(startOffset)) { yield return(new TagSnapshotRange <IIntraLineSpacerTag>(new TextSnapshotRange(snapshotRange.Snapshot, startOffset), new CodeLensTag() { Declaration = declaration, TopMargin = 12 })); } } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // INTERFACE IMPLEMENTATION ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> IEnumerable <TagSnapshotRange <IIntraTextSpacerTag> > ITagger <IIntraTextSpacerTag> .GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { foreach (TagSnapshotRange <ICollapsedRegionTag> tagRange in this.GetTags(snapshotRanges, parameter)) { CollapsedRegionTag tag = tagRange.Tag as CollapsedRegionTag; if (tag != null) { yield return(tag.ToIntraTextSpacerTagRange(tagRange.SnapshotRange)); } } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // INTERFACE IMPLEMENTATION ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> IEnumerable <TagSnapshotRange <IClassificationTag> > ITagger <IClassificationTag> .GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { // We implement ITagger<IClassificationTag> explicitly so that the core CollectionTagger can // return tags of type IReadOnlyRegionTag. This method can return IClassificationTag tags so that the core // SyntaxEditor rendering procedures can update syntax highlighting over the marked ranges if (!highlightReadOnlyRegions) { yield break; } foreach (TagSnapshotRange <IReadOnlyRegionTag> tagRange in this.GetTags(snapshotRanges, parameter)) { yield return(new TagSnapshotRange <IClassificationTag>(tagRange.SnapshotRange, (IClassificationTag)tagRange.Tag)); } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // INTERFACE IMPLEMENTATION ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Returns the tag ranges that intersect with the specified normalized snapshot ranges. /// </summary> /// <param name="snapshotRanges">The collection of normalized snapshot ranges.</param> /// <param name="parameter">An optional parameter that provides contextual information about the tag request.</param> /// <returns>The tag ranges that intersect with the specified normalized snapshot ranges.</returns> IEnumerable <TagSnapshotRange <IClassificationTag> > ITagger <IClassificationTag> .GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter) { // We implement ITagger<IIntraTextSpacerTag> explicitly so that the core CollectionTagger can // return tags of type IIntraTextSpacerTag which SyntaxEditor uses to add intra-text spacing and this // method can return IClassificationTag tags that the core SyntaxEditor rendering procedures // can update syntax highlighting over the marked ranges foreach (TagSnapshotRange <IIntraTextSpacerTag> tagRange in this.GetTags(snapshotRanges, parameter)) { yield return(new TagSnapshotRange <IClassificationTag>(tagRange.SnapshotRange, (IClassificationTag)tagRange.Tag)); } }