Skip to content

Commit

Permalink
Allow hovering over cached modules
Browse files Browse the repository at this point in the history
  • Loading branch information
GearsDatapacks authored and lpil committed Mar 3, 2025
1 parent c73f5ad commit 61d3743
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 31 deletions.
24 changes: 12 additions & 12 deletions compiler-core/src/build/package_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ where
incomplete_modules,
);

let mut modules = match outcome {
let modules = match outcome {
Outcome::Ok(modules) => modules,
Outcome::PartialFailure(modules, error) => {
return Outcome::PartialFailure(
Expand All @@ -207,10 +207,6 @@ where
Outcome::TotalFailure(error) => return Outcome::TotalFailure(error),
};

for mut module in modules.iter_mut() {
module.attach_doc_and_module_comments();
}

tracing::debug!("performing_code_generation");

if let Err(error) = self.perform_codegen(&modules) {
Expand Down Expand Up @@ -515,12 +511,8 @@ fn analyse(
Outcome::Ok(ast) => {
// Module has compiled successfully. Make sure it isn't marked as incomplete.
let _ = incomplete_modules.remove(&name.clone());
// Register the types from this module so they can be imported into
// other modules.
let _ = module_types.insert(name.clone(), ast.type_info.clone());
// Register the successfully type checked module data so that it can be
// used for code generation and in the language server.
modules.push(Module {

let mut module = Module {
dependencies,
origin,
extra,
Expand All @@ -529,7 +521,15 @@ fn analyse(
code,
ast,
input_path: path,
});
};
module.attach_doc_and_module_comments();

// Register the types from this module so they can be imported into
// other modules.
let _ = module_types.insert(module.name.clone(), module.ast.type_info.clone());
// Register the successfully type checked module data so that it can be
// used for code generation and in the language server.
modules.push(module);
}

Outcome::PartialFailure(ast, errors) => {
Expand Down
39 changes: 27 additions & 12 deletions compiler-core/src/language_server/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use lsp_types::{
PrepareRenameResponse, Range, SignatureHelp, SymbolKind, SymbolTag, TextEdit, Url,
WorkspaceEdit,
};
use std::sync::Arc;
use std::{collections::HashSet, sync::Arc};

use super::{
DownloadDependencies, MakeLocker,
Expand Down Expand Up @@ -81,7 +81,7 @@ pub struct LanguageServerEngine<IO, Reporter> {

/// Used to know if to show the "View on HexDocs" link
/// when hovering on an imported value
hex_deps: std::collections::HashSet<EcoString>,
hex_deps: HashSet<EcoString>,
}

impl<'a, IO, Reporter> LanguageServerEngine<IO, Reporter>
Expand Down Expand Up @@ -739,10 +739,15 @@ where
Some(hover_for_module_constant(constant, lines, module))
}
Located::ModuleStatement(Definition::Import(import)) => {
let Some(module) = this.compiler.modules.get(&import.module) else {
let Some(module) = this.compiler.get_module_interface(&import.module) else {
return Ok(None);
};
Some(hover_for_module(module, import.location, &lines))
Some(hover_for_module(
module,
import.location,
&lines,
&this.hex_deps,
))
}
Located::ModuleStatement(_) => None,
Located::UnqualifiedImport(UnqualifiedImport {
Expand Down Expand Up @@ -844,10 +849,10 @@ Unused labelled fields:
Some(hover_for_label(location, type_, lines, module))
}
Located::ModuleName { location, name, .. } => {
let Some(module) = this.compiler.modules.get(name) else {
let Some(module) = this.compiler.get_module_interface(name) else {
return Ok(None);
};
Some(hover_for_module(module, location, &lines))
Some(hover_for_module(module, location, &lines, &this.hex_deps))
}
})
})
Expand Down Expand Up @@ -1155,7 +1160,7 @@ fn hover_for_expression(
expression: &TypedExpr,
line_numbers: LineNumbers,
module: &Module,
hex_deps: &std::collections::HashSet<EcoString>,
hex_deps: &HashSet<EcoString>,
) -> Hover {
let documentation = expression.get_documentation().unwrap_or_default();

Expand Down Expand Up @@ -1207,17 +1212,27 @@ fn hover_for_imported_value(
}
}

fn hover_for_module(module: &Module, location: SrcSpan, line_numbers: &LineNumbers) -> Hover {
let documentation = module.ast.documentation.join("\n");
fn hover_for_module(
module: &ModuleInterface,
location: SrcSpan,
line_numbers: &LineNumbers,
hex_deps: &HashSet<EcoString>,
) -> Hover {
let documentation = module.documentation.join("\n");
let name = &module.name;

let link_section = format_hexdocs_link_section(&module.ast.type_info.package, name, None);
let link_section = if hex_deps.contains(&module.package) {
format_hexdocs_link_section(&module.package, name, None)
} else {
String::new()
};

let contents = format!(
"```gleam
{name}
```
{documentation}{link_section}",
{documentation}
{link_section}",
);
Hover {
contents: HoverContents::Scalar(MarkedString::String(contents)),
Expand Down Expand Up @@ -1472,7 +1487,7 @@ fn get_hexdocs_link_section(
module_name: &str,
name: &str,
ast: &TypedModule,
hex_deps: &std::collections::HashSet<EcoString>,
hex_deps: &HashSet<EcoString>,
) -> Option<String> {
let package_name = ast.definitions.iter().find_map(|def| match def {
Definition::Import(p) if p.module == module_name && hex_deps.contains(&p.package) => {
Expand Down
24 changes: 21 additions & 3 deletions compiler-core/src/language_server/tests/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ pub fn main() {
}
";
assert_hover!(
TestProject::for_source(src).add_module(
TestProject::for_source(src).add_hex_module(
"wibble",
"
//// This is the wibble module.
Expand All @@ -1516,7 +1516,7 @@ pub fn main() {
}
";
assert_hover!(
TestProject::for_source(src).add_module(
TestProject::for_source(src).add_hex_module(
"wibble/wobble",
"
//// The module documentation
Expand All @@ -1540,7 +1540,7 @@ pub fn main(w: wibble.Wibble) {
}
";
assert_hover!(
TestProject::for_source(src).add_module(
TestProject::for_source(src).add_hex_module(
"wibble",
"
//// This is the wibble module.
Expand All @@ -1558,6 +1558,24 @@ pub type Wibble
fn hover_over_imported_module() {
let src = "
import wibble
";
assert_hover!(
TestProject::for_source(src).add_hex_module(
"wibble",
"
//// This is the wibble module.
//// Here is some documentation about it.
//// This module does stuff
"
),
find_position_of("wibble")
);
}

#[test]
fn no_hexdocs_link_when_hovering_over_local_module() {
let src = "
import wibble
";
assert_hover!(
TestProject::for_source(src).add_module(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import wibble
----- Hover content -----
Scalar(
String(
"```gleam\nwibble\n```\n This is the wibble module.\n Here is some documentation about it.\n This module does stuff\nView on [HexDocs](https://hexdocs.pm/app/wibble.html)",
"```gleam\nwibble\n```\n This is the wibble module.\n Here is some documentation about it.\n This module does stuff\n\nView on [HexDocs](https://hexdocs.pm/hex/wibble.html)",
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub fn main() {
----- Hover content -----
Scalar(
String(
"```gleam\nwibble\n```\n This is the wibble module.\n Here is some documentation about it.\n This module does stuff\nView on [HexDocs](https://hexdocs.pm/app/wibble.html)",
"```gleam\nwibble\n```\n This is the wibble module.\n Here is some documentation about it.\n This module does stuff\n\nView on [HexDocs](https://hexdocs.pm/hex/wibble.html)",
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub fn main(w: wibble.Wibble) {
----- Hover content -----
Scalar(
String(
"```gleam\nwibble\n```\n This is the wibble module.\n Here is some documentation about it.\n This module does stuff\nView on [HexDocs](https://hexdocs.pm/app/wibble.html)",
"```gleam\nwibble\n```\n This is the wibble module.\n Here is some documentation about it.\n This module does stuff\n\nView on [HexDocs](https://hexdocs.pm/hex/wibble.html)",
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub fn main() {
----- Hover content -----
Scalar(
String(
"```gleam\nwibble/wobble\n```\n The module documentation\nView on [HexDocs](https://hexdocs.pm/app/wibble/wobble.html)",
"```gleam\nwibble/wobble\n```\n The module documentation\n\nView on [HexDocs](https://hexdocs.pm/hex/wibble/wobble.html)",
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: compiler-core/src/language_server/tests/hover.rs
expression: "\nimport wibble\n"
---
import wibble
▔▔▔▔▔▔▔↑▔▔▔▔▔


----- Hover content -----
Scalar(
String(
"```gleam\nwibble\n```\n This is the wibble module.\n Here is some documentation about it.\n This module does stuff\n",
),
)

0 comments on commit 61d3743

Please sign in to comment.