Skip to content

Commit df702df

Browse files
authored
Merge pull request #10490 from MicrosoftDocs/main638672474446769190sync_temp
Repo sync for protected branch
2 parents 5f3e337 + ec685be commit df702df

File tree

12 files changed

+144
-82
lines changed

12 files changed

+144
-82
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

docs/extensibility/visualstudio.extensibility/diagnostics/visualstudio-extensibility-diagnostics-extension.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.date: 05/09/2024
1111

1212
# Overview
1313

14-
The VisualStudio.Extensibility Diagnostics Explorer extension is designed to help debug VisualStudio.Extensibility extensions while developing them. The target audience for this extension is extension authors, not extension consumers. This extension provides an interface for inspecting the extensibility points that extensions are interacting with, and what configuration objects extensions are providing to the IDE, and the metadata of those configuration objects.
14+
The VisualStudio.Extensibility Diagnostics Explorer extension is designed to help debug VisualStudio.Extensibility extensions while developing them. The target audience for this extension is extension authors, not extension consumers. This extension provides an interface for inspecting the extensibility points that extensions are interacting with, and what configuration objects extensions are providing to the IDE, and the metadata of those configuration objects.
1515

1616
## Get Started
1717

@@ -22,7 +22,7 @@ Follow these instructions to install, launch, and configure the Diagsnotics Expl
2222
You can get the Diagnostics Explorer either directly through the Extension Manager in Visual Studio, or from the Visual Studio Marketplace [here](https://aka.ms/VisualStudio.Extensibility/DiagnosticsExplorer).
2323

2424
> [!NOTE]
25-
> The VisualStudio.Extensibility Diagnostics Explorer extension is compatable with Visual Studio 2022 17.10 and higher.
25+
> The VisualStudio.Extensibility Diagnostics Explorer extension is compatible with Visual Studio 2022 17.12 and higher.
2626
2727
### Open the Diagnostics Explorer in Visual Studio
2828

@@ -38,9 +38,7 @@ The Diagnostics Explorer is configured to collect relevant diagnostic data from
3838

3939
## Diagnostics Pages
4040

41-
The left-hand panel of the Diagnostics Explorer tool window contains a list different *feature pages*. These pages correspond to the extensible features for which you can find diagnostic information about the components and configurations that Visual Studio discovered from your extensions. For example, you can select **Commands** to view diagnostics related to your commands, command sets, menus and toolbars, placements, and more.
42-
43-
Also in the list are two special pages that aren't directly related to any components in your extension: [**Events**](#events-page), a live viewer for extensibility-related events, and [**Activation Constraints**](#activation-constraints-page), which shows relevant IDE state to help you craft your activation constraints.
41+
The left-hand panel of the Diagnostics Explorer tool window contains a list different *diagnostics pages*. There are two types of diagnostics pages - extensible features and extensibility infrastructure. Extensible features pages provide diagnostic information about a specific extensible component within Visual Studio, like commands. Extensibility infrastructure pages provide diagnostic information that's relevant regardless of which extensibility points your extension plugs into. The [**Events**](#events-page) page, a live viewer for extensibility-related events, and the [**Client Contexts**](#client-contexts-page) page, which shows relevant IDE state to help you craft your activation constraints, are the two extensibility infrastructure pages.
4442

4543
![Screenshot of the left hand panel of the tool window.](./media/image-2.png)
4644

@@ -64,7 +62,9 @@ Some cells in the DataGrid display **[Click to Expand]**. This message means tha
6462

6563
![Screenshot of the "Click to Expand" modal dialog."](./media/image-6.png)
6664

67-
### Events Page
65+
### Extensible Infrastructure Pages
66+
67+
#### Events Page
6868

6969
The events page shows you when "something happens" in the IDE. Each event appears as a new row in the DataGrid, along with any properties related to that event. For example, when a command is executed, a **Commands - Executing** event appears in the view showing the ID of the extension that the command belongs to and the ID of the command that was executed.
7070

@@ -76,29 +76,29 @@ Clicking the **Clear All** button deletes all the events currently shown in the
7676

7777
![Screenshot of the Events page.](./media/image-7.png)
7878

79-
#### Column Descriptions
79+
##### Column Descriptions
8080

81-
##### Extension ID
81+
###### Extension ID
8282

8383
The **Extension Id** column shows the ID of the extension that the instance of the event is related to. It's possible for this cell to be empty for some events if they don't belong to a specific extension but instead apply to the IDE as a whole.
8484

85-
##### Item ID
85+
###### Item ID
8686

8787
The **Item Id** column shows the ID of the extension item that the instance of the event is related to. For example, the **Item Id** for the **Commands - Executing** event would be the ID of the command that was executed.
8888

89-
##### Properties
89+
###### Properties
9090

9191
The **Properties** column shows the set of properties related to that instance of the event that could be displayed in a single cell in a DataGrid. More verbose properties would only be visible from the **More Info** dialog, or by changing the **Event** ComboBox to the name of the specific event that you're interested in.
9292

93-
##### More Info
93+
###### More Info
9494

9595
The **More Info** column displays more verbose metadata related to an event. Clicking a cell in this column opens a modal dialog containing all of the metadata related to the event represented by that row.
9696

9797
![Screenshot of the More Info dialog.](./media/image-8.png)
9898

99-
### Activation Constraints Page
99+
#### Client Contexts Page
100100

101-
The activation constraints page shows the state of various properties related to the IDE itself. The **Context** ComboBox changes the data in the view to either show all of the activation constraints related to the currently selected item in the **Acitvation Constraints** ComboBox, or a log of all the times these properties changed since the tool window was opened. When the **Events** context is selected, the **Clear All** button can be used to delete all the events currently being displayed in the view.
101+
The client contexts page shows the state of various properties related to the IDE itself. The **Context** ComboBox changes the data in the view to either show all of the activation constraints related to the currently selected item in the **Client Contexts** ComboBox, or a log of all the times these properties changed since the tool window was opened. When the **Events** context is selected, the **Clear All** button can be used to delete all the events currently being displayed in the view.
102102

103103
![Screenshot of the Activation Constraint's Current State page.](./media/image-9.png)
104104

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ For the initial release of the new Visual Studio extensibility model, only the f
2222
- Performing text edits and selection/caret changes.
2323
- Defining new document types.
2424
- Extending text views with new text view margins.
25+
- Extending text views with new Code Lenses.
2526

2627
The Visual Studio editor generally refers to the functionality of editing text files, known as documents, of any type. Individual files may be opened for editing, and the open editor window is referred to as a `TextView`.
2728

@@ -222,6 +223,20 @@ To avoid misplaced edits, edits from editor extensions are applied as follows:
222223
1. Edit requests are sent to Visual Studio IDE, where it succeeds only if the object being mutated hasn't changed since the version the request was made one. If the document has changed, the change may be rejected, requiring the extension to retry on newer version. Outcome of mutation operation is stored in `result`.
223224
1. Edits are applied atomically, meaning without interruption from other executing threads. The best practice is to do all changes that should occur within a narrow time frame into a single `EditAsync()` call, to reduce the likelihood of unexpected behavior arising from user edits, or language service actions that occur between edits (for example, extension edits getting interleaved with Roslyn C# moving the caret).
224225
226+
## Changing caret position or selecting text from an extension
227+
228+
Editing text document from an extension implicitly affects caret position. For example, inserting some text at the caret will move the caret to the end of the inserted text. Extensions can also use [`ITextViewSnapshot.AsEditable().SetSelections()`](/dotnet/api/microsoft.visualstudio.text.itextvieweditor.setselections) to set the caret explicitly to a different position or make text selections. To illustrate, the following code would insert some text, but keep the caret at the original position:
229+
230+
```csharp
231+
await this.Extensibility.Editor().EditAsync(batch =>
232+
{
233+
var caret = textView.Selection.Extent.Start;
234+
textView.Document.AsEditable(batch).Replace(textView.Selection.Extent, newGuidString);
235+
textView.AsEditable(batch).SetSelections([new Selection(activePosition: caret, anchorPosition: caret, insertionPosition: caret)]);
236+
},
237+
cancellationToken);
238+
```
239+
225240
### Asynchronous execution
226241
227242
[ITextViewSnapshot.GetTextDocumentAsync](/visualstudio/extensibility/visualstudio.extensibility/editor/editor-concepts?view=vs-2022#itextviewsnapshot) opens a copy of the text document in the Visual Studio extension. Since extensions run in a separate process, all extension interactions are asynchronous, cooperative, and have some caveats:
@@ -283,6 +298,97 @@ For more information, see [Word Count Margin Sample](https://github.com/Microsof
283298
284299
Vertical text view margins whose content needs to be aligned with text view lines are not yet supported.
285300
301+
## Extending Visual Studio editor with a new Code Lens
302+
303+
Extensions can contribute new Code Lenses to the Visual Studio editor. A Code Lens 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.
304+
305+
### Text view Code Lens
306+
Text view Code Lens provide text-based information to segments of code. This is the simplest forms of Code Lens. The following concepts illustrate how to create a text view Code Lens:
307+
- [`ICodeLensProvider`](/dotnet/api/microsoft.visualstudio.extensibility.editor.icodelensprovider) interface is the main interface to implement. Your implementation of this interface will define when your Code Lens will be activated, and which segments of code your Code Lens will be applicable to, and how it will be displayed.
308+
- Within your implementation of [`ICodeLensProvider`](/dotnet/api/microsoft.visualstudio.extensibility.editor.icodelensprovider), you will need to define when the Code Lens should be activated by implementing [`CodeLensProviderConfiguration`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelensproviderconfiguration).
309+
- You will also need to implement the [`TryCreateCodeLensAsync`](/dotnet/api/microsoft.visualstudio.extensibility.editor.icodelensprovider.trycreatecodelensasync) method. This method will be invoked when your Code Lens is activated. In this method, you can define how you want your Code Lens to be displayed and when it should be displayed. This method returns an instance of [`CodeLens`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelens).
310+
- You will need to create your own sub-class of [`CodeLens`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelens), where you get to define how you want your display text to appear through the [`GetLabelAsync`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelens.getlabelasync) method. This method returns an instance of [`CodeLensLabel`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelenslabel), which you can use to configure the text, tooltip, and an optional icon.
311+
312+
### Invokable Code Lens
313+
Invokable Code Lens allows extensions to perform some action (e.g. run a unit test) when user clicks on the Code Lens. Extensions can contribute invokable Code Lens by implementing [`InvokableCodeLens`](/dotnet/api/microsoft.visualstudio.extensibility.editor.invokablecodelens), which derives from [`CodeLens`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelens).
314+
315+
### Visual Code Lens
316+
Visual Code Lens allows extensions to provide custom UI, like a list of references to a method, to be displayed in a popup above the Code Lens when user clicks on the Code Lens. Extensions can contribute visual Code Lens by implementing [`VisualCodeLens`](/dotnet/api/microsoft.visualstudio.extensibility.editor.visualcodelens), which derives from [`CodeLens`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelens). Similar to text view margins, because extensions in VisualStudio.Extensibility might be out-of-process from the Visual Studio, visual Code Lenses provide UI by creating a [`RemoteUserControl`](./../inside-the-sdk/remote-ui.md) and the corresponding data template for that control. While there are some simple examples below, we recommend reading the [Remote UI documentation](./../inside-the-sdk/remote-ui.md) when creating visual Code Lens' UI content.
317+
318+
The sample code below demonstrates how to create a text view Code Lens and an invokable Code Lens:
319+
320+
```csharp
321+
public TextViewExtensionConfiguration TextViewExtensionConfiguration => new()
322+
{
323+
AppliesTo = new[]
324+
{
325+
DocumentFilter.FromDocumentType(DocumentType.KnownValues.Code),
326+
},
327+
};
328+
329+
public CodeLensProviderConfiguration CodeLensProviderConfiguration =>
330+
new("CodeLens Sample Provider") {};
331+
332+
public Task<CodeLens?> TryCreateCodeLensAsync(CodeElement codeElement, CodeElementContext codeElementContext, CancellationToken token)
333+
{
334+
if (codeElement.Kind == CodeElementKind.KnownValues.Method)
335+
{
336+
return Task.FromResult<CodeLens?>(new ClickableCodeLens(codeElement, this.Extensibility));
337+
}
338+
339+
return Task.FromResult<CodeLens?>(new WordCountCodeLens(codeElement, codeElementContext, this.Extensibility, this));
340+
}
341+
342+
...
343+
344+
public class ClickableCodeLens : InvokableCodeLens
345+
{
346+
public override Task ExecuteAsync(CodeElementContext codeElementContext, IClientContext clientContext, CancellationToken cancelToken)
347+
{
348+
this.clickCount++;
349+
this.Invalidate();
350+
return Task.CompletedTask;
351+
}
352+
353+
public override Task<CodeLensLabel> GetLabelAsync(CodeElementContext codeElementContext, CancellationToken token)
354+
{
355+
return Task.FromResult(new CodeLensLabel()
356+
{
357+
Text = this.clickCount == 0 ? "Click me" : $"Clicked {this.clickCount} times",
358+
Tooltip = "Invokable CodeLens Sample Tooltip",
359+
});
360+
}
361+
}
362+
363+
public class WordCountCodeLens : VisualCodeLens
364+
{
365+
public override Task<IRemoteUserControl> GetVisualizationAsync(CodeElementContext codeElementContext, IClientContext clientContext, CancellationToken token)
366+
{
367+
return Task.FromResult<IRemoteUserControl>(new WordCountCodeLensVisual(this.wordCountData));
368+
}
369+
370+
public override Task<CodeLensLabel> GetLabelAsync(CodeElementContext codeElementContext, CancellationToken token)
371+
{
372+
this.wordCountData.WordCount = CountWords(codeElementContext.Range);
373+
return Task.FromResult(new CodeLensLabel()
374+
{
375+
Text = $"Words: {this.wordCountData.WordCount}",
376+
Tooltip = "Number of words in this code element",
377+
});
378+
}
379+
}
380+
```
381+
382+
In addition to configuring Code Lens provider display name, Code Lens providers can also configure priority of their Code Lens. The priority value is used to determine the relative ordering of your Code Lens respective to other Code Lenses. This is done through the [`Priority`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelensproviderconfiguration.priority) property in the [`CodeLensProviderConfiguration`](/dotnet/api/microsoft.visualstudio.extensibility.editor.codelensproviderconfiguration) object.
383+
384+
Code Lenses typically visualize some data related to the text view. For example, they might want to display the number of references to a method. To do so, your Code Lens provider should also [listen to text view events](#add-a-text-view-listener) to react to opening, closing of text views and user typing.
385+
386+
Visual Studio only creates one instance of your Code Lens provider regardless of how many applicable text views a user opens. This means that if your Code Lens needs to maintain state, you need to ensure your Code Lens provider has a way to keep the state of currently open text views.
387+
388+
For more information, see [Code Lens Sample](https://github.com/Microsoft/VSExtensibility/tree/main/New_Extensibility_Model/Samples/CodeLensSample/).
389+
390+
*Contributing Code Lenses to new documents types (or existing document types not supporting Code Lens) is not yet supported.*
391+
286392
## Related content
287393
288394
Learn about the editor interfaces and types at [Editor concepts](editor-concepts.md).

0 commit comments

Comments
 (0)