Path: blob/main/extensions/copilot/test/simulation/fixtures/gen-method-issue-3602/editor.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { References } from './peek';6import { Commands } from './workbench';7import { Code } from './code';89const RENAME_BOX = '.monaco-editor .monaco-editor.rename-box';10const RENAME_INPUT = `${RENAME_BOX} .rename-input`;11const EDITOR = (filename: string) => `.monaco-editor[data-uri$="${filename}"]`;12const VIEW_LINES = (filename: string) => `${EDITOR(filename)} .view-lines`;13const LINE_NUMBERS = (filename: string) => `${EDITOR(filename)} .margin .margin-view-overlays .line-numbers`;1415export class Editor {1617private static readonly FOLDING_EXPANDED = '.monaco-editor .margin .margin-view-overlays>:nth-child(${INDEX}) .folding';18private static readonly FOLDING_COLLAPSED = `${Editor.FOLDING_EXPANDED}.collapsed`;1920constructor(private code: Code, private commands: Commands) { }2122async findReferences(filename: string, term: string, line: number): Promise<References> {23await this.clickOnTerm(filename, term, line);24await this.commands.runCommand('Peek References');25const references = new References(this.code);26await references.waitUntilOpen();27return references;28}2930async rename(filename: string, line: number, from: string, to: string): Promise<void> {31await this.clickOnTerm(filename, from, line);32await this.commands.runCommand('Rename Symbol');3334await this.code.waitForActiveElement(RENAME_INPUT);35await this.code.waitForSetValue(RENAME_INPUT, to);3637await this.code.dispatchKeybinding('enter');38}3940async gotoDefinition(filename: string, term: string, line: number): Promise<void> {41await this.clickOnTerm(filename, term, line);42await this.commands.runCommand('Go to Implementations');43}4445async peekDefinition(filename: string, term: string, line: number): Promise<References> {46await this.clickOnTerm(filename, term, line);47await this.commands.runCommand('Peek Definition');48const peek = new References(this.code);49await peek.waitUntilOpen();50return peek;51}5253private async getSelector(filename: string, term: string, line: number): Promise<string> {54const lineIndex = await this.getViewLineIndex(filename, line);55const classNames = await this.getClassSelectors(filename, term, lineIndex);5657return `${VIEW_LINES(filename)}>:nth-child(${lineIndex}) span span.${classNames[0]}`;58}5960async foldAtLine(filename: string, line: number): Promise<any> {61const lineIndex = await this.getViewLineIndex(filename, line);62await this.code.waitAndClick(Editor.FOLDING_EXPANDED.replace('${INDEX}', '' + lineIndex));63await this.code.waitForElement(Editor.FOLDING_COLLAPSED.replace('${INDEX}', '' + lineIndex));64}6566async unfoldAtLine(filename: string, line: number): Promise<any> {67const lineIndex = await this.getViewLineIndex(filename, line);68await this.code.waitAndClick(Editor.FOLDING_COLLAPSED.replace('${INDEX}', '' + lineIndex));69await this.code.waitForElement(Editor.FOLDING_EXPANDED.replace('${INDEX}', '' + lineIndex));70}7172private async clickOnTerm(filename: string, term: string, line: number): Promise<void> {73const selector = await this.getSelector(filename, term, line);74await this.code.waitAndClick(selector);75}7677async waitForEditorFocus(filename: string, lineNumber: number, selectorPrefix = ''): Promise<void> {78const editor = [selectorPrefix || '', EDITOR(filename)].join(' ');79const line = `${editor} .view-lines > .view-line:nth-child(${lineNumber})`;80const textarea = `${editor} textarea`;8182await this.code.waitAndClick(line, 1, 1);83await this.code.waitForActiveElement(textarea);84}8586async waitForTypeInEditor(filename: string, text: string, selectorPrefix = ''): Promise<any> {87if (text.includes('\n')) {88throw new Error('waitForTypeInEditor does not support new lines, use either a long single line or dispatchKeybinding(\'Enter\')');89}90const editor = [selectorPrefix || '', EDITOR(filename)].join(' ');9192await this.code.waitForElement(editor);9394const textarea = `${editor} textarea`;95await this.code.waitForActiveElement(textarea);9697await this.code.waitForTypeInEditor(textarea, text);9899await this.waitForEditorContents(filename, c => c.indexOf(text) > -1, selectorPrefix);100}101102async waitForEditorContents(filename: string, accept: (contents: string) => boolean, selectorPrefix = ''): Promise<any> {103const selector = [selectorPrefix || '', `${EDITOR(filename)} .view-lines`].join(' ');104return this.code.waitForTextContent(selector, undefined, c => accept(c.replace(/\u00a0/g, ' ')));105}106107private async getClassSelectors(filename: string, term: string, viewline: number): Promise<string[]> {108const elements = await this.code.waitForElements(`${VIEW_LINES(filename)}>:nth-child(${viewline}) span span`, false, els => els.some(el => el.textContent === term));109const { className } = elements.filter(r => r.textContent === term)[0];110return className.split(/\s/g);111}112113private async getViewLineIndex(filename: string, line: number): Promise<number> {114const elements = await this.code.waitForElements(LINE_NUMBERS(filename), false, els => {115return els.some(el => el.textContent === `${line}`);116});117118for (let index = 0; index < elements.length; index++) {119if (elements[index].textContent === `${line}`) {120return index + 1;121}122}123124throw new Error('Line not found');125}126}127128129