Repository navigation
feat: add single-line command and field editing #3
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| -- Run with Neovim 0.12.1 from the repository root or pass the fixture path. | ||
| assert(vim.version().major == 0 and vim.version().minor == 12 and vim.version().patch == 1) | ||
| local path = arg[1] or 'tests/command_line.json' | ||
| local cases = vim.json.decode(table.concat(vim.fn.readfile(path), '\n')) | ||
| local observed | ||
| local function observe() | ||
| observed = { text = vim.fn.getcmdline(), caret = vim.fn.getcmdpos() - 1, kind = vim.fn.getcmdtype() } | ||
| return '' | ||
| end | ||
| vim.keymap.set('c', '<F12>', observe, { expr = true }) | ||
|
|
||
| local prefixes = { Ex = ':', SearchForward = '/', SearchBackward = '?', Field = '@' } | ||
| local keys = { | ||
| Left = '<Left>', Backspace = '<BS>', Delete = '<Del>', Enter = '<CR>', | ||
| Escape = '<Esc>', ['Ctrl-u'] = '<C-U>', ['Ctrl-w'] = '<C-W>', | ||
| } | ||
| local function feed(s, flags) | ||
| vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(s, true, false, true), flags, false) | ||
| end | ||
| for _, case in ipairs(cases) do | ||
| observed = nil | ||
| local input, paste = '', nil | ||
| for _, k in ipairs(case.keys) do | ||
| if k:sub(1, 6) == 'Paste:' then | ||
| paste = k:sub(7) | ||
| else | ||
| input = input .. (keys[k] or k) | ||
| end | ||
| end | ||
| if case.kind == 'Field' then | ||
| feed(input .. (case.text and '<F12><CR>' or ''), 't') | ||
| local accepted = vim.fn.input({ prompt = 'Field: ', default = case.initial or '', cancelreturn = 'cancelled' }) | ||
| if not case.text then | ||
| assert(accepted == case.outcome, vim.inspect(case)) | ||
| end | ||
| elseif paste then | ||
| vim.keymap.set('c', '<F11>', function() | ||
| vim.schedule(function() | ||
| vim.api.nvim_paste(paste, true, -1) | ||
| feed('<F12><C-C>', 't') | ||
| end) | ||
| return '' | ||
| end, { expr = true }) | ||
| -- input() blocks on command-line input, so scheduled paste runs in that mode. | ||
| feed(input .. '<F11>', 't') | ||
| vim.fn.input('Paste: ') | ||
| elseif case.text then | ||
| feed(prefixes[case.kind] .. input .. '<F12><C-C>', 'xt') | ||
| else | ||
| feed(prefixes[case.kind] .. input, 'xt') | ||
| assert(vim.fn.getcmdtype() == '', vim.inspect(case)) | ||
| end | ||
| if case.text then | ||
| assert(vim.deep_equal(observed, { text = case.text, caret = case.caret, kind = paste and '@' or prefixes[case.kind] }), | ||
| vim.inspect({ case = case, observed = observed })) | ||
| end | ||
| end | ||
| print(('%d command-line cases match Neovim'):format(#cases)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| //! Edit a one-line prompt with Neovim's Command-line bindings. | ||
|
|
||
| use crate::{ByteOffset, Key, TextSource}; | ||
| use unicode_segmentation::UnicodeSegmentation; | ||
|
|
||
| /// The purpose of a command-line draft. | ||
| #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] | ||
| pub enum PromptKind { | ||
| /// An Ex command opened with `:`. | ||
| #[default] | ||
| Ex, | ||
| /// A forward search opened with `/`. | ||
| SearchForward, | ||
| /// A backward search opened with `?`. | ||
| SearchBackward, | ||
| /// A general input field. Backspace on an empty draft keeps it open. | ||
| Field, | ||
| } | ||
|
|
||
| /// Direction of a history or completion request. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum PromptDirection { | ||
| /// Older history or the previous completion. | ||
| Previous, | ||
| /// Newer history or the next completion. | ||
| Next, | ||
| } | ||
|
|
||
| /// The result of dispatching a key to a prompt. | ||
| #[derive(Debug, PartialEq, Eq)] | ||
| pub enum PromptOutcome { | ||
| /// The text changed. Hosts can refresh dependent previews. | ||
| Edited, | ||
| /// Only the insertion point changed. | ||
| Moved, | ||
| /// The key was handled without changing the draft. | ||
| Consumed, | ||
| /// The host should navigate its history using the current draft and caret. | ||
| History(PromptDirection), | ||
| /// The host should navigate its completion candidates. | ||
| Complete(PromptDirection), | ||
| /// The user accepted this exact draft, which may be empty. | ||
| Accepted(String), | ||
| /// The user cancelled the prompt. | ||
| Cancelled, | ||
| /// The host may handle the key. | ||
| Unhandled, | ||
| } | ||
|
|
||
| /// A draft or insertion point violates the single-line editor's invariants. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub struct InvalidPromptDraft; | ||
|
|
||
| impl std::fmt::Display for InvalidPromptDraft { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.write_str("expected one line and a caret on a UTF-8 grapheme boundary") | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for InvalidPromptDraft {} | ||
|
|
||
| /// An owned, single-line draft with a UTF-8 byte caret. | ||
| /// | ||
| /// The host renders the prompt prefix separately and handles history, completion, | ||
| /// acceptance and cancellation. Use a new instance when opening another prompt. | ||
| #[derive(Clone, Default)] | ||
| pub struct CommandLine { | ||
| kind: PromptKind, | ||
| text: String, | ||
| caret: usize, | ||
| closed: bool, | ||
| skip_operand: bool, | ||
| } | ||
|
|
||
| impl std::fmt::Debug for CommandLine { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("CommandLine") | ||
| .field("kind", &self.kind) | ||
| .finish_non_exhaustive() | ||
| } | ||
| } | ||
|
|
||
| impl CommandLine { | ||
| /// Create an empty draft for the given prompt. | ||
| pub fn empty(kind: PromptKind) -> Self { | ||
| Self { | ||
| kind, | ||
| ..Self::default() | ||
| } | ||
| } | ||
|
|
||
| /// Open a draft with the caret at the end. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if the text contains a line break. | ||
| pub fn new(kind: PromptKind, text: impl Into<String>) -> Result<Self, InvalidPromptDraft> { | ||
| let mut line = Self::empty(kind); | ||
| let text = text.into(); | ||
| let end = ByteOffset(text.len()); | ||
| line.replace(text, end)?; | ||
| Ok(line) | ||
| } | ||
|
|
||
| /// Return the editable text, excluding any prefix or label. | ||
| pub fn text(&self) -> &str { | ||
| &self.text | ||
| } | ||
|
|
||
| /// Return the zero-based UTF-8 byte offset of the insertion point. | ||
| pub fn caret(&self) -> ByteOffset { | ||
| ByteOffset(self.caret) | ||
| } | ||
|
|
||
| /// Replace the active draft, for example after recalling history. | ||
| /// | ||
| /// For asynchronous replies the caller should check that the same prompt, | ||
| /// draft and caret are still active before calling this method. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Rejects closed prompts, line breaks and caret positions inside graphemes. | ||
| pub fn replace(&mut self, text: String, caret: ByteOffset) -> Result<(), InvalidPromptDraft> { | ||
| if self.closed | ||
| || text.contains(['\r', '\n']) | ||
| || !(caret.0 == text.len() | ||
| || text.grapheme_indices(true).any(|(byte, _)| byte == caret.0)) | ||
| { | ||
| return Err(InvalidPromptDraft); | ||
| } | ||
| self.text = text; | ||
| self.caret = caret.0; | ||
| self.skip_operand = false; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Dispatch one normalized key. A text batch inserts at most its first line. | ||
| /// | ||
| /// Accept and cancel close the prompt. Further keys are unhandled. | ||
| pub fn handle(&mut self, key: Key) -> PromptOutcome { | ||
| use PromptOutcome::{Accepted, Cancelled, Complete, Consumed, History, Unhandled}; | ||
|
|
||
| if self.closed { | ||
| return Unhandled; | ||
| } | ||
| if std::mem::take(&mut self.skip_operand) { | ||
| return Consumed; | ||
| } | ||
| let previous = self.text.as_str().prev_grapheme(self.caret); | ||
| let next = self.text.as_str().next_grapheme(self.caret); | ||
| match key { | ||
| Key::Char(ch) if !ch.is_control() => { | ||
| self.edit(self.caret, self.caret, ch.encode_utf8(&mut [0; 4])) | ||
| } | ||
| Key::Text(text) => { | ||
| let first = text.split(['\r', '\n']).next().unwrap_or_default(); | ||
| self.edit(self.caret, self.caret, first) | ||
| } | ||
| Key::Left => self.move_to(previous), | ||
| Key::Right => self.move_to(next), | ||
| Key::Home | Key::Ctrl('b') => self.move_to(0), | ||
| Key::End | Key::Ctrl('e') => self.move_to(self.text.len()), | ||
| Key::Backspace | Key::Ctrl('h') | ||
| if self.text.is_empty() && self.kind != PromptKind::Field => | ||
| { | ||
| self.closed = true; | ||
| Cancelled | ||
| } | ||
| Key::Backspace | Key::Ctrl('h') => self.edit(previous, self.caret, ""), | ||
| Key::Delete if self.caret == self.text.len() => self.edit(previous, self.caret, ""), | ||
| Key::Delete => self.edit(self.caret, next, ""), | ||
| Key::Ctrl('u') => self.edit(0, self.caret, ""), | ||
| Key::Ctrl('w') => self.edit(self.previous_word(), self.caret, ""), | ||
| Key::Ctrl('v' | 'q' | 'r') => { | ||
| self.skip_operand = true; | ||
| Consumed | ||
| } | ||
| Key::Enter | Key::Ctrl('j' | 'm') => { | ||
| self.closed = true; | ||
| Accepted(self.text.clone()) | ||
| } | ||
| Key::Escape | Key::Ctrl('c' | '[') => { | ||
| self.closed = true; | ||
| self.caret = 0; | ||
| self.text.clear(); | ||
| Cancelled | ||
| } | ||
| Key::Up => History(PromptDirection::Previous), | ||
| Key::Down => History(PromptDirection::Next), | ||
| Key::Tab => Complete(PromptDirection::Next), | ||
| Key::Named(name) if name == "BackTab" => Complete(PromptDirection::Previous), | ||
| _ => Unhandled, | ||
| } | ||
| } | ||
|
|
||
| fn move_to(&mut self, caret: usize) -> PromptOutcome { | ||
| if self.caret == caret { | ||
| return PromptOutcome::Consumed; | ||
| } | ||
| self.caret = caret; | ||
| PromptOutcome::Moved | ||
| } | ||
|
|
||
| fn edit(&mut self, start: usize, end: usize, replacement: &str) -> PromptOutcome { | ||
| if start == end && replacement.is_empty() { | ||
| return PromptOutcome::Consumed; | ||
| } | ||
| self.text.replace_range(start..end, replacement); | ||
| let end = start + replacement.len(); | ||
| // Inserting or removing text can join clusters on either side of the edit. | ||
| self.caret = self | ||
| .text | ||
| .grapheme_indices(true) | ||
| .find_map(|(byte, _)| (byte >= end).then_some(byte)) | ||
| .unwrap_or(self.text.len()); | ||
| PromptOutcome::Edited | ||
| } | ||
|
|
||
| fn previous_word(&self) -> usize { | ||
| let mut graphemes = self.text[..self.caret].grapheme_indices(true).rev(); | ||
| let class = |grapheme: &str| { | ||
| let first = grapheme.chars().next().expect("nonempty grapheme"); | ||
| if first.is_whitespace() { | ||
| 0 | ||
| } else if first.is_alphanumeric() || first == '_' { | ||
| 1 | ||
| } else { | ||
| 2 | ||
| } | ||
| }; | ||
| let Some((start, first)) = graphemes.find(|(_, g)| class(g) != 0) else { | ||
| return 0; | ||
| }; | ||
| graphemes | ||
| .take_while(|(_, g)| class(g) == class(first)) | ||
| .last() | ||
| .map_or(start, |(byte, _)| byte) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [ | ||
| {"kind":"Ex","keys":["i","w","d","d","u"],"text":"iwddu","caret":5}, | ||
| {"kind":"Ex","keys":["a","b","界","Left","X"],"text":"abX界","caret":3}, | ||
| {"kind":"Ex","keys":["a","👩","","👩","","👦","Backspace"],"text":"a","caret":1}, | ||
| {"kind":"Ex","keys":["o","n","e"," ","t","w","o","!","Left","Ctrl-w"],"text":"one !","caret":4}, | ||
| {"kind":"Ex","keys":["o","n","e"," ","t","w","o","!","Left","Ctrl-u"],"text":"!","caret":0}, | ||
| {"kind":"Ex","keys":["a","b","c","Delete"],"text":"ab","caret":2}, | ||
| {"kind":"Ex","keys":["Backspace"],"outcome":"cancelled"}, | ||
| {"kind":"SearchForward","keys":["Backspace"],"outcome":"cancelled"}, | ||
| {"kind":"SearchBackward","keys":["d","w","a"],"text":"dwa","caret":3}, | ||
| {"kind":"Field","keys":["Backspace","Z"],"text":"Z","caret":1}, | ||
| {"kind":"Field","initial":"XY","keys":["z","Enter"],"outcome":"XYz"}, | ||
| {"kind":"Field","keys":["Enter"],"outcome":""}, | ||
| {"kind":"Field","initial":"XY","keys":["Escape"],"outcome":"cancelled"}, | ||
| {"kind":"Ex","keys":["f","o","o","Paste:A\r\nB"],"text":"fooA","caret":4} | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For an empty Ex or search prompt,
Key::Deletereachesedit(0, 0, ""), returnsConsumed, and leaves the prompt open. Neovim routes Delete through the same empty-command cancellation logic as Backspace; seecommand_line_erase_chars. Consequently, pressing Delete to dismiss an empty prompt fails, and subsequent typing is still captured as command/search input. Include Delete in the empty-draft cancellation guard while preserving the existing behavior forPromptKind::Field.AGENTS.md reference: AGENTS.md:L5-L5
Useful? React with 👍 / 👎.