Skip to content
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

Support auto doc comment generation #4261

Merged
merged 5 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/omnisharp/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { expect, should } from 'chai';
import * as vscode from 'vscode';
import * as path from 'path';
import { isRazorWorkspace } from './integrationHelpers';
import testAssetWorkspace from './testAssets/testAssetWorkspace';

const onTypeFormatProviderCommand = 'vscode.executeFormatOnTypeProvider';

function normalizeNewlines(original: string): string {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node.js does not have replaceAll, unfortunately...

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 = <vscode.TextEdit[]>(await vscode.commands.executeCommand(onTypeFormatProviderCommand, fileUri, commentPosition, '/'));
expect(formatEdits).ofSize(1);
expect(normalizeNewlines(formatEdits[0].newText)).eq(" <summary>\n /// \n /// </summary>\n /// <param name=\"param1\"></param>\n /// <param name=\"param2\"></param>\n /// <returns></returns>");
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 = <vscode.TextEdit[]>(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);
});
});
13 changes: 13 additions & 0 deletions test/integrationTests/testAssets/singleCsproj/DocComments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class DocComments
{
///
string M(int param1, string param2)
{
return null;
}

/// <summary>

/// </summary>
void M2() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class DocComments
{
///
string M(int param1, string param2)
{
return null;
}

/// <summary>

/// </summary>
void M2() {}
}