From a09adc048f28d4c78c89c54f00dc4f8f618f6231 Mon Sep 17 00:00:00 2001 From: tobias-tengler <45513122+tobias-tengler@users.noreply.github.com> Date: Fri, 5 Apr 2024 21:40:01 +0200 Subject: [PATCH] Implement go to definition support --- .../goto_graphql_definition.rs | 52 +++++++++++++++++++ .../relay-lsp/src/goto_definition/mod.rs | 5 +- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/compiler/crates/relay-lsp/src/goto_definition/goto_graphql_definition.rs b/compiler/crates/relay-lsp/src/goto_definition/goto_graphql_definition.rs index 08cabcf61790d..d8d3f70d98d7e 100644 --- a/compiler/crates/relay-lsp/src/goto_definition/goto_graphql_definition.rs +++ b/compiler/crates/relay-lsp/src/goto_definition/goto_graphql_definition.rs @@ -12,9 +12,13 @@ use common::DirectiveName; use common::Span; use graphql_ir::FragmentDefinitionName; use graphql_syntax::ExecutableDocument; +use graphql_syntax::SchemaDocument; use intern::string_key::StringKey; use resolution_path::ArgumentParent; use resolution_path::ArgumentPath; +use resolution_path::ConstantArgumentParent; +use resolution_path::ConstantArgumentPath; +use resolution_path::ConstantDirectivePath; use resolution_path::DirectivePath; use resolution_path::IdentParent; use resolution_path::IdentPath; @@ -30,6 +34,54 @@ use super::DefinitionDescription; use crate::lsp_runtime_error::LSPRuntimeError; use crate::lsp_runtime_error::LSPRuntimeResult; +pub fn get_graphql_schema_definition_description( + document: SchemaDocument, + position_span: Span, +) -> LSPRuntimeResult { + let node_path = document.resolve((), position_span); + + match node_path { + ResolutionPath::Ident(IdentPath { + inner: type_name, + parent: + IdentParent::NamedTypeAnnotation(_) + | IdentParent::UnionTypeMemberType(_) + | IdentParent::ImplementedInterfaceName(_) + | IdentParent::OperationTypeDefinitionType(_) + | IdentParent::InputObjectTypeExtensionName(_) + | IdentParent::ObjectTypeExtensionName(_) + | IdentParent::InterfaceTypeExtensionName(_) + | IdentParent::UnionTypeExtensionName(_) + | IdentParent::EnumTypeExtensionName(_) + | IdentParent::ScalarTypeExtensionName(_), + }) => Ok(DefinitionDescription::Type { + type_name: type_name.value, + }), + ResolutionPath::Ident(IdentPath { + inner: directive_name, + parent: IdentParent::ConstantDirectiveName(_), + }) => Ok(DefinitionDescription::Directive { + directive_name: DirectiveName(directive_name.value), + }), + ResolutionPath::Ident(IdentPath { + inner: argument_name, + parent: + IdentParent::ConstantArgumentKey(ConstantArgumentPath { + inner: _, + parent: + ConstantArgumentParent::ConstantDirective(ConstantDirectivePath { + inner: directive, + .. + }), + }), + }) => Ok(DefinitionDescription::DirectiveArgument { + directive_name: DirectiveName(directive.name.value), + argument_name: ArgumentName(argument_name.value), + }), + _ => Err(LSPRuntimeError::ExpectedError), + } +} + pub fn get_graphql_definition_description( document: ExecutableDocument, position_span: Span, diff --git a/compiler/crates/relay-lsp/src/goto_definition/mod.rs b/compiler/crates/relay-lsp/src/goto_definition/mod.rs index 47a56a1cbf780..8e44db2453ee4 100644 --- a/compiler/crates/relay-lsp/src/goto_definition/mod.rs +++ b/compiler/crates/relay-lsp/src/goto_definition/mod.rs @@ -31,6 +31,7 @@ use serde::Serialize; use self::goto_docblock_definition::get_docblock_definition_description; use self::goto_graphql_definition::get_graphql_definition_description; +use self::goto_graphql_definition::get_graphql_schema_definition_description; use crate::location::transform_relay_location_to_lsp_location; use crate::lsp_runtime_error::LSPRuntimeError; use crate::lsp_runtime_error::LSPRuntimeResult; @@ -85,7 +86,9 @@ pub fn on_goto_definition( crate::Feature::DocblockIr(docblock_ir) => { get_docblock_definition_description(&docblock_ir, position_span)? } - crate::Feature::SchemaDocument(_) => Err(LSPRuntimeError::ExpectedError)?, + crate::Feature::SchemaDocument(document) => { + get_graphql_schema_definition_description(document, position_span)? + } }; let extra_data_provider = state.get_extra_data_provider();