quickopen.ts 3.7 KB
Newer Older
S
Sandeep Somavarapu 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

J
Joao Moreno 已提交
6
import { API } from '../../api';
J
green!  
Joao Moreno 已提交
7 8
import { Editors } from '../editor/editors';
import { Commands } from '../workbench/workbench';
S
Sandeep Somavarapu 已提交
9 10 11

export class QuickOpen {

12 13
	static QUICK_OPEN_HIDDEN = 'div.monaco-quick-open-widget[aria-hidden="true"]';
	static QUICK_OPEN = 'div.monaco-quick-open-widget[aria-hidden="false"]';
J
Joao Moreno 已提交
14
	static QUICK_OPEN_INPUT = `${QuickOpen.QUICK_OPEN} .quick-open-input input`;
15
	static QUICK_OPEN_FOCUSED_ELEMENT = `${QuickOpen.QUICK_OPEN} .quick-open-tree .monaco-tree-row.focused .monaco-highlighted-label`;
S
Sandeep Somavarapu 已提交
16
	static QUICK_OPEN_ENTRY_SELECTOR = 'div[aria-label="Quick Picker"] .monaco-tree-rows.show-twisties .monaco-tree-row .quick-open-entry';
J
green!  
Joao Moreno 已提交
17
	static QUICK_OPEN_ENTRY_LABEL_SELECTOR = 'div[aria-label="Quick Picker"] .monaco-tree-rows.show-twisties .monaco-tree-row .quick-open-entry .label-name';
S
Sandeep Somavarapu 已提交
18

J
green!  
Joao Moreno 已提交
19
	constructor(private api: API, private commands: Commands, private editors: Editors) { }
S
Sandeep Somavarapu 已提交
20

21
	async openQuickOpen(value: string): Promise<void> {
J
green!  
Joao Moreno 已提交
22
		await this.commands.runCommand('workbench.action.quickOpen');
S
Sandeep Somavarapu 已提交
23
		await this.waitForQuickOpenOpened();
S
Sandeep Somavarapu 已提交
24

25
		if (value) {
J
green!  
Joao Moreno 已提交
26
			await this.api.setValue(QuickOpen.QUICK_OPEN_INPUT, value);
27
		}
28 29
	}

J
Joao Moreno 已提交
30
	async closeQuickOpen(): Promise<void> {
J
green!  
Joao Moreno 已提交
31
		await this.commands.runCommand('workbench.action.closeQuickOpen');
S
Sandeep Somavarapu 已提交
32
		await this.waitForQuickOpenClosed();
S
Sandeep Somavarapu 已提交
33 34
	}

J
Joao Moreno 已提交
35
	async openFile(fileName: string): Promise<void> {
36
		await this.openQuickOpen(fileName);
37

38
		await this.waitForQuickOpenElements(names => names.some(n => n === fileName));
J
Joao Moreno 已提交
39
		await this.api.dispatchKeybinding('enter');
J
green!  
Joao Moreno 已提交
40 41
		await this.editors.waitForActiveTab(fileName);
		await this.editors.waitForEditorFocus(fileName);
42 43
	}

J
Joao Moreno 已提交
44
	async waitForQuickOpenOpened(): Promise<void> {
J
green!  
Joao Moreno 已提交
45
		await this.api.waitForActiveElement(QuickOpen.QUICK_OPEN_INPUT);
S
Sandeep Somavarapu 已提交
46
	}
S
Sandeep Somavarapu 已提交
47

J
Joao Moreno 已提交
48
	private async waitForQuickOpenClosed(): Promise<void> {
J
green!  
Joao Moreno 已提交
49
		await this.api.waitForElement(QuickOpen.QUICK_OPEN_HIDDEN);
S
Sandeep Somavarapu 已提交
50 51
	}

J
Joao Moreno 已提交
52
	async submit(text: string): Promise<void> {
J
green!  
Joao Moreno 已提交
53
		await this.api.setValue(QuickOpen.QUICK_OPEN_INPUT, text);
J
Joao Moreno 已提交
54
		await this.api.dispatchKeybinding('enter');
S
Sandeep Somavarapu 已提交
55 56 57
		await this.waitForQuickOpenClosed();
	}

J
Joao Moreno 已提交
58
	async selectQuickOpenElement(index: number): Promise<void> {
S
Sandeep Somavarapu 已提交
59 60
		await this.waitForQuickOpenOpened();
		for (let from = 0; from < index; from++) {
J
Joao Moreno 已提交
61
			await this.api.dispatchKeybinding('down');
S
Sandeep Somavarapu 已提交
62
		}
J
Joao Moreno 已提交
63
		await this.api.dispatchKeybinding('enter');
S
Sandeep Somavarapu 已提交
64 65
		await this.waitForQuickOpenClosed();
	}
66 67

	async waitForQuickOpenElements(accept: (names: string[]) => boolean): Promise<void> {
J
green!  
Joao Moreno 已提交
68 69 70 71 72 73 74 75 76 77 78
		await this.api.waitForElements(QuickOpen.QUICK_OPEN_ENTRY_LABEL_SELECTOR, els => accept(els.map(e => e.textContent)));
	}

	async runCommand(command: string): Promise<void> {
		await this.openQuickOpen(`> ${command}`);

		// wait for best choice to be focused
		await this.api.waitForTextContent(QuickOpen.QUICK_OPEN_FOCUSED_ELEMENT, command);

		// wait and click on best choice
		await this.api.waitAndClick(QuickOpen.QUICK_OPEN_FOCUSED_ELEMENT);
79 80
	}

J
green!  
Joao Moreno 已提交
81 82
	async openQuickOutline(): Promise<void> {
		let retries = 0;
83

J
green!  
Joao Moreno 已提交
84 85 86
		while (++retries < 10) {
			await this.commands.runCommand('workbench.action.gotoSymbol');

J
Joao Moreno 已提交
87
			const text = await this.api.waitForTextContent('div[aria-label="Quick Picker"] .monaco-tree-rows.show-twisties div.monaco-tree-row .quick-open-entry .monaco-icon-label .label-name .monaco-highlighted-label span');
J
green!  
Joao Moreno 已提交
88 89 90 91 92 93 94 95

			if (text !== 'No symbol information for the file') {
				return;
			}

			await this.closeQuickOpen();
			await new Promise(c => setTimeout(c, 250));
		}
96
	}
S
Sandeep Somavarapu 已提交
97
}