Skip to content

Commit a6e27c2

Browse files
tobias-tenglerfacebook-github-bot
authored andcommitted
Add Inlay Hints for Fragment arguments (#4740)
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
1 parent c3e2b96 commit a6e27c2

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

compiler/crates/relay-lsp/src/inlay_hints.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use common::Location;
99
use common::Span;
1010
use graphql_ir::Argument;
11+
use graphql_ir::FragmentDefinitionName;
1112
use graphql_ir::FragmentSpread;
1213
use graphql_ir::InlineFragment;
14+
use graphql_ir::Program;
1315
use graphql_ir::Visitor;
1416
use intern::string_key::StringKey;
1517
use lsp_types::request::InlayHintRequest;
@@ -40,9 +42,10 @@ pub fn on_inlay_hint_request(
4042

4143
let project_name = state.extract_project_name_from_url(&uri)?;
4244
let schema = state.get_schema(&project_name)?;
45+
let program = state.get_program(&project_name)?;
4346
let asts = state.resolve_executable_definitions(&uri)?;
4447
let irs = build_ir_for_lsp(&schema, &asts).map_err(|_| LSPRuntimeError::ExpectedError)?;
45-
let mut visitor = InlayHintVisitor::new(&schema);
48+
let mut visitor = InlayHintVisitor::new(&program, &schema);
4649
for executable_definition in irs {
4750
visitor.visit_executable_definition(&executable_definition);
4851
}
@@ -95,13 +98,15 @@ impl Hint {
9598
}
9699

97100
struct InlayHintVisitor<'a> {
101+
program: &'a Program,
98102
schema: &'a SDLSchema,
99103
inlay_hints: Vec<Hint>,
100104
}
101105

102106
impl<'a> InlayHintVisitor<'a> {
103-
fn new(schema: &'a SDLSchema) -> Self {
107+
fn new(program: &'a Program, schema: &'a SDLSchema) -> Self {
104108
Self {
109+
program,
105110
schema,
106111
inlay_hints: vec![],
107112
}
@@ -127,6 +132,29 @@ impl<'a> InlayHintVisitor<'a> {
127132
}
128133
}
129134
}
135+
136+
fn add_fragment_argument_hints(
137+
&mut self,
138+
fragment_name: FragmentDefinitionName,
139+
arguments: &[Argument],
140+
) {
141+
if let Some(fragment) = self.program.fragment(fragment_name) {
142+
for arg in arguments {
143+
if let Some(variable_def) = fragment
144+
.variable_definitions
145+
.iter()
146+
.find(|variable| variable.name.item.0 == arg.name.item.0)
147+
{
148+
let arg_type = self.schema.get_type_string(&variable_def.type_);
149+
self.inlay_hints.push(Hint {
150+
location: arg.value.location,
151+
label: arg_type,
152+
tooltip: None,
153+
});
154+
}
155+
}
156+
}
157+
}
130158
}
131159

132160
impl Visitor for InlayHintVisitor<'_> {
@@ -159,8 +187,10 @@ impl Visitor for InlayHintVisitor<'_> {
159187
&spread.fragment.location,
160188
Span::new(initial_span.start - 3, initial_span.end),
161189
);
162-
self.add_alias_hint(alias.item, adjusted_location)
190+
self.add_alias_hint(alias.item, adjusted_location);
163191
}
192+
193+
self.add_fragment_argument_hints(spread.fragment.item, &spread.arguments);
164194
}
165195

166196
fn visit_inline_fragment(&mut self, fragment: &InlineFragment) {

0 commit comments

Comments
 (0)