-
-
Notifications
You must be signed in to change notification settings - Fork 777
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
331 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ mod constant; | |
mod typed; | ||
mod untyped; | ||
|
||
pub mod inlay_hints; | ||
#[cfg(test)] | ||
mod tests; | ||
pub mod visit; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use crate::{ | ||
ast::{ | ||
visit::{self, Visit}, | ||
SrcSpan, TypedAssignment, TypedExpr, TypedModule, | ||
}, | ||
line_numbers::LineNumbers, | ||
type_::pretty::Printer, | ||
}; | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
pub struct InlayHint { | ||
pub label: String, | ||
pub offset: u32, | ||
} | ||
|
||
fn is_simple_lit(expr: &TypedExpr) -> bool { | ||
matches!( | ||
expr, | ||
TypedExpr::Int { .. } | ||
| TypedExpr::Float { .. } | ||
| TypedExpr::String { .. } | ||
| TypedExpr::BitArray { .. } | ||
) | ||
} | ||
|
||
struct InlayHintsVisitor<'a> { | ||
hints: Vec<InlayHint>, | ||
line_numbers: &'a LineNumbers, | ||
} | ||
|
||
impl<'a> InlayHintsVisitor<'a> { | ||
fn new(line_numbers: &'a LineNumbers) -> InlayHintsVisitor<'a> { | ||
InlayHintsVisitor { | ||
hints: vec![], | ||
line_numbers, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, 'ast> Visit<'ast> for InlayHintsVisitor<'a> { | ||
fn visit_typed_expr_pipeline( | ||
&mut self, | ||
_location: &'ast SrcSpan, | ||
assignments: &'ast [TypedAssignment], | ||
finally: &'ast TypedExpr, | ||
) { | ||
fn get_this_line(this: &InlayHintsVisitor<'_>, span: &SrcSpan) -> u32 { | ||
this.line_numbers.line_and_column_number(span.end).line | ||
} | ||
|
||
let mut prev_hint: Option<(u32, Option<InlayHint>)> = None; | ||
for assign in assignments { | ||
let this_line = get_this_line(self, &assign.location); | ||
|
||
if let Some((prev_line, prev_hint)) = prev_hint { | ||
if prev_line != this_line { | ||
if let Some(prev_hint) = prev_hint { | ||
self.hints.push(prev_hint); | ||
} | ||
} | ||
}; | ||
|
||
let this_hint = InlayHint { | ||
label: Printer::new().pretty_print(assign.type_().as_ref(), 0), | ||
offset: assign.location.end, | ||
}; | ||
prev_hint = Some(( | ||
this_line, | ||
if is_simple_lit(&assign.value) { | ||
None | ||
} else { | ||
Some(this_hint) | ||
}, | ||
)); | ||
|
||
visit::visit_typed_expr(self, &assign.value); | ||
} | ||
|
||
if let Some((prev_line, prev_hint)) = prev_hint { | ||
let this_line = get_this_line(self, &finally.location()); | ||
if this_line != prev_line { | ||
if let Some(prev_hint) = prev_hint { | ||
self.hints.push(prev_hint); | ||
} | ||
self.hints.push(InlayHint { | ||
label: Printer::new().pretty_print(finally.type_().as_ref(), 0), | ||
offset: finally.location().end, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn get_inlay_hints(typed_module: TypedModule, line_numbers: &LineNumbers) -> Vec<InlayHint> { | ||
let mut visitor = InlayHintsVisitor::new(line_numbers); | ||
visitor.visit_typed_module(&typed_module); | ||
visitor.hints | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.