Skip to content

Add labels when showing a hovered function type #4328

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 18 additions & 1 deletion compiler-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::type_::error::VariableOrigin;
use crate::type_::expression::Implementations;
use crate::type_::printer::Names;
use crate::type_::{
self, Deprecation, ModuleValueConstructor, PatternConstructor, Type, ValueConstructor,
self, Deprecation, FieldMap, ModuleValueConstructor, PatternConstructor, Type, ValueConstructor,
};
use std::sync::Arc;

Expand Down Expand Up @@ -747,6 +747,23 @@ pub struct ModuleConstant<T, ConstantRecordTag> {
pub implementations: Implementations,
}

impl TypedModuleConstant {
pub(crate) fn field_map(&self) -> Option<&FieldMap> {
match self.value.as_ref() {
Constant::Record { field_map, .. } => field_map.as_ref(),
Constant::Int { .. }
| Constant::Float { .. }
| Constant::String { .. }
| Constant::Tuple { .. }
| Constant::List { .. }
| Constant::BitArray { .. }
| Constant::Var { .. }
| Constant::StringConcatenation { .. }
| Constant::Invalid { .. } => None,
}
}
}

pub type UntypedCustomType = CustomType<()>;
pub type TypedCustomType = CustomType<Arc<Type>>;

Expand Down
47 changes: 39 additions & 8 deletions compiler-core/src/language_server/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use crate::{
line_numbers::LineNumbers,
paths::ProjectPaths,
type_::{
self, Deprecation, ModuleInterface, Type, TypeConstructor, ValueConstructor,
ValueConstructorVariant, error::VariableOrigin, printer::Printer,
self, Deprecation, FieldMap, ModuleInterface, Type, TypeConstructor, ValueConstructor,
ValueConstructorVariant,
error::VariableOrigin,
printer::{Names, Printer},
},
};
use camino::Utf8PathBuf;
Expand All @@ -27,7 +29,10 @@ use lsp_types::{
PrepareRenameResponse, Range, SignatureHelp, SymbolKind, SymbolTag, TextEdit, Url,
WorkspaceEdit,
};
use std::{collections::HashSet, sync::Arc};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};

use super::{
DownloadDependencies, MakeLocker,
Expand Down Expand Up @@ -1069,10 +1074,18 @@ fn hover_for_function_head(
.map(|(_, doc)| doc)
.unwrap_or(&empty_str);
let function_type = get_function_type(fun);
let formatted_type = Printer::new(&module.ast.names).print_type(&function_type);

let index_to_label = fun
.arguments
.iter()
.enumerate()
.filter_map(|(i, arg)| arg.names.get_label().map(|label| (i as u32, label)))
.collect::<HashMap<_, _>>();

let type_ = Printer::new(&module.ast.names).print_with_labels(&function_type, index_to_label);
let contents = format!(
"```gleam
{formatted_type}
{type_}
```
{documentation}"
);
Expand Down Expand Up @@ -1143,7 +1156,7 @@ fn hover_for_module_constant(
module: &Module,
) -> Hover {
let empty_str = EcoString::from("");
let type_ = Printer::new(&module.ast.names).print_type(&constant.type_);
let type_ = print_type_with_field_map(&module.ast.names, &constant.type_, constant.field_map());
let documentation = constant
.documentation
.as_ref()
Expand Down Expand Up @@ -1171,7 +1184,12 @@ fn hover_for_expression(
.unwrap_or("".to_string());

// Show the type of the hovered node to the user
let type_ = Printer::new(&module.ast.names).print_type(expression.type_().as_ref());
let type_ = print_type_with_field_map(
&module.ast.names,
&expression.type_(),
expression.field_map(),
);

let contents = format!(
"```gleam
{type_}
Expand Down Expand Up @@ -1199,7 +1217,7 @@ fn hover_for_imported_value(
});

// Show the type of the hovered node to the user
let type_ = Printer::new(&module.ast.names).print_type(value.type_.as_ref());
let type_ = print_type_with_field_map(&module.ast.names, &value.type_, value.field_map());
let contents = format!(
"```gleam
{type_}
Expand All @@ -1212,6 +1230,19 @@ fn hover_for_imported_value(
}
}

fn print_type_with_field_map(
names: &Names,
type_: &Type,
field_map: Option<&FieldMap>,
) -> EcoString {
let mut printer = Printer::new(names);
if let Some(field_map) = field_map {
printer.print_with_labels(type_, field_map.indices_to_labels())
} else {
printer.print_type(type_)
}
}

fn hover_for_module(
module: &ModuleInterface,
location: SrcSpan,
Expand Down
90 changes: 90 additions & 0 deletions compiler-core/src/language_server/tests/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,96 @@ macro_rules! assert_hover {
};
}

#[test]
fn hover_function_with_labelled_arguments() {
let code = "
pub fn main() {
labelled
}

pub fn labelled(a: Int, label b: String) -> Int { 1 }
";

assert_hover!(
TestProject::for_source(code),
find_position_of("labelled").under_char('b')
);
}

#[test]
fn hover_constructor_with_labelled_arguments() {
let code = "
pub fn main() {
Labelled
}

pub type Labelled {
Labelled(Int, label: String)
}
";

assert_hover!(
TestProject::for_source(code),
find_position_of("Labelled").under_char('b')
);
}

#[test]
fn hover_constant_with_labelled_constructor() {
let code = "
pub const wibble = Labelled

pub type Labelled {
Labelled(Int, label: String)
}
";

assert_hover!(
TestProject::for_source(code),
find_position_of("wibble").under_char('i')
);
}

#[test]
fn hover_imported_labelled_function() {
let code = "
import wibble.{wobble}
";

assert_hover!(
TestProject::for_source(code)
.add_dep_module("wibble", "pub fn wobble(a: Int, label b: String) {}"),
find_position_of("wobble").under_char('i')
);
}

#[test]
fn hover_imported_labelled_constructor() {
let code = "
import wibble.{Wobble}
";

assert_hover!(
TestProject::for_source(code)
.add_dep_module("wibble", "pub type Wobble { Wobble(Int, label: String) }"),
find_position_of("Wobble").under_char('i')
);
}

#[test]
fn hover_labelled_function_head() {
let code = "
pub fn main(a, label b) {
a + b
}
";

assert_hover!(
TestProject::for_source(code),
find_position_of("main").under_char('i')
);
}

#[test]
fn hover_function_definition() {
assert_hover!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: compiler-core/src/language_server/tests/hover.rs
expression: "\npub const wibble = Labelled\n\npub type Labelled {\n Labelled(Int, label: String)\n}\n"
---
pub const wibble = Labelled
▔▔▔▔▔▔▔▔▔▔▔↑▔▔▔▔

pub type Labelled {
Labelled(Int, label: String)
}


----- Hover content -----
Scalar(
String(
"```gleam\nfn(Int, label: String) -> Labelled\n```\n",
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: compiler-core/src/language_server/tests/hover.rs
expression: "\npub fn main() {\n Labelled\n}\n\npub type Labelled {\n Labelled(Int, label: String)\n}\n"
---
pub fn main() {
Labelled
▔▔↑▔▔▔▔▔
}

pub type Labelled {
Labelled(Int, label: String)
}


----- Hover content -----
Scalar(
String(
"```gleam\nfn(Int, label: String) -> Labelled\n```\n",
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: compiler-core/src/language_server/tests/hover.rs
expression: "\npub fn main() {\n labelled\n}\n\npub fn labelled(a: Int, label b: String) -> Int { 1 }\n"
---
pub fn main() {
labelled
▔▔↑▔▔▔▔▔
}

pub fn labelled(a: Int, label b: String) -> Int { 1 }


----- Hover content -----
Scalar(
String(
"```gleam\nfn(Int, label: String) -> Int\n```\n",
),
)
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.{Wobble}\n"
---
import wibble.{Wobble}
↑▔▔▔▔▔


----- Hover content -----
Scalar(
String(
"```gleam\nfn(Int, label: String) -> wibble.Wobble\n```\n",
),
)
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.{wobble}\n"
---
import wibble.{wobble}
↑▔▔▔▔▔


----- Hover content -----
Scalar(
String(
"```gleam\nfn(Int, label: String) -> a\n```\n",
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: compiler-core/src/language_server/tests/hover.rs
expression: "\npub fn main(a, label b) {\n a + b\n}\n"
---
pub fn main(a, label b) {
▔▔▔▔▔▔▔▔▔↑▔▔▔▔▔▔▔▔▔▔▔▔▔
a + b
}


----- Hover content -----
Scalar(
String(
"```gleam\nfn(Int, label: Int) -> Int\n```\n",
),
)
Loading
Loading