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

Easy pick codelenses with keyboard shortcut #91232

Merged
merged 4 commits into from
Mar 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/vs/editor/contrib/codelens/codelensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { onUnexpectedError, onUnexpectedExternalError } from 'vs/base/common/err
import { toDisposable, DisposableStore, dispose } from 'vs/base/common/lifecycle';
import { StableEditorScrollState } from 'vs/editor/browser/core/editorState';
import { ICodeEditor, MouseTargetType, IViewZoneChangeAccessor, IActiveCodeEditor } from 'vs/editor/browser/editorBrowser';
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { registerEditorContribution, EditorCommand, ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { IModelDecorationsChangeAccessor } from 'vs/editor/common/model';
import { CodeLensProviderRegistry, CodeLens } from 'vs/editor/common/modes';
Expand All @@ -20,6 +20,7 @@ import { ICodeLensCache } from 'vs/editor/contrib/codelens/codeLensCache';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import * as dom from 'vs/base/browser/dom';
import { hash } from 'vs/base/common/hash';
import { IQuickInputService, IQuickPickItem, IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput';
jrieken marked this conversation as resolved.
Show resolved Hide resolved

export class CodeLensContribution implements IEditorContribution {

Expand Down Expand Up @@ -402,6 +403,53 @@ export class CodeLensContribution implements IEditorContribution {
}
});
}

public getLenses(): CodeLensWidget[] {
return this._lenses;
}
Copy link
Member

Choose a reason for hiding this comment

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

Why expose the widgets and not the model itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need the line number of the widget on line 421:

const activeLensesWidgets = codelensController.getLenses().filter(lens => lens.getLineNumber() === lineNumber);

Copy link
Member

Choose a reason for hiding this comment

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

Ah, the live/updated line numbers... That makes good sense. Thanks for clarifying

}

export class ShowLensesInCurrentLineCommand extends EditorCommand {
public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void> {
const quickInputService = accessor.get(IQuickInputService);
const commandService = accessor.get(ICommandService);
const notificationService = accessor.get(INotificationService);

const lineNumber = editor.getSelection()?.positionLineNumber;
const codelensController = editor.getContribution(CodeLensContribution.ID) as CodeLensContribution;

const activeLensesWidgets = codelensController.getLenses().filter(lens => lens.getLineNumber() === lineNumber);

const commandArguments: Map<string, any[] | undefined> = new Map();

const items: (IQuickPickItem | IQuickPickSeparator)[] = [];

activeLensesWidgets.forEach(widget => {
widget.getItems().forEach(codelens => {
const command = codelens.symbol.command;
if (!command) {
return;
}
items.push({ id: command.id, label: command.title });

commandArguments.set(command.id, command.arguments);
});
});

// We dont want an empty picker
if (!items.length) {
return;
}

quickInputService.pick(items, { canPickMany: false }).then(item => {
const id = item.id!;
commandService.executeCommand(id, ...(commandArguments.get(id) || [])).catch(err => notificationService.error(err));
});
}

}

registerEditorContribution(CodeLensContribution.ID, CodeLensContribution);

const showLensesInCurrentLineCommand = new ShowLensesInCurrentLineCommand({ id: 'codelens.showLensesInCurrentLine', precondition: undefined });
showLensesInCurrentLineCommand.register();
4 changes: 4 additions & 0 deletions src/vs/editor/contrib/codelens/codelensWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ export class CodeLensWidget {
}
}
}

getItems(): CodeLensItem[] {
return this._data;
}
}

registerThemingParticipant((theme, collector) => {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/platform/quickinput/common/quickInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'
import { CancellationToken } from 'vs/base/common/cancellation';
import { IQuickPickItem, IPickOptions, IInputOptions, IQuickNavigateConfiguration, IQuickPick, IQuickInputButton, IInputBox, QuickPickInput } from 'vs/base/parts/quickinput/common/quickInput';

export { IQuickPickItem, IPickOptions, IInputOptions, IQuickNavigateConfiguration, IQuickPick, IQuickInput, IQuickInputButton, IInputBox, IQuickPickItemButtonEvent, QuickPickInput, IQuickPickSeparator, IKeyMods } from 'vs/base/parts/quickinput/common/quickInput';
export * from 'vs/base/parts/quickinput/common/quickInput';

export const IQuickInputService = createDecorator<IQuickInputService>('quickInputService');

Expand Down