Skip to content

Use new printer for signature help #4589

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 3 commits 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
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,36 @@

([Giacomo Cavalieri](https://github.com/giacomocavalieri))


- The function signature helper now displays original function definition
generic names when arguments are unbound. For example, in this code:

```gleam
pub fn wibble(x: something, y: fn() -> something, z: anything) { Nil }

pub fn main() {
wibble( )
// ↑
}
```

will show a signature help

```gleam
wibble(something, fn() -> something, anything)

```

instead of

```gleam
wibble(a, fn() -> a, b) -> Nil
```

([Samuel Cristobal](https://github.com/scristobal)) and
([Surya Rose](https://github.com/GearsDatapacks))


### Formatter

### Installation
Expand Down
16 changes: 11 additions & 5 deletions compiler-core/src/language_server/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,15 +989,21 @@ Unused labelled fields:
&mut self,
params: lsp_types::SignatureHelpParams,
) -> Response<Option<SignatureHelp>> {
self.respond(
|this| match this.node_at_position(&params.text_document_position_params) {
self.respond(|this| {
let Some(module) =
this.module_for_uri(&params.text_document_position_params.text_document.uri)
else {
return Ok(None);
};

match this.node_at_position(&params.text_document_position_params) {
Some((_lines, Located::Expression { expression, .. })) => {
Ok(signature_help::for_expression(expression))
Ok(signature_help::for_expression(expression, module))
}
Some((_lines, _located)) => Ok(None),
None => Ok(None),
},
)
}
})
}

fn module_node_at_position(
Expand Down
20 changes: 11 additions & 9 deletions compiler-core/src/language_server/signature_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ use lsp_types::{

use crate::{
ast::{CallArg, ImplicitCallArgOrigin, TypedExpr},
type_::{FieldMap, ModuleValueConstructor, Type, pretty::Printer},
build::Module,
type_::{FieldMap, ModuleValueConstructor, Type, printer::Printer},
};

pub fn for_expression(expr: &TypedExpr) -> Option<SignatureHelp> {
pub fn for_expression(expr: &TypedExpr, module: &Module) -> Option<SignatureHelp> {
// If we're inside a function call we can provide signature help,
// otherwise we don't want anything to pop up.
let TypedExpr::Call { fun, args, .. } = expr else {
Expand All @@ -27,7 +28,7 @@ pub fn for_expression(expr: &TypedExpr) -> Option<SignatureHelp> {
// help.
TypedExpr::Var {
constructor, name, ..
} => signature_help(name.clone(), fun, args, constructor.field_map()),
} => signature_help(name.clone(), fun, args, constructor.field_map(), module),

// If we're making a qualified call to another module's function
// then we want to show its type, documentation and the exact name
Expand All @@ -50,7 +51,7 @@ pub fn for_expression(expr: &TypedExpr) -> Option<SignatureHelp> {
| ModuleValueConstructor::Fn { field_map, .. } => field_map.into(),
};
let name = format!("{module_alias}.{label}").into();
signature_help(name, fun, args, field_map)
signature_help(name, fun, args, field_map, module)
}

// If the function bein called is an invalid node we don't want to
Expand All @@ -67,7 +68,7 @@ pub fn for_expression(expr: &TypedExpr) -> Option<SignatureHelp> {
// ^ When the cursor is here we are going to show
// "fn(a: a) -> a" as the help signature.
//
_ => signature_help("fn".into(), fun, args, None),
_ => signature_help("fn".into(), fun, args, None, module),
}
}

Expand All @@ -89,6 +90,7 @@ fn signature_help(
fun: &TypedExpr,
supplied_args: &[CallArg<TypedExpr>],
field_map: Option<&FieldMap>,
module: &Module,
) -> Option<SignatureHelp> {
let (args, return_) = fun.type_().fn_types()?;

Expand All @@ -107,7 +109,7 @@ fn signature_help(
None => HashMap::new(),
};

let printer = Printer::new();
let printer = Printer::new(&module.ast.names);
let (label, parameters) =
print_signature_help(printer, fun_name, args, return_, &index_to_label);

Expand Down Expand Up @@ -220,7 +222,7 @@ fn active_parameter_index(
/// `ParameterInformation` for all its arguments.
///
fn print_signature_help(
mut printer: Printer,
mut printer: Printer<'_>,
function_name: EcoString,
args: Vec<Arc<Type>>,
return_: Arc<Type>,
Expand All @@ -236,7 +238,7 @@ fn print_signature_help(
signature.push_str(label);
signature.push_str(": ");
}
signature.push_str(&printer.pretty_print(arg, 0));
signature.push_str(&printer.print_type(arg));
let arg_end = signature.len();
let label = ParameterLabel::LabelOffsets([arg_start as u32, arg_end as u32]);

Expand All @@ -252,6 +254,6 @@ fn print_signature_help(
}

signature.push_str(") -> ");
signature.push_str(&printer.pretty_print(&return_, 0));
signature.push_str(&printer.print_type(&return_));
(signature, parameter_informations)
}
42 changes: 42 additions & 0 deletions compiler-core/src/language_server/tests/signature_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,45 @@ pub fn main() {
find_position_of(r#"Pokemon(name: "Jirachi",)"#).under_last_char()
);
}

#[test]
pub fn help_for_use_function_call_uses_generic_names_when_missing_all_arguments() {
assert_signature_help!(
r#"
pub fn wibble(x: something, y: fn() -> something, z: anything) { Nil }
pub fn main() {
wibble( )
}
"#,
find_position_of("wibble( ").under_last_char()
);
}

#[test]
pub fn help_for_use_function_call_uses_concrete_types_when_possible_or_generic_names_when_unbound()
{
assert_signature_help!(
r#"
pub fn wibble(x: something, y: fn() -> something, z: anything) { Nil }
pub fn main() {
wibble(1, )
}
"#,
find_position_of("wibble(1, )").under_last_char()
);
}

#[test]
pub fn help_for_use_function_call_uses_concrete_types_when_late_ubound() {
assert_signature_help!(
r#"
fn identity(x: a) -> a { x }

fn main() {
let a = Ok(10)
identity( a)
}
"#,
find_position_of("identity( ").under_last_char()
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: compiler-core/src/language_server/tests/signature_help.rs
expression: "\nfn identity(x: a) -> a { x }\n\nfn main() {\n let a = Ok(10)\n identity( a)\n}\n"
---
fn identity(x: a) -> a { x }

fn main() {
let a = Ok(10)
identity( a)
}


----- Signature help -----
identity(Result(Int, b)) -> Result(Int, b)

No documentation
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: compiler-core/src/language_server/tests/signature_help.rs
expression: "\npub fn wibble(x: something, y: fn() -> something, z: anything) { Nil }\npub fn main() {\n wibble(1, )\n}\n"
---
pub fn wibble(x: something, y: fn() -> something, z: anything) { Nil }
pub fn main() {
wibble(1, )
}


----- Signature help -----
wibble(Int, fn() -> Int, anything) -> Nil
▔▔▔▔▔▔▔▔▔▔▔

No documentation
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: compiler-core/src/language_server/tests/signature_help.rs
expression: "\npub fn wibble(x: something, y: fn() -> something, z: anything) { Nil }\npub fn main() {\n wibble( )\n}\n"
---
pub fn wibble(x: something, y: fn() -> something, z: anything) { Nil }
pub fn main() {
wibble( )
}


----- Signature help -----
wibble(something, fn() -> something, anything) -> Nil
▔▔▔▔▔▔▔▔▔

No documentation
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn main() {


----- Signature help -----
guard(Bool, a, fn() -> a) -> Float
guard(Bool, b, fn() -> b) -> Float

No documentation
2 changes: 2 additions & 0 deletions compiler-core/src/type_/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ impl Environment<'_> {
// Check this in the hydrator, i.e. is it a created type
let v = self.new_unbound_var();
let _ = ids.insert(*id, v.clone());
let new_id = self.previous_uid();
self.names.map_new_variable(*id, new_id);
return v;
}
}
Expand Down
6 changes: 6 additions & 0 deletions compiler-core/src/type_/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ impl Names {
_ = self.type_variables.insert(id, local_alias.clone());
}

pub fn map_new_variable(&mut self, old_id: u64, new_id: u64) {
if let Some(alias) = self.type_variables.get(&old_id) {
_ = self.type_variables.insert(new_id, alias.clone());
}
}

/// Record an imported module in this module.
pub fn imported_module(&mut self, module_name: EcoString, module_alias: EcoString) {
_ = self.imported_modules.insert(module_name, module_alias)
Expand Down
Loading