Skip to content

Commit 8bdec6d

Browse files
GearsDatapackslpil
authored andcommitted
Use correct line number information for generating functions in other
modules
1 parent 03bfad9 commit 8bdec6d

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
incorrect extra patterns in addition to the correct missing patterns.
1717
([Adi Salimgereyev](https://github.com/abs0luty))
1818

19+
- Fixed a bug where triggering the "Generate function" code action to generate
20+
a function in a different module could cause the generated function to appear
21+
in the middle of an existing function, resulting in invalid code.
22+
23+
([Surya Rose](https://github.com/GearsDatapacks))
24+
1925
## v1.13.0-rc1 - 2025-09-29
2026

2127
### Compiler

compiler-core/src/language_server/code_action.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5208,11 +5208,7 @@ impl<'a> GenerateFunction<'a> {
52085208

52095209
if let Some(module) = module {
52105210
if let Some(module) = self.modules.get(module) {
5211-
let insert_at = if module.code.is_empty() {
5212-
0
5213-
} else {
5214-
(module.code.len() - 1) as u32
5215-
};
5211+
let insert_at = module.code.len() as u32;
52165212
self.code_action_for_module(
52175213
module,
52185214
Publicity::Public,
@@ -5230,7 +5226,7 @@ impl<'a> GenerateFunction<'a> {
52305226

52315227
fn code_action_for_module(
52325228
mut self,
5233-
module: &Module,
5229+
module: &'a Module,
52345230
publicity: Publicity,
52355231
function_to_generate: FunctionToGenerate<'a>,
52365232
insert_at: u32,
@@ -5272,6 +5268,10 @@ impl<'a> GenerateFunction<'a> {
52725268

52735269
let publicity = if publicity.is_public() { "pub " } else { "" };
52745270

5271+
// Make sure we use the line number information of the module we are
5272+
// editing, which might not be the module where the code action is
5273+
// triggered.
5274+
self.edits.line_numbers = &module.ast.type_info.line_numbers;
52755275
self.edits.insert(
52765276
insert_at,
52775277
format!("\n\n{publicity}fn {name}({arguments}) -> {return_type} {{\n todo\n}}"),

compiler-core/src/language_server/tests/action.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10637,3 +10637,26 @@ fn wibble(f: fn() -> Float) -> Float { f() }
1063710637
.select_until(find_position_of("}"))
1063810638
);
1063910639
}
10640+
10641+
// https://github.com/gleam-lang/gleam/issues/5036
10642+
#[test]
10643+
fn generate_function_in_other_module_correctly_appends() {
10644+
let src = "import module_breaker/another
10645+
10646+
pub fn main() -> Nil {
10647+
another.function()
10648+
}
10649+
";
10650+
10651+
assert_code_action!(
10652+
GENERATE_FUNCTION,
10653+
TestProject::for_source(src).add_module(
10654+
"module_breaker/another",
10655+
"pub fn useless() {
10656+
Nil
10657+
}
10658+
"
10659+
),
10660+
find_position_of("function").to_selection()
10661+
);
10662+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "import module_breaker/another\n\npub fn main() -> Nil {\n another.function()\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
import module_breaker/another
7+
8+
pub fn main() -> Nil {
9+
another.function()
10+
11+
}
12+
13+
14+
----- AFTER ACTION
15+
// --- Edits applied to module 'module_breaker/another'
16+
pub fn useless() {
17+
Nil
18+
}
19+
20+
21+
pub fn function() -> Nil {
22+
todo
23+
}

0 commit comments

Comments
 (0)