Something went wrong. Try again.
fork of Gleam compiler for Lua backend forked from gleam.run/gleam
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: 2025 The Gleam contributors
use lsp_types::{ DocumentHighlightParams, PartialResultParams, Position, Range, TextDocumentPositionParams, WorkDoneProgressParams,};
use super::{TestProject, find_position_of};
fn find_highlights(tester: &TestProject<'_>, position: Position) -> Option<(String, Vec<Range>)> { tester.at(position, |engine, params, _| { let params = DocumentHighlightParams { text_document_position_params: TextDocumentPositionParams { text_document: params.text_document, position, }, work_done_progress_params: WorkDoneProgressParams::default(), partial_result_params: PartialResultParams::default(), }; let module_name = tester .module_name_from_url(¶ms.text_document_position_params.text_document.uri) .expect("Valid uri"); engine .document_highlight(params) .result .unwrap() .map(|highlights| { highlights .into_iter() .map(|highlight| highlight.range) .collect() }) .map(|highlights| (module_name, highlights)) })}
fn show_highlights(code: &str, position: Option<Position>, ranges: &[Range]) -> String { let mut buffer = String::new();
for (line_number, line) in code.lines().enumerate() { let mut underline = String::new(); let mut underline_empty = true; let line_number = line_number as u32;
for (column_number, _) in line.chars().enumerate() { let current_position = Position::new(line_number, column_number as u32);
// Check if any range covers this specific character let is_in_range = ranges .iter() .any(|range| range.start <= current_position && current_position < range.end);
if Some(current_position) == position { underline_empty = false; underline.push('↑'); } else if is_in_range { underline_empty = false; underline.push('▔'); } else { underline.push(' '); } }
buffer.push_str(line); if !underline_empty { buffer.push('\n'); buffer.push_str(&underline); } buffer.push('\n'); }
buffer}
macro_rules! assert_highlights { ($code:literal, $position:expr $(,)?) => { assert_highlights!(TestProject::for_source($code), $position); };
(($module_name:literal, $module_src:literal), $code:literal, $position:expr $(,)?) => { assert_highlights!( TestProject::for_source($code).add_module($module_name, $module_src), $position ); };
($project:expr, $position:expr $(,)?) => { let project = $project; let src = project.src; let position = $position.find_position(src); let (module_name, result) = find_highlights(&project, position).expect("Higlights not found");
let mut output = String::new(); for (name, src) in project.root_package_modules.iter() { let highlights_in_module = if *name == module_name { &result } else { &Vec::new() };
output.push_str("-- "); output.push_str(name); output.push_str(".gleam\n"); output.push_str(&show_highlights(src, None, highlights_in_module)); output.push_str("\n\n"); } let highlights_in_app_module = if module_name == "app" { &result } else { &Vec::new() }; output.push_str("-- app.gleam\n"); output.push_str(&show_highlights( src, Some(position), highlights_in_app_module, ));
insta::assert_snapshot!(insta::internals::AutoName, output, src); };}
macro_rules! assert_no_highlights { ($code:literal, $position:expr $(,)?) => { let project = TestProject::for_source($code); assert_no_highlights!(&project, $position); };
($project:expr, $position:expr $(,)?) => { let src = $project.src; let position = $position.find_position(src); let result = find_highlights($project, position); assert_eq!(result, None); };}
#[test]fn highlights_for_local_variable() { assert_highlights!( "pub fn main() { let wibble = 10 let wobble = wibble + 1 wibble + wobble}", find_position_of("wibble").nth_occurrence(2), );}
#[test]fn highlights_for_local_variable_from_definition() { assert_highlights!( "pub fn main() { let wibble = 10 let wobble = wibble + 1 wibble + wobble}", find_position_of("wibble"), );}
#[test]fn highlights_for_private_function() { assert_highlights!( "fn wibble() { wibble()}
pub fn main() { let _ = wibble() wibble() + 4}
fn wobble() { wibble() || wobble()}", find_position_of("wibble"), );}
#[test]fn highlights_for_private_function_from_reference() { assert_highlights!( "fn wibble() { wibble()}
pub fn main() { let _ = wibble() wibble() + 4}
fn wobble() { wibble() || wobble()}", find_position_of("wibble").nth_occurrence(2), );}
#[test]fn highlights_for_public_function() { assert_highlights!( ( "mod", "pub fn wibble() { wibble()}" ), "import mod.{wibble}
fn wobble() { mod.wibble()}
fn other() { wibble()}", find_position_of("wibble").nth_occurrence(2), );}
#[test]fn highlights_for_function_from_qualified_reference() { assert_highlights!( ( "mod", "pub fn wibble() { wibble()}" ), "import mod
pub fn main() { let value = mod.wibble() mod.wibble() value}", find_position_of("wibble"), );}
#[test]fn highlights_for_function_from_unqualified_reference() { assert_highlights!( ( "mod", "pub fn wibble() { wibble()}" ), "import mod.{wibble}
pub fn main() { let value = wibble() mod.wibble() value}", find_position_of("wibble()"), );}
#[test]fn highlights_for_function_from_unqualified_aliased_reference() { assert_highlights!( ( "mod", "pub fn wibble() { wibble()}" ), "import mod.{wibble as wobble}
pub fn main() { let value = wobble() mod.wibble() value}", find_position_of("wobble()"), );}
#[test]fn highlights_for_private_constant() { assert_highlights!( "const wibble = 10
pub fn main() { let _ = wibble wibble + 4}
fn wobble() { wibble + wobble()}", find_position_of("wibble"), );}
#[test]fn highlights_for_private_constant_from_reference() { assert_highlights!( "const wibble = 10
pub fn main() { let _ = wibble wibble + 4}
fn wobble() { wibble + wobble()}", find_position_of("wibble").nth_occurrence(2), );}
#[test]fn highlights_for_public_constant() { assert_highlights!( ( "mod", "pub const wibble = 10
pub fn main() { wibble}" ), "import mod.{wibble}
fn wobble() { mod.wibble}
fn other() { wibble}", find_position_of("wibble").nth_occurrence(2), );}
#[test]fn highlights_for_constant_from_qualified_reference() { assert_highlights!( ( "mod", "pub const wibble = 10
fn wobble() { wibble}" ), "import mod
pub fn main() { let value = mod.wibble mod.wibble + value}", find_position_of("wibble"), );}
#[test]fn highlights_for_constant_from_unqualified_reference() { assert_highlights!( ( "mod", "pub const wibble = 10
fn wobble() { wibble}" ), "import mod.{wibble}
pub fn main() { let value = mod.wibble wibble + value}", find_position_of("wibble +"), );}
#[test]fn highlights_for_constant_from_unqualified_aliased_reference() { assert_highlights!( ( "mod", "pub const wibble = 10
fn wobble() { wibble}" ), "import mod.{wibble as wobble}
pub fn main() { let value = mod.wibble wobble + value}", find_position_of("wobble +"), );}
#[test]fn higlights_for_private_type_variant() { assert_highlights!( "type Wibble { Wibble }
fn main() { let _ = Wibble Wibble}
fn wobble() { Wibble wobble()}", find_position_of("Wibble }"), );}
#[test]fn higlights_for_private_type_variant_from_reference() { assert_highlights!( "type Wibble { Wibble }
fn main() { let _ = Wibble Wibble}
fn wobble() { Wibble wobble()}", find_position_of(" = Wibble").under_char('W'), );}
#[test]fn higlights_for_public_type_variant() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
pub fn main() { Wibble}" ), "import mod.{Wibble}
fn wobble() { mod.Wibble}
fn other() { Wibble}", find_position_of("Wibble").nth_occurrence(3), );}
#[test]fn higlights_for_type_variant_from_qualified_reference() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
fn wobble() { Wibble}" ), "import mod
pub fn main() { let value = mod.Wibble mod.Wibble value}", find_position_of("Wibble"), );}
#[test]fn higlights_for_type_variant_from_unqualified_reference() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
fn wobble() { Wibble}" ), "import mod.{Wibble}
pub fn main() { let value = mod.Wibble Wibble}", find_position_of("Wibble").nth_occurrence(3), );}
#[test]fn higlights_for_type_variant_from_unqualified_aliased_reference() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
fn wobble() { Wibble}" ), "import mod.{Wibble as Wobble}
pub fn main() { let value = mod.Wibble Wobble}", find_position_of("Wobble").nth_occurrence(2), );}
#[test]fn no_higlights_for_keyword() { assert_no_highlights!( "pub fn wibble() { todo}", find_position_of("fn") );}
#[test]fn higlights_for_aliased_value() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
pub fn main() { Wibble}" ), "import mod.{Wibble as Wobble}
fn wobble() { Wobble}", find_position_of("Wobble").nth_occurrence(2), );}
#[test]fn higlights_for_private_type() { assert_highlights!( "type Wibble { Wibble }
fn main() -> Wibble { todo}
fn wobble(w: Wibble) { todo}", find_position_of("Wibble"), );}
#[test]fn higlights_for_private_type_from_reference() { assert_highlights!( "type Wibble { Wibble }
fn main() -> Wibble { todo}
fn wobble(w: Wibble) { todo}", find_position_of("-> Wibble").under_char('W'), );}
#[test]fn higlights_for_public_type() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
pub fn main() -> Wibble { todo}" ), "import mod.{type Wibble}
fn wobble() -> Wibble { todo}
fn other(w: mod.Wibble) { todo}", find_position_of("-> Wibble").under_char('W'), );}
#[test]fn higlights_for_type_from_qualified_reference() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
fn wobble() -> Wibble { todo}" ), "import mod
pub fn main() -> mod.Wibble { let _: mod.Wibble = todo}", find_position_of("Wibble"), );}
#[test]fn higlights_for_type_from_unqualified_reference() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
fn wobble() -> Wibble { todo}" ), "import mod.{type Wibble}
pub fn main() -> Wibble { let _: mod.Wibble = todo}", find_position_of("Wibble").nth_occurrence(2), );}
#[test]fn higlights_for_type_from_unqualified_aliased_reference() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
fn wobble() -> Wibble { todo}" ), "import mod.{type Wibble as Wobble}
pub fn main() -> Wobble { let _: mod.Wibble = todo}", find_position_of("Wobble").nth_occurrence(2), );}
#[test]fn higlights_for_aliased_type() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
pub fn main() -> Wibble { todo}" ), "import mod.{type Wibble as Wobble}
fn wobble() -> Wobble { todo}
fn other(w: mod.Wibble) { todo}", find_position_of("-> Wobble").under_char('W'), );}
#[test]fn higlights_for_type_from_let_annotation() { assert_highlights!( ( "mod", "pub type Wibble { Wibble }
fn wobble() -> Wibble { todo}" ), "import mod.{type Wibble}
pub fn main() -> Wibble { let _: mod.Wibble = todo}", find_position_of("mod.Wibble").under_char('W'), );}
#[test]fn higlights_for_prefix_string_suffix_variable_in_case() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let rest = case wibble { \"1\" <> rest -> rest other -> other } rest}", find_position_of("rest").nth_occurrence(2) );}
#[test]fn higlights_for_prefix_string_suffix_variable_in_case_triggered_from_usage() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let rest = case wibble { \"1\" <> rest -> rest other -> other } rest}", find_position_of("rest").nth_occurrence(3) );}
#[test]fn higlights_for_prefix_string_suffix_variable_with_alternative_definition_in_case() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let rest = case wibble { \"1\" <> rest | \"2\" <> rest -> rest other -> other } rest}", find_position_of("rest").nth_occurrence(2), );}
#[test]fn higlights_for_prefix_string_suffix_variable_with_alternative_definition_triggered_from_second_pattern() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let rest = case wibble { \"1\" <> rest | \"2\" <> rest -> rest other -> other } rest}", find_position_of("rest").nth_occurrence(3), );}
#[test]fn higlights_for_prefix_string_suffix_variable_with_alternative_definition_triggered_from_usage() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let rest = case wibble { \"1\" <> rest | \"2\" <> rest -> rest other -> other } rest}", find_position_of("rest").nth_occurrence(4), );}
#[test]fn higlights_for_prefix_string_suffix_variable_in_let_assert() { assert_highlights!( "pub fn main() -> String { let assert \"1\" <> rest = \"1-wibble\" rest}", find_position_of("rest").nth_occurrence(1), );}
#[test]fn higlights_for_prefix_string_suffix_variable_in_let_assert_triggered_from_usage() { assert_highlights!( "pub fn main() -> String { let assert \"1\" <> rest = \"1-wibble\" rest}", find_position_of("rest").nth_occurrence(2), );}
#[test]fn higlights_for_prefix_string_alias_in_case() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let digit = case wibble { \"1\" as digit <> _rest -> digit other -> other } digit}", find_position_of("digit").nth_occurrence(2) );}
#[test]fn higlights_for_prefix_string_alias_in_case_triggered_from_usage() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let digit = case wibble { \"1\" as digit <> _rest -> digit other -> other } digit}", find_position_of("digit").nth_occurrence(3) );}
#[test]fn higlights_for_prefix_string_alias_with_alternative_definitions_in_case() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let digit = case wibble { \"1\" as digit <> _rest | \"2\" as digit <> _rest -> digit other -> other } digit}", find_position_of("digit").nth_occurrence(2) );}
#[test]fn higlights_for_prefix_string_alias_with_alternative_definitions_triggered_from_second_pattern() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let digit = case wibble { \"1\" as digit <> _rest | \"2\" as digit <> _rest -> digit other -> other } digit}", find_position_of("digit").nth_occurrence(3) );}
#[test]fn higlights_for_prefix_string_alias_with_alternative_definitions_triggered_from_usage() { assert_highlights!( "pub fn main() -> String { let wibble = \"1-wibble\" let digit = case wibble { \"1\" as digit <> _rest | \"2\" as digit <> _rest -> digit other -> other } digit}", find_position_of("digit").nth_occurrence(4) );}
#[test]fn higlights_for_prefix_string_alias_in_let_assert() { assert_highlights!( "pub fn main() -> String { let assert \"1\" as digit <> _rest = \"1-wibble\" digit}", find_position_of("digit").nth_occurrence(1) );}
#[test]fn higlights_for_prefix_string_alias_in_let_assert_triggered_from_usage() { assert_highlights!( "pub fn main() -> String { let assert \"1\" as digit <> _rest = \"1-wibble\" digit}", find_position_of("digit").nth_occurrence(2) );}
#[test]fn higlights_for_prefix_string_suffix_variable_nested_in_tuple() { assert_highlights!( "fn main() { case #(\"1-wibble\", 0) { #(\"1\" <> rest, _) -> rest _ -> \"\" }}", find_position_of("rest").nth_occurrence(1) );}
#[test]fn higlights_for_prefix_string_alias_used_in_guard() { assert_highlights!( "fn main() { case \"1-wibble\" { \"1\" as digit <> _rest if digit == \"1\" -> digit _ -> \"\" }}", find_position_of("digit").nth_occurrence(1) );}
#[test]fn higlights_for_prefix_string_suffix_used_in_guard() { assert_highlights!( "fn main() { case \"1-wibble\" { \"1\" <> rest if rest == \"-wibble\" -> rest _ -> \"\" }}", find_position_of("rest").nth_occurrence(1) );}
#[test]fn higlights_for_prefix_string_suffix_shadowing_outer_variable() { assert_highlights!( "fn main() { let rest = \"outer\" case \"1-wibble\" { \"1\" <> rest -> rest _ -> rest }}", find_position_of("rest").nth_occurrence(2) );}
#[test]fn higlights_for_prefix_string_alias_and_suffix_complex_guard() { assert_highlights!( "fn main() { case \"1-wibble\" { \"1\" as digit <> rest if digit == \"1\" && rest == \"-wibble\" -> #(digit, rest) _ -> #(\"\", \"\") }}", find_position_of("digit").nth_occurrence(1) );}
#[test]fn higlights_for_local_variable_from_guard() { assert_highlights!( "pub fn main() { let wibble = True let wobble = False case wibble { True if wobble -> !wibble False if !wobble -> wibble _ -> wobble }}", find_position_of("wobble").nth_occurrence(2).under_char('o') );}
#[test]fn higlights_for_module_select_from_guard() { assert_highlights!( ("mod", "pub const wibble = 10"), "import mod
pub fn main() { let wibble = True case wibble { True if mod.wibble < 5 -> !wibble False if mod.wibble != 10 -> wibble _ -> mod.wibble + 1 }}", find_position_of("mod.wibble").under_char('w') );}
#[test]fn highlights_for_record_field_ignored_by_record_update_spread() { assert_highlights!( "type Wibble { Wibble(wibble: Int, wobble: Int)}
pub fn main(w: Wibble) { Wibble(..w, wibble: 2)}", find_position_of("wobble: Int").under_char('w'), );}