Skip to content

Commit 9cd1f7f

Browse files
OOf, works somewhat ok
1 parent b9c5927 commit 9cd1f7f

File tree

4 files changed

+78
-22
lines changed

4 files changed

+78
-22
lines changed

src/framework/codemirror-lang-typeql/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ export function TypeQL() {
101101
return new LanguageSupport(TypeQLLanguage, [])
102102
}
103103

104-
104+
let typeqlAutocomplete = new NodePrefixAutoComplete(SUGGESTION_MAP, new TypeQLAutocompleteSchema());
105+
function wrappedSuggest(context: CompletionContext) {
106+
return typeqlAutocomplete.autocomplete(context);
107+
}
105108
export function typeqlAutocompleteExtension() {
106-
let typeqlAutocomplete = new NodePrefixAutoComplete(SUGGESTION_MAP, new TypeQLAutocompleteSchema());
107-
let autocomplete_fn = (context: CompletionContext) => typeqlAutocomplete.autocomplete(context);
108-
return autocompletion({ activateOnTypingDelay: 100, override: [autocomplete_fn] });
109+
return autocompletion({ override: [wrappedSuggest] });
109110
}
110111

111112
// A Linter which flags syntax errors from: https://discuss.codemirror.net/t/showing-syntax-errors/3111/6

src/framework/codemirror-lang-typeql/typeQLAutocompleteSchema.ts

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { SyntaxNode, Tree, TreeCursor } from "@lezer/common";
22
import { CompletionContext } from "@codemirror/autocomplete";
33
import * as tokens from "./generated/typeql.grammar.generated.terms";
4-
4+
import {TypeDBHttpDriver} from "../typedb-driver";
5+
import {finalize} from "rxjs";
6+
import {DriverState} from "../../service/driver-state.service";
7+
import {ConceptRow, ConceptRowsQueryResponse} from "../typedb-driver/response";
8+
import {Concept, EntityType, RelationType, Type} from "../typedb-driver/concept";
9+
type ConceptRowAnswer = { involvedBlocks: number[]; data: ConceptRow };
510
type TypeLabel = string;
6-
type AttributeType = {};
7-
8-
interface ObjectType {
11+
type AttributeTypeEntry = {};
12+
interface ObjectTypeEntry {
913
owns: TypeLabel[];
1014
plays: TypeLabel[];
1115
relates: TypeLabel[];
@@ -40,15 +44,15 @@ export class TypeQLAutocompleteSchema {
4044
return Object.keys(this.fromDB.objectTypes).concat(Object.keys(this.fromEditor.objectTypes));
4145
}
4246

43-
attributeType(type: TypeLabel): AttributeType {
47+
attributeType(type: TypeLabel): AttributeTypeEntry {
4448
if (this.fromDB.attributes[type]) {
4549
return this.fromDB.attributes[type];
4650
} else {
4751
return this.fromEditor.attributes[type];
4852
}
4953
}
5054

51-
objectType(type: TypeLabel): ObjectType {
55+
objectType(type: TypeLabel): ObjectTypeEntry {
5256
if (this.fromDB.objectTypes[type]) {
5357
return this.fromDB.objectTypes[type];
5458
} else {
@@ -72,16 +76,16 @@ export class TypeQLAutocompleteSchema {
7276
}
7377

7478
export class TypeQLAutocompleteSchemaImpl {
75-
objectTypes: Record<TypeLabel, ObjectType>;
76-
attributes: Record<TypeLabel, AttributeType>;
79+
objectTypes: Record<TypeLabel, ObjectTypeEntry>;
80+
attributes: Record<TypeLabel, AttributeTypeEntry>;
7781
constructor(
78-
objectTypes: Record<TypeLabel, ObjectType>,
79-
attributes: Record<TypeLabel, AttributeType>,
82+
objectTypes: Record<TypeLabel, ObjectTypeEntry>,
83+
attributes: Record<TypeLabel, AttributeTypeEntry>,
8084
) {
8185
this.attributes = attributes;
8286
this.objectTypes = objectTypes;
8387
}
84-
88+
8589
static fromTypeQL(text: string, tree: Tree) : TypeQLAutocompleteSchemaImpl {
8690
let builder = new SchemaBuilder();
8791
// TODO: Replace iterate with a more targetted traversal that considers only define queries.
@@ -141,25 +145,76 @@ export class TypeQLAutocompleteSchemaImpl {
141145
});
142146
return builder.build();
143147
}
148+
149+
static fromDriver(driver: DriverState, database: string): TypeQLAutocompleteSchemaImpl | null {
150+
function runQuery(driver: DriverState, database: string, query: string): ConceptRowAnswer[] {
151+
// todo: Help please alex
152+
return [
153+
{
154+
involvedBlocks: [0],
155+
data: { // Unioning them all makes it pass
156+
["owner"]: {kind: "entityType", label: "person"},
157+
["owned"]: {kind: "attributeType", label: "name", valueType: "string"},
158+
["relation"]: {kind: "relationType", label: "friendship"},
159+
["related"]: {kind: "roleType", label: "friend"},
160+
["player"]: {kind: "entityType", label: "person"},
161+
["played"]: {kind: "roleType", label: "friend"},
162+
}
163+
}
164+
];
165+
}
166+
let ownsQuery = "match $owner owns $owned;"
167+
let relatesQuery = "match $relation relates $related;";
168+
let playsQuery = "match $player plays $played";
169+
170+
let ownsAnswers = runQuery(driver, database, ownsQuery); // TODO:
171+
let relatesAnswers = runQuery(driver, database, relatesQuery);
172+
let playsAnswers = runQuery(driver, database, playsQuery);
173+
174+
let builder = new SchemaBuilder();
175+
ownsAnswers.forEach((answer) => {
176+
let data: ConceptRow = answer.data;
177+
let owner = (data["owner"] as Type).label;
178+
let owned = (data["owned"] as Type).label;
179+
builder.objectType(owner);
180+
builder.attributeType(owned);
181+
builder.recordOwns(owner, owned);
182+
});
183+
relatesAnswers.forEach((answer) => {
184+
let data: ConceptRow = answer.data;
185+
let relation = (data["relation"] as Type).label;
186+
let related = (data["related"] as Type).label;
187+
builder.objectType(relation);
188+
builder.recordRelates(relation, related)
189+
});
190+
playsAnswers.forEach((answer) => {
191+
let data: ConceptRow = answer.data;
192+
let player = (data["player"] as Type).label;
193+
let played = (data["played"] as Type).label;
194+
builder.objectType(player);
195+
builder.recordRelates(player, played)
196+
})
197+
return builder.build();
198+
}
144199
}
145200

146201
class SchemaBuilder {
147-
objectTypes: Record<TypeLabel, ObjectType>;
148-
attributes: Record<TypeLabel, AttributeType>;
202+
objectTypes: Record<TypeLabel, ObjectTypeEntry>;
203+
attributes: Record<TypeLabel, AttributeTypeEntry>;
149204

150205
constructor() {
151206
this.objectTypes = {};
152207
this.attributes = {};
153208
}
154209

155-
attributeType(type: TypeLabel): AttributeType {
210+
attributeType(type: TypeLabel): AttributeTypeEntry {
156211
if (!this.attributes[type]) {
157212
this.attributes[type] = { owners: [] };
158213
}
159214
return this.attributes[type];
160215
}
161216

162-
objectType(type: TypeLabel): ObjectType {
217+
objectType(type: TypeLabel): ObjectTypeEntry {
163218
if (!this.objectTypes[type]) {
164219
this.objectTypes[type] = { owns: [], plays: [], relates: [] };
165220
}

src/module/query/query-tool.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ <h4>Query</h4>
5959
</div>
6060
<div class="code-editor-container">
6161
@if (!codeEditorHidden) {
62-
<code-editor [extensions]="[codeEditorTheme, TypeQL(), typeqlAutocompleteExtension()]" [formControl]="state.queryControl"/>
62+
<code-editor [extensions]="[codeEditorTheme, TypeQL(), typeqlAutocompleteExtension(), codeEditorKeymap]" [formControl]="state.queryControl"/>
6363
}
6464
</div>
6565
</div>

src/module/query/query-tool.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { SnackbarService } from "../../service/snackbar.service";
3131
import { PageScaffoldComponent } from "../scaffold/page/page-scaffold.component";
3232
import {keymap} from "@codemirror/view";
3333
import {defaultKeymap} from "@codemirror/commands";
34-
import {startCompletion} from "@codemirror/autocomplete";
34+
import {startCompletion, completionKeymap} from "@codemirror/autocomplete";
3535

3636
@Component({
3737
selector: "ts-query-tool",
@@ -132,5 +132,5 @@ export class QueryToolComponent implements OnInit, AfterViewInit, OnDestroy {
132132
readonly TypeQL = TypeQL;
133133
readonly linter = otherExampleLinter;
134134
readonly typeqlAutocompleteExtension = typeqlAutocompleteExtension;
135-
// readonly codeEditorKeymap = keymap.of([...defaultKeymap,{key: "Alt-Space", run: startCompletion, preventDefault: true}]);
135+
readonly codeEditorKeymap = keymap.of([...defaultKeymap, {key: "Alt-Space", run: startCompletion, preventDefault: true}]);
136136
}

0 commit comments

Comments
 (0)