Skip to content
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

Add Inlay Hints for Fragment arguments #4740

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions compiler/crates/relay-lsp/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use common::Location;
use common::Span;
use graphql_ir::Argument;
use graphql_ir::FragmentDefinitionName;
use graphql_ir::FragmentSpread;
use graphql_ir::InlineFragment;
use graphql_ir::Program;
use graphql_ir::Visitor;
use intern::string_key::StringKey;
use lsp_types::request::InlayHintRequest;
Expand Down Expand Up @@ -40,9 +42,10 @@ pub fn on_inlay_hint_request(

let project_name = state.extract_project_name_from_url(&uri)?;
let schema = state.get_schema(&project_name)?;
let program = state.get_program(&project_name)?;
let asts = state.resolve_executable_definitions(&uri)?;
let irs = build_ir_for_lsp(&schema, &asts).map_err(|_| LSPRuntimeError::ExpectedError)?;
let mut visitor = InlayHintVisitor::new(&schema);
let mut visitor = InlayHintVisitor::new(&program, &schema);
for executable_definition in irs {
visitor.visit_executable_definition(&executable_definition);
}
Expand Down Expand Up @@ -95,13 +98,15 @@ impl Hint {
}

struct InlayHintVisitor<'a> {
program: &'a Program,
schema: &'a SDLSchema,
inlay_hints: Vec<Hint>,
}

impl<'a> InlayHintVisitor<'a> {
fn new(schema: &'a SDLSchema) -> Self {
fn new(program: &'a Program, schema: &'a SDLSchema) -> Self {
Self {
program,
schema,
inlay_hints: vec![],
}
Expand All @@ -127,6 +132,29 @@ impl<'a> InlayHintVisitor<'a> {
}
}
}

fn add_fragment_argument_hints(
&mut self,
fragment_name: FragmentDefinitionName,
arguments: &[Argument],
) {
if let Some(fragment) = self.program.fragment(fragment_name) {
for arg in arguments {
if let Some(variable_def) = fragment
.variable_definitions
.iter()
.find(|variable| variable.name.item.0 == arg.name.item.0)
{
let arg_type = self.schema.get_type_string(&variable_def.type_);
self.inlay_hints.push(Hint {
location: arg.value.location,
label: arg_type,
tooltip: None,
});
}
}
}
}
}

impl Visitor for InlayHintVisitor<'_> {
Expand Down Expand Up @@ -157,8 +185,10 @@ impl Visitor for InlayHintVisitor<'_> {
&spread.fragment.location,
Span::new(initial_span.start - 3, initial_span.end),
);
self.add_alias_hint(alias.item, adjusted_location)
self.add_alias_hint(alias.item, adjusted_location);
}

self.add_fragment_argument_hints(spread.fragment.item, &spread.arguments);
}

fn visit_inline_fragment(&mut self, fragment: &InlineFragment) {
Expand Down
Loading