Skip to content

Commit

Permalink
Support auto doc comment generation
Browse files Browse the repository at this point in the history
Adds '\n' and '/' to the on-type format list, enabling vscode support for the newly-added omnisharp feature of auto-generating documentation comments. Closes dotnet#8.
  • Loading branch information
333fred committed Dec 4, 2020
1 parent 2489158 commit 159de98
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
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,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 = <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() {}
}

0 comments on commit 159de98

Please sign in to comment.