1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

fix(lsp): better handling of folders in registry completions (#13250)

This commit is contained in:
Kitson Kelly 2022-01-02 16:27:19 +11:00 committed by Bartek Iwańczuk
parent 74ed6875e8
commit b6ee2ce933
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
2 changed files with 40 additions and 4 deletions

View file

@ -733,7 +733,7 @@ impl Compiler {
let prefix = k.prefix.clone().unwrap_or_default(); let prefix = k.prefix.clone().unwrap_or_default();
let suffix = k.suffix.clone().unwrap_or_default(); let suffix = k.suffix.clone().unwrap_or_default();
for segment in v { for segment in v {
if self.validate { if !segment.is_empty() && self.validate {
if let Some(re) = &self.matches[i] { if let Some(re) = &self.matches[i] {
if !re.is_match(segment) { if !re.is_match(segment) {
return Err(anyhow!( return Err(anyhow!(
@ -911,6 +911,33 @@ mod tests {
assert_eq!(actual, "/x/y@v1.0.0/z/example.ts".to_string()); assert_eq!(actual, "/x/y@v1.0.0/z/example.ts".to_string());
} }
#[test]
fn test_compiler_ends_with_sep() {
let tokens = parse("/x/:a@:b/:c*", None).expect("could not parse");
let mut params = HashMap::<StringOrNumber, StringOrVec>::new();
params.insert(
StringOrNumber::String("a".to_string()),
StringOrVec::String("y".to_string()),
);
params.insert(
StringOrNumber::String("b".to_string()),
StringOrVec::String("v1.0.0".to_string()),
);
params.insert(
StringOrNumber::String("c".to_string()),
StringOrVec::Vec(vec![
"z".to_string(),
"example".to_string(),
"".to_string(),
]),
);
let compiler = Compiler::new(&tokens, None);
let actual = compiler.to_path(&params);
assert!(actual.is_ok());
let actual = actual.unwrap();
assert_eq!(actual, "/x/y@v1.0.0/z/example/".to_string());
}
#[test] #[test]
fn test_string_to_regex() { fn test_string_to_regex() {
test_path("/", None, &[("/test", None), ("/", Some(("/", 0, 1)))]); test_path("/", None, &[("/test", None), ("/", Some(("/", 0, 1)))]);

View file

@ -651,12 +651,17 @@ impl ModuleRegistry {
is_incomplete = true; is_incomplete = true;
} }
for (idx, item) in items.into_iter().enumerate() { for (idx, item) in items.into_iter().enumerate() {
let label = if let Some(p) = &prefix { let mut label = if let Some(p) = &prefix {
format!("{}{}", p, item) format!("{}{}", p, item)
} else { } else {
item.clone() item.clone()
}; };
let kind = if key.name == last_key_name { if label.ends_with('/') {
label.pop();
}
let kind = if key.name == last_key_name
&& !item.ends_with('/')
{
Some(lsp::CompletionItemKind::FILE) Some(lsp::CompletionItemKind::FILE)
} else { } else {
Some(lsp::CompletionItemKind::FOLDER) Some(lsp::CompletionItemKind::FOLDER)
@ -666,8 +671,11 @@ impl ModuleRegistry {
key.name.clone(), key.name.clone(),
StringOrVec::from_str(&item, &key), StringOrVec::from_str(&item, &key),
); );
let path = let mut path =
compiler.to_path(&params).unwrap_or_default(); compiler.to_path(&params).unwrap_or_default();
if path.ends_with('/') {
path.pop();
}
let item_specifier = base.join(&path).ok()?; let item_specifier = base.join(&path).ok()?;
let full_text = item_specifier.as_str(); let full_text = item_specifier.as_str();
let text_edit = Some(lsp::CompletionTextEdit::Edit( let text_edit = Some(lsp::CompletionTextEdit::Edit(
@ -677,6 +685,7 @@ impl ModuleRegistry {
}, },
)); ));
let command = if key.name == last_key_name let command = if key.name == last_key_name
&& !item.ends_with('/')
&& !specifier_exists(&item_specifier) && !specifier_exists(&item_specifier)
{ {
Some(lsp::Command { Some(lsp::Command {