Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Remove redundant positions in UnqualifiedImport

Co-authored-by: Louis Pilfold <louis@lpil.uk>

author
Gavin Morrow
co-author
Louis Pilfold
committer
Louis Pilfold
date (Jul 9, 2026, 6:07 PM +0100) commit 6a6ee278 parent 5ae60203 change-id rztsqxom
+80 -68
+6 -6
compiler-core/src/analyse/imports.rs
··· 120 120 ); 121 121 122 122 let alias_location = SrcSpan { 123 - start: import.imported_name_location.end, 123 + start: import.name_location().end, 124 124 end: import.location.end, 125 125 }; 126 126 ··· 128 128 type_info.module.clone(), 129 129 import.name.clone(), 130 130 imported_name, 131 - import.imported_name_location, 131 + import.name_location(), 132 132 ReferenceKind::Import(alias_location), 133 133 ); 134 134 ··· 203 203 ); 204 204 205 205 let alias_location = SrcSpan { 206 - start: import.imported_name_location.end, 206 + start: import.name_location().end, 207 207 end: import.location.end, 208 208 }; 209 209 ··· 211 211 module.clone(), 212 212 import_name.clone(), 213 213 used_name, 214 - import.imported_name_location, 214 + import.name_location(), 215 215 ReferenceKind::Import(alias_location), 216 216 ); 217 217 } ··· 227 227 ); 228 228 229 229 let alias_location = SrcSpan { 230 - start: import.imported_name_location.end, 230 + start: import.name_location().end, 231 231 end: import.location.end, 232 232 }; 233 233 ··· 235 235 module.clone(), 236 236 import_name.clone(), 237 237 used_name, 238 - import.imported_name_location, 238 + import.name_location(), 239 239 ReferenceKind::Import(alias_location), 240 240 ); 241 241 }
+17 -9
compiler-core/src/ast.rs
··· 1003 1003 1004 1004 if let Some(UnqualifiedImport { 1005 1005 location, 1006 - imported_name_location, 1006 + name_position, 1007 1007 name, 1008 1008 as_name, 1009 1009 }) = self ··· 1015 1015 crate::build::UnqualifiedImport { 1016 1016 name, 1017 1017 module: &self.module, 1018 - is_type: false, 1019 1018 location, 1020 - imported_name_location, 1019 + name_position: *name_position, 1021 1020 as_name: as_name.as_ref(), 1022 1021 }, 1023 1022 )); ··· 1025 1024 1026 1025 if let Some(UnqualifiedImport { 1027 1026 location, 1028 - imported_name_location, 1027 + name_position, 1029 1028 name, 1030 1029 as_name, 1031 1030 }) = self ··· 1037 1036 crate::build::UnqualifiedImport { 1038 1037 name, 1039 1038 module: &self.module, 1040 - is_type: true, 1041 1039 location, 1042 - imported_name_location, 1040 + name_position: *name_position, 1043 1041 as_name: as_name.as_ref(), 1044 1042 }, 1045 1043 )); ··· 1324 1322 #[derive(Debug, Clone, PartialEq, Eq)] 1325 1323 pub struct UnqualifiedImport { 1326 1324 pub location: SrcSpan, 1327 - /// The location excluding the potential `as ...` clause, or the `type` keyword. 1328 - /// For example, in `type Wibble as Wobble`, it covers `Wibble`. 1329 - pub imported_name_location: SrcSpan, 1330 1325 pub name: EcoString, 1326 + /// The position of the original name. For example, in `type Wibble as 1327 + /// Wobble`, it points to the start of `Wibble`. 1328 + pub name_position: u32, 1331 1329 pub as_name: Option<EcoString>, 1332 1330 } 1333 1331 1334 1332 impl UnqualifiedImport { 1335 1333 pub fn used_name(&self) -> &EcoString { 1336 1334 self.as_name.as_ref().unwrap_or(&self.name) 1335 + } 1336 + 1337 + /// The location of the original name, excluding the potential `as ...` 1338 + /// clause or the `type` keyword. For example, in `type Wibble as Wobble`, 1339 + /// it covers `Wibble`. 1340 + pub fn name_location(&self) -> SrcSpan { 1341 + SrcSpan::new( 1342 + self.name_position, 1343 + self.name_position + self.name.len() as u32, 1344 + ) 1337 1345 } 1338 1346 } 1339 1347
+37 -24
compiler-core/src/build.rs
··· 424 424 pub struct UnqualifiedImport<'a> { 425 425 pub name: &'a EcoString, 426 426 pub module: &'a EcoString, 427 - pub is_type: bool, 428 427 pub location: &'a SrcSpan, 429 - /// The location excluding the potential `as ...` clause, or the `type` keyword. 430 - /// For example, in `type Wibble as Wobble`, it covers `Wibble`. 431 - pub imported_name_location: &'a SrcSpan, 428 + /// The position of the original name. For example, in `type Wibble as 429 + /// Wobble`, it points to the start of `Wibble`. 430 + pub name_position: u32, 432 431 pub as_name: Option<&'a EcoString>, 433 432 } 434 433 435 434 impl<'a> UnqualifiedImport<'a> { 435 + /// The location of the original name, excluding the potential `as ...` 436 + /// clause or the `type` keyword. For example, in `type Wibble as Wobble`, 437 + /// it covers `Wibble`. 438 + pub fn name_location(&self) -> SrcSpan { 439 + SrcSpan::new( 440 + self.name_position, 441 + self.name_position + self.name.len() as u32, 442 + ) 443 + } 444 + 436 445 /// If the import is aliased, it is the start of the alias. Otherwise, it is 437 446 /// the start of the imported name. 438 447 /// 439 448 /// For example, in `type Wibble as Wobble`, the used name will start at 440 449 /// `Wobble`. In `type Wibble`, it will start at `Wibble`. 441 - pub fn used_name_start(&self) -> u32 { 450 + pub fn used_name_position(&self) -> u32 { 442 451 match self.as_name { 443 452 // For aliases, the location span will cover the whole of `type 444 453 // Wibble as Wobble`. ··· 447 456 Some(as_name) => self.location.end - as_name.len() as u32, 448 457 // For non-aliased imports, use the start of the imported name 449 458 // location. It covers `Wibble` in `type Wibble as Wobble`. 450 - None => self.imported_name_location.start, 459 + None => self.name_position, 451 460 } 452 461 } 453 462 ··· 462 471 } else { 463 472 Named::Function 464 473 } 474 + } 475 + 476 + pub fn is_type(&self) -> bool { 477 + // If the position is not at the start location then that means 478 + // that there is a `type` keyword before it, along with an amount of 479 + // whitespace and optional comments 480 + self.name_position != self.location.start 465 481 } 466 482 } 467 483 ··· 661 677 module: None, 662 678 span: record.location, 663 679 }), 664 - Self::UnqualifiedImport(UnqualifiedImport { 665 - module, 666 - name, 667 - is_type, 668 - .. 669 - }) => importable_modules.get(*module).and_then(|m| { 670 - if *is_type { 671 - m.types.get(*name).map(|t| DefinitionLocation { 672 - module: Some((*module).clone()), 673 - span: t.origin, 674 - }) 675 - } else { 676 - m.values.get(*name).map(|v| DefinitionLocation { 677 - module: Some((*module).clone()), 678 - span: v.definition_location().span, 679 - }) 680 - } 681 - }), 680 + Self::UnqualifiedImport(import @ UnqualifiedImport { module, name, .. }) => { 681 + importable_modules.get(*module).and_then(|m| { 682 + if import.is_type() { 683 + m.types.get(*name).map(|t| DefinitionLocation { 684 + module: Some((*module).clone()), 685 + span: t.origin, 686 + }) 687 + } else { 688 + m.values.get(*name).map(|v| DefinitionLocation { 689 + module: Some((*module).clone()), 690 + span: v.definition_location().span, 691 + }) 692 + } 693 + }) 694 + } 682 695 Self::Arg(_) => None, 683 696 Self::Annotation { type_, .. } => self.type_location(importable_modules, type_.clone()), 684 697 Self::Label { .. } => None,
+3 -3
compiler-core/src/parse.rs
··· 3122 3122 let mut import = UnqualifiedImport { 3123 3123 name, 3124 3124 location, 3125 - imported_name_location: location, 3125 + name_position: location.start, 3126 3126 as_name: None, 3127 3127 }; 3128 3128 if self.maybe_one(&Token::As).is_some() { ··· 3140 3140 let mut import = UnqualifiedImport { 3141 3141 name, 3142 3142 location, 3143 - imported_name_location: location, 3143 + name_position: location.start, 3144 3144 as_name: None, 3145 3145 }; 3146 3146 if self.maybe_one(&Token::As).is_some() { ··· 3158 3158 let mut import = UnqualifiedImport { 3159 3159 name, 3160 3160 location, 3161 - imported_name_location: SrcSpan::new(name_start, end), 3161 + name_position: name_start, 3162 3162 as_name: None, 3163 3163 }; 3164 3164 if self.maybe_one(&Token::As).is_some() {
+3 -12
compiler-core/src/parse/snapshots/gleam_core__parse__tests__import_type.snap
··· 28 28 start: 28, 29 29 end: 34, 30 30 }, 31 - imported_name_location: SrcSpan { 32 - start: 28, 33 - end: 34, 34 - }, 35 31 name: "Wobble", 32 + name_position: 28, 36 33 as_name: None, 37 34 }, 38 35 ], ··· 42 39 start: 15, 43 40 end: 26, 44 41 }, 45 - imported_name_location: SrcSpan { 46 - start: 20, 47 - end: 26, 48 - }, 49 42 name: "Wobble", 43 + name_position: 20, 50 44 as_name: None, 51 45 }, 52 46 UnqualifiedImport { ··· 54 48 start: 36, 55 49 end: 47, 56 50 }, 57 - imported_name_location: SrcSpan { 58 - start: 41, 59 - end: 47, 60 - }, 61 51 name: "Wabble", 52 + name_position: 41, 62 53 as_name: None, 63 54 }, 64 55 ],
+10 -9
language-server/src/engine.rs
··· 1318 1318 Located::VariantConstructorDefinition(constructor) => { 1319 1319 Some(hover_for_constructor(constructor, lines, module)) 1320 1320 } 1321 - Located::UnqualifiedImport(UnqualifiedImport { 1322 - name, 1323 - module: module_name, 1324 - is_type, 1325 - location, 1326 - imported_name_location: _, 1327 - as_name: _, 1328 - }) => this 1321 + Located::UnqualifiedImport( 1322 + import @ UnqualifiedImport { 1323 + name, 1324 + module: module_name, 1325 + location, 1326 + name_position: _, 1327 + as_name: _, 1328 + }, 1329 + ) => this 1329 1330 .compiler 1330 1331 .get_module_interface(module_name.as_str()) 1331 1332 .and_then(|module_interface| { 1332 - if is_type { 1333 + if import.is_type() { 1333 1334 module_interface.types.get(name).map(|constructor| { 1334 1335 hover_for_annotation( 1335 1336 *location,
+4 -5
language-server/src/reference.rs
··· 407 407 import @ UnqualifiedImport { 408 408 name, 409 409 module, 410 - is_type, 411 410 location, 412 - imported_name_location: _, 411 + name_position: _, 413 412 as_name: _, 414 413 }, 415 414 ) => { 416 - if is_type { 415 + if import.is_type() { 417 416 Some(Referenced::ModuleType { 418 417 module: module.clone(), 419 418 name: name.clone(), 420 419 location: *location, 421 - name_start: import.used_name_start(), 420 + name_start: import.used_name_position(), 422 421 target_kind: RenameTarget::Unqualified, 423 422 }) 424 423 } else { ··· 426 425 module: module.clone(), 427 426 name: name.clone(), 428 427 location: *location, 429 - name_start: import.used_name_start(), 428 + name_start: import.used_name_position(), 430 429 name_kind: import.name_kind(), 431 430 target_kind: RenameTarget::Unqualified, 432 431 })