-
-
Notifications
You must be signed in to change notification settings - Fork 799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lsp/inlay hints on pipe #3290
Draft
ascandone
wants to merge
12
commits into
gleam-lang:main
Choose a base branch
from
ascandone:lsp/inlay-hints-on-pipe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,062
−42
Draft
Lsp/inlay hints on pipe #3290
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c4a5f25
Implemented inlay hints
ascandone 06e57a0
fixes after rebase
ascandone 0b2a21d
fix functionality
ascandone 9fd5113
removed snapshots
ascandone 201f24a
impl hints on fn params
ascandone 7b9083d
cleaned up
ascandone 9cce62b
fix
ascandone 0adfbec
refactor
ascandone 55c0bef
more granular config and fixed tests regression
ascandone 6223e99
added tests
ascandone 2d59b32
added tests
ascandone 338efe1
removed snap
ascandone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use serde::Deserialize; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
pub type SharedConfig = Arc<RwLock<Configuration>>; | ||
|
||
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)] | ||
#[serde(default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Configuration { | ||
pub inlay_hints: InlayHintsConfig, | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)] | ||
#[serde(default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct InlayHintsConfig { | ||
/// Whether to show type inlay hints of multiline pipelines | ||
pub pipelines: bool, | ||
|
||
/// Whether to show type inlay hints of function parameters | ||
pub parameter_types: bool, | ||
|
||
/// Whether to show type inlay hints of return types of functions | ||
pub return_types: bool, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
use lsp_types::{InlayHint, InlayHintKind, InlayHintLabel}; | ||
|
||
use crate::{ | ||
ast::{ | ||
PipelineAssignmentKind, SrcSpan, TypeAst, TypedExpr, TypedModule, TypedPipelineAssignment, | ||
visit::Visit, | ||
}, | ||
line_numbers::LineNumbers, | ||
type_::{self, Type}, | ||
}; | ||
|
||
use super::{configuration::InlayHintsConfig, src_offset_to_lsp_position}; | ||
|
||
struct InlayHintsVisitor<'a> { | ||
config: InlayHintsConfig, | ||
module_names: &'a type_::printer::Names, | ||
current_declaration_printer: type_::printer::Printer<'a>, | ||
|
||
hints: Vec<InlayHint>, | ||
line_numbers: &'a LineNumbers, | ||
} | ||
|
||
fn default_inlay_hint(line_numbers: &LineNumbers, offset: u32, label: String) -> InlayHint { | ||
let position = src_offset_to_lsp_position(offset, line_numbers); | ||
|
||
InlayHint { | ||
position, | ||
label: InlayHintLabel::String(label), | ||
kind: Some(InlayHintKind::TYPE), | ||
text_edits: None, | ||
tooltip: None, | ||
padding_left: Some(true), | ||
padding_right: None, | ||
data: None, | ||
} | ||
} | ||
|
||
impl InlayHintsVisitor<'_> { | ||
pub fn push_binding_annotation( | ||
&mut self, | ||
type_: &Type, | ||
type_annotation_ast: Option<&TypeAst>, | ||
span: &SrcSpan, | ||
) { | ||
if type_annotation_ast.is_some() { | ||
return; | ||
} | ||
|
||
let label = format!(": {}", self.current_declaration_printer.print_type(type_)); | ||
|
||
let mut hint = default_inlay_hint(self.line_numbers, span.end, label); | ||
hint.padding_left = Some(false); | ||
|
||
self.hints.push(hint); | ||
} | ||
|
||
pub fn push_return_annotation( | ||
&mut self, | ||
type_: &Type, | ||
type_annotation_ast: Option<&TypeAst>, | ||
span: &SrcSpan, | ||
) { | ||
if type_annotation_ast.is_some() { | ||
return; | ||
} | ||
|
||
let label = format!("-> {}", self.current_declaration_printer.print_type(type_)); | ||
|
||
let hint = default_inlay_hint(self.line_numbers, span.end, label); | ||
|
||
self.hints.push(hint); | ||
} | ||
} | ||
|
||
impl<'ast> Visit<'ast> for InlayHintsVisitor<'_> { | ||
fn visit_typed_function(&mut self, fun: &'ast crate::ast::TypedFunction) { | ||
// This must be reset on every statement | ||
self.current_declaration_printer = type_::printer::Printer::new(self.module_names); | ||
|
||
for st in &fun.body { | ||
self.visit_typed_statement(st); | ||
} | ||
|
||
if self.config.parameter_types { | ||
for arg in &fun.arguments { | ||
self.push_binding_annotation(&arg.type_, arg.annotation.as_ref(), &arg.location); | ||
} | ||
} | ||
|
||
if self.config.return_types { | ||
self.push_return_annotation( | ||
&fun.return_type, | ||
fun.return_annotation.as_ref(), | ||
&fun.location, | ||
); | ||
} | ||
} | ||
|
||
fn visit_typed_expr_fn( | ||
&mut self, | ||
_location: &'ast SrcSpan, | ||
type_: &'ast std::sync::Arc<Type>, | ||
kind: &'ast crate::ast::FunctionLiteralKind, | ||
args: &'ast [crate::ast::TypedArg], | ||
body: &'ast vec1::Vec1<crate::ast::TypedStatement>, | ||
return_annotation: &'ast Option<TypeAst>, | ||
) { | ||
for st in body { | ||
self.visit_typed_statement(st); | ||
} | ||
|
||
let crate::ast::FunctionLiteralKind::Anonymous { head } = kind else { | ||
return; | ||
}; | ||
|
||
if self.config.parameter_types { | ||
for arg in args { | ||
self.push_binding_annotation(&arg.type_, arg.annotation.as_ref(), &arg.location); | ||
} | ||
} | ||
|
||
if self.config.return_types { | ||
if let Some((_args, ret_type)) = type_.fn_types() { | ||
self.push_return_annotation(&ret_type, return_annotation.as_ref(), head); | ||
} | ||
} | ||
} | ||
|
||
fn visit_typed_expr_pipeline( | ||
&mut self, | ||
_location: &'ast SrcSpan, | ||
first_value: &'ast TypedPipelineAssignment, | ||
assignments: &'ast [(TypedPipelineAssignment, PipelineAssignmentKind)], | ||
finally: &'ast TypedExpr, | ||
_finally_kind: &'ast PipelineAssignmentKind, | ||
) { | ||
self.visit_typed_pipeline_assignment(first_value); | ||
for (assignment, _kind) in assignments { | ||
self.visit_typed_pipeline_assignment(assignment); | ||
} | ||
self.visit_typed_expr(finally); | ||
|
||
if !self.config.pipelines { | ||
return; | ||
} | ||
|
||
let mut prev_hint: Option<(u32, Option<InlayHint>)> = None; | ||
|
||
let assigments_values = | ||
std::iter::once(first_value).chain(assignments.iter().map(|p| &p.0)); | ||
|
||
for assign in assigments_values { | ||
let this_line: u32 = self | ||
.line_numbers | ||
.line_and_column_number(assign.location.end) | ||
.line; | ||
|
||
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 = default_inlay_hint( | ||
self.line_numbers, | ||
assign.location.end, | ||
self.current_declaration_printer | ||
.print_type(assign.type_().as_ref()) | ||
.to_string(), | ||
); | ||
|
||
prev_hint = Some(( | ||
this_line, | ||
if assign.value.is_simple_lit() { | ||
None | ||
} else { | ||
Some(this_hint) | ||
}, | ||
)); | ||
} | ||
|
||
if let Some((prev_line, prev_hint)) = prev_hint { | ||
let this_line = self | ||
.line_numbers | ||
.line_and_column_number(finally.location().end) | ||
.line; | ||
if this_line != prev_line { | ||
if let Some(prev_hint) = prev_hint { | ||
self.hints.push(prev_hint); | ||
} | ||
let hint = default_inlay_hint( | ||
self.line_numbers, | ||
finally.location().end, | ||
self.current_declaration_printer | ||
.print_type(finally.type_().as_ref()) | ||
.to_string(), | ||
); | ||
self.hints.push(hint); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn get_inlay_hints( | ||
config: InlayHintsConfig, | ||
typed_module: TypedModule, | ||
line_numbers: &LineNumbers, | ||
) -> Vec<InlayHint> { | ||
let mut visitor = InlayHintsVisitor { | ||
config, | ||
module_names: &typed_module.names, | ||
current_declaration_printer: type_::printer::Printer::new(&typed_module.names), | ||
hints: vec![], | ||
line_numbers, | ||
}; | ||
|
||
visitor.visit_typed_module(&typed_module); | ||
visitor.hints | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
snake_case please, we never use camelCase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update: I noticed that for a better integration with vscode, it's better to use camelCase instead
you can take a look at how it is rendered in the client side pr: gleam-lang/vscode-gleam#82