|
1 | 1 | import {EditorState} from "@codemirror/state"
|
2 | 2 | import {CompletionContext, CompletionResult, CompletionSource} from "@codemirror/autocomplete"
|
3 |
| -import {schemaCompletionSource, PostgreSQL, MySQL, SQLConfig, SQLDialect} from "@codemirror/lang-sql" |
| 3 | +import {schemaCompletionSource, keywordCompletionSource, PostgreSQL, MySQL, SQLConfig, SQLDialect} from "@codemirror/lang-sql" |
4 | 4 | import ist from "ist"
|
5 | 5 |
|
6 |
| -function get(doc: string, conf: SQLConfig & {explicit?: boolean} = {}) { |
| 6 | +function get(doc: string, conf: SQLConfig & {explicit?: boolean, keywords?: boolean} = {}) { |
7 | 7 | let cur = doc.indexOf("|"), dialect = conf.dialect || PostgreSQL
|
8 | 8 | doc = doc.slice(0, cur) + doc.slice(cur + 1)
|
9 | 9 | let state = EditorState.create({
|
10 | 10 | doc,
|
11 | 11 | selection: {anchor: cur},
|
12 | 12 | extensions: [dialect, dialect.language.data.of({
|
13 |
| - autocomplete: schemaCompletionSource(Object.assign({dialect}, conf)) |
| 13 | + autocomplete: conf.keywords |
| 14 | + ? keywordCompletionSource(dialect, conf.upperCaseKeywords, conf.keywordCompletion) |
| 15 | + : schemaCompletionSource(Object.assign({dialect}, conf)) |
14 | 16 | })]
|
15 | 17 | })
|
16 | 18 | let result = state.languageDataAt<CompletionSource>("autocomplete", cur)[0](new CompletionContext(state, cur, !!conf.explicit))
|
@@ -213,4 +215,16 @@ describe("SQL completion", () => {
|
213 | 215 | ist(get("select foo.|", s)!.options[0].type, "constant")
|
214 | 216 | ist(get("select foo.|", s)!.options.length, 1)
|
215 | 217 | })
|
| 218 | + |
| 219 | + it("can complete keywords", () => { |
| 220 | + ist(get("s|", {keywords: true})!.options.some(c => c.label == "select")) |
| 221 | + }) |
| 222 | + |
| 223 | + it("can complete upper-case keywords", () => { |
| 224 | + ist(get("s|", {keywords: true, upperCaseKeywords: true})!.options.some(c => c.label == "SELECT")) |
| 225 | + }) |
| 226 | + |
| 227 | + it("can transform keyword completions", () => { |
| 228 | + ist(get("s|", {keywords: true, keywordCompletion: l => ({label: l, type: "x"})})!.options.every(c => c.type == "x")) |
| 229 | + }) |
216 | 230 | })
|
0 commit comments