-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable support for an LSP client to open source generated files
This allows source-generated documents to be opened by an LSP client. A custom URI format is defined which uniquely defines a source generated document by serializing the DocumentId; this URI is returned any time we need to navigate to a document or reference the document in any way. A custom request is also defined to fetch the text of a document which can be used for the client; our VS Code extension will implement a TextDocumentContentProvider to call that.
- Loading branch information
1 parent
f8cada0
commit 09447c1
Showing
19 changed files
with
183 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Features/LanguageServer/Protocol/Handler/SourceGenerators/GetTextHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Roslyn.Utilities; | ||
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.Handler | ||
{ | ||
[ExportCSharpVisualBasicStatelessLspService(typeof(GetTextHandler)), Shared] | ||
[Method("sourceGenerator/getText")] | ||
internal class GetTextHandler : ILspServiceDocumentRequestHandler<SourceGeneratorGetTextParams, SourceGeneratedDocumentText> | ||
{ | ||
[ImportingConstructor] | ||
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
public GetTextHandler() | ||
{ | ||
} | ||
|
||
public bool MutatesSolutionState => false; | ||
public bool RequiresLSPSolution => true; | ||
|
||
public LSP.TextDocumentIdentifier GetTextDocumentIdentifier(SourceGeneratorGetTextParams request) => request.TextDocument; | ||
|
||
public async Task<SourceGeneratedDocumentText> HandleRequestAsync(SourceGeneratorGetTextParams request, RequestContext context, CancellationToken cancellationToken) | ||
{ | ||
var document = context.Document; | ||
|
||
// Although nothing here strictly prevents this from working on any other document, we'll assert this since something | ||
// has gone wrong otherwise. | ||
Contract.ThrowIfFalse(document is SourceGeneratedDocument); | ||
return new SourceGeneratedDocumentText { Text = (await document.GetTextAsync(cancellationToken).ConfigureAwait(false)).ToString() }; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Features/LanguageServer/Protocol/Handler/SourceGenerators/SourceGeneratedDocumentText.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.Serialization; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.Handler | ||
{ | ||
[DataContract] | ||
internal class SourceGeneratedDocumentText | ||
{ | ||
[DataMember(Name = "text")] | ||
public required string Text { get; set; } | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...Features/LanguageServer/Protocol/Handler/SourceGenerators/SourceGeneratorGetTextParams.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.Handler | ||
{ | ||
[DataContract] | ||
internal class SourceGeneratorGetTextParams : ITextDocumentParams | ||
{ | ||
[DataMember(Name = "textDocument")] | ||
public required TextDocumentIdentifier TextDocument { get; init; } | ||
} | ||
} |
Oops, something went wrong.