Skip to content

Commit

Permalink
Use QuickInput (#29096)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Aug 6, 2018
1 parent dcd40db commit 2c070ce
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/vs/editor/contrib/indentation/indentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { IEditorContribution, ICommand, ICursorStateComputerData, IEditOperation
import { IIdentifiedSingleEditOperation, ITextModel } from 'vs/editor/common/model';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { registerEditorAction, ServicesAccessor, IActionOptions, EditorAction, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
import { IModelService } from 'vs/editor/common/services/modelService';
import { Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
Expand All @@ -23,6 +22,7 @@ import { TextEdit, StandardTokenType } from 'vs/editor/common/modes';
import * as IndentUtil from './indentUtils';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { IndentConsts } from 'vs/editor/common/modes/supports/indentRules';
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';

export function shiftIndent(tabSize: number, indentation: string, count?: number): string {
count = count || 1;
Expand Down Expand Up @@ -217,7 +217,7 @@ export class ChangeIndentationSizeAction extends EditorAction {
}

public run(accessor: ServicesAccessor, editor: ICodeEditor): TPromise<void> {
const quickOpenService = accessor.get(IQuickOpenService);
const quickInputService = accessor.get(IQuickInputService);
const modelService = accessor.get(IModelService);

let model = editor.getModel();
Expand All @@ -237,7 +237,7 @@ export class ChangeIndentationSizeAction extends EditorAction {
const autoFocusIndex = Math.min(model.getOptions().tabSize - 1, 7);

return TPromise.timeout(50 /* quick open is sensitive to being opened so soon after another */).then(() =>
quickOpenService.pick(picks, { placeHolder: nls.localize({ key: 'selectTabWidth', comment: ['Tab corresponds to the tab key'] }, "Select Tab Size for Current File"), autoFocus: { autoFocusIndex } }).then(pick => {
quickInputService.pick(picks, { placeHolder: nls.localize({ key: 'selectTabWidth', comment: ['Tab corresponds to the tab key'] }, "Select Tab Size for Current File"), activeItem: picks[autoFocusIndex] }).then(pick => {
if (pick) {
model.updateOptions({
tabSize: parseInt(pick.label, 10),
Expand Down
7 changes: 6 additions & 1 deletion src/vs/platform/quickinput/common/quickInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface IPickOptions<T extends IQuickPickItem> {
*/
canPickMany?: boolean;

/**
* an optional property for the item to focus initially.
*/
activeItem?: TPromise<T> | T;

onDidFocus?: (entry: T) => void;
}

Expand Down Expand Up @@ -179,7 +184,7 @@ export interface IQuickInputService {
/**
* Opens the quick input box for selecting items and returns a promise with the user selected item(s) if any.
*/
pick<T extends IQuickPickItem, O extends IPickOptions<T>>(picks: TPromise<T[]>, options?: O, token?: CancellationToken): TPromise<O extends { canPickMany: true } ? T[] : T>;
pick<T extends IQuickPickItem, O extends IPickOptions<T>>(picks: TPromise<T[]> | T[], options?: O, token?: CancellationToken): TPromise<O extends { canPickMany: true } ? T[] : T>;

/**
* Opens the quick input box for text input and returns a promise with the user typed value if any.
Expand Down
22 changes: 13 additions & 9 deletions src/vs/workbench/browser/parts/quickinput/quickInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ export class QuickInputService extends Component implements IQuickInputService {
this.updateStyles();
}

pick<T extends IQuickPickItem, O extends IPickOptions<T>>(picks: TPromise<T[]>, options: O = <O>{}, token: CancellationToken = CancellationToken.None): TPromise<O extends { canPickMany: true } ? T[] : T> {
pick<T extends IQuickPickItem, O extends IPickOptions<T>>(picks: TPromise<T[]> | T[], options: O = <O>{}, token: CancellationToken = CancellationToken.None): TPromise<O extends { canPickMany: true } ? T[] : T> {
return new TPromise<O extends { canPickMany: true } ? T[] : T>((resolve, reject) => {
if (token.isCancellationRequested) {
resolve(undefined);
Expand Down Expand Up @@ -977,15 +977,19 @@ export class QuickInputService extends Component implements IQuickInputService {
input.matchOnDescription = options.matchOnDescription;
input.matchOnDetail = options.matchOnDetail;
input.busy = true;
picks.then(items => {
input.busy = false;
input.items = items;
if (input.canSelectMany) {
input.selectedItems = items.filter(item => item.picked);
}
});
TPromise.join([picks, options.activeItem])
.then(([items, activeItem]) => {
input.busy = false;
input.items = items;
if (input.canSelectMany) {
input.selectedItems = items.filter(item => item.picked);
}
if (activeItem) {
input.activeItems = [activeItem];
}
});
input.show();
picks.then(null, err => {
TPromise.wrap(picks).then(null, err => {
reject(err);
input.hide();
});
Expand Down

0 comments on commit 2c070ce

Please sign in to comment.