-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use single quotes around completions with space/quote/doublequotes
- Loading branch information
Showing
5 changed files
with
142 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
use crate::nu_util; | ||
use reedline::{Completer, Suggestion}; | ||
use std::path::Path; | ||
|
||
pub fn get_suggestions(nu_file_data: &[u8], pwd: &Path, arg_str: &str) -> Vec<String> { | ||
let mut completer = nu_util::extern_completer(pwd, nu_file_data); | ||
let suggestions = completer.complete(arg_str, arg_str.len()); | ||
let suggestion_strings: Vec<String> = suggestions.iter().map(get_suggestion_string).collect(); | ||
suggestion_strings | ||
} | ||
|
||
fn get_suggestion_string(suggestion: &Suggestion) -> String { | ||
let arg = suggestion.value.clone().to_string(); | ||
if arg.len() > 1 && arg.starts_with('`') && arg.ends_with('`') { | ||
// replace the start and end backticks with single quotes | ||
// also replace all single quotes with double single quote | ||
// e.g. completion of `git add ` with a file with a space in it | ||
let replaced_single_quotes = &arg[1..arg.len() - 1].replace('\'', "''"); | ||
return format!("'{replaced_single_quotes}'",); | ||
} | ||
|
||
// for example, `git ch` should be `checkout`, not `git checkout` | ||
if suggestion.span.start == 0 { | ||
return arg.split(' ').last().unwrap().to_string(); | ||
} | ||
|
||
// if the arg contains a space, double quote, or single quote, wrap it in single quotes | ||
if !arg.is_empty() | ||
&& !arg.starts_with('\'') | ||
&& (arg.contains(' ') || arg.contains('\"') || arg.contains('\'')) | ||
{ | ||
let replaced_arg = arg.replace('\'', "''"); | ||
return format!("'{replaced_arg}'"); | ||
} | ||
|
||
arg.to_string() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::DEFAULT_CONFIG_DATA; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_get_suggestions() { | ||
let nu_file_data: &[u8] = DEFAULT_CONFIG_DATA; | ||
let pwd = std::env::current_dir().unwrap(); | ||
let arg_str = "git checkou"; | ||
let expected = vec!["checkout".to_string()]; | ||
let suggestions = get_suggestions(nu_file_data, &pwd, arg_str); | ||
assert_eq!(suggestions, expected); | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
|
@@ -35,6 +35,22 @@ impl TestEnv { | |
|
||
let (temp_dir, profile_path) = | ||
create_working_dir(profile_prefix_data).expect("create successful"); | ||
|
||
let package_json_path = temp_dir.path().join("package.json"); | ||
let package_json_str = r#" | ||
{ | ||
"name": "my-app", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"script1": "node index.js", | ||
"script2": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} | ||
"#; | ||
create_package_json(&package_json_path, package_json_str) | ||
.expect("Able to create package json"); | ||
|
||
TestEnv { | ||
shell: shell.to_string(), | ||
temp_dir, | ||
|
@@ -74,7 +90,7 @@ impl TestEnv { | |
} | ||
|
||
pub fn run_with_profile(&self, command: &str) -> Result<Output, io::Error> { | ||
let root = self.temp_dir.path(); | ||
let root: &Path = self.temp_dir.path(); | ||
let file_contents = format!(". {}\n{command}", self.profile_path.to_str().unwrap()); | ||
let file_path = root.join("file.ps1"); | ||
fs::File::create(&file_path)?.write_all(file_contents.as_bytes())?; | ||
|
@@ -97,6 +113,12 @@ impl TestEnv { | |
|
||
Ok(output) | ||
} | ||
|
||
pub fn create_package_json(self, package_json_contents: &str) -> Result<TestEnv, io::Error> { | ||
let package_json_path = self.temp_dir.path().join("package.json"); | ||
create_package_json(&package_json_path, package_json_contents)?; | ||
Ok(self) | ||
} | ||
} | ||
|
||
fn prepend_to_path_var(path: &Path) -> OsString { | ||
|
@@ -145,30 +167,18 @@ fn create_working_dir(profile_prefix_data: Vec<&str>) -> Result<(TempDir, PathBu | |
run_git(&["checkout", "-b", "testbranch23"]); | ||
run_git(&["remote", "add", "origin", "[email protected]"]); | ||
|
||
let package_json_path = root.join("package.json"); | ||
create_package_json(&package_json_path)?; | ||
|
||
profile_path | ||
}; | ||
|
||
Ok((temp_dir, profile_path)) | ||
} | ||
|
||
fn create_package_json(package_json_path: &Path) -> Result<(), io::Error> { | ||
let package_json_str = r#" | ||
{ | ||
"name": "my-app", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"script1": "node index.js", | ||
"script2": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} | ||
"#; | ||
|
||
fn create_package_json( | ||
package_json_path: &Path, | ||
package_json_contents: &str, | ||
) -> Result<(), io::Error> { | ||
let mut file = fs::File::create(package_json_path)?; | ||
file.write_all(package_json_str.as_bytes())?; | ||
file.write_all(package_json_contents.as_bytes())?; | ||
|
||
Ok(()) | ||
} | ||
|
This file contains 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