-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathquick_open-helpers.ts
More file actions
126 lines (106 loc) · 5.35 KB
/
quick_open-helpers.ts
File metadata and controls
126 lines (106 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {assert} from 'chai';
import type {DevToolsPage} from '../shared/frontend-helper.js';
import {SourceFileEvents, waitForSourceFiles} from './sources-helpers.js';
export const QUICK_OPEN_SELECTOR = '[aria-label="Quick open"],[aria-label="Abrir rápido"]';
const QUICK_OPEN_ITEMS_SELECTOR = '.filtered-list-widget-item';
const QUICK_OPEN_SELECTED_ITEM_SELECTOR = `${QUICK_OPEN_ITEMS_SELECTOR}.selected`;
export const openCommandMenu = async (
devToolsPage: DevToolsPage,
) => {
await devToolsPage.pressKey('P', {control: true, shift: true});
await devToolsPage.waitFor(QUICK_OPEN_SELECTOR);
};
export const openFileQuickOpen = async (devtoolsPage: DevToolsPage) => {
await devtoolsPage.pressKey('P', {control: true});
await devtoolsPage.waitFor(QUICK_OPEN_SELECTOR);
};
export async function readQuickOpenResults(devtoolsPage: DevToolsPage): Promise<string[]> {
const items = await devtoolsPage.$$('.filtered-list-widget-item');
return await Promise.all(items.map(element => element.evaluate(el => el.deepInnerText().split('\n')[0])));
}
/** Does not play well with pptr:evaluate scripts. crbug.com/391533572 */
export const openFileWithQuickOpen = async (sourceFile: string, filePosition = 0, devtoolsPage: DevToolsPage) => {
await waitForSourceFiles(
SourceFileEvents.SOURCE_FILE_LOADED,
files => files.some(f => f.endsWith(sourceFile)),
async () => {
await openFileQuickOpen(devtoolsPage);
await typeIntoQuickOpen(sourceFile, undefined, devtoolsPage);
const firstItem = await getMenuItemAtPosition(filePosition, devtoolsPage);
await firstItem.click();
},
devtoolsPage,
);
};
export async function runCommandWithQuickOpen(command: string, devtoolsPage: DevToolsPage): Promise<void> {
await openCommandMenu(devtoolsPage);
await devtoolsPage.typeText(command);
// TODO: it should actually wait for rendering to finish.
await devtoolsPage.drainTaskQueue();
await devtoolsPage.pressKey('Enter');
}
export const openGoToLineQuickOpen = async (devtoolsPage: DevToolsPage) => {
// This shortcut explicitly uses Control rather then meta on Mac
// So we can't use our Helper.
await devtoolsPage.page.keyboard.down('Control');
await devtoolsPage.pressKey('G');
await devtoolsPage.page.keyboard.up('Control');
await devtoolsPage.waitFor(QUICK_OPEN_SELECTOR);
};
export const showSnippetsAutocompletion = async (devtoolsPage: DevToolsPage) => {
// Clear the `>` character, as snippets use a `!` instead
await devtoolsPage.pressKey('Backspace');
await devtoolsPage.typeText('!');
};
export async function getAvailableSnippets(devtoolsPage: DevToolsPage) {
const quickOpenElement = await devtoolsPage.waitFor(QUICK_OPEN_SELECTOR);
const snippetsDOMElements = await devtoolsPage.$$(QUICK_OPEN_ITEMS_SELECTOR, quickOpenElement);
const snippets = await Promise.all(snippetsDOMElements.map(elem => elem.evaluate(elem => elem.textContent)));
return snippets;
}
export async function getMenuItemAtPosition(position: number, devtoolsPage: DevToolsPage) {
const quickOpenElement = await devtoolsPage.waitFor(QUICK_OPEN_SELECTOR);
await devtoolsPage.waitFor(QUICK_OPEN_ITEMS_SELECTOR);
const itemsHandles = await devtoolsPage.$$(QUICK_OPEN_ITEMS_SELECTOR, quickOpenElement);
const item = itemsHandles[position];
assert.isOk(item, `Quick open: could not find item at position: ${position}.`);
return item;
}
export async function getMenuItemTitleAtPosition(position: number, devtoolsPage: DevToolsPage) {
const quickOpenElement = await devtoolsPage.waitFor(QUICK_OPEN_SELECTOR);
await devtoolsPage.waitFor(QUICK_OPEN_ITEMS_SELECTOR);
const itemsHandles = await devtoolsPage.$$(QUICK_OPEN_ITEMS_SELECTOR, quickOpenElement);
const item = itemsHandles[position];
assert.isOk(item, `Quick open: could not find item at position: ${position}.`);
const title = await item.evaluate(elem => elem.deepInnerText().split('\n')[0]);
return title;
}
export const closeDrawer = async (devToolsPage: DevToolsPage) => {
await devToolsPage.click('[aria-label="Close drawer"]');
};
export const getSelectedItemText = async (devToolsPage: DevToolsPage) => {
const quickOpenElement = await devToolsPage.waitFor(QUICK_OPEN_SELECTOR);
const selectedRow = await devToolsPage.waitFor(QUICK_OPEN_SELECTED_ITEM_SELECTOR, quickOpenElement);
const textContent = await selectedRow.getProperty('textContent');
assert.isOk(textContent, 'Quick open: could not get selected item textContent');
return await textContent.jsonValue();
};
export async function typeIntoQuickOpen(query: string, expectEmptyResults = false, devtoolsPage: DevToolsPage) {
await openFileQuickOpen(devtoolsPage);
const prompt = await devtoolsPage.waitFor('[aria-label="Quick open prompt"]');
await prompt.type(query);
if (expectEmptyResults) {
await devtoolsPage.waitFor('.filtered-list-widget :not(.hidden).not-found-text');
} else {
await devtoolsPage.waitForFunction(async () => {
const matches = await devtoolsPage.$$(`.filtered-list-widget-item devtools-highlight`);
const ranges = await Promise.all(matches.map(async m => {
return await m.evaluate(m => m.getAttribute('ranges'));
}));
return ranges.some(r => (r?.match(/,1/g) || []).length === query.length);
});
}
}