Skip to content

Commit

Permalink
feat: set cursor to column in line
Browse files Browse the repository at this point in the history
close #173
  • Loading branch information
Vinzent03 committed Jul 14, 2024
1 parent 7d2ad11 commit 414277d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
20 changes: 10 additions & 10 deletions docs/Actions/Navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
> Use the [open mode](Navigation%20Parameters.md#open-mode) parameter to open the file always in a new tab or in a new window.
>
| / | parameters | explanation |
| ---------------------- | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| workspace | workspace | Opens the workspace called `workspace` |
| 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 in file | <identification\>, line | Opens line `line` in file |
| 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\> |
| settings tab | settingid | Opens a settings tab by id, all plugins are supported. See [here](Settings%20navigation.md) for a list of all available options |
| / | parameters | explanation | |
| -------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | --- |
| workspace | workspace | Opens the workspace called `workspace` | |
| 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) | |
| 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\> | |
| settings tab | settingid | Opens a settings tab by id, all plugins are supported. See [here](Settings%20navigation.md) for a list of all available options | |


> [!example]
Expand Down
16 changes: 8 additions & 8 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Tools from "./tools";
import { Parameters } from "./types";
import { copyText, getAlternativeFilePath } from "./utils";
export default class Handlers {
constructor(private readonly plugin: AdvancedURI) {}
constructor(private readonly plugin: AdvancedURI) { }
app = this.plugin.app;
public get tools(): Tools {
return this.plugin.tools;
Expand Down Expand Up @@ -163,7 +163,7 @@ export default class Handlers {
editor.setValue("");
}
}
} else if (parameters.line) {
} else if (parameters.line != undefined || parameters.column != undefined) {
await this.plugin.open({
file: parameters.filepath,
mode: "source",
Expand Down Expand Up @@ -233,7 +233,7 @@ export default class Handlers {
editor.setValue("");
}
}
} else if (parameters.line) {
} else if (parameters.line != undefined || parameters.column != undefined) {
await this.plugin.open({
file: parameters.filepath,
mode: "source",
Expand Down Expand Up @@ -430,7 +430,7 @@ export default class Handlers {
setting: this.plugin.settings.openFileWithoutWriteInNewPane,
parameters: parameters,
});
if (parameters.line != undefined) {
if (parameters.line != undefined || parameters.column != undefined) {
await this.plugin.setCursorInLine(parameters);
}
}
Expand Down Expand Up @@ -547,10 +547,10 @@ export default class Handlers {
}

async handleUpdatePlugins(parameters: Parameters) {
new Notice("Checking for updates…");
await app.plugins.checkForUpdates();
const updateCount = Object.keys((this.app as any).plugins.updates).length;
new Notice("Checking for updates…");
await app.plugins.checkForUpdates();

const updateCount = Object.keys((this.app as any).plugins.updates).length;
if (updateCount > 0) {
parameters.settingid = "community-plugins";
this.handleOpenSettings(parameters);
Expand Down
12 changes: 8 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ export default class AdvancedURI extends Plugin {
setting: this.settings.openFileOnWriteInNewPane,
parameters,
});
if (parameters.line != undefined) {
if (parameters.line != undefined || parameters.column != undefined) {
await this.setCursorInLine(parameters);
}
}
Expand Down Expand Up @@ -595,18 +595,22 @@ export default class AdvancedURI extends Plugin {
}

async setCursorInLine(parameters: Parameters) {
const rawLine = Number(parameters.line);
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const viewState = view.leaf.getViewState();

const rawLine = parameters.line != undefined ? Number(parameters.line) : undefined;
const rawColumn = parameters.column ? Number(parameters.column) : undefined;
viewState.state.mode = "source";
await view.leaf.setViewState(viewState);

const line = Math.min(rawLine - 1, view.editor.lineCount() - 1);
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);
view.editor.focus();
view.editor.setCursor({
line: line,
ch: view.editor.getLine(line).length,
ch: column ?? maxColumn,
});

await new Promise((resolve) => setTimeout(resolve, 10));
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export interface Parameters {
saveworkspace?: "true";
updateplugins?: "true";
line?: string;
column?: string;
/**
* @deprecated Use "openMode" instead
*/
Expand Down

0 comments on commit 414277d

Please sign in to comment.