Files
nushell/tests/repl/test_commandline.rs
T
Ian ChamberlainandDarren Schroeder 0cd5474e7e Add commandline complete to invoke nushell completions (#17941)
<!--
Thank you for improving Nushell!
Please, read our contributing guide:
https://github.com/nushell/nushell/blob/main/CONTRIBUTING.md
-->
Closes #16951

There are a couple things that would be nice to have, but I think this
PR stands on its own without them. I can probably do some followup PRs
if they seem desirable:

- This line should probably not clone the world just to run this
command:
  ```rs
let completer = NuCompleter::new(Arc::new(engine_state.clone()),
Arc::new(stack.clone()));
  ```
It definitely is possible to make a `NuCompleterRef<'_>` type or
something to avoid this, but it would be a bigger refactor and this PR
already feels big, so I intend to do that as a followup.

- Color conversion back from `Style` -> `NuStyle` is incomplete; the
default `LS_COLORS` are `Fixed(_)` so they don't convert cleanly back to
strings yet. This means file completions won't roundtrip properly when
passed into a completer, but it should be easy to make that work.

Questions:
- ~Am I using spans appropriately here? Particularly converting between
reedline span + nushell span, I'm not sure what the "source" for these
completions really should be (or if it needs to be offset according to
the cursor position).~
	- After some more thorough testing, I think I got this right now?

- ~adding `impl TryIntoValue for SuggestionKind` could be a breaking
change, and I'm not sure it's the right semantics either... should I
mvoe this into a custom wrapper in the command itself or `nu-cli`
instead?~
- I moved this logic into `SemanticSuggestion::into_value`, avoids
locking in any specific JSON representation. Still not 100% sure about
my choices for the table format though

- bikeshed on the name for `--detailed(-d)` (mirroring `describe`)? This
felt more descriptive than `--long` (from `ps`, `ls`) to me, not sure if
there are other builtins with similar flags for examples

## Release notes summary - What our users need to know
<!--
This section will be included as part of our release notes. See the
contributing guide for more details.
Please include only details relevant for users here. Motivation and
technical details can be added above or below this section.

You may leave this section blank until your PR is finalized. Ask a core
team member if you need help filling this section.
-->
Add `commandline complete` command. This can be used to obtain the
suggestions that nushell itself would normally suggest. Some examples:
- `commandline complete` — return completions based on the current
commandline contents (i.e. what would be suggested when pressing `Tab`)
- `'./a' | commandline complete --type directory` — return directory
paths relative to the current directory that start with `a`
- `'%ls -' | commandline complete --detailed` — return all flags the
builtin `ls` command accepts, including their descriptions.

These completions can be used in custom command completions, for example
to wrap a builtin command with additional options, or to obtain a list
of suggested paths for further filtering or processing.

## Tasks after submitting
<!-- Remove any tasks which aren't relevant for your PR, or add your own
-->
- [ ] Update the
[documentation](https://github.com/nushell/nushell.github.io)

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2026-06-01 08:53:36 -05:00

238 lines
5.8 KiB
Rust

use rstest::rstest;
use crate::repl::tests::{TestResult, fail_test, run_test};
#[test]
fn commandline_test_get_empty() -> TestResult {
run_test("commandline", "")
}
#[test]
fn commandline_test_append() -> TestResult {
run_test(
"commandline edit --replace '0👩‍❤️‍👩2'\n\
commandline set-cursor 2\n\
commandline edit --append 'ab'\n\
print (commandline)\n\
commandline get-cursor",
"0👩‍❤️‍👩2ab\n\
2",
)
}
#[test]
fn commandline_test_insert() -> TestResult {
run_test(
"commandline edit --replace '0👩‍❤️‍👩2'\n\
commandline set-cursor 2\n\
commandline edit --insert 'ab'\n\
print (commandline)\n\
commandline get-cursor",
"0👩‍❤️‍👩ab2\n\
4",
)
}
#[test]
fn commandline_test_replace() -> TestResult {
run_test(
"commandline edit --replace '0👩‍❤️‍👩2'\n\
commandline edit --replace 'ab'\n\
print (commandline)\n\
commandline get-cursor",
"ab\n\
2",
)
}
#[test]
fn commandline_test_cursor() -> TestResult {
run_test(
"commandline edit --replace '0👩‍❤️‍👩2'\n\
commandline set-cursor 1\n\
commandline edit --insert 'x'\n\
commandline",
"0x👩‍❤️‍👩2",
)?;
run_test(
"commandline edit --replace '0👩‍❤️‍👩2'\n\
commandline set-cursor 2\n\
commandline edit --insert 'x'\n\
commandline",
"0👩‍❤️‍👩x2",
)
}
#[test]
fn commandline_test_cursor_show_pos_begin() -> TestResult {
run_test(
"commandline edit --replace '0👩‍❤️‍👩'\n\
commandline set-cursor 0\n\
commandline get-cursor",
"0",
)
}
#[test]
fn commandline_test_cursor_show_pos_end() -> TestResult {
run_test(
"commandline edit --replace '0👩‍❤️‍👩'\n\
commandline set-cursor 2\n\
commandline get-cursor",
"2",
)
}
#[test]
fn commandline_test_cursor_show_pos_mid() -> TestResult {
run_test(
"commandline edit --replace '0👩‍❤️‍👩2'\n\
commandline set-cursor 1\n\
commandline get-cursor",
"1",
)?;
run_test(
"commandline edit --replace '0👩‍❤️‍👩2'\n\
commandline set-cursor 2\n\
commandline get-cursor",
"2",
)
}
#[test]
fn commandline_test_cursor_too_small() -> TestResult {
run_test(
"commandline edit --replace '123456'\n\
commandline set-cursor -1\n\
commandline edit --insert '0'\n\
commandline",
"0123456",
)
}
#[test]
fn commandline_test_cursor_too_large() -> TestResult {
run_test(
"commandline edit --replace '123456'\n\
commandline set-cursor 10\n\
commandline edit --insert '0'\n\
commandline",
"1234560",
)
}
#[test]
fn commandline_test_cursor_invalid() -> TestResult {
fail_test(
"commandline edit --replace '123456'\n\
commandline set-cursor 'abc'",
"expected int",
)
}
#[test]
fn commandline_test_cursor_end() -> TestResult {
run_test(
"commandline edit --insert '🤔🤔'; commandline set-cursor --end; commandline get-cursor",
"2", // 2 graphemes
)
}
#[test]
fn commandline_test_cursor_type() -> TestResult {
run_test("commandline get-cursor | describe", "int")
}
#[test]
fn commandline_test_accepted_command() -> TestResult {
run_test(
"commandline edit --accept \"print accepted\"\n | commandline",
"print accepted",
)
}
#[test]
fn commandline_test_complete_input() -> TestResult {
run_test(
"def test-bar [] {}\n\
def test-baz [] {}\n\
'test-' | commandline complete | to nuon",
"[test-bar, test-baz]",
)
}
#[test]
fn commandline_test_complete_no_input() -> TestResult {
run_test(
"def test-bar [] {}\n\
def test-baz [] {}\n\
commandline edit --replace 'test-'\n\
commandline complete | to nuon",
"[test-bar, test-baz]",
)
}
#[test]
fn commandline_test_complete_flags() -> TestResult {
run_test(
"def cmd [ --flag: string, --switch(-s) ] {}\n\
'cmd -' | commandline complete | to nuon",
"[--flag, --help, --switch, -h, -s]",
)
}
#[test]
fn commandline_test_complete_reentrant() -> TestResult {
run_test(
"def recurse [a: string@[a, b, c]] {\n\
'recurse ' | commandline complete\n\
}\n\
def wrapped [arg:string@recurse] {}\n\
\n\
'wrapped ' | commandline complete | to nuon",
"[a, b, c]",
)
}
#[rstest]
#[case::cmd(
"test-",
r#"{value: test-cmd, span: {start: 0, end: 5}, description: "", kind: command, type: custom}"#
)]
#[case::int(
"test-cmd --int ",
r#"{value: "1", span: {start: 15, end: 15}, kind: value, type: int}"#
)]
#[case::string(
"test-cmd --string ",
"{value: a, span: {start: 18, end: 18}, kind: value, type: string}"
)]
fn commandline_test_complete_detailed(#[case] cmd: &str, #[case] expected: &str) -> TestResult {
run_test(
&format!(
"
def complete-int [] {{ [ 1 ] }}
def test-cmd [
--int: int@complete-int,
--string: string@[a],
] {{}}\n\
\n\
'{cmd}' | commandline complete --detailed | first | to nuon"
),
expected,
)
}
#[rstest]
#[case::invalid_input("123 | commandline complete", "command doesn't support int input")]
#[case::invalid_type(
"commandline complete --type foo",
r#"expected type "directory", "path", or "glob", but got "foo""#
)]
fn commandline_test_complete_invalid_input(
#[case] cmd: &str,
#[case] expected_err: &str,
) -> TestResult {
fail_test(cmd, expected_err)
}