Skip to content

Commit da99fdf

Browse files
williamdesmarijnh
authored andcommitted
[sql-hint addon] Add tests and fix codemirror#5817
1 parent 2c123a4 commit da99fdf

File tree

2 files changed

+84
-19
lines changed

2 files changed

+84
-19
lines changed

addon/hint/sql-hint.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -275,24 +275,29 @@
275275
if (search.charAt(0) == "." || search.charAt(0) == identifierQuote) {
276276
start = nameCompletion(cur, token, result, editor);
277277
} else {
278-
addMatches(result, search, defaultTable, function(w) {return {text:w, className: "CodeMirror-hint-table CodeMirror-hint-default-table"};});
279-
addMatches(
280-
result,
281-
search,
282-
tables,
283-
function(w) {
284-
if (typeof w === 'object') {
285-
w.className = "CodeMirror-hint-table";
286-
} else {
287-
w = {text: w, className: "CodeMirror-hint-table"};
288-
}
289-
290-
return w;
291-
}
292-
);
293-
if (!disableKeywords)
294-
addMatches(result, search, keywords, function(w) {return {text: w.toUpperCase(), className: "CodeMirror-hint-keyword"};});
295-
}
278+
var objectOrClass = function(w, className) {
279+
if (typeof w === "object") {
280+
w.className = className;
281+
} else {
282+
w = { text: w, className: className };
283+
}
284+
return w;
285+
};
286+
addMatches(result, search, defaultTable, function(w) {
287+
return objectOrClass(w, "CodeMirror-hint-table CodeMirror-hint-default-table");
288+
});
289+
addMatches(
290+
result,
291+
search,
292+
tables, function(w) {
293+
return objectOrClass(w, "CodeMirror-hint-table");
294+
}
295+
);
296+
if (!disableKeywords)
297+
addMatches(result, search, keywords, function(w) {
298+
return objectOrClass(w.toUpperCase(), "CodeMirror-hint-keyword");
299+
});
300+
}
296301

297302
return {list: result, from: Pos(cur.line, start), to: Pos(cur.line, end)};
298303
});

test/sql-hint-test.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,44 @@
2121
{text: "name", displayText: "name | The name"}]
2222
}];
2323

24+
var displayTextTablesWithDefault = [
25+
{
26+
text: "Api__TokenAliases",
27+
columns: [
28+
{
29+
text: "token",
30+
displayText: "token | varchar(255) | Primary",
31+
columnName: "token",
32+
columnHint: "varchar(255) | Primary"
33+
},
34+
{
35+
text: "alias",
36+
displayText: "alias | varchar(255) | Primary",
37+
columnName: "alias",
38+
columnHint: "varchar(255) | Primary"
39+
}
40+
]
41+
},
42+
{
43+
text: "mytable",
44+
columns: [
45+
{ text: "id", displayText: "id | Unique ID" },
46+
{ text: "name", displayText: "name | The name" }
47+
]
48+
}
49+
];
50+
2451
namespace = "sql-hint_";
2552

2653
function test(name, spec) {
2754
testCM(name, function(cm) {
2855
cm.setValue(spec.value);
2956
cm.setCursor(spec.cursor);
30-
var completion = CodeMirror.hint.sql(cm, {tables: spec.tables});
57+
var completion = CodeMirror.hint.sql(cm, {
58+
tables: spec.tables,
59+
defaultTable: spec.defaultTable,
60+
disableKeywords: spec.disableKeywords
61+
});
3162
if (!deepCompare(completion.list, spec.list))
3263
throw new Failure("Wrong completion results " + JSON.stringify(completion.list) + " vs " + JSON.stringify(spec.list));
3364
eqCharPos(completion.from, spec.from);
@@ -46,6 +77,15 @@
4677
to: Pos(0, 3)
4778
});
4879

80+
test("keywords_disabled", {
81+
value: "SEL",
82+
cursor: Pos(0, 3),
83+
disableKeywords: true,
84+
list: [],
85+
from: Pos(0, 0),
86+
to: Pos(0, 3)
87+
});
88+
4989
test("from", {
5090
value: "SELECT * fr",
5191
cursor: Pos(0, 11),
@@ -185,6 +225,26 @@
185225
mode: "text/x-sqlite"
186226
});
187227

228+
test("displayText_default_table", {
229+
value: "SELECT a",
230+
cursor: Pos(0, 8),
231+
disableKeywords: true,
232+
defaultTable: "Api__TokenAliases",
233+
tables: displayTextTablesWithDefault,
234+
list: [
235+
{
236+
text: "alias",
237+
displayText: "alias | varchar(255) | Primary",
238+
columnName: "alias",
239+
columnHint: "varchar(255) | Primary",
240+
className: "CodeMirror-hint-table CodeMirror-hint-default-table"
241+
},
242+
{ text: "Api__TokenAliases", className: "CodeMirror-hint-table" }
243+
],
244+
from: Pos(0, 7),
245+
to: Pos(0, 8)
246+
});
247+
188248
test("displayText_table", {
189249
value: "SELECT myt",
190250
cursor: Pos(0, 10),

0 commit comments

Comments
 (0)