-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation comment creation to the FormatAfterKeystrokeService #2023
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a109f92
Add documentation comment creation to the FormatAfterKeystrokeService
333fred 2d36cdb
Merge branch 'master' into auto-doc-comment
filipw 471addd
Merge branch 'master' into auto-doc-comment
JoeRobich 26e2308
Merge branch 'master' into auto-doc-comment
JoeRobich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/OmniSharp.Roslyn.CSharp/DocumentationComments/DocumentationCommentSnippetService.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,110 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Reflection; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Completion; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Options; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace OmniSharp.Roslyn.DocumentationComments | ||
{ | ||
/// <summary> | ||
/// Proxy service for Microsoft.CodeAnalysis.DocumentationComments.IDocumentationCommentSnippetService. | ||
/// Implementation was based on the service as of this commit: 2834b74995bb66a7cb19cb09069c17812819afdc | ||
/// See: https://github.com/dotnet/roslyn/blob/2834b74995bb66a7cb19cb09069c17812819afdc/src/Features/Core/Portable/DocumentationComments/IDocumentationCommentSnippetService.cs | ||
/// </summary> | ||
public struct DocumentationCommentSnippetService | ||
{ | ||
/// <summary> | ||
/// IDocumentationCommentService HostLanguageServices.GetRequiredService<IDocumentationCommentService>() | ||
/// </summary> | ||
private static MethodInfo s_getRequiredService; | ||
/// <summary> | ||
/// DocumentationCommentSnippet IDocumentationCommentService.GetDocumentationCommentSnippetOnCharacterTyped(SyntaxTree, SourceText, int, DocumentOptionSet, CancellationToken) | ||
/// </summary> | ||
private static MethodInfo s_getDocumentationCommentSnippetOnCharacterTyped; | ||
/// <summary> | ||
/// DocumentationCommentSnippet IDocumentationCommentService.GetDocumentationCommentSnippetOnEnterTyped(SyntaxTree, SourceText, int, DocumentOptionSet, CancellationToken) | ||
/// </summary> | ||
private static MethodInfo s_getDocumentationCommentSnippetOnEnterTyped; | ||
/// <summary> | ||
/// TextSpan DocumentationCommentSnippet.SpanToReplace | ||
/// </summary> | ||
private static PropertyInfo s_spanToReplace; | ||
/// <summary> | ||
/// string DocumentationCommentSnippet.SnippetText | ||
/// </summary> | ||
private static PropertyInfo s_snippetText; | ||
|
||
static DocumentationCommentSnippetService() | ||
{ | ||
var iDocumentationCommentSnippetServiceType = typeof(CompletionItem).Assembly.GetType("Microsoft.CodeAnalysis.DocumentationComments.IDocumentationCommentSnippetService"); | ||
s_getDocumentationCommentSnippetOnCharacterTyped = iDocumentationCommentSnippetServiceType.GetMethod(nameof(GetDocumentationCommentSnippetOnCharacterTyped)); | ||
s_getDocumentationCommentSnippetOnEnterTyped = iDocumentationCommentSnippetServiceType.GetMethod(nameof(GetDocumentationCommentSnippetOnEnterTyped)); | ||
|
||
var documentationCommentSnippet = typeof(CompletionItem).Assembly.GetType("Microsoft.CodeAnalysis.DocumentationComments.DocumentationCommentSnippet"); | ||
s_spanToReplace = documentationCommentSnippet.GetProperty(nameof(DocumentationCommentSnippet.SpanToReplace)); | ||
s_snippetText = documentationCommentSnippet.GetProperty(nameof(DocumentationCommentSnippet.SnippetText)); | ||
|
||
s_getRequiredService = typeof(HostLanguageServices).GetMethod(nameof(HostLanguageServices.GetRequiredService)).MakeGenericMethod(iDocumentationCommentSnippetServiceType); | ||
} | ||
|
||
public static DocumentationCommentSnippetService GetDocumentationCommentSnippetService(Document document) | ||
{ | ||
var service = s_getRequiredService.Invoke(document.Project.LanguageServices, Array.Empty<object>()); | ||
return new DocumentationCommentSnippetService(service); | ||
} | ||
|
||
private object _underlying; | ||
|
||
private DocumentationCommentSnippetService(object underlying) | ||
{ | ||
_underlying = underlying; | ||
} | ||
|
||
public DocumentationCommentSnippet? GetDocumentationCommentSnippetOnCharacterTyped(SyntaxTree syntaxTree, SourceText text, int position, DocumentOptionSet options, CancellationToken cancellationToken) | ||
{ | ||
var originalSnippet = s_getDocumentationCommentSnippetOnCharacterTyped.Invoke(_underlying, new object[] { syntaxTree, text, position, options, cancellationToken }); | ||
return ConvertSnippet(originalSnippet); | ||
} | ||
|
||
public DocumentationCommentSnippet? GetDocumentationCommentSnippetOnEnterTyped(SyntaxTree syntaxTree, SourceText text, int position, DocumentOptionSet options, CancellationToken cancellationToken) | ||
{ | ||
var originalSnippet = s_getDocumentationCommentSnippetOnEnterTyped.Invoke(_underlying, new object[] { syntaxTree, text, position, options, cancellationToken }); | ||
return ConvertSnippet(originalSnippet); | ||
} | ||
|
||
private static DocumentationCommentSnippet? ConvertSnippet(object? originalSnippet) | ||
{ | ||
if (originalSnippet == null) | ||
{ | ||
return null; | ||
} | ||
else | ||
{ | ||
return new DocumentationCommentSnippet((TextSpan)s_spanToReplace.GetValue(originalSnippet), (string)s_snippetText.GetValue(originalSnippet)); | ||
} | ||
} | ||
} | ||
|
||
public struct DocumentationCommentSnippet | ||
{ | ||
public TextSpan SpanToReplace { get; } | ||
public string SnippetText { get; } | ||
|
||
public DocumentationCommentSnippet(TextSpan spanToReplace, string snippetText) | ||
{ | ||
SpanToReplace = spanToReplace; | ||
SnippetText = snippetText; | ||
} | ||
} | ||
|
||
public static class WorkspaceExtensions | ||
{ | ||
public static DocumentationCommentSnippetService GetDocumentationCommentSnippetService(this Document document) | ||
=> DocumentationCommentSnippetService.GetDocumentationCommentSnippetService(document); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw that there is also
GetDocumentationCommentSnippetOnCommandInvoke
, I think it is used in VS when you invoke "insert comment" command from insert snippet feature. we may also want to expose it in the future