Skip to content

Commit

Permalink
Expand code action range to entire document range when source actions…
Browse files Browse the repository at this point in the history
… are requested without any active selection

Fixes #53525
  • Loading branch information
mjbvz committed Jan 23, 2019
1 parent cc0be8d commit 2d79ffe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/vs/editor/contrib/codeAction/codeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export function getCodeActions(
trigger: trigger.type === 'manual' ? CodeActionTriggerKind.Manual : CodeActionTriggerKind.Automatic
};

if (filter.kind && CodeActionKind.Source.contains(filter.kind) && rangeOrSelection.isEmpty()) {
rangeOrSelection = model.getFullModelRange();
}

const promises = CodeActionProviderRegistry.all(model)
// Avoid calling providers that we know will not return code actions of interest
.filter(provider => {
Expand Down
46 changes: 46 additions & 0 deletions src/vs/editor/contrib/codeAction/test/codeAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getCodeActions } from 'vs/editor/contrib/codeAction/codeAction';
import { CodeActionKind } from 'vs/editor/contrib/codeAction/codeActionTrigger';
import { IMarkerData, MarkerSeverity } from 'vs/platform/markers/common/markers';
import { CancellationToken } from 'vs/base/common/cancellation';
import { ITextModel } from 'vs/editor/common/model';

suite('CodeAction', () => {

Expand Down Expand Up @@ -216,4 +217,49 @@ suite('CodeAction', () => {
assert.strictEqual(actions.length, 0);
assert.strictEqual(wasInvoked, false);
});

test('getCodeActions requests for source actions should expand source actions range to entire document #53525', async function () {
const provider = new class implements CodeActionProvider {
provideCodeActions(model: ITextModel, range: Range): CodeAction[] {
return [{
title: rangeToString(range),
kind: CodeActionKind.Source.value,
}];
}
};

disposables.push(CodeActionProviderRegistry.register('fooLang', provider));

{
const actions = await getCodeActions(model, new Range(1, 1, 1, 1), {
type: 'manual',
filter: {
kind: CodeActionKind.Source,
includeSourceActions: true,
}
}, CancellationToken.None);
assert.strictEqual(actions.length, 1);
assert.strictEqual(actions[0].title, rangeToString(model.getFullModelRange()));
}

{
const range = new Range(1, 1, 1, 2);

// But we should not expand for non-empty selections
const actions = await getCodeActions(model, range, {
type: 'manual',
filter: {
kind: CodeActionKind.Source,
includeSourceActions: true,
}
}, CancellationToken.None);
assert.strictEqual(actions.length, 1);
assert.strictEqual(actions[0].title, rangeToString(range));
}
});
});

function rangeToString(range: Range): string {
return `${range.startLineNumber},${range.startColumn} ${range.endLineNumber},${range.endColumn} `;
}

0 comments on commit 2d79ffe

Please sign in to comment.