mirror of
https://github.com/Silverquark/dance.git
synced 2024-09-11 10:45:43 +03:00
api: use search in moveTo (fixes #251)
This commit is contained in:
parent
fe55de8a74
commit
e5a821ebb9
@ -16,10 +16,11 @@ export function search(
|
||||
re: RegExp,
|
||||
origin: vscode.Position,
|
||||
end?: vscode.Position,
|
||||
document?: vscode.TextDocument,
|
||||
) {
|
||||
return direction === Direction.Backward
|
||||
? searchBackward(re, origin, end)
|
||||
: searchForward(re, origin, end);
|
||||
? searchBackward(re, origin, end, document)
|
||||
: searchForward(re, origin, end, document);
|
||||
}
|
||||
|
||||
export declare namespace search {
|
||||
@ -62,11 +63,15 @@ export declare namespace search {
|
||||
* abc
|
||||
* ```
|
||||
*/
|
||||
export function searchBackward(re: RegExp, origin: vscode.Position, end?: vscode.Position): search.Result {
|
||||
export function searchBackward(
|
||||
re: RegExp,
|
||||
origin: vscode.Position,
|
||||
end?: vscode.Position,
|
||||
document = Context.current.document,
|
||||
): search.Result {
|
||||
end ??= Positions.zero;
|
||||
|
||||
const document = Context.current.document,
|
||||
searchStart = document.offsetAt(end),
|
||||
const searchStart = document.offsetAt(end),
|
||||
searchEnd = document.offsetAt(origin),
|
||||
possibleSearchLength = searchEnd - searchStart;
|
||||
|
||||
@ -121,9 +126,12 @@ export function searchBackward(re: RegExp, origin: vscode.Position, end?: vscode
|
||||
* abc
|
||||
* ```
|
||||
*/
|
||||
export function searchForward(re: RegExp, origin: vscode.Position, end?: vscode.Position): search.Result {
|
||||
const document = Context.current.document;
|
||||
|
||||
export function searchForward(
|
||||
re: RegExp,
|
||||
origin: vscode.Position,
|
||||
end?: vscode.Position,
|
||||
document = Context.current.document,
|
||||
): search.Result {
|
||||
end ??= Positions.last(document);
|
||||
|
||||
const searchStart = document.offsetAt(origin),
|
||||
@ -370,7 +378,9 @@ function searchOneOfForward(
|
||||
match = re.exec(text);
|
||||
|
||||
if (match !== null) {
|
||||
return [new vscode.Position(line, match.index), match] as search.Result;
|
||||
const character = line === originLine ? origin.character + match.index : match.index;
|
||||
|
||||
return [new vscode.Position(line, character), match] as search.Result;
|
||||
}
|
||||
|
||||
for (let i = 1; i < lineRange; i++) {
|
||||
|
@ -1,8 +1,10 @@
|
||||
import * as vscode from "vscode";
|
||||
|
||||
import { search } from ".";
|
||||
import { Context } from "../context";
|
||||
import * as Positions from "../positions";
|
||||
import { Direction } from "../types";
|
||||
import { escapeForRegExp } from "../../utils/regexp";
|
||||
|
||||
/**
|
||||
* Moves the given position towards the given direction until the given string
|
||||
@ -19,41 +21,11 @@ export function moveTo(
|
||||
origin: vscode.Position,
|
||||
document = Context.current.document,
|
||||
) {
|
||||
let line = origin.line,
|
||||
character: number | undefined = origin.character;
|
||||
|
||||
for (;;) {
|
||||
const text = document.lineAt(line).text;
|
||||
|
||||
if (character === undefined) {
|
||||
character = text.length;
|
||||
}
|
||||
|
||||
const idx = direction === Direction.Backward
|
||||
? text.lastIndexOf(string, character)
|
||||
: text.indexOf(string, character);
|
||||
|
||||
if (idx !== -1) {
|
||||
return new vscode.Position(line, idx);
|
||||
}
|
||||
|
||||
// No match on this line, let's keep going.
|
||||
if (direction === Direction.Backward) {
|
||||
if (line === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
line--;
|
||||
character = undefined;
|
||||
} else {
|
||||
if (line === document.lineCount - 1) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
line++;
|
||||
character = 0;
|
||||
}
|
||||
if (direction === Direction.Backward) {
|
||||
origin = Positions.offsetOrEdge(origin, string.length, document);
|
||||
}
|
||||
|
||||
return search(direction, new RegExp(escapeForRegExp(string)), origin, undefined, document)?.[0];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -211,3 +211,42 @@ abcdefghijk
|
||||
abcdefghijk
|
||||
^ 0
|
||||
```
|
||||
|
||||
# 3
|
||||
|
||||
```
|
||||
abc
|
||||
def
|
||||
ghi
|
||||
| 0
|
||||
jkl
|
||||
mno
|
||||
```
|
||||
|
||||
## 3 select-to-line-end
|
||||
[up](#3)
|
||||
|
||||
- .seek { input: "\n" }
|
||||
|
||||
```
|
||||
abc
|
||||
def
|
||||
ghi
|
||||
^^ 0
|
||||
jkl
|
||||
mno
|
||||
```
|
||||
|
||||
## 3 select-to-line-end-included
|
||||
[up](#3)
|
||||
|
||||
- .seek { input: "\n", include: true }
|
||||
|
||||
```
|
||||
abc
|
||||
def
|
||||
ghi
|
||||
^^^ 0
|
||||
jkl
|
||||
mno
|
||||
```
|
||||
|
50
test/suite/commands/seek.test.ts
generated
50
test/suite/commands/seek.test.ts
generated
@ -351,5 +351,55 @@ suite("./test/suite/commands/seek.md", function () {
|
||||
`);
|
||||
});
|
||||
|
||||
test("3 > select-to-line-end", async function () {
|
||||
// Set-up document to be in expected initial state.
|
||||
await ExpectedDocument.apply(editor, 6, String.raw`
|
||||
abc
|
||||
def
|
||||
ghi
|
||||
| 0
|
||||
jkl
|
||||
mno
|
||||
`);
|
||||
|
||||
// Perform all operations.
|
||||
await executeCommand("dance.seek", { input: "\n" });
|
||||
|
||||
// Ensure document is as expected.
|
||||
ExpectedDocument.assertEquals(editor, "./test/suite/commands/seek.md:226:1", 6, String.raw`
|
||||
abc
|
||||
def
|
||||
ghi
|
||||
^^ 0
|
||||
jkl
|
||||
mno
|
||||
`);
|
||||
});
|
||||
|
||||
test("3 > select-to-line-end-included", async function () {
|
||||
// Set-up document to be in expected initial state.
|
||||
await ExpectedDocument.apply(editor, 6, String.raw`
|
||||
abc
|
||||
def
|
||||
ghi
|
||||
| 0
|
||||
jkl
|
||||
mno
|
||||
`);
|
||||
|
||||
// Perform all operations.
|
||||
await executeCommand("dance.seek", { input: "\n", include: true });
|
||||
|
||||
// Ensure document is as expected.
|
||||
ExpectedDocument.assertEquals(editor, "./test/suite/commands/seek.md:240:1", 6, String.raw`
|
||||
abc
|
||||
def
|
||||
ghi
|
||||
^^^ 0
|
||||
jkl
|
||||
mno
|
||||
`);
|
||||
});
|
||||
|
||||
groupTestsByParentName(this);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user