Skip to content

Commit af43308

Browse files
authored
Update to fix InvokeAsync usage
InvokeAsync extension method internally handles the event handler being null, per recommendation patterns like "await StartAsync?.InvokeAsync" should be awaited since it will cause NullReferenceException in case StartAsync is null. Removing "?" and letting InvokeAsync handle it does the expected action which is a no-op
1 parent 401be39 commit af43308

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

docs/extensibility/adding-an-lsp-extension.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ The LSP does not include specification on how to provide text colorization for l
126126

127127
4. Create a *.pkgdef* file and add a line similar to this:
128128

129-
```xml
130-
[$RootKey$\TextMate\Repositories]
131-
"MyLang"="$PackageFolder$\Grammars"
132-
```
129+
```xml
130+
[$RootKey$\TextMate\Repositories]
131+
"MyLang"="$PackageFolder$\Grammars"
132+
```
133133

134134
5. Right-click on the files and select **Properties**. Change the **Build** action to **Content** and the **Include in VSIX** property to true.
135135

@@ -193,7 +193,7 @@ namespace MockLanguageExtension
193193

194194
public async Task OnLoadedAsync()
195195
{
196-
await StartAsync?.InvokeAsync(this, EventArgs.Empty);
196+
await StartAsync.InvokeAsync(this, EventArgs.Empty);
197197
}
198198

199199
public async Task OnServerInitializeFailedAsync(Exception e)
@@ -207,7 +207,7 @@ namespace MockLanguageExtension
207207
}
208208
}
209209
}
210-
```
210+
```
211211

212212
The main methods that need to be implemented are [OnLoadedAsync](/dotnet/api/microsoft.visualstudio.languageserver.client.ilanguageclient.onloadedasync?view=visualstudiosdk-2017) and [ActivateAsync](/dotnet/api/microsoft.visualstudio.languageserver.client.ilanguageclient.activateasync?view=visualstudiosdk-2017). [OnLoadedAsync](/dotnet/api/microsoft.visualstudio.languageserver.client.ilanguageclient.onloadedasync?view=visualstudiosdk-2017) is called when Visual Studio has loaded your extension and your language server is ready to be started. In this method, you can invoke the [StartAsync](/dotnet/api/microsoft.visualstudio.languageserver.client.ilanguageclient.startasync?view=visualstudiosdk-2017) delegate immediately to signal that the language server should be started, or you can do additional logic and invoke [StartAsync](/dotnet/api/microsoft.visualstudio.languageserver.client.ilanguageclient.startasync?view=visualstudiosdk-2017) later. **To activate your language server, you must call StartAsync at some point.**
213213

@@ -220,7 +220,7 @@ Once your language client class is implemented, you'll need to define two attrib
220220
```csharp
221221
[Export(typeof(ILanguageClient))]
222222
[ContentType("bar")]
223-
```
223+
```
224224

225225
### MEF
226226

@@ -263,7 +263,7 @@ namespace MockLanguageExtension
263263
internal static FileExtensionToContentTypeDefinition BarFileExtensionDefinition;
264264
}
265265
}
266-
```
266+
```
267267

268268
In the previous example, a content type definition is created for files that end in *.bar* file extension. The content type definition is given the name "bar" and **must** derive from [CodeRemoteContentTypeName](/dotnet/api/microsoft.visualstudio.languageserver.client.coderemotecontentdefinition.coderemotecontenttypename?view=visualstudiosdk-2017).
269269

@@ -275,7 +275,7 @@ After adding a content type definition, you can then define when to load your la
275275
public class BarLanguageClient : ILanguageClient
276276
{
277277
}
278-
```
278+
```
279279

280280
Adding support for LSP language servers does not require you to implement your own project system in Visual Studio. Customers can open a single file or a folder in Visual Studio to start using your language service. In fact, support for LSP language servers is designed to work only in open folder/file scenarios. If a custom project system is implemented, some features (such as settings) will not work.
281281

@@ -289,63 +289,63 @@ Follow these steps below to add support for settings to your LSP language servic
289289

290290
1. Add a JSON file (for example, *MockLanguageExtensionSettings.json*) in your project that contains the settings and their default values. For example:
291291

292-
```json
293-
{
292+
```json
293+
{
294294
"foo.maxNumberOfProblems": -1
295-
}
296-
```
295+
}
296+
```
297297
2. Right-click on the JSON file and select **Properties**. Change the **Build** action to "Content" and the "Include in VSIX' property to true.
298298

299299
3. Implement ConfigurationSections and return the list of prefixes for the settings defined in the JSON file (In Visual Studio Code, this would map to the configuration section name in package.json):
300300

301-
```csharp
302-
public IEnumerable<string> ConfigurationSections
303-
{
301+
```csharp
302+
public IEnumerable<string> ConfigurationSections
303+
{
304304
get
305305
{
306306
yield return "foo";
307307
}
308-
}
309-
```
308+
}
309+
```
310310
4. Add a .pkgdef file to the project (add new text file and change the file extension to .pkgdef). The pkgdef file should contain this info:
311311

312-
```xml
312+
```xml
313313
[$RootKey$\OpenFolder\Settings\VSWorkspaceSettings\[settings-name]]
314314
@="$PackageFolder$\[settings-file-name].json"
315-
```
315+
```
316316

317317
5. Right click on the .pkgdef file and select **Properties**. Change the **Build** action to **Content** and the **Include in VSIX** property to true.
318318

319319
6. Open up the *source.extension.vsixmanifest* file and add an asset in the **Asset** tab:
320320

321-
![edit vspackage asset](media/lsp-add-vspackage-asset.png)
321+
![edit vspackage asset](media/lsp-add-vspackage-asset.png)
322322

323-
* **Type**: Microsoft.VisualStudio.VsPackage
324-
* **Source**: File on filesystem
325-
* **Path**: [Path to your *.pkgdef* file]
323+
* **Type**: Microsoft.VisualStudio.VsPackage
324+
* **Source**: File on filesystem
325+
* **Path**: [Path to your *.pkgdef* file]
326326

327327
### User editing of settings for a workspace
328328

329329
1. User opens a workspace containing files your server owns.
330330
2. User adds a file in the *.vs* folder called *VSWorkspaceSettings.json*.
331331
3. User adds a line to the *VSWorkspaceSettings.json* file for a setting the server provides. For example:
332332

333-
```json
334-
{
333+
```json
334+
{
335335
"foo.maxNumberOfProblems": 10
336-
}
337-
```
338-
### Enabling diagnostics tracing
339-
Diagnostics tracing can be enabled to output all messages between the client and server, which can be useful when debugging issues. To enable diagnostic tracing, do the following:
336+
}
337+
```
338+
### Enabling diagnostics tracing
339+
Diagnostics tracing can be enabled to output all messages between the client and server, which can be useful when debugging issues. To enable diagnostic tracing, do the following:
340340

341-
4. Open or create the workspace settings file *VSWorkspaceSettings.json* (see "User editing of settings for a workspace").
342-
5. Add the following line in the settings json file:
341+
4. Open or create the workspace settings file *VSWorkspaceSettings.json* (see "User editing of settings for a workspace").
342+
5. Add the following line in the settings json file:
343343

344344
```json
345345
{
346346
"foo.trace.server": "Off"
347347
}
348-
```
348+
```
349349

350350
There are three possible values for trace verbosity:
351351
* "Off": tracing turned off completely
@@ -391,7 +391,7 @@ internal class MockCustomLanguageClient : MockLanguageClient, ILanguageClientCus
391391
}
392392
}
393393
}
394-
```
394+
```
395395

396396
#### Sending custom messages
397397

@@ -424,7 +424,7 @@ internal class MockCustomLanguageClient : MockLanguageClient, ILanguageClientCus
424424
return await this.customMessageRpc.InvokeAsync<string>("OnCustomRequest", test);
425425
}
426426
}
427-
```
427+
```
428428

429429
### Middle layer
430430

@@ -455,7 +455,7 @@ public class MockLanguageClient: ILanguageClient, ILanguageClientCustomMessage
455455
}
456456
}
457457
}
458-
```
458+
```
459459

460460
The middle layer feature is still under development and not yet comprehensive.
461461

0 commit comments

Comments
 (0)