Skip to content

Fix autocomplete suggesting erratically & add more suggestions #866

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 36 commits into
base: development
Choose a base branch
from

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. This is not fully integrated and does not have an effect yet. It can be manually tested.
We add a build step to generate the parser from the grammar into our build system, so the code is properly integrated into studio.

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)

alexjpwalker and others added 8 commits April 30, 2025 16:39
## Release notes: product changes

### New features

- **Simple graph visualization**

We add simple graph visualization. To try it, run a query that returns
ConceptRows and select the graph output type.

- **Better visual feedback on startup connection, query errors and
switching database**

Auto-reconnection on startup, query errors, and switching database now
all trigger popup notifications in addition to their usual behaviour.

### Bugs fixed

- **Correctly render attribute values in log output**
- **Fix some buttons having no state change on hover**
- **If creating a database fails, show the error**
- **Add field validation to database creation dialog**
- **Add field validation to connection address**

---------

Co-authored-by: Krishnan Govindraj <[email protected]>
Copy link

netlify bot commented May 26, 2025

Deploy Preview for typedb-studio failed. Why did it fail? →

Name Link
🔨 Latest commit 7982e8c
🔍 Latest deploy log https://app.netlify.com/projects/typedb-studio/deploys/685303a9ce2d6a0008abcebe

@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

@@ -3,6 +3,7 @@
"version": "3.4.0-ad30523694ad2b07d11ed0a406b3fe3676358705",
"scripts": {
"ng": "ng",
"generate-grammar": "lezer-generator --typeScript src/framework/codemirror-lang-typeql/typeql.grammar -o src/framework/codemirror-lang-typeql/generated/typeql.grammar.generated",
Copy link
Member Author

Choose a reason for hiding this comment

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

We have to run this step every time we update the typeql.grammar file. I should probably add a comment in that file.

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).

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
@@ -128,4 +131,6 @@ export class QueryToolComponent implements OnInit, AfterViewInit, OnDestroy {
readonly JSON = JSON;
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 (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 Author

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.

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 from 55561a1 to c9990bb Compare June 18, 2025 18:17
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