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]>

tmp

Signed-off-by: Dominik Jelinek <[email protected]>

tmp

Signed-off-by: Dominik Jelinek <[email protected]>
  • Loading branch information
djelinek committed Apr 10, 2024
1 parent 4fcedf8 commit 4898c2f
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 38 deletions.
19 changes: 18 additions & 1 deletion docs/TextEditor.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
![editor](https://user-images.githubusercontent.com/4181232/56643754-81b9ee00-667a-11e9-9c7a-de39f342d676.png)

#### Lookup

```typescript
import { TextEditor, EditorView } from 'vscode-extension-tester';
...
Expand All @@ -11,7 +12,9 @@ const editor1 = await new EditorView().openEditor('package.json');
```

#### Text Retrieval

Note: Most text retrieval and editing actions will make use of the clipboard.

```typescript
// get all text
const text = await editor.getText();
Expand All @@ -22,6 +25,7 @@ const numberOfLines = await editor.getNumberOfLines();
```

#### Editing Text

```typescript
// replace all text with a string
await editor.setText('my fabulous text');
Expand All @@ -33,11 +37,19 @@ await editor.typeText('I have the best text');
await editor.typeTextAt(1, 3, ' absolutely');
// format the whole document with built-in tools
await editor.formatDocument();
// move the cursor to the given coordinates
await editor.moveCursor(3, 6);
// set cursor to given position using command prompt :Ln,Col
await editor.setCursor(2, 5);
// get the current cursor coordinates as number array [x,y]
const coords = await editor.getCoordinates();
// get the current indentation of opened editor
const indent = await editor.getIndentation();
console.log(`indentation label = ${indent.label} and value = ${indent.value}`);
```

#### Save Changes

```typescript
// find if the editor has changes
const hasChanges = await editor.isDirty();
Expand All @@ -48,11 +60,13 @@ const prompt = await editor.saveAs();
```

#### Get Document File Path

```typescript
const path = await editor.getFilePath();
```

#### Content Assist

```typescript
// open content assist at current position
const contentAssist = await editor.toggleContentAssist(true);
Expand All @@ -61,6 +75,7 @@ await editor.toggleContentAssist(false);
```

#### Search for Text

```typescript
// get line number that contains some text
const lineNum = await editor.getLineOfText('some text');
Expand All @@ -75,12 +90,14 @@ const find = await editor.openFindWidget();
```

#### Breakpoints

```typescript
// toggle breakpoint on a line with given number
await editor.toggleBreakpoint(1);
```

#### CodeLenses

```typescript
// get a code lens by (partial) text
const lens = await editor.getCodeLens('my code lens text');
Expand All @@ -95,4 +112,4 @@ await lens.click();
// or just get the text
const text = await lens.getText();
const tooltip = await lens.getTooltip();
```
```
202 changes: 179 additions & 23 deletions packages/page-objects/src/components/editor/TextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class TextEditor extends Editor {
* Find and select a given text. Not usable for multi line selection.
*
* @param text text to select
* @param occurrence specify which onccurrence of text to select if multiple are present in the document
* @param occurrence specify which occurrence of text to select if multiple are present in the document
*/
async selectText(text: string, occurrence = 1): Promise<void> {
const lineNum = await this.getLineOfText(text, occurrence);
Expand Down 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(150);
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(250);
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(150);
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(250);
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: 3 additions & 0 deletions tests/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 tests/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
Loading

0 comments on commit 4898c2f

Please sign in to comment.