Skip to content

Commit 8d72c22

Browse files
Cleanup some comments
1 parent 71df3cd commit 8d72c22

File tree

4 files changed

+28
-61
lines changed

4 files changed

+28
-61
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { EditorView } from "@codemirror/view";
77
import { linter } from '@codemirror/lint'
88
import { autocompletion, CompletionContext } from "@codemirror/autocomplete";
99
import { NodePrefixAutoComplete } from "./complete"
10-
import { Schema, SchemaImpl } from "./schema";
10+
import { TypeQLAutocompleteSchema, TypeQLAutocompleteSchemaImpl } from "./typeQLAutocompleteSchema";
1111
import { SUGGESTION_MAP } from "./typeql_suggestions";
1212

1313
export const TypeQLLanguage = LRLanguage.define({
@@ -103,7 +103,7 @@ export function TypeQL() {
103103

104104

105105
export function typeqlAutocompleteExtension() {
106-
let typeqlAutocomplete = new NodePrefixAutoComplete(SUGGESTION_MAP, new Schema());
106+
let typeqlAutocomplete = new NodePrefixAutoComplete(SUGGESTION_MAP, new TypeQLAutocompleteSchema());
107107
let autocomplete_fn = (context: CompletionContext) => typeqlAutocomplete.autocomplete(context);
108108
return autocompletion({ activateOnTypingDelay: 100, override: [autocomplete_fn] });
109109
}
@@ -127,7 +127,3 @@ export function otherExampleLinter() {
127127
return diagnostics;
128128
});
129129
}
130-
131-
export function typeqlSchemaFromText(text: string): SchemaImpl {
132-
return SchemaImpl.fromTypeQL(text, parser.parse(text));
133-
}

src/framework/codemirror-lang-typeql/schema.ts renamed to src/framework/codemirror-lang-typeql/typeQLAutocompleteSchema.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ function extractText(text: string, from: number, to: number): string {
1515
return text.slice(from, to);
1616
}
1717

18-
export class Schema {
19-
fromDB: SchemaImpl;
20-
fromEditor: SchemaImpl;
18+
export class TypeQLAutocompleteSchema {
19+
fromDB: TypeQLAutocompleteSchemaImpl;
20+
fromEditor: TypeQLAutocompleteSchemaImpl;
2121

2222
constructor() {
23-
this.fromDB = new SchemaImpl({}, {});
24-
this.fromEditor = new SchemaImpl({}, {});
23+
this.fromDB = new TypeQLAutocompleteSchemaImpl({}, {});
24+
this.fromEditor = new TypeQLAutocompleteSchemaImpl({}, {});
2525
}
2626

27-
updateFromDB(schema: SchemaImpl): void {
27+
updateFromDB(schema: TypeQLAutocompleteSchemaImpl): void {
2828
this.fromDB = schema;
2929
}
3030

3131
mayUpdateFromEditorState(context: CompletionContext, tree: Tree): void {
32-
this.fromEditor = SchemaImpl.fromTypeQL(context.state.sliceDoc(), tree);
32+
this.fromEditor = TypeQLAutocompleteSchemaImpl.fromTypeQL(context.state.sliceDoc(), tree);
3333
}
3434

3535
attributeTypes(): TypeLabel[] {
@@ -71,7 +71,7 @@ export class Schema {
7171
}
7272
}
7373

74-
export class SchemaImpl {
74+
export class TypeQLAutocompleteSchemaImpl {
7575
objectTypes: Record<TypeLabel, ObjectType>;
7676
attributes: Record<TypeLabel, AttributeType>;
7777
constructor(
@@ -82,7 +82,7 @@ export class SchemaImpl {
8282
this.objectTypes = objectTypes;
8383
}
8484

85-
static fromTypeQL(text: string, tree: Tree) : SchemaImpl {
85+
static fromTypeQL(text: string, tree: Tree) : TypeQLAutocompleteSchemaImpl {
8686
let builder = new SchemaBuilder();
8787
// TODO: Replace iterate with a more targetted traversal that considers only define queries.
8888
// Extract all type declarations from the tree
@@ -185,7 +185,7 @@ class SchemaBuilder {
185185
}
186186
}
187187

188-
build(): SchemaImpl {
189-
return new SchemaImpl(this.objectTypes, this.attributes);
188+
build(): TypeQLAutocompleteSchemaImpl {
189+
return new TypeQLAutocompleteSchemaImpl(this.objectTypes, this.attributes);
190190
}
191191
}

src/framework/codemirror-lang-typeql/typeql.grammar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TypeQL grammar for lezer. run `pnpm run generate-grammar` to generate the parser and propagate these changes to typescript.
2+
13
// TODO: This is a direct translation of the original pest grammar (which is PEG iirc)
24
// It may be better to rewrite as much as possible so that it's better suited to the LR parser.
35
@top Query { QuerySchema | QueryPipelinePreambled (END SEMICOLON)? }

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

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,41 @@ import * as tokens from "./generated/typeql.grammar.generated.terms";
22
import { CompletionContext, Completion } from "@codemirror/autocomplete";
33
import { SyntaxNode, NodeType, Tree } from "@lezer/common"
44
import { SuggestionMap, SuffixOfPrefixSuggestion, suggest } from "./complete";
5-
import { Schema } from "./schema";
5+
import { TypeQLAutocompleteSchema } from "./typeQLAutocompleteSchema";
66

7-
// The actual suggestions
8-
// TODO: See if we can make this declarative based on token sequences expected as prefixes of a given node.
9-
function suggestAttributeTypeLabels(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
10-
// TODO: We could do better by climbing up the tree using `atNode.parentNode` to predict based on position as well.
11-
// We could also refine the suggestions by creating datastructures based on the declarations in the schema, rather than blindly suggesting every label.
7+
function suggestAttributeTypeLabels(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
128
var options: Completion[] = [];
139
schema.attributeTypes().forEach((label) => {options.push(suggest("AttributeType", label));})
1410
return options;
1511
}
1612

17-
function suggestObjectTypeLabels(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
18-
// TODO: We could do better by climbing up the tree using `atNode.parentNode` to predict based on position as well.
19-
// We could also refine the suggestions by creating datastructures based on the declarations in the schema, rather than blindly suggesting every label.
13+
function suggestObjectTypeLabels(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
2014
var options: Completion[] = [];
2115
schema.objectTypes().forEach((label) => {options.push(suggest("ObjectType", label));})
2216
return options;
2317
}
2418

25-
function suggestRoleTypeLabelsScoped(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
19+
function suggestRoleTypeLabelsScoped(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
2620
var options: Completion[] = [];
2721
schema.objectTypes()
2822
.flatMap((label) => schema.objectType(label).relates)
2923
.forEach((label) => { options.push(suggest("RoleType", label));});
3024
return options;
3125
}
3226

33-
function suggestRoleTypeLabelsUnscoped(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
27+
function suggestRoleTypeLabelsUnscoped(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
3428
var options: Completion[] = [];
3529
schema.objectTypes()
3630
.flatMap((label) => schema.objectType(label).relates)
3731
.forEach((label) => { options.push(suggest("RoleType", label.split(":")[1]));}); return options;
3832
}
3933

40-
41-
// The actual suggestions
42-
// TODO: See if we can make this declarative based on token sequences expected as prefixes of a given node.
43-
function suggestThingTypeLabels(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
34+
function suggestThingTypeLabels(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
4435
return suggestAttributeTypeLabels(context, tree, parseAt, climbedTo, prefix, schema).concat(
4536
suggestObjectTypeLabels(context, tree, parseAt, climbedTo, prefix, schema)
4637
);
4738
}
4839

49-
50-
// // TODO: See if we can make this declarative based on token sequences expected as prefixes of a given node.
51-
// function suggestLabels(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
52-
// // TODO: We could do better by climbing up the tree using `atNode.parentNode` to predict based on position as well.
53-
// // We could also refine the suggestions by creating datastructures based on the declarations in the schema, rather than blindly suggesting every label.
54-
// var options: Completion[] = [];
55-
// tree.iterate({
56-
// enter: (other: SyntaxNode) => {
57-
// if (other.type.id == tokens.LABEL) {
58-
// let label = context.state.sliceDoc(other.from, other.to);
59-
// options.push(suggest("type", label));
60-
// }
61-
// }
62-
// });
63-
// return options;
64-
65-
// }
66-
6740
function suggestVariables(context: CompletionContext, tree: Tree, boost=0): Completion[] {
6841
var options: Completion[] = [];
6942
tree.iterate({
@@ -85,7 +58,6 @@ function suggestVariablesAtMinus10(context: CompletionContext, tree: Tree): Comp
8558
return suggestVariables(context, tree, -10);
8659
}
8760

88-
8961
function suggestThingConstraintKeywords(): Completion[] {
9062
return ["isa", "has", "links"].map((constraintName) => {
9163
return {
@@ -107,37 +79,34 @@ function suggestTypeConstraintKeywords(): Completion[] {
10779
});
10880
}
10981

110-
function suggestDefinedKeywords(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
82+
function suggestDefinedKeywords(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
11183
return ["define", "redefine", "undefine"].map((keyword) => suggest("keyword", keyword, 1));
11284
}
11385

114-
function suggestPipelineStages(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
86+
function suggestPipelineStages(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
11587
return ["match", "insert", "delete", "update", "put", "select", "reduce", "sort", "limit", "offset", "end"].map((keyword) => suggest("keyword", keyword, 1))
11688
}
11789

118-
function suggestKinds(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
90+
function suggestKinds(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
11991
return ["entity", "attribute", "relation"].map((keyword) => suggest("kind", keyword, 2));
12092
}
12193

122-
function suggestNestedPatterns(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: Schema): Completion[] {
94+
function suggestNestedPatterns(context: CompletionContext, tree: Tree, parseAt: SyntaxNode, climbedTo: SyntaxNode, prefix: NodeType[], schema: TypeQLAutocompleteSchema): Completion[] {
12395
return ["not {};", "{} or {};", "try {};"].map((keyword) => suggest("method", keyword, 2));
12496
}
12597

126-
// Hopefully you only have to touch this.
127-
12898
const SUFFIX_VAR_OR_COMMA = [[tokens.COMMA], [tokens.VAR]];
12999

130100

131-
// Will pick the first matching suffix. If you want to handle things manually, use an empty suffix, duh.
132-
133-
const SUGGESTION_GROUP_FOR_THING_STATEMENTS: SuffixOfPrefixSuggestion<Schema>[] = [
101+
// Will pick the first matching suffix. If you want to handle things manually, use an empty suffix.
102+
const SUGGESTION_GROUP_FOR_THING_STATEMENTS: SuffixOfPrefixSuggestion<TypeQLAutocompleteSchema>[] = [
134103
{ suffixes: SUFFIX_VAR_OR_COMMA, suggestions: [suggestThingConstraintKeywords] },
135104
{ suffixes: [[tokens.HAS]], suggestions: [suggestAttributeTypeLabels, suggestVariablesAtMinus10] },
136105
{ suffixes: [[tokens.ISA]], suggestions: [suggestThingTypeLabels, suggestVariablesAtMinus10] },
137106
{ suffixes: [[tokens.HAS, tokens.TypeRef], [tokens.ISA, tokens.TypeRef]], suggestions: [suggestVariablesAtMinus10] },
138107
];
139108

140-
export const SUGGESTION_MAP: SuggestionMap<Schema> = {
109+
export const SUGGESTION_MAP: SuggestionMap<TypeQLAutocompleteSchema> = {
141110
[tokens.LABEL]: [{ suffixes: [[]], suggestions: [suggestThingTypeLabels] }],
142111
[tokens.VAR]: [{ suffixes: [[]], suggestions: [suggestVariablesAt10] }],
143112

0 commit comments

Comments
 (0)