Skip to content

Commit

Permalink
Implement go to definition support
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Apr 5, 2024
1 parent 4cefb54 commit a09adc0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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
5 changes: 4 additions & 1 deletion compiler/crates/relay-lsp/src/goto_definition/mod.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_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

0 comments on commit a09adc0

Please sign in to comment.