Skip to content

Commit

Permalink
feat: set cursor to specific line
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Nov 19, 2021
1 parent 7bc7471 commit 35b8455
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ I am not using a boolean value to better support upcoming WYSIWYG mode.

### Writing

| / | parameters | explanation |
| --------- | --------------------------------------- | ------------------------------------------------------------------- |
| write | <identification\>, data | Only writes `data` to `filepath` if the file is not already present |
| overwrite | <identification\>, data, mode=overwrite | Writes `data` to `filepath` even if the file already exists |
| append | <identification\>, data, mode=append | Only appends `data` to `filepath` |
| prepend | <identification\>, data, mode=prepend | Only prepends `data` to `filepath` |
| / | parameters | explanation |
| --------- | --------------------------------------- | ----------------------------------------------------------------- |
| write | <identification\>, data | Only writes `data` to the file if the file is not already present |
| overwrite | <identification\>, data, mode=overwrite | Writes `data` to `filepath` even if the file already exists |
| append | <identification\>, data, mode=append | Only appends `data` to the file |
| prepend | <identification\>, data, mode=prepend | Only prepends `data` to the file |

The `heading` parameter is for `mode=append` and `mode=prepend` supported too.

Expand All @@ -63,21 +63,23 @@ The `heading` parameter is for `mode=append` and `mode=prepend` supported too.
| ---------------------- | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| 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 | <identification\>, heading | Opens the `heading` in `filepath` |
| block reference | <identification\>, block | Opens the `block` in `filepath` |
| settings tab | settingid | Opens a settings tab by id, all plugins are supported (e.g. `editor`, `community-plugins`, `plugin-browser`, `theme-browser`...) |

### Execute command

| / | parameters | explanation |
| --------------- | ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| execute by name | commandname | Executes command by its name |
| execute by name | commandname, <identification\> | Opens `filepath` and then executes command by its name |
| execute by name | commandname, <identification\>, mode=append | Opens `filepath`, adds empty line at the end and sets cursor, then executes command by its name |
| execute by name | commandname, <identification\>, mode=prepend | Opens `filepath`, adds empty line at the beginning and sets cursor, then executes command by its name |
| execute by name | commandname, <identification\>, mode=overwrite | Opens `filepath`, clears the file, then executes command by its name |
| execute by id | commandid | Executes command by its id |
| execute by id | commandid, filepath, (same modes as execute by name) | Opens `filepath` and then executes command by its id |
| / | parameters | explanation |
| --------------- | ---------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| execute by name | commandname | Executes command by its name |
| execute by name | commandname, <identification\> | Opens file and then executes command by its name |
| execute by name | commandname, <identification\>, mode=append | Opens file, adds empty line at the end and sets cursor, then executes command by its name |
| execute by name | commandname, <identification\>, mode=prepend | Opens file, adds empty line at the beginning and sets cursor, then executes command by its name |
| execute by name | commandname, <identification\>, mode=overwrite | Opens file, clears the file, then executes command by its name |
| execute by id | commandid | Executes command by its id |
| execute by id | commandid, filepath, (same modes as execute by name) | Opens file and then executes command by its id |

### Search and replace
| / | parameters | explanation |
Expand Down
10 changes: 10 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface Parameters {
"x-error"?: string;
saveworkspace?: "true";
updateplugins?: "true";
line?: number;
}

export default class AdvancedURI extends Plugin {
Expand Down Expand Up @@ -364,6 +365,7 @@ export default class AdvancedURI extends Plugin {
if (!view) return;
const cache = this.app.metadataCache.getFileCache(view.file);
const heading = cache.headings.find((e) => e.heading === parameters.heading);
view.editor.focus();
view.editor.setCursor({ line: heading.position.start.line + 1, ch: 0 });
}
else if (parameters.block != undefined) {
Expand All @@ -372,11 +374,19 @@ export default class AdvancedURI extends Plugin {
if (!view) return;
const cache = this.app.metadataCache.getFileCache(view.file);
const block = cache.blocks[parameters.block];
view.editor.focus();
view.editor.setCursor({ line: block.position.start.line, ch: 0 });
}
else {
if (!fileIsAlreadyOpened)
await this.app.workspace.openLinkText(parameters.filepath, "", this.settings.openFileWithoutWriteInNewPane, this.getViewStateFromMode(parameters));
if (parameters.line != undefined) {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const line = Math.min(parameters.line - 1, view.editor.lineCount() - 1);
view.editor.focus();
view.editor.setCursor({ line: line, ch: view.editor.getLine(line).length });
}
}
if (parameters.mode != undefined) {
await this.setCursor(parameters.mode);
Expand Down

0 comments on commit 35b8455

Please sign in to comment.