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 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
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 @@ -7,10 +7,15 @@

use std::sync::Arc;

use common::ArgumentName;
use common::DirectiveName;
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 +36,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: DirectiveName(directive.name.value),
argument_name: ArgumentName(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,
ArgumentName(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,
ArgumentName(argument_name.value),
selection_path.parent,
schema,
),
ResolutionPath::Ident(IdentPath {
inner: field_name,
parent:
Expand Down Expand Up @@ -68,7 +122,7 @@ pub fn get_graphql_definition_description(
inner: directive_name,
parent: IdentParent::DirectiveName(_),
}) => Ok(DefinitionDescription::Directive {
directive_name: directive_name.value,
directive_name: DirectiveName(directive_name.value),
}),
ResolutionPath::Ident(IdentPath {
inner: type_name,
Expand All @@ -94,3 +148,20 @@ fn resolve_field(
field_name,
})
}

fn resolve_field_argument(
field_name: StringKey,
argument_name: ArgumentName,
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,
})
}
88 changes: 85 additions & 3 deletions compiler/crates/relay-lsp/src/goto_definition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod goto_graphql_definition;
use std::str;
use std::sync::Arc;

use common::ArgumentName;
use common::DirectiveName;
use graphql_ir::FragmentDefinitionName;
use intern::string_key::Intern;
Expand Down Expand Up @@ -44,14 +45,23 @@ pub enum DefinitionDescription {
parent_type: Type,
field_name: StringKey,
},
FieldArgument {
parent_type: Type,
field_name: StringKey,
argument_name: ArgumentName,
},
DirectiveArgument {
directive_name: DirectiveName,
argument_name: ArgumentName,
},
Fragment {
fragment_name: FragmentDefinitionName,
},
Type {
type_name: StringKey,
},
Directive {
directive_name: StringKey,
directive_name: DirectiveName,
},
}

Expand Down Expand Up @@ -81,6 +91,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 @@ -135,11 +162,11 @@ fn locate_fragment_definition(
}

fn locate_directive_definition(
directive_name: StringKey,
directive_name: DirectiveName,
schema: &Arc<SDLSchema>,
root_dir: &std::path::Path,
) -> Result<GotoDefinitionResponse, LSPRuntimeError> {
let directive = schema.get_directive(DirectiveName(directive_name));
let directive = schema.get_directive(directive_name);

directive
.map(|directive| directive.name.location)
Expand Down Expand Up @@ -203,6 +230,61 @@ fn locate_type_definition(
}
}

fn locate_field_argument_definition(
schema: &Arc<SDLSchema>,
parent_type: Type,
field_name: StringKey,
argument_name: ArgumentName,
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 == 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: DirectiveName,
argument_name: ArgumentName,
root_dir: &std::path::PathBuf,
) -> LSPRuntimeResult<GotoDefinitionResponse> {
let directive =
schema
.get_directive(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 == 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
Loading
Loading