Skip to content

Commit

Permalink
issue-931: moveCursor fails on file with tabs
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Jelinek <[email protected]>
  • Loading branch information
djelinek committed Mar 20, 2024
1 parent eda4c7a commit a09c7fb
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 37 deletions.
200 changes: 178 additions & 22 deletions page-objects/src/components/editor/TextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,30 @@ export class TextEditor extends Editor {
await inputarea.sendKeys(text);
}

/**
* Set cursor to given position using command prompt :Ln,Col
* @param line line number to set to
* @param column column number to set to
* @returns Promise resolving when the cursor has reached the given coordinates
*/
async setCursor(line: number, column: number): Promise<void> {
const input = await new Workbench().openCommandPrompt();
await input.setText(`:${line},${column}`);
await input.confirm();
await this.waitForCursorPositionAt(line, column);
}

/**
* Get indentation from the status bar for the currently opened text editor
* @returns \{ string, number \} object which contains label and value of indentation
*/
async getIndentation(): Promise<{label: string, value: number}> {
const indentation = await new StatusBar().getCurrentIndentation();
const value = Number(indentation.match(/\d+/g)?.at(0));
const label = indentation.match(/^[a-zA-Z\s]+/g)?.at(0) as string;
return { label, value };
}

/**
* Move the cursor to the given coordinates
* @param line line number to move to
Expand All @@ -329,34 +353,166 @@ export class TextEditor extends Editor {
if (column < 1) {
throw new Error(`Column number ${column} does not exist`);
}
if (process.platform === 'darwin') {
const input = await new Workbench().openCommandPrompt();
await input.setText(`:${line},${column}`);
await input.confirm();
} else {
const inputarea = await this.findElement(TextEditor.locators.Editor.inputArea);
let coordinates = await this.getCoordinates();
const lineGap = coordinates[0] - line;
const lineKey = lineGap >= 0 ? Key.UP : Key.DOWN;
for (let i = 0; i < Math.abs(lineGap); i++) {
await inputarea.sendKeys(lineKey);
const inputarea = await this.findElement(TextEditor.locators.Editor.inputArea);
await this.moveCursorToLine(inputarea, line);
await this.moveCursorToColumn(inputarea, column);
}

/**
* (private) Move the cursor to the given line coordinate
* @param inputArea WebElement of an editor input area
* @param line line number to move to
* @returns Promise resolving when the cursor has reached the given line coordinate
*/
private async moveCursorToLine(inputArea: WebElement, line: number): Promise<void> {
const coordinates = await this.getCoordinates();
const lineGap = coordinates[0] - line;
const lineKey = lineGap >= 0 ? Key.UP : Key.DOWN;
let nextLine = coordinates[0];

for (let i = 0; i < Math.abs(lineGap); i++) {
if(await this.isLine(line)) break;
await inputArea.sendKeys(lineKey);
await inputArea.getDriver().sleep(100);
if(await this.isLine(line)) break;

switch (lineKey) {
case Key.UP:
nextLine = nextLine - 1;
break;
case Key.DOWN:
nextLine = nextLine + 1;
break;
}

coordinates = await this.getCoordinates();
const columnGap = coordinates[1] - column;
const columnKey = columnGap >= 0 ? Key.LEFT : Key.RIGHT;
for (let i = 0; i < Math.abs(columnGap); i++) {
await inputarea.sendKeys(columnKey);
let actualCoordinates = (await this.getCoordinates())[0];
if (actualCoordinates != coordinates[0]) {
throw new Error(`Column number ${column} is not accessible on line ${line}`);
try {
await this.waitForCursorPositionAtLine(nextLine);
} catch (error) {
if(await this.isLine(line)) break;
await inputArea.sendKeys(lineKey);
await inputArea.getDriver().sleep(200);
if(await this.isLine(line)) break;

if(lineKey == Key.DOWN && nextLine < line) {
await this.waitForCursorPositionAtLine(nextLine);
} else {
await this.waitForCursorPositionAtLine(line);
}
}

}
await this.getDriver().wait(async () => {
}

/**
* (private) Move the cursor to the given column coordinate
* @param inputArea WebElement of an editor input area
* @param column column number to move to
* @returns Promise resolving when the cursor has reached the given column coordinate
*/
private async moveCursorToColumn(inputArea: WebElement, column: number): Promise<void> {
const coordinates = await this.getCoordinates();
const columnGap = coordinates[1] - column;
const columnKey = columnGap >= 0 ? Key.LEFT : Key.RIGHT;
let nextCol = coordinates[1];

for (let i = 0; i < Math.abs(columnGap); i++) {
if(await this.isColumn(column)) break;
await inputArea.sendKeys(columnKey);
await inputArea.getDriver().sleep(100);
if(await this.isColumn(column)) break;
if ((await this.getCoordinates())[0] != coordinates[0]) {
throw new Error(`Column number ${column} is not accessible on line ${coordinates[0]}`);
}

switch (columnKey) {
case Key.LEFT:
nextCol = nextCol - 1;
break;
case Key.RIGHT:
nextCol = nextCol + 1;
break;
}

try {
await this.waitForCursorPositionAtColumn(nextCol);
} catch (error) {
if(await this.isColumn(column)) break;
await inputArea.sendKeys(columnKey);
await inputArea.getDriver().sleep(200);
if(await this.isColumn(column)) break;
if ((await this.getCoordinates())[0] != coordinates[0]) {
throw new Error(`Column number ${column} is not accessible on line ${coordinates[0]}`);
}

if(columnKey === Key.RIGHT && nextCol < column) {
await this.waitForCursorPositionAtColumn(nextCol);
} else {
await this.waitForCursorPositionAtColumn(column);
}
}

}
}

/**
* (private) Check if the cursor is already on requested line
* @param line line number to check against current cursor position
* @returns true / false
*/
private async isLine(line: number): Promise<boolean> {
const actualCoordinates = await this.getCoordinates();
if(actualCoordinates[0] === line) {
return true;
}
return false;
}

/**
* (private) Check if the cursor is already on requested column
* @param column column number to check against current cursor position
* @returns true / false
*/
private async isColumn(column: number): Promise<boolean> {
const actualCoordinates = await this.getCoordinates();
if(actualCoordinates[1] === column) {
return true;
}
return false;
}


/**
* (private) Dynamic waiting for cursor position movements
* @param line line number to wait
* @param column column number to wait
* @param timeout default timeout
*/
private async waitForCursorPositionAt(line: number, column: number): Promise<void> {
await this.waitForCursorPositionAtLine(line) && await this.waitForCursorPositionAtColumn(column);
}

/**
* (private) Dynamic waiting for cursor position movements at line
* @param line line number to wait
* @param timeout default timeout is wait for 1.5s
*/
private async waitForCursorPositionAtLine(line: number, timeout: number = 1_500): Promise<boolean> {
return await this.getDriver().wait(async () => {
const coor = await this.getCoordinates();
return coor[0] === line;
}, timeout, `Unable to set cursor at line ${line}`);
}

/**
* (private) Dynamic waiting for cursor position movements at column
* @param column column number to wait
* @param timeout default timeout is wait for 1.5s
*/
private async waitForCursorPositionAtColumn(column: number, timeout: number = 1_500): Promise<boolean> {
return await this.getDriver().wait(async () => {
const coor = await this.getCoordinates();
return coor[0] === line && coor[1] === column;
}, 10000, `Unable to set cursor at position ${column}:${line}`);
return coor[1] === column;
}, timeout, `Unable to set cursor at column ${column}`);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/test-project/.vscodeignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/**
test-resources/**
test-extensions/**
test-extensions/**
resources
2 changes: 1 addition & 1 deletion test/test-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
"lint": "eslint src --ext .ts",
"watch": "tsc -watch -p ./",
"cb-init": "echo hello_ExTester | clipboard",
"ui-test": "npm run cb-init && extest setup-and-run './out/src/test/cli/order-3-test.js' './out/src/test/cli/order-2-test.js' './out/src/test/cli/order-1-test.js' './out/src/test/**/*-test.js' './out/src/test/system/clipboard.test.js' -u -i -r . -e ./test-extensions"
"ui-test": "npm run cb-init && extest setup-and-run './out/src/test/cli/order-3-test.js' './out/src/test/cli/order-2-test.js' './out/src/test/cli/order-1-test.js' './out/src/test/**/textEditor*-test.js' './out/src/test/system/clipboard.test.js' -u -i -r . -e ./test-extensions"
},
"devDependencies": {
"@types/chai": "^4.3.14",
Expand Down
3 changes: 3 additions & 0 deletions test/test-project/resources/file-with-spaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
first row
second row
third row
3 changes: 3 additions & 0 deletions test/test-project/resources/file-with-tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
first row
second row
third row
54 changes: 42 additions & 12 deletions test/test-project/src/test/editor/textEditor-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import { expect } from 'chai';
import { TextEditor, EditorView, StatusBar, InputBox, ContentAssist, Workbench, FindWidget, VSBrowser, Notification, after, before } from "vscode-extension-tester";
import { TextEditor, EditorView, StatusBar, InputBox, ContentAssist, Workbench, FindWidget, VSBrowser, after, before } from "vscode-extension-tester";

describe('ContentAssist', async function () {
let assist: ContentAssist;
Expand Down Expand Up @@ -30,6 +30,7 @@ describe('ContentAssist', async function () {
});

beforeEach(async function () {
this.timeout(15000);
assist = await editor.toggleContentAssist(true) as ContentAssist;
await new Promise(res => setTimeout(res, 2000));
});
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('TextEditor', function () {
const testText = process.platform === 'win32' ? `line1\r\nline2\r\nline3` : `line1\nline2\nline3`;

before(async function () {
this.timeout(8000);
this.timeout(15000);
await new Workbench().executeCommand('Create: New File...');
await (await InputBox.create()).selectQuickPick('Text File');
await new Promise((res) => { setTimeout(res, 1000); });
Expand Down Expand Up @@ -102,14 +103,14 @@ describe('TextEditor', function () {
});

it('can type text at given coordinates', async function () {
this.timeout(5000);
this.timeout(10000);
await editor.typeTextAt(1, 6, '1');
const line = await editor.getTextAtLine(1);
expect(line).has.string('line11');
});

it('getCoordinates works', async function () {
this.timeout(15000);
this.timeout(20000);

await editor.moveCursor(1, 1);
expect(await editor.getCoordinates()).to.deep.equal([1, 1]);
Expand Down Expand Up @@ -143,6 +144,42 @@ describe('TextEditor', function () {
expect(await editor.formatDocument()).not.to.throw;
});

describe('move/set cursor', function () {

const params = [
{ file: 'file-with-spaces.ts', spaces: 'spaces'},
{ file: 'file-with-tabs.ts', spaces: 'tabs'}
];

params.forEach(param => describe(`file using ${param.spaces}`, function () {

let editor: TextEditor;

before(async function() {
await VSBrowser.instance.openResources(path.resolve(__dirname, '..', '..', '..', '..', 'resources', param.file));
editor = new TextEditor();
});

after(async function() {
await new EditorView().closeEditor(param.file);
});

[[2, 5], [3, 9]].forEach(coor => it(`move cursor to position [Ln ${coor[0]}, Col ${coor[1]}]`, async function () {
this.timeout(30000);
await editor.moveCursor(coor[0], coor[1]);
expect(await editor.getCoordinates()).to.deep.equal(coor);
}));

// set cursor using command prompt is not working properly for tabs indentation in VS Code, see https://github.com/microsoft/vscode/issues/198780
[[2, 12], [3, 15]].forEach(coor => ((param.spaces === 'tabs') ? it.skip : it)(`set cursor to position [Ln ${coor[0]}, Col ${coor[1]}]`, async function () {
this.timeout(30000);
await editor.setCursor(coor[0], coor[1]);
expect(await editor.getCoordinates()).to.deep.equal(coor);
}));
}));

});

describe('searching', function () {

before(async function () {
Expand Down Expand Up @@ -323,14 +360,7 @@ describe('TextEditor', function () {
await lens.click();
await lens.getDriver().sleep(1000);
const notifications = await new Workbench().getNotifications();
let notification: Notification;

for (const not of notifications) {
if ((await not.getMessage()).startsWith('CodeLens action clicked')) {
notification = not;
break;
}
}
const notification = notifications.find(async notification => { return (await notification.getMessage()).startsWith('Codelens action clicked'); });
expect(notification).not.undefined;
});
});
Expand Down
Loading

0 comments on commit a09c7fb

Please sign in to comment.