lionel- / codegrip

Reshaping and navigation commands for R code
Other
208 stars 5 forks source link

Return new cursor position from `reshape_info()` #22

Open dgkf opened 2 weeks ago

dgkf commented 2 weeks ago

Currently, after issue only a "change" action from the LSP, the entire region of replaced text is selected after the code action. Instead, it would be more intuitive if the same cursor remained selected through the transformation, mapping its original position to its final position.

To support this, it would be helpful if reshape_info also returned the new cursor position. The rest can be handled on the languageserver side.

dgkf commented 2 weeks ago

After implementing this, I learned that LSP doesn't actually support this yet. It won't be rolling out until the next release.

With the new LSP capability, a replacement can be provided as a snippet, using $0 to indicate the final cursor location after inserting the snippet.

Although codegrip could return this snippet text to simplify things, I think it would be better that it return the cursor position (perhaps as number of characters into the replacement?), and then let the LSP escape other $'s and insert this special $0 string.

One potential downside of this for editors like helix or vim, is that you probably don't want cursor positioning handled exactly like a snippet if you're in normal mode. I'm assuming most modal editors would drop you into edit mode for stepping through snippet tabstops, but in this case you probably want to be left in whatever mode you issued the code-action in. Will probably wait to see what it looks like after 3.18 rolls out before speculating further.

lionel- commented 2 weeks ago

That makes sense, happy to return more info such as cursor position.

Regarding editor modality, I guess the LSP clients in these editors should decide what to do in that case? In my experience (vscodevim, emacs evil) it's fine to do snippet transforms in normal state without dropping in insert state.