Skip to content

Commit

Permalink
add docs and test
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri authored and lpil committed Jan 16, 2025
1 parent 0bc6bb2 commit 87b8150
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
20 changes: 19 additions & 1 deletion compiler-core/src/language_server/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3760,7 +3760,25 @@ fn pretty_constructor_name(
}
}

/// .
/// Builder for the "generate function" code action.
/// Whenever someone hovers an invalid expression that is inferred to have a
/// function type the language server can generate a function definition for it.
/// For example:
///
/// ```gleam
/// pub fn main() {
/// wibble(1, 2, "hello")
/// // ^ [generate function]
/// }
/// ```
///
/// Will generate the following definition:
///
/// ```gleam
/// pub fn wibble(arg_0: Int, arg_1: Int, arg_2: String) -> a {
/// todo
/// }
/// ```
///
pub struct GenerateFunction<'a> {
module: &'a Module,
Expand Down
18 changes: 16 additions & 2 deletions compiler-core/src/language_server/tests/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const EXPAND_FUNCTION_CAPTURE: &str = "Expand function capture";
const GENERATE_DYNAMIC_DECODER: &str = "Generate dynamic decoder";
const PATTERN_MATCH_ON_ARGUMENT: &str = "Pattern match on argument";
const PATTERN_MATCH_ON_VARIABLE: &str = "Pattern match on variable";
const GENERATE_FUNCTION: &str = "Generate function";

macro_rules! assert_code_action {
($title:expr, $code:literal, $range:expr $(,)?) => {
Expand Down Expand Up @@ -4690,10 +4691,23 @@ fn map(list: List(a), fun: fn(a) -> b) { todo }
);
}

#[test]
fn generate_function_works_with_invalid_call() {
assert_code_action!(
GENERATE_FUNCTION,
"
pub fn main() -> Bool {
wibble(1, True, 2.3)
}
",
find_position_of("wibble").to_selection()
);
}

#[test]
fn generate_function_works_with_pipeline_steps() {
assert_code_action!(
"Generate function",
GENERATE_FUNCTION,
"
pub fn main() {
[1, 2, 3]
Expand All @@ -4712,7 +4726,7 @@ fn int_to_string(n: Int) -> String {
#[test]
fn generate_function_works_with_pipeline_steps_1() {
assert_code_action!(
"Generate function",
GENERATE_FUNCTION,
"
pub fn main() {
[1, 2, 3]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: compiler-core/src/language_server/tests/action.rs
expression: "\npub fn main() -> Bool {\n wibble(1, True, 2.3)\n}\n"
---
----- BEFORE ACTION

pub fn main() -> Bool {
wibble(1, True, 2.3)
}


----- AFTER ACTION

pub fn main() -> Bool {
wibble(1, True, 2.3)
}

fn wibble(arg_1: Int, arg_2: Bool, arg_3: Float) -> Bool {
todo
}

0 comments on commit 87b8150

Please sign in to comment.