forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathquery-editor.js
164 lines (143 loc) · 5.39 KB
/
query-editor.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'brace';
import 'brace/mode/python';
import 'brace/mode/sql';
import 'brace/mode/json';
import 'brace/ext/language_tools';
import { map } from 'underscore';
// By default Ace will try to load snippet files for the different modes and fail.
// We don't need them, so we use these placeholders until we define our own.
function defineDummySnippets(mode) {
window.ace.define(`ace/snippets/${mode}`, ['require', 'exports', 'module'], (require, exports) => {
exports.snippetText = '';
exports.scope = mode;
});
}
defineDummySnippets('python');
defineDummySnippets('sql');
defineDummySnippets('json');
function queryEditor(QuerySnippet) {
return {
restrict: 'E',
scope: {
query: '=',
schema: '=',
syntax: '=',
},
template: '<div ui-ace="editorOptions" ng-model="query.query"></div>',
link: {
pre($scope) {
$scope.syntax = $scope.syntax || 'sql';
$scope.editorOptions = {
mode: 'json',
// require: ['ace/ext/language_tools'],
advanced: {
behavioursEnabled: true,
enableSnippets: true,
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
autoScrollEditorIntoView: true,
},
onLoad(editor) {
// Release Cmd/Ctrl+L to the browser
editor.commands.bindKey('Cmd+L', null);
editor.commands.bindKey('Ctrl+L', null);
QuerySnippet.query((snippets) => {
window.ace.acequire(['ace/snippets'], (snippetsModule) => {
const snippetManager = snippetsModule.snippetManager;
const m = {
snippetText: '',
};
m.snippets = snippetManager.parseSnippetFile(m.snippetText);
snippets.forEach((snippet) => {
m.snippets.push(snippet.getSnippet());
});
snippetManager.register(m.snippets || [], m.scope);
});
});
editor.$blockScrolling = Infinity;
editor.getSession().setUseWrapMode(true);
editor.setShowPrintMargin(false);
$scope.$watch('syntax', (syntax) => {
const newMode = `ace/mode/${syntax}`;
editor.getSession().setMode(newMode);
});
$scope.$watch('autoCompleteSchema', (newSchema, oldSchema) => {
if (newSchema !== oldSchema) {
const tokensCount =
newSchema.reduce((totalLength, table) => totalLength + table.columns.length, 0);
// If there are too many tokens we disable live autocomplete,
// as it makes typing slower.
if (tokensCount > 3000) {
editor.setOption('enableLiveAutocompletion', false);
editor.setOption('enableBasicAutocompletion', false);
} else {
editor.setOption('enableLiveAutocompletion', true);
editor.setOption('enableBasicAutocompletion', true);
}
}
});
$scope.$parent.$on('angular-resizable.resizing', () => {
editor.resize();
});
editor.focus();
},
};
function removeExtraSchemaInfo(data) {
let newColumns = [];
data.forEach((table) => {
table.columns.forEach((column) => {
if (column.charAt(column.length - 1) === ')') {
let parensStartAt = column.indexOf('(') - 1;
column = column.substring(0, parensStartAt);
parensStartAt = 1; // linter complains without this line
}
// remove '[P] ' for partition keys
if (column.charAt(0) === '[') {
column = column.substring(4, column.length);
}
newColumns.push(column);
});
table.autoCompleteColumns = newColumns;
});
newColumns = []; // linter complains without this line
return data;
}
const schemaCompleter = {
getCompletions(state, session, pos, prefix, callback) {
// make a variable for the auto completion in the query editor
$scope.autoCompleteSchema = removeExtraSchemaInfo($scope.schema);
if (prefix.length === 0 || !$scope.autoCompleteSchema) {
callback(null, []);
return;
}
if (!$scope.autoCompleteSchema.keywords) {
const keywords = {};
$scope.autoCompleteSchema.forEach((table) => {
keywords[table.name] = 'Table';
table.autoCompleteColumns.forEach((c) => {
keywords[c] = 'Column';
keywords[`${table.name}.${c}`] = 'Column';
});
});
$scope.autoCompleteSchema.keywords = map(keywords, (v, k) =>
({
name: k,
value: k,
score: 0,
meta: v,
})
);
}
callback(null, $scope.autoCompleteSchema.keywords);
},
};
window.ace.acequire(['ace/ext/language_tools'], (langTools) => {
langTools.addCompleter(schemaCompleter);
});
},
},
};
}
export default function (ngModule) {
ngModule.directive('queryEditor', queryEditor);
}