Skip to content

Commit

Permalink
Add SchemaDocument go to definition LSP support (#4669)
Browse files Browse the repository at this point in the history
Summary:
Adds go to definition support for types and directives within a `.graphql` file.

Pull Request resolved: #4669

Differential Revision: D58590082

Pulled By: captbaritone

fbshipit-source-id: 847a0e665e7db8c50f250bead4798d83d1a70066
  • Loading branch information
tobias-tengler authored and facebook-github-bot committed Jun 14, 2024
1 parent cc439c6 commit 68c7cca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/crates/relay-lsp/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_on_disk_to_lsp_location;
use crate::lsp_runtime_error::LSPRuntimeError;
use crate::lsp_runtime_error::LSPRuntimeResult;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DefinitionDescription> {
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,
Expand Down

0 comments on commit 68c7cca

Please sign in to comment.