Skip to content

Commit 90852d0

Browse files
Merge pull request #10764 from MicrosoftDocs/main638834726407935646sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents f7e31b5 + e1d8833 commit 90852d0

File tree

9 files changed

+195
-53
lines changed

9 files changed

+195
-53
lines changed

docs/extensibility/visualstudio.extensibility/editor/editor-concepts.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ms.subservice: extensibility-integration
1414

1515
This article describes the extensibility object model that represents the Visual Studio editor and the text document that is opened for editing. For an introduction to working with the editor extension functionality, see [Use Visual Studio editor extensibility](editor.md).
1616

17-
The Visual Studio Editor extensibility object model is composed of a few integral parts. This article covers [ITextViewSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextviewsnapshot), [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot), and other abstract representations of the whole document, as well as `Position` and `Span`, which represent location and spans of text, respectively.
17+
The Visual Studio Editor extensibility object model is composed of a few integral parts. This article covers [ITextViewSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextviewsnapshot), [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot), and other abstract representations of the whole document, as well as [TextPosition](/dotnet/api/microsoft.visualstudio.extensibility.editor.textposition) and [TextRange](/dotnet/api/microsoft.visualstudio.extensibility.editor.textrange), which represent locations and spans of text, respectively.
1818

1919
## ITextViewSnapshot
2020

@@ -36,25 +36,33 @@ If you're familiar with legacy Visual Studio extensions, [ITextDocumentSnapshot]
3636

3737
Best Practices:
3838

39-
- You can use Position and Span to represent substrings in the document without expending resources copying or allocating strings. Most APIs operate in terms of these primitives.
39+
- You can use Position and Range to represent substrings in the document without expending resources copying or allocating strings. Most APIs operate in terms of these primitives.
4040
- You can use the indexer syntax, `textDocument[0]`, to read character by character in the document without copying it to a string.
41-
- If you must create a string such as for use as a dictionary key, use the overload that takes a `Span`, to avoid creating a large throwaway string from the entire line or document.
41+
- If you must create a string such as for use as a dictionary key, use the overload that takes a `Range`, to avoid creating a large throwaway string from the entire line or document.
4242
- Avoid assuming lines or documents will be short. Many languages minify into huge lines or consume very large files
43-
- - [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot) references large data structures that may consume memory if an old enough version is stored. Best practice is to periodically update positions and spans that you're storing long term to the latest document version via their `TranslateTo()` method so the old `ITextDocumentSnapshot` version can be garbage collected.
43+
- - [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot) references large data structures that may consume memory if an old enough version is stored. Best practice is to periodically update positions and ranges that you're storing long term to the latest document version via their `TranslateTo()` method so the old `ITextDocumentSnapshot` version can be garbage collected.
4444

4545
## Position
4646

47-
Represents a position within the text document. As opposed to `int` positions, the Position type is aware of the [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot) it came from and supports `GetChar()` to directly get the character at that point.
47+
[TextPosition](/dotnet/api/microsoft.visualstudio.extensibility.editor.textposition) represents a position within the text document. As opposed to `int` positions, the `TextPosition` type is aware of the [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot) it came from and supports `GetChar()` to directly get the character at that point.
4848

49-
If you're familiar with legacy Visual Studio extensions, Position is almost the same as [SnapshotPoint](/dotnet/api/microsoft.visualstudio.text.snapshotpoint) and supports most of the same methods.
49+
If you're familiar with legacy Visual Studio extensions, `TextPosition` is almost the same as [SnapshotPoint](/dotnet/api/microsoft.visualstudio.text.snapshotpoint) and supports most of the same methods.
5050

51-
## Span
51+
## Range
5252

53-
Represents a contiguous substring of characters within an [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot). As opposed to a string created with `string.Substring()` or `ITextDocumentSnapshot.CopyToString()`, creating a Span doesn't require any allocations or additional memory. You can later call `Span.GetText()` to realize it into a string in a deferred fashion.
53+
[TextRange](/dotnet/api/microsoft.visualstudio.extensibility.editor.textrange) represents a contiguous substring of characters within an [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot). As opposed to a string created with `string.Substring()` or `ITextDocumentSnapshot.CopyToString()`, creating a `TextRange` doesn't require any allocations or additional memory. You can later call [CopyToString()](/dotnet/api/microsoft.visualstudio.extensibility.editor.textextensions.copytostring) to realize it into a string in a deferred fashion.
5454

55-
If you're familiar with legacy Visual Studio extensions, `Position` is almost the same as
55+
If you're familiar with legacy Visual Studio extensions, `TextRange` is almost the same as
5656
[SnapshotSpan](/dotnet/api/microsoft.visualstudio.text.snapshotSpan) and supports most of the same methods.
5757

58+
## Tracking modes
59+
60+
`TextPosition` and `TextRange` are associated with a specific `ITextDocumentSnapshot`: the state of the document at a specific time. Such positions and ranges can be translated to a different snapshot using the `TranslateTo` methods. Such translation takes in account any text added or removed before, after (or, in the case of ranges, in the middle) the position or range. When any of such edits happen exactly at the position or exactly at the edge of the range, [TextPositionTrackingMode](/dotnet/api/microsoft.visualstudio.extensibility.editor.textpositiontrackingmode) and [TextRangeTrackingMode](/dotnet/api/microsoft.visualstudio.extensibility.editor.textrangetrackingmode) are used to specify how the translation should behave.
61+
62+
## Tag
63+
64+
[Taggers](./walkthroughs/taggers.md) are used to associate data (a [tag](/dotnet/api/microsoft.visualstudio.extensibility.editor.itag)) with spans of text, such data is consumed by other Visual Studio features (E.g., [CodeLens](./walkthroughs/codelens.md), [classification](./walkthroughs/classification.md), etc.).
65+
5866
## Related content
5967

6068
Review sample code for a simple editor-based extension:

docs/extensibility/visualstudio.extensibility/editor/editor.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The Visual Studio editor generally refers to the functionality of editing text f
1818

1919
The editor object model is described at [Editor concepts](editor-concepts.md).
2020

21-
The following sections summarize the high level editor extensibility scenarios that VisualStudio.Extensibility supports. However, because each of these scenarios are rich in concepts and APIs, a separate, more detailed section will be linked to provide a more in-depth explanation of the APIs and how to use them.
21+
The following sections summarize the high level editor extensibility scenarios that VisualStudio.Extensibility supports. However, because each of these scenarios is rich in concepts and APIs, a separate, more detailed section will be linked to provide a more in-depth explanation of the APIs and how to use them.
2222

2323
## Reading text and observing changes in the editor
2424
The most fundamental extensibility point for the Visual Studio editor is to manipulate text, either reading the text in the editor, or editing the text. These scenarios are core to any editor based extensions. For example, in order to provide diagnostics information such as warnings or errors, the extension has to read the code that's in the editor, and then interpret it. An extension also needs a way to detect when the text in the editor is being changed, or if there's a new file being opened, or an open file is being closed.
@@ -36,15 +36,15 @@ Besides manipulating text, extensions can also plugin into many features in the
3636
### Text view margin
3737
Extensions can contribute new text view margins to the Visual Studio editor. A text view margin is a rectangular UI control attached to a text view on one of its four sides. These UI controls offer additional information at a glance, which allows developers to stay productive working in the editor. Many of the most popular features in the Visual Studio editor utilize text view margins, like the zoom control, breakpoint indicator, line number indicator, and many more.
3838

39-
For detailed walkthough of how to customize text view margin with your extension, refer to [Extending Visual Studio editor with a new margin](./walkthroughs/textview-margin.md)
39+
For detailed walkthrough of how to customize text view margin with your extension, refer to [Extending Visual Studio editor with a new margin](./walkthroughs/textview-margin.md)
4040

4141
### CodeLens
4242
Extensions can contribute new CodeLenses to the Visual Studio editor. A CodeLens is a visual indicator displayed above lines of text providing actionable contextual information such as number of references to a code element, results of the last unit test run or actions to run/debug a unit test. Some common CodeLenses include code reference CodeLens, which provide information on all the different parts of the code that reference a particular block of code, or the code history CodeLens, which provide information on how the particular block of code has been changed in source control.
4343

4444
For detailed walkthrough of how to provide your own CodeLens with your extension, refer to [Extending Visual Studio editor with a new CodeLens](./walkthroughs/codelens.md)
4545

4646
### Taggers
47-
Extensions can contribute new taggers to the Visual Studio editor. Taggers are used to associate data with spans of text, such data is consumed by other Visual Studio features (E.g., CodeLens).
47+
Extensions can contribute new taggers to the Visual Studio editor. Taggers are used to associate data with ranges of text, such data is consumed by other Visual Studio features (E.g., [CodeLens](./walkthroughs/codelens.md), [classification](./walkthroughs/classification.md), etc.).
4848

4949
For detailed walkthrough of how to provide your own taggers with your extension, refer to [Extending Visual Studio editor with a new tagger](./walkthroughs/taggers.md)
5050

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: Customizing classification in the editor
3+
description: A walkthrough of how to provide your own classification in the Visual Studio editor using extensions
4+
ms.date: 4/23/2025
5+
ms.topic: conceptual
6+
ms.author: maprospe
7+
monikerRange: ">=vs-2022"
8+
author: tinaschrepfer
9+
manager: mijacobs
10+
ms.subservice: extensibility-integration
11+
---
12+
13+
# Extending Visual Studio editor with a new classification tagger
14+
15+
A Visual Studio extension can classify a document's syntax allowing for
16+
the text to be colorized accordingly, which is achieved by contributing
17+
a tagger that returns `ClassificationTag` values.
18+
19+
A detailed description of how to provide a tagger can be found in the
20+
[Extending Visual Studio editor with a new tagger](./taggers.md) article.
21+
22+
To provide classification, we first implement a tagger provider and a
23+
tagger:
24+
25+
```cs
26+
[VisualStudioContribution]
27+
internal class MyClassificationTaggerProvider :
28+
ExtensionPart,
29+
ITextViewTaggerProvider<ClassificationTag>,
30+
ITextViewChangedListener
31+
{
32+
...
33+
```
34+
35+
```cs
36+
internal class MyClassificationTagger :
37+
TextViewTagger<ClassificationTag>
38+
{
39+
...
40+
```
41+
42+
Since we want the document colorization to appear as instantly as possible,
43+
the generation of taggers needs to be as fast as possible. [This](./taggers.md) article
44+
stresses the importance of:
45+
- only generating tags for the requested document portion (or a small
46+
superset of it), not the whole document;
47+
- avoiding parsing the whole document in order to generate tags.
48+
49+
Once the tagger structure is ready and the syntax parsing for the specific
50+
file format is implemented, the tagger can provide text classification, by creating `ClassificationTag` values using the available
51+
`ClassificationType` know values, and calling `UpdateTagsAsync`.
52+
53+
```cs
54+
List<TaggedTrackingTextRange<ClassificationTag>> tags = new();
55+
List<TextRange> ranges = new();
56+
57+
...
58+
59+
ranges.Add(new(document, lineStart, lineLength));
60+
tags.Add(
61+
new TaggedTrackingTextRange<ClassificationTag>(
62+
new TrackingTextRange(
63+
document,
64+
tagStartPosition,
65+
tagLength,
66+
TextRangeTrackingMode.ExtendNone),
67+
new ClassificationTag(ClassificationType.KnownValues.Operator)));
68+
69+
...
70+
71+
await this.UpdateTagsAsync(ranges, tags, CancellationToken.None);
72+
```
73+
74+
At this time, VisualStudio.Extensibility doesn't support defining text colors
75+
for new classification types yet, so we must use existing classification types
76+
(`ClassificationType.KnownValues`).
77+
78+
[VisualStudio.Extensibility in-proc extension](../../get-started/in-proc-extensions.md), can use [ClassificationTypeDefinition](/dotnet/api/microsoft.visualstudio.text.classification.classificationtypedefinition)
79+
to define new classification types. Their name can be referenced using
80+
`ClassificationType.Custom`.

docs/extensibility/visualstudio.extensibility/editor/walkthroughs/taggers.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ ms.subservice: extensibility-integration
1111
---
1212

1313
# Extending Visual Studio editor with a new tagger
14-
Extensions can contribute new taggers to Visual Studio. Taggers are used to associate data with spans of text. The data provided by taggers are consumed by other Visual Studio features (for example, [CodeLens](./codelens.md)).
14+
Extensions can contribute new taggers to Visual Studio. Taggers are used to associate data with ranges of text. The data provided by taggers are consumed by other Visual Studio features (for example, [CodeLens](./codelens.md)).
1515

1616
VisualStudio.Extensibility only supports tag types that are provided by the [Microsoft.VisualStudio.Extensibility](https://www.nuget.org/packages/Microsoft.VisualStudio.Extensibility) package and implement the `Microsoft.VisualStudio.Extensibility.Editor.ITag` interface:
1717

1818
- `CodeLensTag` can be used together with an [ICodeLensProvider](./codelens.md) to add Code Lenses to documents
1919
- `TextMarkerTag` can be used to highlight portions of documents. VisualStudio.Extensibility doesn't support defining new Text Marker styles yet, so only styles that are built into Visual Studio or provided by a VSSDK extension can be used for now (a [VisualStudio.Extensibility in-proc extension](../../get-started/in-proc-extensions.md) can create Text Marker styles with an `[Export(typeof(EditorFormatDefinition))]`).
20+
- `ClassificationTag` can be used to classify a document's syntax allowing for the text to be colorized accordingly.
2021

2122
To generate tags, the extension must contribute an extension part that implements `ITextViewTaggerProvider<>` for the type (or types) of tags provided. The extension part also needs to implement `ITextViewChangedListener` to react to document changes:
2223

@@ -142,7 +143,7 @@ internal class MarkdownCodeLensTagger : TextViewTagger<CodeLensTag>
142143

143144
Both the `TextViewChangedAsync` and `RequestTagsAsync` methods, should call `UpdateTagsAsync` providing the ranges that tags are being updated for and the new tags themselves. The `TextViewTagger<>` base class holds a cache of previously generated tags, calling `UpdateTagsAsync` invalidates all existing tags for the provided ranges and replaces them with the newly provided ones.
144145

145-
While generating tags for the entire document is a possible strategy, it is preferrable to only generate tags for the requested ranges (in `RequestTagsAsync`) and the edited ranges (in `TextViewChangedAsync`). It is also common to have to extend such ranges to cover meaningful spans of the document syntax (for example, entire statements, entire lines of code, etc.).
146+
While generating tags for the entire document is a possible strategy, it is preferable to only generate tags for the requested ranges (in `RequestTagsAsync`) and the edited ranges (in `TextViewChangedAsync`). It is also common to have to extend such ranges to cover meaningful spans of the document syntax (for example, entire statements, entire lines of code, etc.).
146147

147148
Handling text view changes in particular requires some additional code. The following code snippet is an example:
148149

docs/extensibility/visualstudio.extensibility/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ items:
4646
- name: Extending Visual Studio editor with a new CodeLens
4747
href: ./editor/walkthroughs/codelens.md
4848
- name: Extending Visual Studio editor with a new tagger
49-
href: ./editor/walkthroughs/taggers.md
49+
href: ./editor/walkthroughs/taggers.md
50+
- name: Extending Visual Studio editor with new classification tagger
51+
href: ./editor/walkthroughs/classification.md
5052
- name: Editor concepts
5153
href: ./editor/editor-concepts.md
5254
- name: Editor RPC
6.79 KB
Loading
9.58 KB
Loading

0 commit comments

Comments
 (0)