Skip to content

Commit 2cc4772

Browse files
committed
Refactoring, file split
1 parent a90a76f commit 2cc4772

File tree

5 files changed

+171
-107
lines changed

5 files changed

+171
-107
lines changed

packages/web-console/src/scenes/Editor/Monaco/questdb-sql/createSchemaCompletionProvider.ts

Lines changed: 36 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,12 @@
11
import { Table, uniq } from "../../../../utils"
22
import * as monaco from "monaco-editor"
3-
import { InformationSchemaColumn } from "./types"
4-
import { editor, IRange } from "monaco-editor"
5-
import { languages } from "monaco-editor"
3+
import { CompletionItemPriority, InformationSchemaColumn } from "./types"
4+
import { editor } from "monaco-editor"
65
import IStandaloneCodeEditor = editor.IStandaloneCodeEditor
76
import { findMatches, getQueryFromCursor } from "../utils"
8-
import { operators } from "./operators"
9-
import { dataTypes, functions, keywords } from "@questdb/sql-grammar"
10-
11-
const getLanguageCompletions = (range: IRange) => [
12-
...functions.map((qdbFunction) => {
13-
return {
14-
label: qdbFunction,
15-
kind: languages.CompletionItemKind.Function,
16-
insertText: qdbFunction,
17-
range,
18-
}
19-
}),
20-
...dataTypes.map((item) => {
21-
return {
22-
label: item,
23-
kind: languages.CompletionItemKind.Keyword,
24-
insertText: item,
25-
range,
26-
}
27-
}),
28-
...keywords.map((item) => {
29-
const keyword = item.toUpperCase()
30-
return {
31-
label: keyword,
32-
kind: languages.CompletionItemKind.Keyword,
33-
insertText: keyword,
34-
range,
35-
}
36-
}),
37-
...operators.map((item) => {
38-
const operator = item.toUpperCase()
39-
return {
40-
label: operator,
41-
kind: languages.CompletionItemKind.Operator,
42-
insertText: operator.toUpperCase(),
43-
range,
44-
}
45-
}),
46-
]
47-
48-
export const getColumnCompletions = (
49-
columns: InformationSchemaColumn[],
50-
range: IRange,
51-
withTableName?: boolean,
52-
) => {
53-
// For JOIN ON ... completions, return `table.column` text
54-
if (withTableName) {
55-
return columns.map((item) => ({
56-
label: {
57-
label: `${item.table_name}.${item.column_name}`,
58-
detail: "",
59-
description: item.data_type,
60-
},
61-
kind: languages.CompletionItemKind.Enum,
62-
insertText: `${item.table_name}.${item.column_name}`,
63-
sortText: "1",
64-
range,
65-
}))
66-
// For everything else, return a list of unique column names.
67-
} else {
68-
return uniq(columns.map((item) => item.column_name)).map((columnName) => {
69-
const tableNames = columns
70-
.filter((item) => item.column_name === columnName)
71-
.map((item) => item.table_name)
72-
return {
73-
label: {
74-
label: columnName,
75-
detail: ` (${tableNames.sort().join(", ")})`,
76-
// If the column is present in multiple tables, show their list here, otherwise return the column type.
77-
description:
78-
tableNames.length > 1
79-
? ""
80-
: columns.find((item) => item.column_name === columnName)
81-
?.data_type,
82-
},
83-
kind: languages.CompletionItemKind.Enum,
84-
insertText: columnName,
85-
sortText: "1",
86-
range,
87-
}
88-
})
89-
}
90-
}
7+
import { getTableCompletions } from "./getTableCompletions"
8+
import { getColumnCompletions } from "./getColumnCompletions"
9+
import { getLanguageCompletions } from "./getLanguageCompletions"
9110

9211
export const createSchemaCompletionProvider = (
9312
editor: IStandaloneCodeEditor,
@@ -149,20 +68,6 @@ export const createSchemaCompletionProvider = (
14968
const openQuote = textUntilPosition.substr(-1) === '"'
15069
const nextCharQuote = nextChar == '"'
15170

152-
const tableSuggestions = tables.map((item) => {
153-
return {
154-
label: item.table_name,
155-
kind: languages.CompletionItemKind.Class,
156-
insertText: openQuote
157-
? item.table_name + (nextCharQuote ? "" : '"')
158-
: /^[a-z0-9_]+$/i.test(item.table_name)
159-
? item.table_name
160-
: `"${item.table_name}"`,
161-
sortText: "1",
162-
range,
163-
}
164-
})
165-
16671
if (
16772
/(FROM|INTO|(ALTER|BACKUP|DROP|REINDEX|RENAME|TRUNCATE|VACUUM) TABLE|JOIN|UPDATE)\s$/gim.test(
16873
textUntilPosition,
@@ -171,7 +76,13 @@ export const createSchemaCompletionProvider = (
17176
!textUntilPosition.endsWith("= '"))
17277
) {
17378
return {
174-
suggestions: tableSuggestions,
79+
suggestions: getTableCompletions({
80+
tables,
81+
range,
82+
priority: CompletionItemPriority.High,
83+
openQuote,
84+
nextCharQuote,
85+
}),
17586
}
17687
}
17788

@@ -186,21 +97,33 @@ export const createSchemaCompletionProvider = (
18697
textUntilPosition.match(/\sON\s/gim) !== null
18798
return {
18899
suggestions: [
189-
...getColumnCompletions(
190-
informationSchemaColumns.filter((item) =>
100+
...getColumnCompletions({
101+
columns: informationSchemaColumns.filter((item) =>
191102
tableContext.includes(item.table_name),
192103
),
193104
range,
194105
withTableName,
195-
),
106+
priority: CompletionItemPriority.High,
107+
}),
196108
...getLanguageCompletions(range),
197109
],
198110
}
199111
} else {
200112
return {
201113
suggestions: [
202-
...getColumnCompletions(informationSchemaColumns, range),
203-
...tableSuggestions,
114+
...getColumnCompletions({
115+
columns: informationSchemaColumns,
116+
range,
117+
withTableName: false,
118+
priority: CompletionItemPriority.High,
119+
}),
120+
...getTableCompletions({
121+
tables,
122+
range,
123+
priority: CompletionItemPriority.MediumHigh,
124+
openQuote,
125+
nextCharQuote,
126+
}),
204127
...getLanguageCompletions(range),
205128
],
206129
}
@@ -210,7 +133,13 @@ export const createSchemaCompletionProvider = (
210133
if (word.word) {
211134
return {
212135
suggestions: [
213-
...tableSuggestions,
136+
...getTableCompletions({
137+
tables,
138+
range,
139+
priority: CompletionItemPriority.High,
140+
openQuote,
141+
nextCharQuote,
142+
}),
214143
...getLanguageCompletions(range),
215144
],
216145
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { uniq } from "../../../../utils"
2+
import { CompletionItemPriority, InformationSchemaColumn } from "./types"
3+
import { IRange } from "monaco-editor"
4+
import { languages } from "monaco-editor"
5+
6+
export const getColumnCompletions = ({
7+
columns,
8+
range,
9+
withTableName,
10+
priority,
11+
}: {
12+
columns: InformationSchemaColumn[]
13+
range: IRange
14+
withTableName: boolean
15+
priority: CompletionItemPriority
16+
}) => {
17+
// For JOIN ON ... completions, return `table.column` text
18+
if (withTableName) {
19+
return columns.map((item) => ({
20+
label: {
21+
label: `${item.table_name}.${item.column_name}`,
22+
detail: "",
23+
description: item.data_type,
24+
},
25+
kind: languages.CompletionItemKind.Enum,
26+
insertText: `${item.table_name}.${item.column_name}`,
27+
sortText: priority,
28+
range,
29+
}))
30+
// For everything else, return a list of unique column names.
31+
} else {
32+
return uniq(columns.map((item) => item.column_name)).map((columnName) => {
33+
const tableNames = columns
34+
.filter((item) => item.column_name === columnName)
35+
.map((item) => item.table_name)
36+
return {
37+
label: {
38+
label: columnName,
39+
detail: ` (${tableNames.sort().join(", ")})`,
40+
// If the column is present in multiple tables, show their list here, otherwise return the column type.
41+
description:
42+
tableNames.length > 1
43+
? ""
44+
: columns.find((item) => item.column_name === columnName)
45+
?.data_type,
46+
},
47+
kind: languages.CompletionItemKind.Enum,
48+
insertText: columnName,
49+
sortText: priority,
50+
range,
51+
}
52+
})
53+
}
54+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { IRange } from "monaco-editor"
2+
import { languages } from "monaco-editor"
3+
import { operators } from "./operators"
4+
import { dataTypes, functions, keywords } from "@questdb/sql-grammar"
5+
6+
export const getLanguageCompletions = (range: IRange) => [
7+
...functions.map((qdbFunction) => {
8+
return {
9+
label: qdbFunction,
10+
kind: languages.CompletionItemKind.Function,
11+
insertText: qdbFunction,
12+
range,
13+
}
14+
}),
15+
...dataTypes.map((item) => {
16+
return {
17+
label: item,
18+
kind: languages.CompletionItemKind.Keyword,
19+
insertText: item,
20+
range,
21+
}
22+
}),
23+
...keywords.map((item) => {
24+
const keyword = item.toUpperCase()
25+
return {
26+
label: keyword,
27+
kind: languages.CompletionItemKind.Keyword,
28+
insertText: keyword,
29+
range,
30+
}
31+
}),
32+
...operators.map((item) => {
33+
const operator = item.toUpperCase()
34+
return {
35+
label: operator,
36+
kind: languages.CompletionItemKind.Operator,
37+
insertText: operator.toUpperCase(),
38+
range,
39+
}
40+
}),
41+
]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Table } from "../../../../utils"
2+
import { CompletionItemPriority } from "./types"
3+
import { IRange } from "monaco-editor"
4+
import { languages } from "monaco-editor"
5+
6+
export const getTableCompletions = ({
7+
tables,
8+
range,
9+
priority,
10+
openQuote,
11+
nextCharQuote,
12+
}: {
13+
tables: Table[]
14+
range: IRange
15+
priority: CompletionItemPriority
16+
openQuote: boolean
17+
nextCharQuote: boolean
18+
}) => {
19+
return tables.map((item) => {
20+
return {
21+
label: item.table_name,
22+
kind: languages.CompletionItemKind.Class,
23+
insertText: openQuote
24+
? item.table_name + (nextCharQuote ? "" : '"')
25+
: /^[a-z0-9_]+$/i.test(item.table_name)
26+
? item.table_name
27+
: `"${item.table_name}"`,
28+
sortText: priority,
29+
range,
30+
}
31+
})
32+
}

packages/web-console/src/scenes/Editor/Monaco/questdb-sql/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ export type InformationSchemaColumn = {
44
column_name: string
55
data_type: string
66
}
7+
8+
export enum CompletionItemPriority {
9+
High = "1",
10+
MediumHigh = "2",
11+
Medium = "3",
12+
MediumLow = "4",
13+
Low = "5",
14+
}

0 commit comments

Comments
 (0)