You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
15
+
This article describes the extensibility object model that represents the Visual Studio editor and the text document that's opened for editing. For an introduction to working with the editor extension functionality, see [Use Visual Studio editor extensibility](editor.md).
16
16
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.
17
+
The extensibility object model for the Visual Studio editor consists 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. The article also introduces[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.
18
18
19
19
## ITextViewSnapshot
20
20
21
-
[ITextViewSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextviewsnapshot) contains the URI and version information necessary to acquire an [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot)as well as some properties of the text view, such as selections.
21
+
The [ITextViewSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextviewsnapshot)class contains the URI and version information necessary to acquire an [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot)class and some properties of the text view, such as selections.
22
22
23
-
- This object is immutable and will never change after it is created.
24
-
- You can use `ITextViewSnapshot.GetTextDocumentAsync()` to get the content from the document. Calling this method is expensive and only should be done if you need the document content.
25
-
-`ITextViewSnapshot` can't be changed directly. All changes are requested via mutation. See[Make changes in a text document from an extension](./walkthroughs/editing-text.md).
23
+
- This object is immutable and never changes after it's created.
24
+
- You can use `ITextViewSnapshot.GetTextDocumentAsync()` to get the content from the document. Calling this method is expensive, and you should do it only if you need the document content.
25
+
-The `ITextViewSnapshot`class can't be changed directly. All changes are requested via mutation. For more information, see[Make changes in a text document from an extension](./walkthroughs/editing-text.md).
26
26
27
27
## ITextDocumentSnapshot
28
28
29
-
[ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot) contains the content of the text document from a point in time or version.
29
+
The [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot) class contains the content of the text document from a point in time or version.
30
30
31
-
- This object is immutable and will never change after it is created.
32
-
-`ITextDocumentSnapshot` can't be changed directly. All changes are requested via mutation. See[Make changes in a text document from an extension](./walkthroughs/editing-text.md).
31
+
- This object is immutable and never changes after it's created.
32
+
-The `ITextDocumentSnapshot`class can't be changed directly. All changes are requested via mutation. For more information, see[Make changes in a text document from an extension](./walkthroughs/editing-text.md).
33
33
34
34
If you're familiar with legacy Visual Studio extensions, [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot) is almost the same as
35
35
[ITextSnapshot](/dotnet/api/microsoft.visualstudio.text.itextsnapshot) and supports most of the same methods for accessing the text.
36
36
37
-
Best Practices:
37
+
### Best practices
38
38
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.
40
-
-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 `Range`, to avoid creating a large throwaway string from the entire line or document.
42
-
- 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 oldenough 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.
39
+
-Use position and range to represent substrings in the document without expending resources by copying or allocating strings. Most APIs operate in terms of these primitives.
40
+
-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 `Range` to avoid creating a large throwaway string from the entire line or document.
42
+
- Avoid the assumption that lines or documents are short. Many languages minify into huge lines or consume large files.
43
+
-The[ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot)class references large data structures that might consume memory if an old-enough version is stored. The 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 that the old `ITextDocumentSnapshot` version can be garbage collected.
44
44
45
45
## Position
46
46
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.
47
+
The [TextPosition](/dotnet/api/microsoft.visualstudio.extensibility.editor.textposition)class 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)class that it came from and supports `GetChar()` to get the character directly at that point.
48
48
49
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.
50
50
51
51
## Range
52
52
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.
53
+
The [TextRange](/dotnet/api/microsoft.visualstudio.extensibility.editor.textrange)class represents a contiguous substring of characters within an [ITextDocumentSnapshot](/dotnet/api/microsoft.visualstudio.extensibility.editor.itextdocumentsnapshot) class. As opposed to a string created with `string.Substring()` or `ITextDocumentSnapshot.CopyToString()`, creating a `TextRange`class doesn't require any allocations or extra memory. Later, you can call [CopyToString()](/dotnet/api/microsoft.visualstudio.extensibility.editor.textextensions.copytostring) to realize it into a string in a deferred fashion.
54
54
55
55
If you're familiar with legacy Visual Studio extensions, `TextRange` is almost the same as
56
56
[SnapshotSpan](/dotnet/api/microsoft.visualstudio.text.snapshotSpan) and supports most of the same methods.
57
57
58
58
## Tracking modes
59
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.
60
+
The `TextPosition` and `TextRange`classes are associated with a specific `ITextDocumentSnapshot` class, which is the state of the document at a specific time. You can use the `TranslateTo` methods to translate such positions and ranges to a different snapshot. Such translation takes into account any text that was added or removed before, after (or, in the case of ranges, in the middle of) the position or range. When any of these 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
61
62
62
## Tag
63
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.).
64
+
[Taggers](./walkthroughs/taggers.md) are used to associate data (a [tag](/dotnet/api/microsoft.visualstudio.extensibility.editor.itag)) with spans of text. Other Visual Studio features (for example, [CodeLens](./walkthroughs/codelens.md) and [classification](./walkthroughs/classification.md)) consume such data.
65
65
66
66
## Related content
67
67
68
-
Review sample code for a simple editor-based extension:
- Review sample code for a simple editor-based extension in [DocumentSelectorSample](https://github.com/Microsoft/VSExtensibility/tree/main/New_Extensibility_Model/Samples/DocumentSelectorSample/).
Since the new Visual Studio extensibility model is entirely in a separate process, and communication between the extension process and the Visual Studio process occurs through a stream, all APIs have to at some level operate with serializable data types. Typically, extensions can ignore these implementation details. In some scenarios, an extension may need to interface directly with RPC services acquired from `this.Extensibility.ServiceBroker`.
15
+
The new Visual Studio extensibility model is entirely in a separate process, and communication between the extension process and the Visual Studio process occurs through a stream. As a result, all APIs have to operate at some level with serializable data types. Typically, extensions can ignore these implementation details. In some scenarios, an extension might need to interface directly with remote procedure call (RPC) services acquired from `this.Extensibility.ServiceBroker`.
16
16
17
17
## Serializable RPC types
18
18
19
19
To facilitate interactions with RPC services, the object model exposes [RpcContract](/dotnet/api/microsoft.visualstudio.extensibility.editor.selection.rpccontract) properties on most core types, and the following serializable RPC types:
20
20
21
-
-`VersionedTextDocumentRange` - 1:1 serializable version of `Span`, which you can access through the `RpcContract` property. This type should be used in most RPC contracts between processes.
22
-
-`VersionedTextDocumentPosition` - 1:1 serializable version of `Position`, which you can access through the `RpcContract` property. This type should be used in most RPC contracts between processes.
23
-
-`Range` - Serializable version of Span, omitting the Uri and version number.
24
-
-`Microsoft.VisualStudio.RpcContracts.Utilities.Position` - Serializable version of `Position`, omitting the Uri and version number.
25
-
-`TextView` - 1:1 serialized form of `ITextView`, which you can access through the `RpcContract` property.
26
-
-`TextDocument` - 1:1 serialized form of `ITextDocument` through the `RpcContract` property.
21
+
-`VersionedTextDocumentRange`: 1:1 serializable version of `Span`, which you can access through the `RpcContract` property. Use this type in most RPC contracts between processes.
22
+
-`VersionedTextDocumentPosition`: 1:1 serializable version of `Position`, which you can access through the `RpcContract` property. Use this type in most RPC contracts between processes.
23
+
-`Range`: Serializable version of Span, which omits the URI and version number.
24
+
-`Microsoft.VisualStudio.RpcContracts.Utilities.Position`: Serializable version of `Position`, which omits the URI and version number.
25
+
-`TextView`: 1:1 serialized form of `ITextView`, which you can access through the `RpcContract` property.
26
+
-`TextDocument`: 1:1 serialized form of `ITextDocument` through the `RpcContract` property.
27
27
28
-
As opposed to `VersionedTextDocumentRange` and `VersionedTextDocumentPosition`, `Range` and `Microsoft.VisualStudio.RpcContracts.Utilities.Position` omit the Uri and document version, making for a smaller serializable representation. This type should be used in RPC contracts that contain lots of span/range equivalents that need to reduce their payload size for performance. These RPC contracts need to pass the document Uri and version for the spans or range to be instantiated into `Span` and `Position` objects by the [IEditorHostService](/dotnet/api/microsoft.visualstudio.extensibility.editor.ieditorhostservice). `IEditorHostService` interfaces with extension-local copies of the text buffer, and manages opening and closing of documents described by the RPC types.
28
+
As opposed to `VersionedTextDocumentRange` and `VersionedTextDocumentPosition`, `Range` and `Microsoft.VisualStudio.RpcContracts.Utilities.Position` omit the URI and document version, which makes for a smaller serializable representation. Use this type in RPC contracts that contain lots of span or range equivalents that need to reduce their payload size for performance. These RPC contracts need to pass the document URI and version for the spans or range to be instantiated into `Span` and `Position` objects by [IEditorHostService](/dotnet/api/microsoft.visualstudio.extensibility.editor.ieditorhostservice). The `IEditorHostService` interfaces with extension-local copies of the text buffer and manages the opening and closing of documents described by the RPC types.
29
29
30
30
## Related content
31
31
32
-
Learn more about Remote UI, the RPC model used in VS.Extensibilty at[Remote UI](../inside-the-sdk/remote-ui.md).
32
+
- To learn more about Remote UI, the RPC model used in `VisualStudio.Extensibility`, see[Remote UI](../inside-the-sdk/remote-ui.md).
0 commit comments