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

Feature: Nested input type field completions #4148

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
9 changes: 1 addition & 8 deletions compiler/crates/graphql-ir/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,7 @@ impl<'schema, 'signatures, 'options> Builder<'schema, 'signatures, 'options> {
.map(|x| (x.name.item, x.clone()))
.collect();

let directives = self.build_directives(
&operation.directives,
match kind {
OperationKind::Query => DirectiveLocation::Query,
OperationKind::Mutation => DirectiveLocation::Mutation,
OperationKind::Subscription => DirectiveLocation::Subscription,
},
);
let directives = self.build_directives(&operation.directives, kind.into());
let operation_type_reference = TypeReference::Named(operation_type);
// assert the subscription only contains one selection
if let OperationKind::Subscription = kind {
Expand Down
11 changes: 11 additions & 0 deletions compiler/crates/graphql-syntax/src/node/type_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use intern::string_key::StringKey;
use super::constant_directive::ConstantDirective;
use super::constant_value::ConstantValue;
use super::constant_value::StringNode;
use super::executable::OperationKind;
use super::primitive::*;
use super::type_annotation::TypeAnnotation;

Expand Down Expand Up @@ -323,6 +324,16 @@ pub enum DirectiveLocation {
VariableDefinition,
}

impl From<OperationKind> for DirectiveLocation {
fn from(operation: OperationKind) -> Self {
match operation {
OperationKind::Query => Self::Query,
OperationKind::Mutation => Self::Mutation,
OperationKind::Subscription => Self::Subscription,
}
}
}

impl fmt::Display for DirectiveLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Expand Down
234 changes: 227 additions & 7 deletions compiler/crates/relay-lsp/src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use graphql_syntax::InlineFragment;
use graphql_syntax::LinkedField;
use graphql_syntax::List;
use graphql_syntax::OperationDefinition;
use graphql_syntax::OperationKind;
use graphql_syntax::ScalarField;
use graphql_syntax::Selection;
use graphql_syntax::TokenKind;
Expand All @@ -51,6 +50,7 @@ use lsp_types::MarkupContent;
use lsp_types::MarkupKind;
use schema::Argument as SchemaArgument;
use schema::Directive as SchemaDirective;
use schema::InputObject;
use schema::SDLSchema;
use schema::Schema;
use schema::Type;
Expand Down Expand Up @@ -86,6 +86,11 @@ pub enum CompletionKind {
InlineFragmentType {
existing_inline_fragment: bool,
},
InputObjectFieldName {
name: StringKey,
existing_names: FnvHashSet<StringKey>,
input_field_path: Vec<StringKey>,
},
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -188,11 +193,7 @@ impl CompletionRequestBuilder {
..
} = operation;

let directive_location = match kind {
OperationKind::Query => DirectiveLocation::Query,
OperationKind::Mutation => DirectiveLocation::Mutation,
OperationKind::Subscription => DirectiveLocation::Subscription,
};
let directive_location = kind.into();

if let Some(req) = self.build_request_from_selection_or_directives(
selections,
Expand Down Expand Up @@ -363,6 +364,117 @@ impl CompletionRequestBuilder {
))
}

fn build_request_from_constant_input_value(
&self,
position_span: Span,
type_path: Vec<TypePathItem>,
mut input_field_path: Vec<StringKey>,
constant_value: &ConstantValue,
name: StringKey,
) -> Option<CompletionRequest> {
match constant_value {
ConstantValue::List(list) => list
.items
.iter()
.find(|arg| arg.span().contains(position_span))
.and_then(|constant_value| {
self.build_request_from_constant_input_value(
position_span,
type_path,
input_field_path,
constant_value,
name,
)
}),
ConstantValue::Object(arguments) => {
if let Some(constant_argument) = arguments
.items
.iter()
.find(|arg| arg.span.contains(position_span))
{
input_field_path.push(constant_argument.name());
self.build_request_from_constant_input_value(
position_span,
type_path,
input_field_path,
&constant_argument.value,
name,
)
} else {
Some(self.new_request(
CompletionKind::InputObjectFieldName {
name,
existing_names:
arguments.items.iter().map(|item| item.name()).collect(),
input_field_path,
},
type_path,
))
}
}
_ => None,
}
}

fn build_request_from_input_value(
&self,
position_span: Span,
type_path: Vec<TypePathItem>,
mut input_field_path: Vec<StringKey>,
value: &Value,
name: StringKey,
) -> Option<CompletionRequest> {
match value {
Value::List(list) => list
.items
.iter()
.find(|arg| arg.span().contains(position_span))
.and_then(|value| {
self.build_request_from_input_value(
position_span,
type_path,
input_field_path,
value,
name,
)
}),
Value::Object(arguments) => {
if let Some(position_argument) = arguments
.items
.iter()
.find(|arg| arg.span.contains(position_span))
{
input_field_path.push(position_argument.name());
self.build_request_from_input_value(
position_span,
type_path,
input_field_path,
&position_argument.value,
name,
)
} else {
Some(self.new_request(
CompletionKind::InputObjectFieldName {
name,
existing_names:
arguments.items.iter().map(|item| item.name()).collect(),
input_field_path,
},
type_path,
))
}
}
Value::Constant(constant_value) => self.build_request_from_constant_input_value(
position_span,
type_path,
input_field_path,
constant_value,
name,
),
_ => None,
}
}

fn build_request_from_arguments(
&self,
arguments: &List<Argument>,
Expand Down Expand Up @@ -406,6 +518,14 @@ impl CompletionRequestBuilder {
type_path,
))
}
Value::Constant(constant_value) => self
.build_request_from_constant_input_value(
position_span,
type_path,
Default::default(),
constant_value,
name.value,
),
Value::Variable(_) => Some(self.new_request(
CompletionKind::ArgumentValue {
argument_name: name.value,
Expand All @@ -414,7 +534,13 @@ impl CompletionRequestBuilder {
},
type_path,
)),
_ => None,
value => self.build_request_from_input_value(
position_span,
type_path,
Default::default(),
value,
name.value,
),
}
} else {
None
Expand Down Expand Up @@ -689,6 +815,56 @@ fn completion_items_for_request(
existing_inline_fragment,
))
}
CompletionKind::InputObjectFieldName {
name,
existing_names,
input_field_path,
} => {
let (_, field) = request.type_path.resolve_current_field(schema)?;

fn resolve_root_input_field<'a>(
schema: &'a SDLSchema,
input_object: &'a TypeReference<Type>,
) -> Option<&'a InputObject> {
match input_object {
TypeReference::Named(Type::InputObject(input_object_id)) => {
Some(schema.input_object(*input_object_id))
}
TypeReference::Named(_) => None,
TypeReference::NonNull(inner) => resolve_root_input_field(schema, &*inner),
TypeReference::List(inner) => resolve_root_input_field(schema, &*inner),
}
}

fn resolve_input_field<'a>(
schema: &'a SDLSchema,
input_object: &'a InputObject,
field_name: &StringKey,
) -> Option<&'a InputObject> {
input_object
.fields
.iter()
.find(|field| field.name.0 == *field_name)
.and_then(|field| resolve_root_input_field(schema, &field.type_))
}

let field_argument = field
.arguments
.iter()
.find(|argument| argument.name() == name)?;

let mut input_object = resolve_root_input_field(schema, &field_argument.type_)?;

for input_field_name in input_field_path.iter() {
input_object = resolve_input_field(schema, input_object, input_field_name)?;
}

Some(resolve_completion_items_for_input_object(
input_object,
schema,
existing_names,
))
}
}
}

Expand All @@ -702,6 +878,50 @@ fn resolve_completion_items_typename(type_: Type, schema: &SDLSchema) -> Vec<Com
}
}

fn resolve_completion_items_for_input_object(
input_object: &InputObject,
schema: &SDLSchema,
existing_names: FnvHashSet<StringKey>,
) -> Vec<CompletionItem> {
input_object
.fields
.iter()
.filter(|arg| !existing_names.contains(&arg.name()))
.map(|arg| {
let label = arg.name().lookup().to_string();
let detail = schema.get_type_string(arg.type_());
let kind = match arg.type_().inner() {
Type::InputObject(_) => Some(CompletionItemKind::STRUCT),
Type::Scalar(_) => Some(CompletionItemKind::FIELD),
_ => None,
};

CompletionItem {
label: label.clone(),
kind,
detail: Some(detail),
documentation: None,
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(format!("{}: $1", label)),
insert_text_format: Some(lsp_types::InsertTextFormat::SNIPPET),
text_edit: None,
additional_text_edits: None,
command: Some(lsp_types::Command::new(
"Suggest".into(),
"editor.action.triggerSuggest".into(),
None,
)),
data: None,
tags: None,
..Default::default()
}
})
.collect()
}

fn resolve_completion_items_for_argument_name<T: ArgumentLike>(
arguments: impl Iterator<Item = T>,
schema: &SDLSchema,
Expand Down
53 changes: 53 additions & 0 deletions compiler/crates/relay-lsp/src/completion/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,59 @@ fn fragment_spread_on_interface() {
assert_labels(items.unwrap(), vec!["TestFragment", "TestFragment2"]);
}

#[test]
fn argument_value_object() {
let items = parse_and_resolve_completion_items(
r#"
fragment Test on Mutation {
commentCreate(inpu|) {
__typename
}
}
"#,
None,
);
assert_labels(items.unwrap(), vec!["input"]);
}

#[test]
fn argument_value_constant_object() {
let items = parse_and_resolve_completion_items(
r#"
fragment Test on Mutation {
commentCreate(input: {
feedbackId: "some-id"
|
}) {
__typename
}
}
"#,
None,
);
assert_labels(items.unwrap(), vec!["client_mutation_id", "feedback"]);
}

#[test]
fn argument_value_constant_object_nested() {
let items = parse_and_resolve_completion_items(
r#"
fragment Test on Mutation {
commentCreate(input: {
feedbackId: "some-id"
feedback: {
|
}
}) {
__typename
}
}
"#,
None,
);
assert_labels(items.unwrap(), vec!["comment"]);
}

#[test]
fn argument_value() {
let items = parse_and_resolve_completion_items(
Expand Down
Loading