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

Configure Feed

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

Print public type names when generating documentation

+282 -36
+18 -13
compiler-core/src/analyse.rs
··· 1403 1403 }, 1404 1404 )?; 1405 1405 1406 - environment.insert_type_alias( 1407 - name.clone(), 1408 - TypeAliasConstructor { 1409 - origin: *location, 1410 - module: self.module_name.clone(), 1411 - type_, 1412 - publicity: *publicity, 1413 - deprecation: deprecation.clone(), 1414 - documentation: documentation.as_ref().map(|(_, doc)| doc.clone()), 1415 - arity, 1416 - parameters, 1417 - }, 1418 - )?; 1406 + let alias = TypeAliasConstructor { 1407 + origin: *location, 1408 + module: self.module_name.clone(), 1409 + type_, 1410 + publicity: *publicity, 1411 + deprecation: deprecation.clone(), 1412 + documentation: documentation.as_ref().map(|(_, doc)| doc.clone()), 1413 + arity, 1414 + parameters, 1415 + }; 1416 + 1417 + environment.names.maybe_register_reexport_alias( 1418 + &environment.current_package, 1419 + &name, 1420 + &alias, 1421 + ); 1422 + 1423 + environment.insert_type_alias(name.clone(), alias)?; 1419 1424 1420 1425 if let Some(name) = hydrator.unused_type_variables().next() { 1421 1426 return Err(Error::UnusedTypeAliasParameter {
+55 -14
compiler-core/src/docs/printer.rs
··· 15 15 docvec, 16 16 pretty::{Document, Documentable, break_, join, line, nil, zero_width_string}, 17 17 type_::{ 18 - Deprecation, PRELUDE_MODULE_NAME, PRELUDE_PACKAGE_NAME, Type, TypeVar, printer::Names, 18 + Deprecation, PRELUDE_MODULE_NAME, PRELUDE_PACKAGE_NAME, Type, TypeVar, 19 + printer::{Names, PrintMode}, 19 20 }, 20 21 }; 21 22 ··· 279 280 280 281 let arguments = constructor.arguments.iter().map( 281 282 |RecordConstructorArg { label, type_, .. }| match label { 282 - Some((_, label)) => self.variable(label).append(": ").append(self.type_(type_)), 283 - None => self.type_(type_), 283 + Some((_, label)) => self 284 + .variable(label) 285 + .append(": ") 286 + .append(self.type_(type_, PrintMode::Normal)), 287 + None => self.type_(type_, PrintMode::Normal), 284 288 }, 285 289 ); 286 290 ··· 309 313 self.title(name), 310 314 parameters, 311 315 " =", 312 - line().append(self.type_(type_)).nest(INDENT) 316 + line() 317 + .append(self.type_(type_, PrintMode::ExpandAliases)) 318 + .nest(INDENT) 313 319 ] 314 320 } 315 321 ··· 320 326 self.keyword("pub const "), 321 327 self.title(name), 322 328 ": ", 323 - self.type_(type_) 329 + self.type_(type_, PrintMode::Normal) 324 330 ] 325 331 } 326 332 ··· 340 346 } else { 341 347 Self::wrap_arguments(arguments.iter().map(|argument| { 342 348 let name = self.variable(self.argument_name(argument)); 343 - docvec![name, ": ", self.type_(&argument.type_)].group() 349 + docvec![name, ": ", self.type_(&argument.type_, PrintMode::Normal)].group() 344 350 })) 345 351 }; 346 352 ··· 349 355 self.title(name), 350 356 arguments, 351 357 " -> ", 352 - self.type_(return_type) 358 + self.type_(return_type, PrintMode::Normal) 353 359 ] 354 360 .group() 355 361 } ··· 387 393 .surround("(", ")") 388 394 } 389 395 390 - fn type_(&mut self, type_: &Type) -> Document<'static> { 396 + fn type_(&mut self, type_: &Type, print_mode: PrintMode) -> Document<'static> { 391 397 match type_ { 392 398 Type::Named { 393 399 package, ··· 397 403 publicity, 398 404 .. 399 405 } => { 400 - let name = self.named_type_name(publicity, package, module, name); 406 + let name = match print_mode { 407 + // If we are printing a type for a type alias, and the alias 408 + // is reexporting an internal type, we want to show that it 409 + // is aliasing that internal type, rather than showing it as 410 + // aliasing itself. 411 + PrintMode::ExpandAliases if *package == self.package => { 412 + self.named_type_name(publicity, package, module, name) 413 + } 414 + // If we are printing a type alias which aliases an internal 415 + // type from a different package, we still want to print the 416 + // public name for that type. If we are not printing a type 417 + // alias at all, we also want to use the public name. 418 + PrintMode::ExpandAliases | PrintMode::Normal => { 419 + // If we are using a reexported internal type, we want to 420 + // print it public name, whether it is from this package 421 + // or otherwise. 422 + if let Some((module, alias)) = 423 + self.names.reexport_alias(module.clone(), name.clone()) 424 + { 425 + self.named_type_name(&Publicity::Public, package, module, alias) 426 + } else { 427 + self.named_type_name(publicity, package, module, name) 428 + } 429 + } 430 + }; 431 + 401 432 if arguments.is_empty() { 402 433 name 403 434 } else { 404 435 name.append(Self::type_arguments( 405 - arguments.iter().map(|argument| self.type_(argument)), 436 + arguments 437 + .iter() 438 + .map(|argument| self.type_(argument, PrintMode::Normal)), 406 439 )) 407 440 } 408 441 } 409 442 Type::Fn { arguments, return_ } => docvec![ 410 443 self.keyword("fn"), 411 - Self::type_arguments(arguments.iter().map(|argument| self.type_(argument))), 444 + Self::type_arguments( 445 + arguments 446 + .iter() 447 + .map(|argument| self.type_(argument, PrintMode::Normal)) 448 + ), 412 449 " -> ", 413 - self.type_(return_) 450 + self.type_(return_, PrintMode::Normal) 414 451 ], 415 452 Type::Tuple { elements } => docvec![ 416 453 "#", 417 - Self::type_arguments(elements.iter().map(|element| self.type_(element))), 454 + Self::type_arguments( 455 + elements 456 + .iter() 457 + .map(|element| self.type_(element, PrintMode::Normal)) 458 + ), 418 459 ], 419 460 Type::Var { type_ } => match type_.as_ref().borrow().deref() { 420 - TypeVar::Link { type_ } => self.type_(type_), 461 + TypeVar::Link { type_ } => self.type_(type_, PrintMode::Normal), 421 462 422 463 TypeVar::Unbound { id } | TypeVar::Generic { id } => { 423 464 let name = self.type_variable(*id);
+29
compiler-core/src/docs/snapshots/gleam_core__docs__tests__function_uses_reexport_of_internal_type.snap
··· 1 + --- 2 + source: compiler-core/src/docs/tests.rs 3 + expression: output 4 + --- 5 + ---- SOURCE CODE 6 + -- thepackage/internal.gleam 7 + pub type Internal 8 + 9 + -- main.gleam 10 + 11 + import thepackage/internal 12 + 13 + pub type External = internal.Internal 14 + 15 + pub fn do_thing(value: internal.Internal) -> External { 16 + value 17 + } 18 + 19 + 20 + ---- TYPES 21 + 22 + --- External 23 + <pre><code>pub type External = 24 + @internal Internal</code></pre> 25 + 26 + ---- VALUES 27 + 28 + --- do_thing 29 + <pre><code>pub fn do_thing(value: <a href="#External">External</a>) -> <a href="#External">External</a></code></pre>
+28
compiler-core/src/docs/snapshots/gleam_core__docs__tests__function_uses_reexport_of_internal_type_in_other_module.snap
··· 1 + --- 2 + source: compiler-core/src/docs/tests.rs 3 + expression: output 4 + --- 5 + ---- SOURCE CODE 6 + -- thepackage/internal.gleam 7 + pub type Internal 8 + 9 + -- thepackage/something.gleam 10 + 11 + import thepackage/internal 12 + 13 + pub type External = internal.Internal 14 + 15 + 16 + -- main.gleam 17 + 18 + import thepackage/something 19 + 20 + pub fn do_thing(value: something.External) { 21 + value 22 + } 23 + 24 + 25 + ---- VALUES 26 + 27 + --- do_thing 28 + <pre><code>pub fn do_thing(value: <a href="thepackage/something.html#External" title="thepackage/something.{type External}">something.External</a>) -> <a href="thepackage/something.html#External" title="thepackage/something.{type External}">something.External</a></code></pre>
+27
compiler-core/src/docs/snapshots/gleam_core__docs__tests__use_reexport_from_other_package.snap
··· 1 + --- 2 + source: compiler-core/src/docs/tests.rs 3 + expression: output 4 + --- 5 + ---- SOURCE CODE 6 + -- some_package/internal.gleam 7 + pub type Internal 8 + 9 + -- some_package/api.gleam 10 + 11 + import some_package/internal 12 + pub type External = internal.Internal 13 + 14 + 15 + -- main.gleam 16 + 17 + import some_package/api 18 + 19 + pub fn do_thing(value: api.External) { 20 + value 21 + } 22 + 23 + 24 + ---- VALUES 25 + 26 + --- do_thing 27 + <pre><code>pub fn do_thing(value: <a href="https://hexdocs.pm/some_package/1.0.0/some_package/api.html#External" title="some_package/api.{type External}">api.External</a>) -> <a href="https://hexdocs.pm/some_package/1.0.0/some_package/api.html#External" title="some_package/api.{type External}">api.External</a></code></pre>
+63
compiler-core/src/docs/tests.rs
··· 1124 1124 } 1125 1125 1126 1126 #[test] 1127 + fn use_reexport_from_other_package() { 1128 + assert_documentation!( 1129 + ("some_package", "some_package/internal", "pub type Internal"), 1130 + ( 1131 + "some_package", 1132 + "some_package/api", 1133 + " 1134 + import some_package/internal 1135 + pub type External = internal.Internal 1136 + " 1137 + ), 1138 + " 1139 + import some_package/api 1140 + 1141 + pub fn do_thing(value: api.External) { 1142 + value 1143 + } 1144 + ", 1145 + ONLY_LINKS 1146 + ); 1147 + } 1148 + 1149 + #[test] 1150 + fn function_uses_reexport_of_internal_type() { 1151 + assert_documentation!( 1152 + ("thepackage/internal", "pub type Internal"), 1153 + " 1154 + import thepackage/internal 1155 + 1156 + pub type External = internal.Internal 1157 + 1158 + pub fn do_thing(value: internal.Internal) -> External { 1159 + value 1160 + } 1161 + ", 1162 + ONLY_LINKS 1163 + ); 1164 + } 1165 + 1166 + #[test] 1167 + fn function_uses_reexport_of_internal_type_in_other_module() { 1168 + assert_documentation!( 1169 + ("thepackage/internal", "pub type Internal"), 1170 + ( 1171 + "thepackage/something", 1172 + " 1173 + import thepackage/internal 1174 + 1175 + pub type External = internal.Internal 1176 + " 1177 + ), 1178 + " 1179 + import thepackage/something 1180 + 1181 + pub fn do_thing(value: something.External) { 1182 + value 1183 + } 1184 + ", 1185 + ONLY_LINKS 1186 + ); 1187 + } 1188 + 1189 + #[test] 1127 1190 fn constructor_with_long_types_and_many_fields() { 1128 1191 assert_documentation!( 1129 1192 ("option", "pub type Option(a)"),
+31
compiler-core/src/language_server/tests/action.rs
··· 9894 9894 find_position_of("main").to_selection(), 9895 9895 ); 9896 9896 } 9897 + 9898 + #[test] 9899 + fn add_type_annotations_uses_internal_name_for_same_package() { 9900 + let src = " 9901 + import thepackage/internal 9902 + 9903 + pub fn main() { 9904 + internal.Constructor 9905 + } 9906 + "; 9907 + 9908 + assert_code_action!( 9909 + ADD_ANNOTATION, 9910 + TestProject::for_source(src) 9911 + .add_module( 9912 + "thepackage/internal", 9913 + " 9914 + pub type Internal { Constructor } 9915 + " 9916 + ) 9917 + .add_module( 9918 + "thepackage/external", 9919 + " 9920 + import thepackage/internal 9921 + 9922 + pub type External = internal.Internal 9923 + " 9924 + ), 9925 + find_position_of("main").to_selection(), 9926 + ); 9927 + }
+21
compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__add_type_annotations_uses_internal_name_for_same_package.snap
··· 1 + --- 2 + source: compiler-core/src/language_server/tests/action.rs 3 + expression: "\nimport thepackage/internal\n\npub fn main() {\n internal.Constructor\n}\n" 4 + --- 5 + ----- BEFORE ACTION 6 + 7 + import thepackage/internal 8 + 9 + pub fn main() { 10 + 11 + internal.Constructor 12 + } 13 + 14 + 15 + ----- AFTER ACTION 16 + 17 + import thepackage/internal 18 + 19 + pub fn main() -> internal.Internal { 20 + internal.Constructor 21 + }
+1 -8
compiler-core/src/type_/environment.rs
··· 113 113 .get(PRELUDE_MODULE_NAME) 114 114 .expect("Unable to find prelude in importable modules"); 115 115 116 - let names = Self::build_names(&current_package, prelude, importable_modules); 116 + let names = Self::build_names(prelude, importable_modules); 117 117 118 118 Self { 119 119 current_package, ··· 144 144 } 145 145 146 146 fn build_names( 147 - current_package: &str, 148 147 prelude: &ModuleInterface, 149 148 importable_modules: &im::HashMap<EcoString, ModuleInterface>, 150 149 ) -> Names { ··· 164 163 165 164 // Find potential type aliases which reexport internal types 166 165 for module in importable_modules.values() { 167 - // Internal types are accessibly within the package they are defined, 168 - // so it doesn't make sense to look for reexports within the same 169 - // package. 170 - if module.package == current_package { 171 - continue; 172 - } 173 166 // Internal modules are not part of the public API so they are also 174 167 // not considered. 175 168 if module.is_internal {
+9 -1
compiler-core/src/type_/printer.rs
··· 256 256 } 257 257 258 258 /// Get the name and optional module qualifier for a named type. 259 - pub fn named_type<'a>( 259 + fn named_type<'a>( 260 260 &'a self, 261 261 module: &'a EcoString, 262 262 name: &'a EcoString, ··· 334 334 335 335 pub fn get_type_variable(&self, id: u64) -> Option<&EcoString> { 336 336 self.type_variables.get(&id) 337 + } 338 + 339 + pub fn reexport_alias( 340 + &self, 341 + module: EcoString, 342 + name: EcoString, 343 + ) -> Option<&(EcoString, EcoString)> { 344 + self.reexport_aliases.get(&(module, name)) 337 345 } 338 346 } 339 347