New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for copying lines up and down #23
Conversation
src/actions.ts
Outdated
editor.transaction({ | ||
changes: [ | ||
{text: '', from: previousLineStart, to: fromLineStart}, | ||
{text: '\n' + contentsOfPreviousLine.trimEnd(), from: toLineEnd}, | ||
], | ||
selection: { | ||
from: { line: from.line - 1, ch: from.ch }, | ||
to: { line: to.line - 1, ch: to.ch } | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this allow you to group changes to be applied all at once instead of doing replaceRange
and setSelection
separately?
I've been meaning to look into editor.transaction
as well but the Obsidian API for it seems to differ from the CodeMirror 6 API...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly. Before I had two separate replaceRange
calls but then Cmd+Z was stepping through them individually. I tried to merge into one replaceRange
call but that left the selection in the wrong state. Then I found transaction
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! transaction
seems like it would be especially useful for multiple selections, which I'm working on right now.
I'll keep the existing shortcuts to avoid introducing breaking changes since users can customise them in settings anyway. Tests would need a larger rework to pass since they're using CodeMirror 5 at the moment, which doesn't utilise transactions for state updates (which is what your implementation of move line up/down relies on). Is the behaviour of moving lines different to the built-in functionality though (#13)? If not, perhaps you could use that instead of writing a custom version? |
I see, perhaps you could have a switch to let users choose "VS Code style" or "Sublime style" hotkeys, since most people looking for a specific one and finding this plugin. No, "Swap line" is exactly what I wanted 🤦♂️ Not sure how I missed that. In that case all I need is copy up/down. I can try rewriting it without |
Ok, I've amended the patch to leave I suppose you could also name them "Duplicate line up" and "Duplicate line down", leaving the former unset and using Mod+Shift+D for the latter. I think a big benefit of this plugin would be the convenience to get the same shortcuts you expect from a particular editor without fiddling around with key mappings, though. |
Yeah that's a good idea, I'll look into adding some plugin-specific settings in a future release. |
Huh that's weird, I can't reproduce it. It works as expected for me. |
Hmm I can reproduce it consistently. I'm on Obsidian v0.13.23 with no other plugins except for this one. It only happens when the cursor (or selection end) is after the last character of a line. For example, at the end of |
Also mention Alt+Up/Down in README.
I customized this to meet my needs better:
I realized you probably won't want to merge this as-is since it will break what users currently expect (Ctrl-Shift-D) but feel free to incorporate it somehow if you wish.
Also, I can't get tests to work. I'm getting
TypeError: editor.transaction is not a function
. But it works when I test the plugin manually.