From 414277d0edef26348aaed6998e879519fca055c9 Mon Sep 17 00:00:00 2001 From: Vinzent Date: Sun, 14 Jul 2024 23:38:57 +0200 Subject: [PATCH] feat: set cursor to column in line close #173 --- docs/Actions/Navigation.md | 20 ++++++++++---------- src/handlers.ts | 16 ++++++++-------- src/main.ts | 12 ++++++++---- src/types.ts | 1 + 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/docs/Actions/Navigation.md b/docs/Actions/Navigation.md index 097be6a..10f7e5c 100644 --- a/docs/Actions/Navigation.md +++ b/docs/Actions/Navigation.md @@ -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 | | Opens file | -| line in file | , line | Opens line `line` in file | -| heading in file | , heading | Opens the `heading` in file | -| block reference in file | , block | Opens the `block` in file | -| global block reference | block | Searches the whole vault for that block id and uses that file for | -| 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 | | Opens file | | +| line and/or column in file | , line, column | Opens `column` in `line` in file (1 indexed) | | +| heading in file | , heading | Opens the `heading` in file | | +| block reference in file | , block | Opens the `block` in file | | +| global block reference | block | Searches the whole vault for that block id and uses that file for | | +| 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] diff --git a/src/handlers.ts b/src/handlers.ts index c20575c..98c2113 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -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; @@ -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", @@ -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", @@ -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); } } @@ -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); diff --git a/src/main.ts b/src/main.ts index 1e37a8f..62eaeae 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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); } } @@ -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)); diff --git a/src/types.ts b/src/types.ts index 21eadb3..b92a78c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -135,6 +135,7 @@ export interface Parameters { saveworkspace?: "true"; updateplugins?: "true"; line?: string; + column?: string; /** * @deprecated Use "openMode" instead */