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

Add better autocomplete patch to playground #498

Merged
merged 4 commits into from
Oct 22, 2023
Merged
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
30 changes: 25 additions & 5 deletions experiments/browser_based_querying/patches/[email protected]
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
diff --git a/esm/interface/getAutocompleteSuggestions.js b/esm/interface/getAutocompleteSuggestions.js
index ac159d132d095a789b966627d315f4238c79ab60..a3be2d27c9d5c7d6fd3745093301a07d28e79a8d 100644
index ac159d132d095a789b966627d315f4238c79ab60..1d6f624a8ad888ebbe8aa9092feb845a7d8205b0 100644
--- a/esm/interface/getAutocompleteSuggestions.js
+++ b/esm/interface/getAutocompleteSuggestions.js
@@ -234,7 +234,12 @@ export function getAutocompleteSuggestions(schema, queryText, cursor, contextTok
@@ -63,6 +63,8 @@ const hasTypeSystemDefinitions = (sdl) => {
}
return hasTypeSystemDef;
};
+const primitives = new Set(['Int', 'Float', 'String', 'Boolean', 'ID'])
+const propertyDirectives = new Set(['filter', 'tag', 'output', 'transform'])
export function getAutocompleteSuggestions(schema, queryText, cursor, contextToken, fragmentDefs, options) {
var _a;
const opts = Object.assign(Object.assign({}, options), { schema });
@@ -234,7 +236,23 @@ export function getAutocompleteSuggestions(schema, queryText, cursor, contextTok
return getSuggestionsForVariableDefinition(token, schema, kind);
}
if (kind === RuleKinds.DIRECTIVE) {
- return getSuggestionsForDirective(token, state, schema, kind);
+ // We should autocomplete fields on the line after we autocomplete a directive
+ if (state.needsAdvance) { // active after '@' or many other kinds of punctuation, so perfect for when we need to autocomplete directives
+ return getSuggestionsForDirective(token, state, schema, kind);
+ if (state.needsAdvance || state.name === null) { // active after '@' or many other kinds of punctuation, so perfect for when we need to autocomplete directives.
+ // `state` is `null` when the user autocompletes after typing just '@' then opening autocomplete menu
+ // further restrict the directives we suggest based on the type of the field we are on
+ // x.label does not include the '@'
+ let fieldType = typeInfo.fieldDef.type;
+ while (fieldType.ofType) {
+ fieldType = fieldType.ofType
+ }
+ if (primitives.has(fieldType.name)) {
+ return getSuggestionsForDirective(token, state, schema, kind).filter(x => propertyDirectives.has(x.label))
+ } else {
+ return getSuggestionsForDirective(token, state, schema, kind) // Don't filter edges until we can decide if there is a @transform(op: "count") before this directive, which would force us to switch to property directives
+ }
+ } else { // there has been no '@' so we should autocomplete fields
+ return getSuggestionsForFieldNames(token, typeInfo, opts);
+ }
Expand All @@ -28,4 +48,4 @@ index 38a10439b1a5d7e7adbdd3e16c743a9cedff9dab..c699c3166cf458a5f9e85a794591fd44
+ for (let i = 0; i <= location.line; i++) { // fix console error spam from hovering a comment on the first line of a file by properly visiting the first line of a file
stream = new CharacterStream(lines[i]);
while (!stream.eol()) {
const style = parser.token(stream, state);
const style = parser.token(stream, state);
15 changes: 10 additions & 5 deletions experiments/browser_based_querying/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.