Skip to content

Commit

Permalink
Easy pick codelenses with keyboard shortcut
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Sampaio <gbritosampaio@gmail.com>
  • Loading branch information
GustavoKatel committed Feb 23, 2020
1 parent 127c344 commit e9ddb0f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 10 deletions.
67 changes: 67 additions & 0 deletions src/vs/editor/contrib/codelens/codelensCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Command } from 'vs/editor/browser/editorExtensions';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { CodeLensContribution } from 'vs/editor/contrib/codelens/codelensController';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IQuickInputService, IQuickPickItem, IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { INotificationService } from 'vs/platform/notification/common/notification';

export class ShowLensesInCurrentLineCommand extends Command {
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();
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);
});
});


console.log(items);
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();
}

}
28 changes: 18 additions & 10 deletions src/vs/editor/contrib/codelens/codelensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
* 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 { IActiveCodeEditor, ICodeEditor, IViewZoneChangeAccessor, MouseTargetType } from 'vs/editor/browser/editorBrowser';
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
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 { ShowLensesInCurrentLineCommand } from 'vs/editor/contrib/codelens/codelensCommands';
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';

export class CodeLensContribution implements IEditorContribution {

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

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

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

0 comments on commit e9ddb0f

Please sign in to comment.