esm7/obsidian-vimrc-support

`editor` object in jsfile / jscommand fails to get selection

twio142 opened this issue · 8 comments

The editor.getSelection() function seems to fail in jsfile or jscommand, no matter selected with insert or visual mode.

jscommand { console.log(editor.getSelection()) } gets nothing, no matter mapped in vimrc file or put in editor command line.

But editor.getCursor() works just fine.

esm7 commented

There's an Obsidian/CodeMirror bug/behavior (maybe that's intentional, I don't know), that the selection is cleared when a Vim command is initiated. For this reason plugin functionalities that rely on the selection use ugly trickery that constantly saves the last selection.
Since you can't do that as part of a jscommand, I guess it makes sense to expose the last selection as a variable.

expose the last selection as a variable

Could you please explain me how?

esm7 commented

It's not something you can do right now unfortunately; I will add the functionality that like jscommand receives editor and view, it will also receive lastSelection.

esm7 commented

Added a selection parameter in 0.6.2.

huyz commented

There's an Obsidian/CodeMirror bug/behavior (maybe that's intentional, I don't know), that the selection is cleared when a Vim command is initiated. For this reason plugin functionalities that rely on the selection use ugly trickery that constantly saves the last selection

@esm7 Can you tell me a bit more about this hack? Cause it doesn't seem to work 100% for me. I'm trying to do

exmap TransformTitlecase obcommand obsidian-editor-shortcuts:transformToTitlecase
vmap <A-ç>T :TransformTitlecase

But the selection is still lost (and thus only one word gets titlecased, rather than the whole selection)

esm7 commented

Unfortunately that hack only applies to the internal workings of the Vimrc plugin, e.g. if you use jscommand you can get the "original" selection (before the Ex command clears it). AFAIK when it comes to Obsidian editor commands, they take the editor selection as-is, and it happens to be empty after executing an Ex command :(
I suggest you implement your TransformTitlecase command as a jscommand instead of calling the Obsidian functionality.

huyz commented

Got it, thanks:

exmap TransformTitlecaseSelection jscommand { editor.setSelections([selection]); this.app.commands.commands['obsidian-editor-shortcuts:transformToTitlecase'].editorCallback(editor) }
vmap <A-ç>T :TransformTitlecaseSelection
huyz commented

More examples at #146