Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/fixtures/gen-method-issue-3602/editor.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { References } from './peek';
7
import { Commands } from './workbench';
8
import { Code } from './code';
9
10
const RENAME_BOX = '.monaco-editor .monaco-editor.rename-box';
11
const RENAME_INPUT = `${RENAME_BOX} .rename-input`;
12
const EDITOR = (filename: string) => `.monaco-editor[data-uri$="${filename}"]`;
13
const VIEW_LINES = (filename: string) => `${EDITOR(filename)} .view-lines`;
14
const LINE_NUMBERS = (filename: string) => `${EDITOR(filename)} .margin .margin-view-overlays .line-numbers`;
15
16
export class Editor {
17
18
private static readonly FOLDING_EXPANDED = '.monaco-editor .margin .margin-view-overlays>:nth-child(${INDEX}) .folding';
19
private static readonly FOLDING_COLLAPSED = `${Editor.FOLDING_EXPANDED}.collapsed`;
20
21
constructor(private code: Code, private commands: Commands) { }
22
23
async findReferences(filename: string, term: string, line: number): Promise<References> {
24
await this.clickOnTerm(filename, term, line);
25
await this.commands.runCommand('Peek References');
26
const references = new References(this.code);
27
await references.waitUntilOpen();
28
return references;
29
}
30
31
async rename(filename: string, line: number, from: string, to: string): Promise<void> {
32
await this.clickOnTerm(filename, from, line);
33
await this.commands.runCommand('Rename Symbol');
34
35
await this.code.waitForActiveElement(RENAME_INPUT);
36
await this.code.waitForSetValue(RENAME_INPUT, to);
37
38
await this.code.dispatchKeybinding('enter');
39
}
40
41
async gotoDefinition(filename: string, term: string, line: number): Promise<void> {
42
await this.clickOnTerm(filename, term, line);
43
await this.commands.runCommand('Go to Implementations');
44
}
45
46
async peekDefinition(filename: string, term: string, line: number): Promise<References> {
47
await this.clickOnTerm(filename, term, line);
48
await this.commands.runCommand('Peek Definition');
49
const peek = new References(this.code);
50
await peek.waitUntilOpen();
51
return peek;
52
}
53
54
private async getSelector(filename: string, term: string, line: number): Promise<string> {
55
const lineIndex = await this.getViewLineIndex(filename, line);
56
const classNames = await this.getClassSelectors(filename, term, lineIndex);
57
58
return `${VIEW_LINES(filename)}>:nth-child(${lineIndex}) span span.${classNames[0]}`;
59
}
60
61
async foldAtLine(filename: string, line: number): Promise<any> {
62
const lineIndex = await this.getViewLineIndex(filename, line);
63
await this.code.waitAndClick(Editor.FOLDING_EXPANDED.replace('${INDEX}', '' + lineIndex));
64
await this.code.waitForElement(Editor.FOLDING_COLLAPSED.replace('${INDEX}', '' + lineIndex));
65
}
66
67
async unfoldAtLine(filename: string, line: number): Promise<any> {
68
const lineIndex = await this.getViewLineIndex(filename, line);
69
await this.code.waitAndClick(Editor.FOLDING_COLLAPSED.replace('${INDEX}', '' + lineIndex));
70
await this.code.waitForElement(Editor.FOLDING_EXPANDED.replace('${INDEX}', '' + lineIndex));
71
}
72
73
private async clickOnTerm(filename: string, term: string, line: number): Promise<void> {
74
const selector = await this.getSelector(filename, term, line);
75
await this.code.waitAndClick(selector);
76
}
77
78
async waitForEditorFocus(filename: string, lineNumber: number, selectorPrefix = ''): Promise<void> {
79
const editor = [selectorPrefix || '', EDITOR(filename)].join(' ');
80
const line = `${editor} .view-lines > .view-line:nth-child(${lineNumber})`;
81
const textarea = `${editor} textarea`;
82
83
await this.code.waitAndClick(line, 1, 1);
84
await this.code.waitForActiveElement(textarea);
85
}
86
87
async waitForTypeInEditor(filename: string, text: string, selectorPrefix = ''): Promise<any> {
88
if (text.includes('\n')) {
89
throw new Error('waitForTypeInEditor does not support new lines, use either a long single line or dispatchKeybinding(\'Enter\')');
90
}
91
const editor = [selectorPrefix || '', EDITOR(filename)].join(' ');
92
93
await this.code.waitForElement(editor);
94
95
const textarea = `${editor} textarea`;
96
await this.code.waitForActiveElement(textarea);
97
98
await this.code.waitForTypeInEditor(textarea, text);
99
100
await this.waitForEditorContents(filename, c => c.indexOf(text) > -1, selectorPrefix);
101
}
102
103
async waitForEditorContents(filename: string, accept: (contents: string) => boolean, selectorPrefix = ''): Promise<any> {
104
const selector = [selectorPrefix || '', `${EDITOR(filename)} .view-lines`].join(' ');
105
return this.code.waitForTextContent(selector, undefined, c => accept(c.replace(/\u00a0/g, ' ')));
106
}
107
108
private async getClassSelectors(filename: string, term: string, viewline: number): Promise<string[]> {
109
const elements = await this.code.waitForElements(`${VIEW_LINES(filename)}>:nth-child(${viewline}) span span`, false, els => els.some(el => el.textContent === term));
110
const { className } = elements.filter(r => r.textContent === term)[0];
111
return className.split(/\s/g);
112
}
113
114
private async getViewLineIndex(filename: string, line: number): Promise<number> {
115
const elements = await this.code.waitForElements(LINE_NUMBERS(filename), false, els => {
116
return els.some(el => el.textContent === `${line}`);
117
});
118
119
for (let index = 0; index < elements.length; index++) {
120
if (elements[index].textContent === `${line}`) {
121
return index + 1;
122
}
123
}
124
125
throw new Error('Line not found');
126
}
127
}
128
129