feat: add support for fish-like abbreviations#1060
Conversation
* feat(editor): detect if cursor is in unclosed string
|
@fdncred any feedback on this? the implementation is a little crude, it could be modified to more closely resemble how keybindings are defined |
|
Sorry, it's hard to fine people to review reedline PRs. It would be good to have an example with it too. |
recording.mp4no worries. i'm assuming you mean an example of it in action and i've included one here. just showing that it works anywhere in the buffer and doesn't expand when it appears as a subword. to produce this, i modified the basic example like so: diff --git a/examples/basic.rs b/examples/basic.rs
index 825d954..dc0c680 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -4,11 +4,13 @@
// You can browse the local (non persistent) history using Up/Down or Ctrl n/p.
use reedline::{DefaultPrompt, Reedline, Signal};
-use std::io;
+use std::{collections::HashMap, io};
fn main() -> io::Result<()> {
+ let mut abbrevs = HashMap::new();
+ abbrevs.insert(String::from("ll"), String::from("ls -l"));
// Create a new Reedline engine with a local History that is not synchronized to a file.
- let mut line_editor = Reedline::create();
+ let mut line_editor = Reedline::create().with_abbreviations(abbrevs);
let prompt = DefaultPrompt::default();
loop { |
|
I mean there should be a special example in the reedline examples folder. Let's not modify an existing one please. you can copy it and change it but let's keep the examples the way they are except for your new one. What is the trigger to expand the abbreviation? Is it just the space after |
|
can do. yes the trigger is space after |
|
I think it's cool. Let's see what copilot thinks of your changes. |
There was a problem hiding this comment.
Pull request overview
This PR adds first-pass “fish-like abbreviations” support to the Reedline engine, allowing configured abbreviation keys (e.g. ll) to expand into longer commands during common editing/submit flows.
Changes:
- Add an
abbreviations: HashMap<String, String>store toReedlineplus awith_abbreviationsbuilder. - Attempt abbreviation expansion on
Enter/Submit/SubmitOrNewline, and after anEditthat inserts a space. - Add a string-literal detection helper and an example demonstrating abbreviation setup.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
src/engine.rs |
Stores abbreviations, exposes builder, and triggers expansion during submit/space insertion events. |
src/core_editor/editor.rs |
Adds is_inside_string_literal helper used to suppress expansion inside quotes. |
examples/abbreviations.rs |
New example showing how to configure and use abbreviations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thanks! Are you interested in adding the ability to use this functionality in nushell? |
|
Yeah I can work on that! Thanks! |
|
Make sure to do |
| } | ||
|
|
||
| /// Check if `position` (a byte offset) is inside an unclosed string literal. | ||
| pub fn is_inside_string_literal(&self, position: usize) -> bool { |
There was a problem hiding this comment.
Hi @casedami, I'm afraid reedline isn't the right place for such checking. I mean different downstream shells might have different syntaxes of string literal. For example, in nushell, there's another form of string literal in backticks.
There was a problem hiding this comment.
Besides, this task is actually much more difficult than it seems because of string interpolation
There was a problem hiding this comment.
That's a good point, hadn't considered downstream shell usage. If you're more comfortable removing this func and leaving this as a problem to tackle in another way down the line then I can submit a PR that removes this. Would also apply to #1073
There was a problem hiding this comment.
@casedami Sorry, I didn't go over the linked issues in detail, according to my limited knowledge, the "ideal" way for such matter would be a protocol to query downstream apps for the string literal checking result, and they might be able to reuse the cached parsing results from the highlighting task.
There was a problem hiding this comment.
I mean, this func should be removed one way or another. I just don't know what impact it will land on the abbreviation feature, if just more false positives, acceptable IMHO.
There was a problem hiding this comment.
Basically the change to the abbreviation feature would be that they now expand even when you're typing inside a string literal, so overall it's fine to remove it I would say
There was a problem hiding this comment.
Basically the change to the abbreviation feature would be that they now expand even when you're typing inside a string literal, so overall it's fine to remove it I would say
Sure, let's do this please
Following up on my comment in #556 i figured i would go ahead and get something working. This would be the initial step to getting abbreviations in nushell and would require
a parsing implementationabbreviations to be stored in nushell config and then passed to reedline. Open to any comments or opinions as far as implementation goes but this is a feature that i would personally love to have in nushell.I've essentially added a check during the following events that will check if the word before the cursor is an abbreviation and expand it if true:
SubmitandSubmitOrNewlineEnterEditif the first command in the event is to insert a spaceAs for adding this to nushell, here's a simple example of how this might look in the nushell config:
Note, that the
is_inside_string_literalfunction could potentially be used for a bug fix for!$history expansion uses naive last token, rather than the last argument to previous command nushell#6971As an aside, here's how im currently getting abbreviations with this hacky solution