Skip to content

Commit 16f9fed

Browse files
GearsDatapackslpil
authored andcommitted
Show documentation for each variant when a field is shared across variants
1 parent a24cf9a commit 16f9fed

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

compiler-core/src/analyse.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::{
3838
warning::TypeWarningEmitter,
3939
};
4040
use camino::Utf8PathBuf;
41-
use ecow::EcoString;
41+
use ecow::{EcoString, eco_format};
4242
use hexpm::version::Version;
4343
use itertools::Itertools;
4444
use name::{check_argument_names, check_name_case};
@@ -1979,7 +1979,20 @@ fn get_compatible_record_fields(constructors: &[TypeValueConstructor]) -> Vec<Re
19791979
None => continue 'next_argument,
19801980
};
19811981

1982-
let mut documentation = first_parameter.documentation.clone();
1982+
let mut documentation = if constructors.len() == 1 {
1983+
// If there is only one constructor, we simply show the documentation
1984+
// for the field.
1985+
first_parameter.documentation.clone()
1986+
} else if let Some(field_documentation) = &first_parameter.documentation {
1987+
// If there are multiple constructors, we show the documentation of
1988+
// this field for each of the variants.
1989+
Some(eco_format!("## {}\n\n{}", first.name, field_documentation))
1990+
} else {
1991+
// If there is no documentation on the field of the first constructor,
1992+
// we leave it as `None` until we find a field which does have
1993+
// documentation.
1994+
None
1995+
};
19831996

19841997
// Check each variant to see if they have an field in the same position
19851998
// with the same label and the same type
@@ -2004,9 +2017,20 @@ fn get_compatible_record_fields(constructors: &[TypeValueConstructor]) -> Vec<Re
20042017
continue 'next_argument;
20052018
}
20062019

2007-
// If there is more than one variant, we can't show documentation
2008-
// as we do not know which field it is.
2009-
documentation = None;
2020+
if let Some(field_documentation) = &parameter.documentation {
2021+
let field_documentation =
2022+
eco_format!("## {}\n\n{}", constructor.name, field_documentation);
2023+
2024+
match &mut documentation {
2025+
None => {
2026+
documentation = Some(field_documentation);
2027+
}
2028+
Some(documentation) => {
2029+
documentation.push('\n');
2030+
documentation.push_str(&field_documentation);
2031+
}
2032+
}
2033+
}
20102034
}
20112035

20122036
// The previous loop did not find any incompatible fields in the other

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ pub type Wibble {
17801780
wibble: Int
17811781
)
17821782
Wobble(
1783-
/// This won't show up because we don't know which wibble field it is
1783+
/// Here's some documentation explaining a field of Wobble
17841784
wibble: Int
17851785
)
17861786
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__hover__no_documentation_for_shared_record_field.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
source: compiler-core/src/language_server/tests/hover.rs
3-
expression: "\npub type Wibble {\n Wibble(\n /// This is some documentation about the wibble field.\n wibble: Int\n )\n Wobble(\n /// This won't show up because we don't know which wibble field it is\n wibble: Int\n )\n}\n\npub fn wibble(w: Wibble) {\n w.wibble\n}\n"
3+
expression: "\npub type Wibble {\n Wibble(\n /// This is some documentation about the wibble field.\n wibble: Int\n )\n Wobble(\n /// Here's some documentation explaining a field of Wobble\n wibble: Int\n )\n}\n\npub fn wibble(w: Wibble) {\n w.wibble\n}\n"
44
---
55
pub type Wibble {
66
Wibble(
77
/// This is some documentation about the wibble field.
88
wibble: Int
99
)
1010
Wobble(
11-
/// This won't show up because we don't know which wibble field it is
11+
/// Here's some documentation explaining a field of Wobble
1212
wibble: Int
1313
)
1414
}
@@ -22,6 +22,6 @@ pub fn wibble(w: Wibble) {
2222
----- Hover content -----
2323
Scalar(
2424
String(
25-
"```gleam\nInt\n```\n",
25+
"```gleam\nInt\n```\n## Wibble\n\n This is some documentation about the wibble field.\n\n## Wobble\n\n Here's some documentation explaining a field of Wobble\n",
2626
),
2727
)

0 commit comments

Comments
 (0)