Skip to content

Add __id to LSP completions #4839

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 28 additions & 8 deletions compiler/crates/relay-lsp/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ use lsp_types::Documentation;
use lsp_types::InsertTextFormat;
use lsp_types::MarkupContent;
use lsp_types::MarkupKind;
use schema::field_descriptions::CLIENT_ID_DESCRIPTION;
use schema::field_descriptions::TYPENAME_DESCRIPTION;
use schema::Argument as SchemaArgument;
use schema::Directive as SchemaDirective;
use schema::InputObject;
Expand Down Expand Up @@ -691,7 +693,7 @@ fn completion_items_for_request(
schema_documentation,
existing_linked_field,
),
resolve_completion_items_typename(Type::Interface(interface_id), schema),
resolve_special_completion_items(Type::Interface(interface_id), schema),
resolve_completion_items_for_inline_fragment(
Type::Interface(interface_id),
schema,
Expand All @@ -712,7 +714,7 @@ fn completion_items_for_request(
schema_documentation,
existing_linked_field,
),
resolve_completion_items_typename(Type::Object(object_id), schema),
resolve_special_completion_items(Type::Object(object_id), schema),
resolve_completion_items_for_fragment_spread(
Type::Object(object_id),
program,
Expand All @@ -721,7 +723,7 @@ fn completion_items_for_request(
),
])),
Type::Union(union_id) => Some(merge_completion_items_ordered([
resolve_completion_items_typename(Type::Union(union_id), schema),
resolve_special_completion_items(Type::Union(union_id), schema),
resolve_completion_items_for_inline_fragment(Type::Union(union_id), schema, false),
resolve_completion_items_for_fragment_spread(
Type::Union(union_id),
Expand Down Expand Up @@ -868,13 +870,31 @@ fn completion_items_for_request(
}
}

fn resolve_completion_items_typename(type_: Type, schema: &SDLSchema) -> Vec<CompletionItem> {
fn resolve_special_completion_items(type_: Type, schema: &SDLSchema) -> Vec<CompletionItem> {
let __id_item = CompletionItem {
label: "__id".to_owned(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be exposed by the SDLSchema? A little nervous having this fieldname and type hard coded in multiple modules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tobias-tengler Thoughts on this? Would love to merge this improvement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tobias-tengler Thoughts on this? Would love to merge this improvement.

detail: Some("ID!".to_owned()),
documentation: Some(Documentation::String(
CLIENT_ID_DESCRIPTION.lookup().to_owned(),
)),
kind: Some(CompletionItemKind::FIELD),
..Default::default()
};

if type_.is_root_type(schema) {
vec![]
vec![__id_item]
} else {
let mut item = CompletionItem::new_simple("__typename".to_owned(), "String!".to_owned());
item.kind = Some(CompletionItemKind::FIELD);
vec![item]
let __typename_item = CompletionItem {
label: "__typename".to_owned(),
detail: Some("String!".to_owned()),
documentation: Some(Documentation::String(
TYPENAME_DESCRIPTION.lookup().to_owned(),
)),
kind: Some(CompletionItemKind::FIELD),
..Default::default()
};

vec![__typename_item, __id_item]
}
}

Expand Down
18 changes: 12 additions & 6 deletions compiler/crates/relay-lsp/src/completion/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn scalar_field() {
);
assert_labels(
items.unwrap(),
vec!["uri", "width", "height", "test_enums", "__typename"],
vec!["uri", "width", "height", "test_enums", "__typename", "__id"],
);
}

Expand All @@ -121,7 +121,10 @@ fn linked_field() {
"#,
None,
);
assert_labels(items.unwrap(), vec!["location", "categories", "__typename"]);
assert_labels(
items.unwrap(),
vec!["location", "categories", "__typename", "__id"],
);
}

#[test]
Expand All @@ -139,7 +142,7 @@ fn whitespace_in_linked_field() {
);
assert_labels(
items.unwrap(),
vec!["uri", "test_enums", "width", "height", "__typename"],
vec!["uri", "test_enums", "width", "height", "__typename", "__id"],
);
}

Expand All @@ -155,7 +158,7 @@ fn whitespace_in_fragment() {
);
assert_labels(
items.unwrap(),
vec!["uri", "width", "height", "test_enums", "__typename"],
vec!["uri", "width", "height", "test_enums", "__typename", "__id"],
);
}

Expand All @@ -173,7 +176,7 @@ fn whitespace_in_inline_fragment() {
);
assert_labels(
items.unwrap(),
vec!["uri", "width", "height", "test_enums", "__typename"],
vec!["uri", "width", "height", "test_enums", "__typename", "__id"],
);
}

Expand Down Expand Up @@ -207,6 +210,7 @@ fn whitespace_in_object_type() {
"cursor",
"node",
"__typename",
"__id",
"...ObjectTypeFragment",
"...InterfaceFragment",
],
Expand Down Expand Up @@ -244,6 +248,7 @@ fn whitespace_in_interface() {
"source",
"node",
"__typename",
"__id",
"... on CommentsEdge",
"...ImplementingFragment",
"...InterfaceFragment",
Expand Down Expand Up @@ -279,6 +284,7 @@ fn whitespace_in_union() {
items.unwrap(),
vec![
"__typename",
"__id",
"... on MarkdownCommentBody",
"... on PlainCommentBody",
"...UnionVariantFragment",
Expand Down Expand Up @@ -779,7 +785,7 @@ fn field_documentation() {
.map(|item| (item.label, item.documentation))
.collect::<HashMap<String, Option<Documentation>>>();

assert_eq!(docs.len(), 5);
assert_eq!(docs.len(), 6);
assert_eq!(
*docs.get("uri").unwrap(),
Some(make_markdown_table_documentation(
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

pub mod definitions;
mod errors;
mod field_descriptions;
pub mod field_descriptions;
mod flatbuffer;
mod graphql_schema;
mod in_memory;
Expand Down
Loading