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

[relay-lsp] Support go to definition for arguments #4605

Closed
Closed
Show file tree
Hide file tree
Changes from 6 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
10 changes: 5 additions & 5 deletions compiler/crates/graphql-ir/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ impl<'schema, 'signatures, 'options> Builder<'schema, 'signatures, 'options> {
None => {
let possible_argument_names = argument_definitions
.iter()
.map(|arg_def| arg_def.name.0)
.map(|arg_def| arg_def.name.item.0)
.collect::<Vec<_>>();
let suggestions = suggestion_list::suggestion_list(
argument.name.value,
Expand Down Expand Up @@ -1268,9 +1268,9 @@ impl<'schema, 'signatures, 'options> Builder<'schema, 'signatures, 'options> {
arguments
.iter()
.flat_map(|args| &args.items)
.all(|arg| arg.name.value != required_arg_def.name.0)
.all(|arg| arg.name.value != required_arg_def.name.item.0)
})
.map(|missing_arg| missing_arg.name.0)
.map(|missing_arg| missing_arg.name.item.0)
.filter(is_non_nullable_field_required)
.collect::<Vec<_>>();
if !missing_arg_names.is_empty() {
Expand Down Expand Up @@ -1570,7 +1570,7 @@ impl<'schema, 'signatures, 'options> Builder<'schema, 'signatures, 'options> {
.fields
.iter()
.filter(|x| x.type_.is_non_null())
.map(|x| x.name.0)
.map(|x| x.name.item.0)
.collect::<StringKeySet>();

let fields = object
Expand Down Expand Up @@ -1721,7 +1721,7 @@ impl<'schema, 'signatures, 'options> Builder<'schema, 'signatures, 'options> {
.fields
.iter()
.filter(|x| x.type_.is_non_null())
.map(|x| x.name.0)
.map(|x| x.name.item.0)
.collect::<StringKeySet>();

let mut errors = vec![];
Expand Down
4 changes: 2 additions & 2 deletions compiler/crates/relay-lsp/src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ trait ArgumentLike {

impl ArgumentLike for &SchemaArgument {
fn name(&self) -> StringKey {
self.name.0
self.name.item.0
}
fn type_(&self) -> &TypeReference<Type> {
&self.type_
Expand Down Expand Up @@ -844,7 +844,7 @@ fn completion_items_for_request(
input_object
.fields
.iter()
.find(|field| field.name.0 == *field_name)
.find(|field| field.name.item.0 == *field_name)
.and_then(|field| resolve_root_input_field(schema, &field.type_))
}

Expand Down
29 changes: 29 additions & 0 deletions compiler/crates/relay-lsp/src/docblock_resolution_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
use common::Span;
use graphql_ir::reexport::StringKey;
use graphql_ir::FragmentDefinitionName;
use graphql_syntax::Identifier;
use relay_docblock::DocblockIr;
use relay_docblock::On;

pub enum DocblockResolutionInfo {
Type(StringKey),
RootFragment(FragmentDefinitionName),
FieldName(StringKey),
FieldArgumentName {
field_name: Identifier,
argument_name: Identifier,
},
Deprecated,
}

Expand Down Expand Up @@ -51,6 +56,18 @@ pub fn create_docblock_resolution_info(
));
}

// Field arguments
if let Some(field_arguments) = &resolver_ir.field.arguments {
for field_argument in &field_arguments.items {
if field_argument.name.span.contains(position_span) {
return Some(DocblockResolutionInfo::FieldArgumentName {
field_name: resolver_ir.field.name,
argument_name: field_argument.name,
});
}
}
}

// Return type
if let Some(output_type) = &resolver_ir.output_type {
if output_type.inner().location.contains(position_span) {
Expand Down Expand Up @@ -100,6 +117,18 @@ pub fn create_docblock_resolution_info(
));
}

// Field arguments
if let Some(field_arguments) = &resolver_ir.field.arguments {
for field_argument in &field_arguments.items {
if field_argument.name.span.contains(position_span) {
return Some(DocblockResolutionInfo::FieldArgumentName {
field_name: resolver_ir.field.name,
argument_name: field_argument.name,
});
}
}
}

// @deprecated key
if let Some(deprecated) = resolver_ir.deprecated {
if deprecated.key_location().contains(position_span) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ fn get_schema_explorer_input_object(
get_empty_schema_explorer_type_reference(arg.type_.inner(), schema, documentation);

SchemaExplorerFieldArgument {
argument_name: arg.name.to_string(),
argument_name: arg.name.item.to_string(),
argument_description: None,
default_value: arg.default_value.as_ref().map(|value| value.to_string()),
rendered_type_name: schema.get_type_string(&arg.type_),
Expand Down Expand Up @@ -399,12 +399,12 @@ fn get_schema_explorer_field(
.get_field_argument_description(
parent_type_name.lookup(),
&field_name,
arg.name.0.lookup(),
arg.name.item.0.lookup(),
)
.map(|field_argument_description| field_argument_description.to_string());

SchemaExplorerFieldArgument {
argument_name: arg.name.to_string(),
argument_name: arg.name.item.to_string(),
argument_description,
rendered_type_name: schema.get_type_string(&arg.type_),
type_reference,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ pub fn get_docblock_definition_description(
Ok(DefinitionDescription::Fragment { fragment_name })
}
DocblockResolutionInfo::FieldName(_) => {
// The field name _id_ the definition of the field.
// The field name _is_ the definition of the field.
Err(LSPRuntimeError::ExpectedError)
}
DocblockResolutionInfo::FieldArgumentName {
field_name: _,
argument_name: _,
} => {
// The argument name _is_ the definition of the argument.
Err(LSPRuntimeError::ExpectedError)
}
DocblockResolutionInfo::Deprecated => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use common::Span;
use graphql_ir::FragmentDefinitionName;
use graphql_syntax::ExecutableDocument;
use intern::string_key::StringKey;
use resolution_path::ArgumentParent;
use resolution_path::ArgumentPath;
use resolution_path::DirectivePath;
use resolution_path::IdentParent;
use resolution_path::IdentPath;
use resolution_path::LinkedFieldPath;
Expand All @@ -31,13 +34,62 @@ pub fn get_graphql_definition_description(
schema: &Arc<SDLSchema>,
) -> LSPRuntimeResult<DefinitionDescription> {
let node_path = document.resolve((), position_span);

match node_path {
ResolutionPath::Ident(IdentPath {
inner: fragment_name,
parent: IdentParent::FragmentSpreadName(_),
}) => Ok(DefinitionDescription::Fragment {
fragment_name: FragmentDefinitionName(fragment_name.value),
}),
ResolutionPath::Ident(IdentPath {
inner: argument_name,
parent:
IdentParent::ArgumentName(ArgumentPath {
inner: _,
parent:
ArgumentParent::Directive(DirectivePath {
inner: directive, ..
}),
}),
}) => Ok(DefinitionDescription::DirectiveArgument {
directive_name: directive.name.value,
argument_name: argument_name.value,
}),
ResolutionPath::Ident(IdentPath {
inner: argument_name,
parent:
IdentParent::ArgumentName(ArgumentPath {
inner: _,
parent:
ArgumentParent::ScalarField(ScalarFieldPath {
inner: field,
parent: selection_path,
}),
}),
}) => resolve_field_argument(
field.name.value,
argument_name.value,
selection_path.parent,
schema,
),
ResolutionPath::Ident(IdentPath {
inner: argument_name,
parent:
IdentParent::ArgumentName(ArgumentPath {
inner: _,
parent:
ArgumentParent::LinkedField(LinkedFieldPath {
inner: field,
parent: selection_path,
}),
}),
}) => resolve_field_argument(
field.name.value,
argument_name.value,
selection_path.parent,
schema,
),
ResolutionPath::Ident(IdentPath {
inner: field_name,
parent:
Expand Down Expand Up @@ -94,3 +146,20 @@ fn resolve_field(
field_name,
})
}

fn resolve_field_argument(
field_name: StringKey,
argument_name: StringKey,
selection_parent: SelectionParent<'_>,
schema: &Arc<SDLSchema>,
) -> LSPRuntimeResult<DefinitionDescription> {
let parent_type = selection_parent
.find_parent_type(schema)
.ok_or(LSPRuntimeError::ExpectedError)?;

Ok(DefinitionDescription::FieldArgument {
parent_type,
field_name,
argument_name,
})
}
80 changes: 80 additions & 0 deletions compiler/crates/relay-lsp/src/goto_definition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ pub enum DefinitionDescription {
parent_type: Type,
field_name: StringKey,
},
FieldArgument {
parent_type: Type,
field_name: StringKey,
argument_name: StringKey,
},
DirectiveArgument {
directive_name: StringKey,
tobias-tengler marked this conversation as resolved.
Show resolved Hide resolved
argument_name: StringKey,
},
Fragment {
fragment_name: FragmentDefinitionName,
},
Expand Down Expand Up @@ -81,6 +90,23 @@ pub fn on_goto_definition(
let root_dir = state.root_dir();

let goto_definition_response: GotoDefinitionResponse = match definition_description {
DefinitionDescription::FieldArgument {
parent_type,
field_name,
argument_name,
} => locate_field_argument_definition(
&schema,
parent_type,
field_name,
argument_name,
&root_dir,
)?,
DefinitionDescription::DirectiveArgument {
directive_name,
argument_name,
} => {
locate_directive_argument_definition(&schema, directive_name, argument_name, &root_dir)?
}
DefinitionDescription::Field {
parent_type,
field_name,
Expand Down Expand Up @@ -203,6 +229,60 @@ fn locate_type_definition(
}
}

fn locate_field_argument_definition(
schema: &Arc<SDLSchema>,
parent_type: Type,
field_name: StringKey,
argument_name: StringKey,
root_dir: &std::path::Path,
) -> Result<GotoDefinitionResponse, LSPRuntimeError> {
let field = schema.field(schema.named_field(parent_type, field_name).ok_or_else(|| {
LSPRuntimeError::UnexpectedError(format!("Could not find field with name {}", field_name))
})?);

let argument = field
.arguments
.iter()
.find(|argument| argument.name.item.0 == argument_name)
.ok_or_else(|| {
LSPRuntimeError::UnexpectedError(format!(
"Could not find argument with name {} on field with name {}",
argument_name, field_name,
))
})?;

transform_relay_location_to_lsp_location(root_dir, argument.name.location)
.map(|location| Ok(GotoDefinitionResponse::Scalar(location)))?
}

fn locate_directive_argument_definition(
schema: &SDLSchema,
directive_name: StringKey,
argument_name: StringKey,
root_dir: &std::path::PathBuf,
) -> LSPRuntimeResult<GotoDefinitionResponse> {
let directive = schema.get_directive(DirectiveName(directive_name)).ok_or(
LSPRuntimeError::UnexpectedError(format!(
"Could not find directive with name {}",
directive_name
)),
)?;

let argument = directive
.arguments
.iter()
.find(|argument| argument.name.item.0 == argument_name)
.ok_or_else(|| {
LSPRuntimeError::UnexpectedError(format!(
"Could not find argument with name {} on directive with name {}",
argument_name, directive_name,
))
})?;

transform_relay_location_to_lsp_location(root_dir, argument.name.location)
.map(|location| Ok(GotoDefinitionResponse::Scalar(location)))?
}

fn locate_field_definition(
schema: &Arc<SDLSchema>,
parent_type: Type,
Expand Down
4 changes: 2 additions & 2 deletions compiler/crates/relay-lsp/src/hover/with_resolution_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ fn get_scalar_or_linked_field_hover_content(
let arg_type_name = schema.get_type_name(arg.type_.inner()).lookup();
hover_contents.push(MarkedString::from_markdown(format!(
"{}: **{}**{}\n\n{}",
arg.name,
arg.name.item,
content_consumer_type.render_text_with_params(
&schema.get_type_string(&arg.type_),
&GraphQLSchemaExplorerParams {
Expand All @@ -729,7 +729,7 @@ fn get_scalar_or_linked_field_hover_content(
if let Some(description) = schema_documentation.get_field_argument_description(
parent_type_name,
field.name.item.lookup(),
arg.name.0.lookup(),
arg.name.item.0.lookup(),
) {
description.to_string()
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn build_schema_with_extensions<T: AsRef<str>, U: AsRef<str>>(
if let Some(directive) = schema.get_directive_mut(*directive_name) {
let mut next_args: Vec<_> = directive.arguments.iter().cloned().collect();
for arg in next_args.iter_mut() {
if arg.name == *LABEL {
if arg.name.item == *LABEL {
if let TypeReference::NonNull(of) = &arg.type_ {
arg.type_ = *of.clone()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn build_refetch_operation(
alias: None,
definition: WithLocation::new(fragment.name.location, fetch_field_id),
arguments: vec![Argument {
name: WithLocation::new(fragment.name.location, id_arg.name),
name: WithLocation::new(fragment.name.location, id_arg.name.item),
value: WithLocation::new(
fragment.name.location,
Value::Variable(Variable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn build_refetch_operation(
alias: None,
definition: WithLocation::new(fragment.name.location, node_field_id),
arguments: vec![Argument {
name: WithLocation::new(fragment.name.location, id_arg.name),
name: WithLocation::new(fragment.name.location, id_arg.name.item),
value: WithLocation::new(
fragment.name.location,
Value::Variable(Variable {
Expand Down
Loading
Loading