mattwparas / helix

A post-modern modal text editor.
https://helix-editor.com
Mozilla Public License 2.0
34 stars 0 forks source link

Add editor-switch-action! command to be able to switch to an existing buffer #2

Closed voidcontext closed 4 months ago

voidcontext commented 4 months ago

Hi @mattwparas , what do you think about having a function to be able to switch to a buffer by replacing one? This would solve my question in https://github.com/helix-editor/helix/discussions/11102.

mattwparas commented 4 months ago

https://github.com/mattwparas/helix-config/blob/816a1df004c82f214c0998dc42493978abee449a/cogs/labelled-buffers.scm#L96

Is this what you're looking for?

voidcontext commented 4 months ago

https://github.com/mattwparas/helix-config/blob/816a1df004c82f214c0998dc42493978abee449a/cogs/labelled-buffers.scm#L96

Is this what you're looking for?

Almost: editor-switch! is implemented as

fn cx_switch(cx: &mut Context, doc_id: DocumentId) {
    cx.editor.switch(doc_id, Action::VerticalSplit)
}

But what I need is Action::Replace instead of Action::VerticalSplit. I am not sure what your plan is regarding the API, the action could be a parameter, or there could be named functions for each action.

Update: the problem with open is that it resets the cursor, while switch just brings back the buffer into the view preserving the last place of the cursor.

mattwparas commented 4 months ago

So I think id probably want to do one function with a parameter for the action kind. The fact that I chose vertical split was arbitrary in the first place for the most part.

That would be breaking given the current API, so maybe separate functions is fine. When I get back to my computer I can take a closer look

Either way this is functionality that I'd like to have in the API somehow

voidcontext commented 4 months ago

So I think id probably want to do one function with a parameter for the action kind. The fact that I chose vertical split was arbitrary in the first place for the most part.

That would be breaking given the current API, so maybe separate functions is fine. When I get back to my computer I can take a closer look

Either way this is functionality that I'd like to have in the API somehow

I think I almost got this, instead of editor-switch-replace! I started implementing editor-switch-action! which takes 2 parameters the doc-id and the action (pushed the changes into this PR). But I got stuck, as I am not sure how I can create an Action instance from scheme, passing a string obviously doesn't work:

(editor-switch-action! doc-id "Replace")
Screenshot 2024-07-06 at 16 00 44

Update: oh, actually, I think I found the example I was missing: https://github.com/mattwparas/steel/blob/master/examples/register_types.rs#L67-L68, I'll try this.

Update 2: d611f7f seems to be working as expected:

(editor-switch-action! doc-id (Action::Replace))