-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcreateSchemaCompletionProvider.ts
169 lines (153 loc) · 5.76 KB
/
createSchemaCompletionProvider.ts
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
165
166
167
168
169
import { Table, uniq } from "../../../../utils"
import * as monaco from "monaco-editor"
import { CompletionItemPriority, InformationSchemaColumn } from "./types"
import { editor } from "monaco-editor"
import IStandaloneCodeEditor = editor.IStandaloneCodeEditor
import { findMatches, getQueryFromCursor } from "../utils"
import { getTableCompletions } from "./getTableCompletions"
import { getColumnCompletions } from "./getColumnCompletions"
import { getLanguageCompletions } from "./getLanguageCompletions"
const trimQuotesFromTableName = (tableName: string) => {
return tableName.replace(/(^")|("$)/g, "")
}
export const createSchemaCompletionProvider = (
editor: IStandaloneCodeEditor,
tables: Table[] = [],
informationSchemaColumns: InformationSchemaColumn[] = [],
) => {
const completionProvider: monaco.languages.CompletionItemProvider = {
triggerCharacters:
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n ."'.split(""),
provideCompletionItems(model, position) {
const word = model.getWordUntilPosition(position)
const queryAtCursor = getQueryFromCursor(editor)
let tableContext: string[] = []
if (queryAtCursor) {
const matches = findMatches(model, queryAtCursor.query)
if (matches.length > 0) {
const cursorMatch = matches.find(
(m) => m.range.startLineNumber === queryAtCursor.row + 1,
)
const fromMatch = queryAtCursor.query.match(/(?<=FROM\s)([^ )]+)/gim)
const joinMatch = queryAtCursor.query.match(/(JOIN)\s+([^ ]+)/i)
const alterTableMatch = queryAtCursor.query.match(
/(ALTER TABLE)\s+([^ ]+)/i,
)
if (fromMatch) {
tableContext = uniq(fromMatch)
} else if (alterTableMatch && alterTableMatch[2]) {
tableContext.push(alterTableMatch[2])
}
if (joinMatch && joinMatch[2]) {
tableContext.push(joinMatch[2])
}
tableContext = tableContext.map(trimQuotesFromTableName)
const textUntilPosition = model.getValueInRange({
startLineNumber: cursorMatch?.range.startLineNumber ?? 1,
startColumn: cursorMatch?.range.startColumn ?? 1,
endLineNumber: position.lineNumber,
endColumn: word.startColumn,
})
const range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: word.startColumn,
endColumn: word.endColumn,
}
const nextChar = model.getValueInRange({
startLineNumber: position.lineNumber,
startColumn: word.endColumn,
endLineNumber: position.lineNumber,
endColumn: word.endColumn + 1,
})
const openQuote = textUntilPosition.substr(-1) === '"'
const nextCharQuote = nextChar == '"'
if (
/(FROM|INTO|(ALTER|BACKUP|DROP|REINDEX|RENAME|TRUNCATE|VACUUM) TABLE|JOIN|UPDATE)\s$/gim.test(
textUntilPosition,
) ||
(/'$/gim.test(textUntilPosition) &&
!textUntilPosition.endsWith("= '"))
) {
return {
suggestions: getTableCompletions({
tables,
range,
priority: CompletionItemPriority.High,
openQuote,
nextCharQuote,
}),
}
}
// get text value in the current line
const textInLine = model.getValueInRange({
startLineNumber: position.lineNumber,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column,
})
// check if `textInLine` contains whitespaces only
const isWhitespaceOnly = /^\s*$/.test(textInLine)
if (
/(?:(SELECT|UPDATE).*?(?:(?:,(?:COLUMN )?)|(?:ALTER COLUMN ))?(?:WHERE )?(?: BY )?(?: ON )?(?: SET )?$|ALTER COLUMN )/gim.test(
textUntilPosition,
) &&
!isWhitespaceOnly
) {
if (tableContext.length > 0) {
const withTableName =
textUntilPosition.match(/\sON\s/gim) !== null
return {
suggestions: [
...getColumnCompletions({
columns: informationSchemaColumns.filter((item) =>
tableContext.includes(item.table_name),
),
range,
withTableName,
priority: CompletionItemPriority.High,
}),
...getLanguageCompletions(range),
],
}
} else {
return {
suggestions: [
...getColumnCompletions({
columns: informationSchemaColumns,
range,
withTableName: false,
priority: CompletionItemPriority.High,
}),
...getTableCompletions({
tables,
range,
priority: CompletionItemPriority.MediumHigh,
openQuote,
nextCharQuote,
}),
...getLanguageCompletions(range),
],
}
}
}
if (word.word) {
return {
suggestions: [
...getTableCompletions({
tables,
range,
priority: CompletionItemPriority.High,
openQuote,
nextCharQuote,
}),
...getLanguageCompletions(range),
],
}
}
}
}
},
}
return completionProvider
}