Skip to content

Conversation

@krishnangovindraj
Copy link
Member

@krishnangovindraj krishnangovindraj commented May 26, 2025

Release notes: product changes

We fix a bug where the suggestions of variables & labels were unpredictable, and implement more suggestions in an extensible, but ad-hoc way.
We enable suggestions based on a datastructure representing the schema, allowing labels to be suggested based on the schema. The schema is loaded both from the database and from the editor state.

Motivation

Better autocomplete.

Implementation

We walk up the tree till we find a node we want to do a suggestion at. We're provided with a "prefix". This is the union of the direct children of every node on the path from this suggesting node to the node being parsed*. The nodes on the path are excluded.
E.g. If we reach ClauseMatch where ClauseMatch { MATCH Patterns }; Pattern { (Pattern SEMICOLON)+ } on our climb,
match $x isa person; $y , prefix would be [MATCH, Pattern, SEMICOLON, VAR].

  • (Any children to the right of th e parse point are ignored, because we could be editing in-between nodes)

Ideally, we'd be able to partially generate suggestions from the grammar. Lezer provides a grammar for lezer grammar files.

Manually testing the schema-based suggestions

# Run the following query.
match $default-owner owns $default-owned; limit 1;
match $default-relation relates $default-related; limit 1;
match $default-player plays $default-played; limit 1;
match

{ $owner owns $owned; $relation is $default-relation; $related is $default-related; $player is $default-player; $played is $default-played; } or 
{ $owner is $default-owner; $owned is $default-owned; $relation relates $related; $player is $default-player; $played is $default-played; } or
{ $owner is $default-owner; $owned is $default-owned; $relation is $default-relation; $related is $default-related; $player plays $played; };

And this in the JS console:
_updateSchemaFromDB(_lastQueryAnswers, _lastQueryAnswers, _lastQueryAnswers)

@netlify
Copy link

netlify bot commented May 26, 2025

Deploy Preview for typedb-studio ready!

Name Link
🔨 Latest commit d9c6388
🔍 Latest deploy log https://app.netlify.com/projects/typedb-studio/deploys/68594932f4fc6c000842609b
😎 Deploy Preview https://deploy-preview-866--typedb-studio.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@krishnangovindraj krishnangovindraj force-pushed the update-typeql-autocomplete branch 3 times, most recently from 8d72c22 to 9cd1f7f Compare June 12, 2025 14:40
Copy link
Member Author

@krishnangovindraj krishnangovindraj left a comment

Choose a reason for hiding this comment

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

quick self review

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a rather generic framework for autocomplete. I don't know its limitations.
You should not have to change this file to add new suggestions, just update typeql_suggestions.ts


static fromDriver(driver: DriverState, database: string): TypeQLAutocompleteSchemaImpl | null {
function runQuery(driver: DriverState, database: string, query: string): ConceptRowAnswer[] {
// todo: Help please alex
Copy link
Member Author

Choose a reason for hiding this comment

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

@alexjpwalker , This is the function I don't know how to implement in the current state of studio. Can you sketch me an implementation? (or do it if you think it's faster that way).

Copy link
Member Author

Choose a reason for hiding this comment

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

You did tell me to add a subscriber in the constructor of (schema-state-service iirc) and it works.

return autocompletion({ override: [wrappedAutocomplete] });
}

function updateSchemaFromDB(driver: DriverState, database: string) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this is the function that needs to be called when the user wants to reload the schema

@krishnangovindraj krishnangovindraj marked this pull request as ready for review June 12, 2025 19:28
readonly TypeQL = TypeQL;
readonly linter = otherExampleLinter;
readonly typeqlAutocompleteExtension = typeqlAutocompleteExtension;
readonly codeEditorKeymap = keymap.of([...defaultKeymap, {key: "Alt-Space", run: startCompletion, preventDefault: true}]);
Copy link
Member Author

Choose a reason for hiding this comment

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

I added this for mac. I couldn't get the default combination to work.

if (isApiErrorResponse(res)) return;

if (res.ok.answerType == "conceptRows" && res.ok.query != null) {
(window as any)._lastQueryAnswers = res.ok.answers;
Copy link
Member Author

Choose a reason for hiding this comment

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

Sneak this into the global namespace for testing schema based suggestions.

{ suffixes: [ [tokens.PLAYS] ], suggestions: [ suggestRoleTypeLabelsScoped ] },
{ suffixes: [ [tokens.RELATES] ], suggestions: [ suggestRoleTypeLabelsUnscoped ] },
],
// TODO: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

Anything we find missing.

@krishnangovindraj krishnangovindraj force-pushed the update-typeql-autocomplete branch from 778b45f to 6d40872 Compare June 18, 2025 13:06
Copy link
Member Author

Choose a reason for hiding this comment

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

This file will ideally go away and be replaced by the schema state built for the schema tree.

Copy link
Member

Choose a reason for hiding this comment

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

Now that schema state is merged can we do this refactor?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, we need to keep it since it wraps two instances of the tree.
One that originates from the DB and one from the editor-state

Copy link
Member Author

@krishnangovindraj krishnangovindraj Jun 17, 2025

Choose a reason for hiding this comment

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

This file will have to be updated to use your state instead of mine.
Doesn't, since my state now wraps two instances of your state.

return autocompletion({ override: [wrappedAutocomplete] });
}

function updateSchemaFromDB(schema: Schema) {
Copy link
Member Author

Choose a reason for hiding this comment

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

How do I call this on schema change, Alex?

@krishnangovindraj krishnangovindraj force-pushed the update-typeql-autocomplete branch 2 times, most recently from 7982e8c to 6150679 Compare June 22, 2025 15:34
@krishnangovindraj krishnangovindraj force-pushed the update-typeql-autocomplete branch from 6150679 to 0f6b708 Compare June 23, 2025 10:40
Copy link
Member

Choose a reason for hiding this comment

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

Now that schema state is merged can we do this refactor?

@alexjpwalker alexjpwalker merged commit 5df465f into typedb:development Jun 23, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants