Skip to content

Commit

Permalink
fix(lsp): display module types only dependencies on hover (#12683)
Browse files Browse the repository at this point in the history
Fixes: #12675
  • Loading branch information
kitsonk authored Nov 8, 2021
1 parent 91f8bdd commit 182de14
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 10 deletions.
27 changes: 27 additions & 0 deletions cli/lsp/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ impl Document {
self.maybe_lsp_version.is_some()
}

fn maybe_types_dependency(&self) -> deno_graph::Resolved {
let module_result = self.maybe_module.as_ref()?;
let module = module_result.as_ref().ok()?;
let (_, maybe_dep) = module.maybe_types_dependency.as_ref()?;
maybe_dep.clone()
}

fn media_type(&self) -> MediaType {
if let Some(Ok(module)) = &self.maybe_module {
module.media_type
Expand Down Expand Up @@ -597,6 +604,16 @@ impl Inner {
})
}

fn get_maybe_types_for_dependency(
&mut self,
dependency: &deno_graph::Dependency,
) -> deno_graph::Resolved {
let code_dep = dependency.maybe_code.as_ref()?;
let (specifier, _) = code_dep.as_ref().ok()?;
let doc = self.get(specifier)?;
doc.maybe_types_dependency()
}

fn get_navigation_tree(
&mut self,
specifier: &ModuleSpecifier,
Expand Down Expand Up @@ -952,6 +969,16 @@ impl Documents {
self.0.lock().get_maybe_dependency(specifier, position)
}

/// For a given dependency, try to resolve the maybe_types_dependency for the
/// dependency. This covers modules that assert their own types, like via the
/// triple-slash reference, or the `X-TypeScript-Types` header.
pub fn get_maybe_types_for_dependency(
&self,
dependency: &deno_graph::Dependency,
) -> deno_graph::Resolved {
self.0.lock().get_maybe_types_for_dependency(dependency)
}

/// Get a reference to the navigation tree stored for a given specifier, if
/// any.
pub fn get_navigation_tree(
Expand Down
28 changes: 18 additions & 10 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,21 +1087,34 @@ impl Inner {
&specifier,
&params.text_document_position_params.position,
) {
let value = match (&dep.maybe_code, &dep.maybe_type) {
(Some(code_dep), Some(type_dep)) => format!(
let maybe_types_dependency =
self.documents.get_maybe_types_for_dependency(&dep);
let value = match (&dep.maybe_code, &dep.maybe_type, &maybe_types_dependency) {
(Some(code_dep), Some(type_dep), None) => format!(
"**Resolved Dependency**\n\n**Code**: {}\n\n**Types**: {}\n",
to_hover_text(code_dep),
to_hover_text(type_dep)
),
(Some(code_dep), None) => format!(
(Some(code_dep), Some(type_dep), Some(types_dep)) => format!(
"**Resolved Dependency**\n\n**Code**: {}\n**Types**: {}\n**Import Types**: {}\n",
to_hover_text(code_dep),
to_hover_text(types_dep),
to_hover_text(type_dep)
),
(Some(code_dep), None, None) => format!(
"**Resolved Dependency**\n\n**Code**: {}\n",
to_hover_text(code_dep)
),
(None, Some(type_dep)) => format!(
(Some(code_dep), None, Some(types_dep)) => format!(
"**Resolved Dependency**\n\n**Code**: {}\n\n**Types**: {}\n",
to_hover_text(code_dep),
to_hover_text(types_dep)
),
(None, Some(type_dep), _) => format!(
"**Resolved Dependency**\n\n**Types**: {}\n",
to_hover_text(type_dep)
),
(None, None) => unreachable!("{}", json!(params)),
(None, None, _) => unreachable!("{}", json!(params)),
};
Some(Hover {
contents: HoverContents::Markup(MarkupContent {
Expand Down Expand Up @@ -2661,11 +2674,6 @@ impl Inner {
.performance
.mark("virtual_text_document", Some(&params));
let specifier = self.url_map.normalize_url(&params.text_document.uri);
info!(
"virtual_text_document\n{}\nspecifier: {}",
json!(params),
specifier
);
let contents = if specifier.as_str() == "deno:/status.md" {
let mut contents = String::new();
let mut documents_specifiers = self.documents.specifiers(false, false);
Expand Down
70 changes: 70 additions & 0 deletions cli/tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,76 @@ fn lsp_hover_dependency() {
);
}

#[test]
fn lsp_hover_typescript_types() {
let _g = http_server();
let mut client = init("initialize_params.json");
did_open(
&mut client,
json!({
"textDocument": {
"uri": "file:///a/file.ts",
"languageId": "typescript",
"version": 1,
"text": "import * as a from \"http://127.0.0.1:4545/xTypeScriptTypes.js\";\n\nconsole.log(a.foo);\n",
}
}),
);
let (maybe_res, maybe_err) = client
.write_request::<_, _, Value>(
"deno/cache",
json!({
"referrer": {
"uri": "file:///a/file.ts",
},
"uris": [
{
"uri": "http://127.0.0.1:4545/xTypeScriptTypes.js",
}
],
}),
)
.unwrap();
assert!(maybe_err.is_none());
assert!(maybe_res.is_some());
let (maybe_res, maybe_err) = client
.write_request::<_, _, Value>(
"textDocument/hover",
json!({
"textDocument": {
"uri": "file:///a/file.ts"
},
"position": {
"line": 0,
"character": 24
}
}),
)
.unwrap();
assert!(maybe_res.is_some());
assert!(maybe_err.is_none());
assert_eq!(
json!(maybe_res.unwrap()),
json!({
"contents": {
"kind": "markdown",
"value": "**Resolved Dependency**\n\n**Code**: http&#8203;://127.0.0.1:4545/xTypeScriptTypes.js\n\n**Types**: http&#8203;://127.0.0.1:4545/xTypeScriptTypes.d.ts\n"
},
"range": {
"start": {
"line": 0,
"character": 19
},
"end": {
"line": 0,
"character": 62
}
}
})
);
shutdown(&mut client);
}

#[test]
fn lsp_call_hierarchy() {
let mut client = init("initialize_params.json");
Expand Down

0 comments on commit 182de14

Please sign in to comment.