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 2 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
85 changes: 74 additions & 11 deletions src/vs/editor/contrib/codelens/codelensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { CancelablePromise, RunOnceScheduler, createCancelablePromise, disposableTimeout } from 'vs/base/common/async';

import * as dom from 'vs/base/browser/dom';
import { CancelablePromise, createCancelablePromise, disposableTimeout, RunOnceScheduler } from 'vs/base/common/async';
import { onUnexpectedError, onUnexpectedExternalError } from 'vs/base/common/errors';
import { toDisposable, DisposableStore, dispose } from 'vs/base/common/lifecycle';
import { hash } from 'vs/base/common/hash';
import { DisposableStore, dispose, toDisposable } 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 { IActiveCodeEditor, ICodeEditor, IViewZoneChangeAccessor, MouseTargetType } from 'vs/editor/browser/editorBrowser';
import { Command, registerEditorContribution, ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { IModelDecorationsChangeAccessor } from 'vs/editor/common/model';
import { CodeLensProviderRegistry, CodeLens } from 'vs/editor/common/modes';
import { CodeLensModel, getCodeLensData, CodeLensItem } from 'vs/editor/contrib/codelens/codelens';
import { CodeLensWidget, CodeLensHelper } from 'vs/editor/contrib/codelens/codelensWidget';
import { CodeLens, CodeLensProviderRegistry } from 'vs/editor/common/modes';
import { CodeLensItem, CodeLensModel, getCodeLensData } from 'vs/editor/contrib/codelens/codelens';
import { ICodeLensCache } from 'vs/editor/contrib/codelens/codeLensCache';
import { CodeLensHelper, CodeLensWidget } from 'vs/editor/contrib/codelens/codelensWidget';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { INotificationService } from 'vs/platform/notification/common/notification';
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 +405,66 @@ 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 Command {
Copy link
Member

Choose a reason for hiding this comment

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

Use EditorCommand instead of the generic command. It will already have the correct editor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! Changed to use it. Thanks @jrieken

public runCommand(accessor: ServicesAccessor, args: any): void | Promise<void> {
const quickInputService = accessor.get(IQuickInputService);
const commandService = accessor.get(ICommandService);
const notificationService = accessor.get(INotificationService);

const focusedEditor = accessor.get(ICodeEditorService).getFocusedCodeEditor();
if (!focusedEditor?.getSelection()?.isEmpty()) {
return;
}
const lineNumber = focusedEditor.getSelection()?.positionLineNumber;
const codelensController = focusedEditor.getContribution(CodeLensContribution.ID) as CodeLensContribution;

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

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

const picker = quickInputService.createQuickPick();
Copy link
Member

Choose a reason for hiding this comment

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

You can use IQuickInputService#pick instead of createQuickPick. For simple cases like this you don't to write that much code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! Just updated it! Thanks @jrieken

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);
});
});

picker.items = items;
picker.canSelectMany = false;
picker.onDidAccept(_ => {
const selectedItems = picker.selectedItems;
if (selectedItems.length === 1) {
const id = selectedItems[0].id!;

if (!id) {
picker.hide();
return;
}

commandService.executeCommand(id, ...(commandArguments.get(id) || [])).catch(err => notificationService.error(err));
}
picker.hide();
});
picker.show();
}

}

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