Skip to content
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

fix(lsp): handle repeating patterns in registry correctly #13275

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions cli/lsp/path_to_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ impl StringOrVec {
}
}

pub fn to_string(&self, maybe_key: Option<&Key>) -> String {
pub fn to_string(
&self,
maybe_key: Option<&Key>,
omit_initial_prefix: bool,
) -> String {
match self {
Self::String(s) => s.clone(),
Self::Vec(v) => {
Expand All @@ -262,8 +266,12 @@ impl StringOrVec {
("/".to_string(), "".to_string())
};
let mut s = String::new();
for segment in v {
s.push_str(&format!("{}{}{}", prefix, segment, suffix));
for (i, segment) in v.iter().enumerate() {
if omit_initial_prefix && i == 0 {
s.push_str(&format!("{}{}", segment, suffix));
} else {
s.push_str(&format!("{}{}{}", prefix, segment, suffix));
}
}
s
}
Expand Down
99 changes: 77 additions & 22 deletions cli/lsp/registries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn get_completor_type(
if let StringOrNumber::String(name) = &k.name {
let value = match_result
.get(name)
.map(|s| s.to_string(Some(k)))
.map(|s| s.to_string(Some(k), false))
.unwrap_or_default();
len += value.chars().count();
if offset <= len {
Expand Down Expand Up @@ -213,11 +213,12 @@ fn get_endpoint_with_match(
Token::Key(k) if k.name == *key => Some(k),
_ => None,
});
url = url.replace(&format!("${{{}}}", name), &value.to_string(maybe_key));
url = url
.replace(&format!("${{{}}}", name), &value.to_string(maybe_key, true));
url = url.replace(
&format!("${{{{{}}}}}", name),
&percent_encoding::percent_encode(
value.to_string(maybe_key).as_bytes(),
value.to_string(maybe_key, true).as_bytes(),
COMPONENT,
)
.to_string(),
Expand Down Expand Up @@ -1212,15 +1213,7 @@ mod tests {
.await;
assert!(completions.is_some());
let completions = completions.unwrap().items;
assert_eq!(completions.len(), 1);
assert_eq!(completions[0].label, "/x");
assert_eq!(
completions[0].text_edit,
Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
range,
new_text: "https://localhost:4545/x".to_string()
}))
);
assert_eq!(completions.len(), 3);
let range = lsp::Range {
start: lsp::Position {
line: 0,
Expand All @@ -1236,15 +1229,7 @@ mod tests {
.await;
assert!(completions.is_some());
let completions = completions.unwrap().items;
assert_eq!(completions.len(), 1);
assert_eq!(completions[0].label, "/x");
assert_eq!(
completions[0].text_edit,
Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
range,
new_text: "https://localhost:4545/x".to_string()
}))
);
assert_eq!(completions.len(), 3);
let range = lsp::Range {
start: lsp::Position {
line: 0,
Expand Down Expand Up @@ -1328,17 +1313,40 @@ mod tests {
"documentation": format!("https://localhost:4545/lsp/registries/doc_a_{}.json", completions[0].label),
}))
);

let range = lsp::Range {
start: lsp::Position {
line: 0,
character: 20,
},
end: lsp::Position {
line: 0,
character: 53,
character: 49,
},
};
let completions = module_registry
.get_completions("https://localhost:4545/x/a@v1.", 29, &range, |_| false)
.await;
assert!(completions.is_some());
let completions = completions.unwrap().items;
assert_eq!(completions.len(), 2);
assert_eq!(
completions[0].data,
Some(json!({
"documentation": format!("https://localhost:4545/lsp/registries/doc_a_{}.json", completions[0].label),
}))
);

let range = lsp::Range {
start: lsp::Position {
line: 0,
character: 20,
},
end: lsp::Position {
line: 0,
character: 53,
},
};
let completions = module_registry
.get_completions("https://localhost:4545/x/[email protected]/", 33, &range, |_| {
false
Expand All @@ -1353,6 +1361,53 @@ mod tests {
assert_eq!(completions[1].detail, Some("(path)".to_string()));
assert_eq!(completions[0].kind, Some(lsp::CompletionItemKind::FILE));
assert!(completions[1].command.is_some());

let range = lsp::Range {
start: lsp::Position {
line: 0,
character: 20,
},
end: lsp::Position {
line: 0,
character: 54,
},
};
let completions = module_registry
.get_completions("https://localhost:4545/x/[email protected]/b", 34, &range, |_| {
false
})
.await;
assert!(completions.is_some());
let completions = completions.unwrap().items;
assert_eq!(completions.len(), 1);
assert_eq!(completions[0].detail, Some("(path)".to_string()));
assert_eq!(completions[0].kind, Some(lsp::CompletionItemKind::FILE));
assert!(completions[0].command.is_some());

let range = lsp::Range {
start: lsp::Position {
line: 0,
character: 20,
},
end: lsp::Position {
line: 0,
character: 55,
},
};
let completions = module_registry
.get_completions(
"https://localhost:4545/x/[email protected]/b/",
35,
&range,
|_| false,
)
.await;
assert!(completions.is_some());
let completions = completions.unwrap().items;
assert_eq!(completions.len(), 1);
assert_eq!(completions[0].detail, Some("(path)".to_string()));
assert_eq!(completions[0].kind, Some(lsp::CompletionItemKind::FILE));
assert!(completions[0].command.is_some());
}

#[tokio::test]
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/testdata/lsp/registries/a_v1.0.0_b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"b/c.ts"
]
4 changes: 4 additions & 0 deletions cli/tests/testdata/lsp/registries/a_versions_v1..json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"v1.0.0",
"v1.0.1"
]
28 changes: 25 additions & 3 deletions cli/tests/testdata/lsp/registries/deno-import-intellisense.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
{
"key": "version",
"documentation": "/lsp/registries/doc_${module}_${{version}}.json",
"url": "/lsp/registries/${module}_versions.json"
"url": "/lsp/registries/${module}_versions_${{version}}.json"
},
{
"key": "path",
"url": "/lsp/registries/${module}_${{version}}.json"
"url": "/lsp/registries/${module}_${{version}}_${path}.json"
}
]
},
Expand All @@ -30,7 +30,29 @@
},
{
"key": "path",
"url": "/lsp/registries/${module}_latest.json"
"url": "/lsp/registries/${module}_latest_${path}.json"
}
]
},
{
"schema": "/std@:version?/:path*",
"variables": [
{
"key": "version",
"url": "/lsp/registries/std_${{version}}.json"
},
{
"key": "path",
"url": "/lsp/registries/std_${{version}}_${path}.json"
}
]
},
{
"schema": "/std/:path*",
"variables": [
{
"key": "path",
"url": "/lsp/registries/std_latest_${path}.json"
}
]
}
Expand Down