Skip to content

Commit

Permalink
Start handling completion options (#28)
Browse files Browse the repository at this point in the history
completion options
  • Loading branch information
reubeno authored May 22, 2024
1 parent 9cbd0d6 commit 5b8b96c
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 172 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,22 @@ jobs:
- name: "Upload test results"
uses: actions/upload-artifact@v4
if: always()
with:
name: test-reports
path: test-results-*.xml

- name: "Generate code coverage report"
uses: clearlyip/code-coverage-report-action@v5
if: always()
id: "code_coverage_report"
with:
artifact_download_workflow_names: "CI"
filename: "codecov.xml"

- name: "Upload code coverage report"
uses: actions/upload-artifact@v4
if: always()
with:
name: codecov-reports
path: code-coverage-results.md
Expand Down
45 changes: 13 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ assert_cmd = "2.0.14"
assert_fs = "1.1.1"
colored = "2.1.0"
descape = "1.1.2"
diff = "0.1.13"
dir-cmp = "0.1.0"
expectrl = "0.7.1"
glob = "0.3.1"
indent = "0.1.1"
junit-report = "0.8.3"
pathdiff = "0.2.1"
prettydiff = { version = "0.7.0", default-features = false }
regex = "1.10.4"
serde = { version = "1.0.202", features = ["derive"] }
serde_yaml = "0.9.34"
Expand Down
6 changes: 5 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ fn main() {
let targets = match event {
TraceEvent::Arithmetic => vec!["parser::arithmetic"],
TraceEvent::Commands => vec!["commands"],
TraceEvent::Complete => vec!["shell::completion", "shell::builtins::complete"],
TraceEvent::Complete => vec![
"completion",
"shell::completion",
"shell::builtins::complete",
],
TraceEvent::Expand => vec![],
TraceEvent::Parse => vec!["parse"],
TraceEvent::Pattern => vec!["shell::pattern"],
Expand Down
16 changes: 7 additions & 9 deletions cli/tests/cases/builtins/compgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@ cases:
alias myalias="ls"
alias myalias2="echo hi"
compgen -A alias myalias
compgen -A alias myalias | sort
- name: "compgen -A builtin"
stdin: |
compgen -A builtin cd
compgen -A builtin cd | sort
- name: "compgen -A directory"
known_failure: true
stdin: |
touch somefile
mkdir somedir
mkdir somedir2
compgen -A directory some
compgen -A directory some | sort
- name: "compgen -A file"
known_failure: true
stdin: |
touch somefile
mkdir somedir
mkdir somedir2
compgen -A file some
compgen -A file some | sort
- name: "compgen -A function"
stdin: |
Expand All @@ -39,15 +37,15 @@ cases:
echo hello
}
compgen -A function myfunc
compgen -A function myfunc | sort
- name: "compgen -A keyword"
stdin: |
compgen -A keyword esa
compgen -A keyword esa | sort
- name: "compgen -A variable"
stdin: |
declare myvar=10
declare myvar2=11
compgen -A variable myvar
compgen -A variable myvar | sort
12 changes: 6 additions & 6 deletions cli/tests/cases/compound_cmds/while.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ cases:
stdin: |
while false; do echo 'In loop'; done
# - name: "break in while loop"
# stdin: |
# while true; do
# echo 'In loop'
# break
# done
- name: "break in while loop"
stdin: |
while true; do
echo 'In loop'
break
done
- name: "Arithmetic in while loop"
stdin: |
Expand Down
10 changes: 5 additions & 5 deletions cli/tests/cases/redirection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ cases:
stdin: |
ls -d . non-existent-dir &>/dev/null
# - name: "Process substitution: input + output"
# known_failure: true # Issue #151
# stdin: |
# shopt -u -o posix
# cp <(echo hi) >(cat)
- name: "Process substitution: input + output"
known_failure: true # Issue #151
stdin: |
shopt -u -o posix
cp <(echo hi) >(cat)
41 changes: 25 additions & 16 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ impl TestCaseResult {
" {}",
"------ Test: stdout [cleaned]------------------------".cyan()
)?;

writeln!(
writer,
"{}",
Expand All @@ -578,14 +579,7 @@ impl TestCaseResult {
"------ Oracle <> Test: stdout ---------------------------------".cyan()
)?;

writeln!(
writer,
"{}",
indent::indent_all_by(
8,
prettydiff::diff_lines(o.as_str(), t.as_str()).format()
)
)?;
write_diff(&mut writer, 8, o.as_str(), t.as_str())?;

writeln!(
writer,
Expand Down Expand Up @@ -613,14 +607,7 @@ impl TestCaseResult {
"------ Oracle <> Test: stderr ---------------------------------".cyan()
)?;

writeln!(
writer,
"{}",
indent::indent_all_by(
8,
prettydiff::diff_lines(o.as_str(), t.as_str()).format()
)
)?;
write_diff(&mut writer, 8, o.as_str(), t.as_str())?;

writeln!(
writer,
Expand Down Expand Up @@ -1275,6 +1262,28 @@ fn make_expectrl_output_readable<S: AsRef<str>>(output: S) -> String {
strip_ansi_escapes::strip_str(unescaped)
}

fn write_diff(
writer: &mut impl std::io::Write,
indent: usize,
left: &str,
right: &str,
) -> Result<()> {
let indent_str = " ".repeat(indent);

let diff = diff::lines(left, right);
for d in diff {
let formatted = match d {
diff::Result::Left(l) => std::format!("{indent_str}- {l}").red(),
diff::Result::Both(l, _) => std::format!("{indent_str} {l}").bright_black(),
diff::Result::Right(r) => std::format!("{indent_str}+ {r}").green(),
};

writeln!(writer, "{formatted}")?;
}

Ok(())
}

fn main() {
let unparsed_args: Vec<_> = std::env::args().collect();
let options = TestOptions::parse_from(unparsed_args);
Expand Down
30 changes: 28 additions & 2 deletions interactive-shell/src/interactive_shell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use rustyline::validate::ValidationResult;
use std::{borrow::Cow, io::Write, path::PathBuf};
use std::{
borrow::Cow,
io::Write,
path::{Path, PathBuf},
};

type Editor = rustyline::Editor<EditorHelper, rustyline::history::FileHistory>;

Expand Down Expand Up @@ -228,11 +232,33 @@ impl EditorHelper {
}
};

let completions = result.unwrap_or_else(|_| shell::Completions {
let mut completions = result.unwrap_or_else(|_| shell::Completions {
start: pos,
candidates: vec![],
options: shell::CandidateProcessingOptions::default(),
});

// TODO: implement completion postprocessing
let completing_end_of_line = pos == line.len();
if completions.options.treat_as_filenames {
for candidate in &mut completions.candidates {
// Check if it's a directory.
if !candidate.ends_with('/') && Path::new(candidate).is_dir() {
candidate.push('/');
}
}
}
if completions.options.no_autoquote_filenames {
tracing::debug!(target: "completion", "don't autoquote filenames");
}
if completing_end_of_line && !completions.options.no_trailing_space_at_end_of_line {
for candidate in &mut completions.candidates {
if !completions.options.treat_as_filenames || !candidate.ends_with('/') {
candidate.push(' ');
}
}
}

let candidates = completions
.candidates
.into_iter()
Expand Down
Loading

0 comments on commit 5b8b96c

Please sign in to comment.