Skip to content

Commit

Permalink
feat: set cursor at offset
Browse files Browse the repository at this point in the history
close #181
  • Loading branch information
Vinzent03 committed Aug 24, 2024
1 parent 58f92ea commit 0d450ca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/Actions/Navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
| save current workspace | saveworkspace=true | Saves the current workspace. (Can be combined with `workspace` to open a new workspace afterwards) | |
| file | <identification\> | Opens file | |
| line and/or column in file | <identification\>, line, column | Opens `column` in `line` in file (1 indexed) | |
| offset in file | <identification\>, offset | Sets the cursor at `offset` in file. Offset is the character count from the start |
| heading in file | <identification\>, heading | Opens the `heading` in file | |
| block reference in file | <identification\>, block | Opens the `block` in file | |
| global block reference | block | Searches the whole vault for that block id and uses that file for <identification\> | |
Expand Down
9 changes: 6 additions & 3 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ export default class Handlers {
}
} else if (
parameters.line != undefined ||
parameters.column != undefined
parameters.column != undefined ||
parameters.offset != undefined
) {
await this.plugin.open({
file: parameters.filepath,
Expand Down Expand Up @@ -259,7 +260,8 @@ export default class Handlers {
}
} else if (
parameters.line != undefined ||
parameters.column != undefined
parameters.column != undefined ||
parameters.offset != undefined
) {
await this.plugin.open({
file: parameters.filepath,
Expand Down Expand Up @@ -459,7 +461,8 @@ export default class Handlers {
});
if (
parameters.line != undefined ||
parameters.column != undefined
parameters.column != undefined ||
parameters.offset != undefined
) {
await this.plugin.setCursorInLine(parameters);
}
Expand Down
28 changes: 18 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ export default class AdvancedURI extends Plugin {
});
if (
parameters.line != undefined ||
parameters.column != undefined
parameters.column != undefined ||
parameters.offset != undefined
) {
await this.setCursorInLine(parameters);
}
Expand Down Expand Up @@ -666,21 +667,28 @@ export default class AdvancedURI extends Plugin {
viewState.state.mode = "source";
await view.leaf.setViewState(viewState);

const line =
rawLine != undefined
? Math.min(rawLine - 1, view.editor.lineCount() - 1)
: view.editor.getCursor().line;
const maxColumn = view.editor.getLine(line).length - 1;
const column = Math.min(rawColumn - 1, maxColumn);
let line: number, column: number;
if (parameters.offset != undefined) {
const pos = view.editor.offsetToPos(Number(parameters.offset));
line = pos.line;
column = pos.ch;
} else {
line =
rawLine != undefined
? Math.min(rawLine - 1, view.editor.lineCount() - 1)
: view.editor.getCursor().line;
const maxColumn = view.editor.getLine(line).length - 1;
column = Math.min(rawColumn - 1, maxColumn);
}
view.editor.focus();
view.editor.setCursor({
line: line,
ch: column ?? maxColumn,
ch: column,
});
view.editor.scrollIntoView(
{
from: { line: line, ch: column ?? maxColumn },
to: { line: line, ch: column ?? maxColumn },
from: { line: line, ch: column },
to: { line: line, ch: column },
},
true
);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export interface Parameters {
*/
canvasviewport?: string;
confirm?: string;
offset?: string;
}

export type OpenMode = "silent" | "popover" | PaneType | "true" | "false";
Expand Down

0 comments on commit 0d450ca

Please sign in to comment.