From 159de9839a2dbdafc79ec5cee7bfb8578a303ff5 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Thu, 3 Dec 2020 21:12:35 -0800 Subject: [PATCH] Support auto doc comment generation Adds '\n' and '/' to the on-type format list, enabling vscode support for the newly-added omnisharp feature of auto-generating documentation comments. Closes https://github.com/OmniSharp/omnisharp-vscode/issues/8. --- src/omnisharp/extension.ts | 2 +- ...nCommentAutoFormatting.integration.test.ts | 61 +++++++++++++++++++ .../testAssets/singleCsproj/DocComments.cs | 13 ++++ .../slnWithCsproj/src/app/DocComments.cs | 13 ++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/integrationTests/documentationCommentAutoFormatting.integration.test.ts create mode 100644 test/integrationTests/testAssets/singleCsproj/DocComments.cs create mode 100644 test/integrationTests/testAssets/slnWithCsproj/src/app/DocComments.cs diff --git a/src/omnisharp/extension.ts b/src/omnisharp/extension.ts index 2b2731897..f6cd2d450 100644 --- a/src/omnisharp/extension.ts +++ b/src/omnisharp/extension.ts @@ -88,7 +88,7 @@ export async function activate(context: vscode.ExtensionContext, packageJSON: an localDisposables.add(vscode.languages.registerRenameProvider(documentSelector, new RenameProvider(server, languageMiddlewareFeature))); if (options.useFormatting) { localDisposables.add(vscode.languages.registerDocumentRangeFormattingEditProvider(documentSelector, new FormatProvider(server, languageMiddlewareFeature))); - localDisposables.add(vscode.languages.registerOnTypeFormattingEditProvider(documentSelector, new FormatProvider(server, languageMiddlewareFeature), '}', ';')); + localDisposables.add(vscode.languages.registerOnTypeFormattingEditProvider(documentSelector, new FormatProvider(server, languageMiddlewareFeature), '}', '/', '\n', ';')); } localDisposables.add(vscode.languages.registerCompletionItemProvider(documentSelector, new CompletionProvider(server, languageMiddlewareFeature), '.', ' ')); localDisposables.add(vscode.languages.registerWorkspaceSymbolProvider(new WorkspaceSymbolProvider(server, optionProvider, languageMiddlewareFeature))); diff --git a/test/integrationTests/documentationCommentAutoFormatting.integration.test.ts b/test/integrationTests/documentationCommentAutoFormatting.integration.test.ts new file mode 100644 index 000000000..a79166f97 --- /dev/null +++ b/test/integrationTests/documentationCommentAutoFormatting.integration.test.ts @@ -0,0 +1,61 @@ +import { expect, should } from 'chai'; +import * as vscode from 'vscode'; +import * as path from 'path'; +import { isRazorWorkspace } from './integrationHelpers'; +import testAssetWorkspace from './testAssets/testAssetWorkspace'; +import { skip } from 'rxjs/operators'; + +const onTypeFormatProviderCommand = 'vscode.executeFormatOnTypeProvider'; + +function normalizeNewlines(original: string): string { + while (original.indexOf('\r\n') != -1) { + original = original.replace('\r\n', '\n'); + } + + return original; +} + +suite(`Documentation Comment Auto Formatting: ${testAssetWorkspace.description}`, function () { + let fileUri: vscode.Uri; + + suiteSetup(async function () { + should(); + + if (isRazorWorkspace(vscode.workspace)) { + // The format-on-type provider does not run for razor files. + this.skip(); + } + + const projectDirectory = testAssetWorkspace.projects[0].projectDirectoryPath; + const filePath = path.join(projectDirectory, 'DocComments.cs'); + fileUri = vscode.Uri.file(filePath); + + await vscode.commands.executeCommand('vscode.open', fileUri); + }); + + suiteTeardown(async () => { + await testAssetWorkspace.cleanupWorkspace(); + }); + + test('Triple slash inserts doc comment snippet', async () => { + const commentPosition = new vscode.Position(2, 7); + const formatEdits = (await vscode.commands.executeCommand(onTypeFormatProviderCommand, fileUri, commentPosition, '/')); + expect(formatEdits).ofSize(1); + expect(normalizeNewlines(formatEdits[0].newText)).eq(" \n /// \n /// \n /// \n /// \n /// "); + expect(formatEdits[0].range.start.line).eq(commentPosition.line); + expect(formatEdits[0].range.start.character).eq(commentPosition.character); + expect(formatEdits[0].range.end.line).eq(commentPosition.line); + expect(formatEdits[0].range.end.character).eq(commentPosition.character); + }); + + test('Enter in comment inserts triple-slashes preceding', async () => { + const commentPosition = new vscode.Position(9, 0); + const formatEdits = (await vscode.commands.executeCommand(onTypeFormatProviderCommand, fileUri, commentPosition, '\n')); + expect(formatEdits).ofSize(1); + expect(formatEdits[0].newText).eq(" /// "); + expect(formatEdits[0].range.start.line).eq(commentPosition.line); + expect(formatEdits[0].range.start.character).eq(commentPosition.character); + expect(formatEdits[0].range.end.line).eq(commentPosition.line); + expect(formatEdits[0].range.end.character).eq(commentPosition.character); + }); +}); diff --git a/test/integrationTests/testAssets/singleCsproj/DocComments.cs b/test/integrationTests/testAssets/singleCsproj/DocComments.cs new file mode 100644 index 000000000..fc321b3c4 --- /dev/null +++ b/test/integrationTests/testAssets/singleCsproj/DocComments.cs @@ -0,0 +1,13 @@ +class DocComments +{ + /// + string M(int param1, string param2) + { + return null; + } + + /// + + /// + void M2() {} +} diff --git a/test/integrationTests/testAssets/slnWithCsproj/src/app/DocComments.cs b/test/integrationTests/testAssets/slnWithCsproj/src/app/DocComments.cs new file mode 100644 index 000000000..fc321b3c4 --- /dev/null +++ b/test/integrationTests/testAssets/slnWithCsproj/src/app/DocComments.cs @@ -0,0 +1,13 @@ +class DocComments +{ + /// + string M(int param1, string param2) + { + return null; + } + + /// + + /// + void M2() {} +}