Automatically merge import lines from same module.

I slightly altered the way we parse import definitions to ensure we
  merge imports from the same modules (that aren't aliased) together.

  This prevents an annoying warning with duplicated import lines and
  makes it just more convenient overall.

  As a trade-off, we can no longer interleave import definitions with
  other definitions. This should be a minor setback only since the
  formatter was already ensuring that all import definitions would be
  grouped at the top.

  ---

  Note that, I originally attempted to implement this in the formatter
  instead of the parser. As it felt more appropriate there. However, the
  formatter operates on (unmutable) borrowed definitions, which makes it
  annoyingly hard to perform any AST manipulations. The `Document`
  returns by the format carries a lifetime that prevents the creation of
  intermediate local values.

  So instead, slightly tweaking the parser felt like the right thing to
  do.
This commit is contained in:
KtorZ
2024-06-04 10:35:21 +02:00
parent 7f4ca60d9a
commit d7ec2131ef
15 changed files with 272 additions and 78 deletions

View File

@@ -0,0 +1,51 @@
---
source: crates/aiken-lang/src/parser.rs
description: "Code:\n\nuse aiken/list.{bar, foo}\nuse aiken/list.{baz}\n"
---
Module {
name: "",
docs: [],
type_info: (),
definitions: [
Use(
Use {
as_name: None,
location: 0..25,
module: [
"aiken",
"list",
],
package: (),
unqualified: [
UnqualifiedImport {
location: 16..19,
name: "bar",
as_name: None,
},
UnqualifiedImport {
location: 21..24,
name: "foo",
as_name: None,
},
UnqualifiedImport {
location: 42..45,
name: "baz",
as_name: None,
},
],
},
),
],
lines: LineNumbers {
line_starts: [
0,
26,
47,
],
length: 47,
last: Some(
47,
),
},
kind: Validator,
}