Skip to content

Commit

Permalink
Add Inlay Hints for Fragment arguments (#4740)
Browse files Browse the repository at this point in the history
Summary:
![CleanShot 2024-07-13 at 16 31 43@2x](https://github.com/user-attachments/assets/d1927de0-524f-47e2-a86c-d4653f7011fd)

Pull Request resolved: #4740

Reviewed By: tyao1

Differential Revision: D60604769

Pulled By: captbaritone

fbshipit-source-id: 50a0e817c61c8d147767423246061a3bd1550afd
  • Loading branch information
tobias-tengler authored and facebook-github-bot committed Aug 5, 2024
1 parent c3e2b96 commit a6e27c2
Showing 1 changed file with 33 additions and 3 deletions.
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 @@ -159,8 +187,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

0 comments on commit a6e27c2

Please sign in to comment.