Skip to content

Commit

Permalink
feat: track more element locations of import statements
Browse files Browse the repository at this point in the history
Only the keyword location was repoted as semantic token (in the LSP),
but more information was already available which is now properly added
to the token list.
  • Loading branch information
dnaka91 committed Feb 21, 2024
1 parent 6a6de5a commit ba12d5e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion crates/mabo-compiler/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Module<'_> {
})?
};

if let Some(element) = import.element.as_ref() {
if let Some((_, element)) = import.element.as_ref() {
let definition = module
.types
.iter()
Expand Down
5 changes: 4 additions & 1 deletion crates/mabo-compiler/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,10 @@ fn simplify_import<'a>(item: &'a mabo_parser::Import<'_>) -> Import<'a> {
Import {
source: item,
segments: item.segments.iter().map(mabo_parser::Name::get).collect(),
element: item.element.as_ref().map(|element| element.get().into()),
element: item
.element
.as_ref()
.map(|(_,element)| element.get().into()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/mabo-compiler/src/validate/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub(crate) fn validate_names_in_module(value: &[Definition<'_>]) -> Result<(), D
Definition::TypeAlias(a) => &a.name,
Definition::Const(c) => &c.name,
Definition::Import(Import {
element: Some(name),
element: Some((_, name)),
..
}) => name,
Definition::Import(Import { segments, .. }) => segments.last()?,
Expand Down
11 changes: 5 additions & 6 deletions crates/mabo-lsp/src/handlers/document_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,11 @@ fn visit_import(index: &Index, item: &Import<'_>) -> Result<DocumentSymbol> {
let mut name = item.segments[0].get().to_owned();
let mut span = Range::from(item.segments[0].span());

for segment in item
.segments
.iter()
.skip(1)
.chain(std::iter::once(&item.element).flatten())
{
for segment in item.segments.iter().skip(1).chain(
std::iter::once(&item.element)
.flatten()
.map(|(_, element)| element),
) {
let _ = write!(&mut name, "::{segment}");
span.end = Range::from(segment.span()).end;
}
Expand Down
10 changes: 9 additions & 1 deletion crates/mabo-lsp/src/handlers/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,14 @@ impl<'a> Visitor<'a> {
}

fn visit_import(&mut self, item: &Import<'_>) -> Result<()> {
self.add_span(&item.keyword, &types::KEYWORD, &[])
self.add_span(&item.keyword, &types::KEYWORD, &[])?;
for segment in &item.segments {
self.add_span(segment, &types::NAMESPACE, &[])?;
}
if let Some((token, element)) = &item.element {
self.add_span(token, &types::DOUBLE_COLON, &[])?;
self.add_span(element, &types::TYPE, &[])?;
}
Ok(())
}
}
6 changes: 3 additions & 3 deletions crates/mabo-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ pub struct Import<'a> {
pub segments: Vec<Name<'a>>,
/// Optional final element that allows to fully import the type, making it look as it would be
/// defined in the current schema.
pub element: Option<Name<'a>>,
pub element: Option<(token::DoubleColon, Name<'a>)>,
/// Trailing semicolon to complete the definition.
pub semicolon: token::Semicolon,
}
Expand All @@ -1309,8 +1309,8 @@ impl Print for Import<'_> {
f.write_str(segment.get())?;
}

if let Some(element) = element {
write!(f, "::{element}")?;
if let Some((token, element)) = element {
write!(f, "{token}{element}")?;
}

write!(f, "{semicolon}")
Expand Down
6 changes: 3 additions & 3 deletions crates/mabo-parser/src/parser/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Range;
use mabo_derive::{ParserError, ParserErrorCause};
use winnow::{
ascii::space1,
combinator::{alt, cut_err, opt, preceded, separated, terminated},
combinator::{alt, cut_err, opt, separated, terminated},
error::ErrorKind,
stream::{Location, Stream},
token::{one_of, take_while},
Expand Down Expand Up @@ -74,8 +74,8 @@ pub(super) fn parse<'i>(input: &mut Input<'i>) -> Result<Import<'i>, ParseError>
cut_err((
(
separated(1.., parse_segment, token::DoubleColon::VALUE.span()),
opt(preceded(
token::DoubleColon::VALUE.span(),
opt((
token::DoubleColon::VALUE.span().output_into(),
alt((
structs::parse_name.map_err(Cause::from),
enums::parse_name.map_err(Cause::from),
Expand Down
5 changes: 2 additions & 3 deletions crates/mabo-parser/src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,8 @@ fn parse_external<'i>(input: &mut Input<'i>) -> Result<ExternalType<'i>, Cause>
1..,
(
imports::parse_segment.map_err(Cause::from),
token::DoubleColon::VALUE.span(),
)
.map(|(segment, token)| (segment, token.into())),
token::DoubleColon::VALUE.span().output_into(),
),
))
.map(Option::unwrap_or_default),
parse_external_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ Schema {
},
],
element: Some(
Name {
value: "Sample",
},
(
DoubleColon,
Name {
value: "Sample",
},
),
),
semicolon: Semicolon,
},
Expand Down

0 comments on commit ba12d5e

Please sign in to comment.