forked from DTStack/monaco-sql-languages
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseSQLWorker.ts
More file actions
90 lines (79 loc) · 2.61 KB
/
baseSQLWorker.ts
File metadata and controls
90 lines (79 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { BasicSQL } from 'dt-sql-parser/dist/parser/common/basicSQL';
import { worker } from './fillers/monaco-editor-core';
import { Suggestions, ParseError, EntityContext } from 'dt-sql-parser';
import { Position } from './fillers/monaco-editor-core';
import { SemanticContext } from 'dt-sql-parser/dist/parser/common/types';
export interface ICreateData {
languageId: string;
}
export abstract class BaseSQLWorker {
protected abstract _ctx: worker.IWorkerContext;
protected abstract parser: BasicSQL;
protected keywords: string[] = [];
constructor(_ctx: worker.IWorkerContext, _createData: ICreateData) {}
async doValidation(code: string): Promise<ParseError[]> {
code = code || this.getTextDocument();
if (code) {
const result = this.parser.validate(code);
return Promise.resolve(result);
}
return Promise.resolve([]);
}
async parserTreeToString(code: string): Promise<string> {
if (code) {
const parser = this.parser.createParser(code);
const parseTree = parser.program();
const result = parseTree.toStringTree(parser);
return Promise.resolve(result);
}
return Promise.resolve('');
}
async doCompletion(code: string, position: Position): Promise<Suggestions | null> {
code = code || this.getTextDocument();
if (code) {
const suggestions = this.parser.getSuggestionAtCaretPosition(code, position);
return Promise.resolve(suggestions);
}
return Promise.resolve(null);
}
async doCompletionWithEntities(
code: string,
position: Position
): Promise<{
suggestions: Suggestions | null;
allEntities: EntityContext[] | null;
context: SemanticContext | null;
}> {
code = code || this.getTextDocument();
if (code) {
const suggestions = this.parser.getSuggestionAtCaretPosition(code, position);
let allEntities = null;
if (suggestions?.syntax?.length) {
allEntities = this.parser.getAllEntities(code, position);
}
const semanticContext = this.parser.getSemanticContextAtCaretPosition(code, position);
return Promise.resolve({
suggestions,
allEntities,
context: semanticContext
});
}
return Promise.resolve({
suggestions: null,
allEntities: null,
context: null
});
}
async getAllEntities(code: string, position?: Position): Promise<EntityContext[] | null> {
code = code || this.getTextDocument();
if (code) {
const allEntities = this.parser.getAllEntities(code, position);
return Promise.resolve(allEntities);
}
return Promise.resolve(null);
}
private getTextDocument(): string {
const model = this._ctx.getMirrorModels()[0]; // When there are multiple files open, this will be an array
return model && model.getValue();
}
}