-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Goto buffer #4756
Goto buffer #4756
Conversation
Inlined some functions, that had their origin in old implementation detail, and are now unnecessary.
Now able to go to specific buffers by pressing CTRL+<[1-9]>.
helix-term/src/commands.rs
Outdated
fn goto_first_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 0); | ||
} | ||
|
||
fn goto_second_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 1); | ||
} | ||
|
||
fn goto_third_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 2); | ||
} | ||
|
||
fn goto_fourth_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 3); | ||
} | ||
|
||
fn goto_fifth_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 4); | ||
} | ||
|
||
fn goto_sixth_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 5); | ||
} | ||
|
||
fn goto_seventh_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 6); | ||
} | ||
|
||
fn goto_eight_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 7); | ||
} | ||
|
||
fn goto_ninth_buffer(cx: &mut Context) { | ||
goto_buffer_by_idx_impl(cx.editor, 8); | ||
} |
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.
If this were a command, it should be implemented as a single command that uses cx.count()
for the buffer number. I don't think that adding another command is necessary though: you can add the bindings you're after with just the typable command like so:
[keys.normal]
"C-1" = ":b 1"
"C-2" = ":b 2"
# ...
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.
This is much cleaner. But the only location I could (relevantly) find "keys.normal" was in book/src/remapping.md
. Does that mean there shouldn't keybindings by default? If yes, I have to disagree, as this is such an essential feature. Maybe I am using something incorrectly?
How would I use cx.count()
, or better, how is count "provided"?.
Returns 1 if no explicit count was provided
I seem to always get 1, probably I'm not using it correctly.
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.
We have typable variants of the pipe
commands, so there's precedence. You can have a typable version, and another version that utilizes cx.count()
. As keybindings can be customizable, it's a bit ugly to work around that to support it in the default config.
fn goto_buffer(editor: &mut Editor, direction: Direction) { | ||
let current = view!(editor).doc; |
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.
I don't think it's necessary to change the body of the old goto_buffer
function - it has the same behavior before and after except that going to the previous buffer skips from the opposite direction
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.
I mainly replaced the body of the function because I saw nearly identical code in both branches of the match statement. That just triggered my refactoring sense, lol.
After looking into it, using an iterator the way it is used here may be hard to understand / overkill for such a simple task.
except that going to the previous buffer skips from the opposite direction
I'm honestly not certain what you mean by this. buffer-next
goes "to the right", and buffer-previous
goes "to the left". Have I missed something that the original implementation does?
Previously collected into a vector, and then indexed that.
can we add this only to command so that we can take advantage of cx.count()? Regarding typable commands, I think it would be better to support name and path too . #4771 |
@QiBaobin I haven't yet figured out how Regarding using the buffer name instead of the buffer ID (I assume you mean index, because the ID is different AFAIK). I think that doesn't really work, since two buffers can be named the same thing. For example, I was testing with multiple I appreciate any suggestions and ideas relevant to this pull request :) |
@Elias-Graf Good point. I recommend use name or path as I want to bind a key to do something like scatchpad, will would launch one specific buffer like scratch or terminal very quick. Regarding cx.count(), you can get it in a function defined in src/commands.rs. It would work like that: you type number first then the key you bind to that function for example C-b. So that 1 C-b would take you to the first buffer, 2 C-b would be the second etc. |
@QiBaobin ohhh, thanks for telling me that. I bet it would have been ages until I figured that out. Sadly, this isn't really in-line with browsers / file explorers / terminals / other editors (maybe also see: #475 (comment)), which can be frustrating to use because the functionality is essentially the same :/ |
@Elias-Graf Then we only need add a TypableCommand to accept the argument, we don't need touch commands.rs, just bind keys in the default config or our own config file to use them like However, I still think it's better to support name and path too, can you add them so that I can close my PR? If you don't have time, I can copy your logic to my PR. |
@QiBaobin I will place them into the default config once I find it :P. I'll see about the buffer name / path once I'm done with the things mentioned in the review. I would also appreciate the feedback of members / maintainers on this specifically (maybe @kirawi or @the-mikedavis). I personally don't see the use case (yet? :D), but I'm happy to add it if it's generally desired. Some questions that come into mind:
#4393 might be relevant depending on answers to those questions (and how the renaming is implemented). |
@QiBaobin Would be so kind and tell me where to do this:
I'm only able to find |
After looking at the code, it turned out we don't have any global config file like languages.toml yet, maybe we can create one so that we don't need write functions for every key. |
Poking my head in 'cause I was about to try to implement this anyway... iirc other editors suffix duplicate buffer names with a number when this happens, so if you have a buffer |
Closing this as stale. The vuffer picker and "gui like" order tabs workflow is not really a priority for us so I don't see us going forward with this. Thank you for contributing! |
Implemented a command to go to a specific buffer by index.
Command:
buffer-goto <i>
Alias:
b <i>