Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Get current line

Open JeppeKlitgaard opened this issue 3 years ago • 5 comments

I would like to implement a "copy-line up/down" hotkey like the one I use in VSCode (CTRL + ALT + DOWN copies the current line(s) down).

It might be slightly niche, but perhaps Templater could implement an internal function to get the current line(s).

The signature would be something like:

tp.file.cursor_lines() -> [lines, start_char, stop_char]

Where:

  • lines is an array of strings (or string with \n, alternatively) containing the FULL (not just selection) lines that are currently in the selection
  • start_char is an int giving the position of the start of the cursor (in the first line)
  • stop_char is an int giving the position of the start of the cursor (in the last line)

Examples

image

would return: [["- Sweep voltage from positive to negative and find _stopping voltage_ where the current of electrons stops"], 4, 4]


image

would return: [["- Sweep voltage from positive to negative and find _stopping voltage_ where the current of electrons stops"], 4, 7]


image

would return:

[
    [
     "- Sweep voltage from positive to negative and find _stopping voltage_ where the current of electrons stops",
     "- Freeing an electron requires overcoming _the work function_"
    ],
    4, 3
]

JeppeKlitgaard avatar May 23 '21 15:05 JeppeKlitgaard

I realised that this can actually be done using the exposed Obsidian API, though this functionality would probably be nice to have directly in Templater nonetheless.

JeppeKlitgaard avatar Jun 03 '21 23:06 JeppeKlitgaard

Hey @JeppeKlitgaard that's a good idea, could you share what you already coded so I can understand your implementation better? From what I see getCursor only returns the EditorPosition, but not the full line of the cursor.

SilentVoid13 avatar Jun 17 '21 14:06 SilentVoid13

Hey @JeppeKlitgaard that's a good idea, could you share what you already coded so I can understand your implementation better? From what I see getCursor only returns the EditorPosition, but not the full line of the cursor.

any update?

hiocean avatar Sep 13 '21 07:09 hiocean

Pass getCursor().line to getLine to get the full text line at the cursor position.

Here's something I came up with

const doc = this.app.workspace.activeLeaf.view.sourceMode.cmEditor.doc
const cursorPos = doc.getCursor()
const lineText = doc.getLine(cursorPos.line).trim()
const regex = /^(?<pre>[-+*\d\.\s]*\[[x|\s]*\]\s*)(?<text>.*)$/i
const match = regex.exec(lineText)
if (match) {
	const ch = match.groups.pre.length
	doc.setCursor(cursorPos.line, ch)
}

if the current line starts with or similar to "- [ ] ", it sets the cursor position right after this substring.

      👇
- [x] │make pancakes
      👆

iwconfig avatar Oct 14 '21 18:10 iwconfig

Note that cmdEditor is depreciated. So I came up with the following:

const editor = this.app.workspace.activeEditor.editor;
const line = editor.getCursor().line;
const input = editor.getLine(line).trim();
editor.setLine(line, "");

bloaryth avatar Jun 26 '23 07:06 bloaryth