Skip to content

Commit ad435d6

Browse files
committed
feat: add tooltip for suggest sql function usage
1 parent a4852d4 commit ad435d6

18 files changed

+1144
-474
lines changed

package-lock.json

+332-467
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"build": "concurrently \"npm run build:main\" \"npm run build:renderer\"",
2121
"build:main": "cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.main.prod.ts",
2222
"build:renderer": "cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.prod.ts",
23+
"build:dialect": "ts-node src/dialects/build_dialect.ts",
2324
"postinstall": "ts-node .erb/scripts/check-native-dep.js && electron-builder install-app-deps && cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.dev.dll.ts",
2425
"lint": "cross-env NODE_ENV=development eslint . --ext .js,.jsx,.ts,.tsx",
2526
"package": "ts-node ./.erb/scripts/clean.js dist && npm run build && electron-builder build --publish never",
@@ -91,6 +92,7 @@
9192
"react-dom": "^18.2.0",
9293
"react-router-dom": "^6.8.1",
9394
"react-syntax-highlighter": "^15.5.0",
95+
"showdown": "^2.1.0",
9496
"sql-formatter": "^13.0.0",
9597
"sqlstring": "^2.3.3",
9698
"swr": "^2.2.1",
@@ -127,6 +129,7 @@
127129
"@types/react-dom": "^18.0.10",
128130
"@types/react-syntax-highlighter": "^15.5.7",
129131
"@types/react-test-renderer": "^18.0.0",
132+
"@types/showdown": "^2.0.1",
130133
"@types/source-map-support": "^0.5.6",
131134
"@types/sqlstring": "^2.3.0",
132135
"@types/terser-webpack-plugin": "^5.0.4",

src/dialects/MySQLDialect.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { SQLDialect } from './../language/dist';
2+
import dialect from './mysql/dialect.json';
3+
import tooltips from './mysql/suggestion.json';
4+
5+
export const MySQLDialect = SQLDialect.define(dialect);
6+
export const MySQLTooltips: Record<
7+
string,
8+
{ syntax: string; description: string }
9+
> = tooltips;

src/dialects/build_dialect.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { TooltipDirectionary } from 'renderer/components/CodeEditor/exampleTooltip';
4+
import showdown from 'showdown';
5+
6+
function readKeywordsFromFile(file: string): string[] {
7+
const keywords = fs
8+
.readFileSync(file)
9+
.toString()
10+
.split('\n')
11+
.map((r) => r.trim());
12+
13+
return keywords;
14+
}
15+
16+
function build_dialect(folderName: string) {
17+
const functionFiles = fs.readdirSync(
18+
path.join(__dirname, folderName, 'functions')
19+
);
20+
const functions: TooltipDirectionary = {};
21+
22+
const mdConverter = new showdown.Converter();
23+
for (const functionFile of functionFiles) {
24+
const mdContent = fs
25+
.readFileSync(path.join(__dirname, folderName, 'functions', functionFile))
26+
.toString();
27+
28+
const mdContentLines = mdContent.split('\n');
29+
30+
functions[path.parse(functionFile).name] = {
31+
syntax: mdContentLines[0],
32+
description: mdConverter.makeHtml(mdContentLines.slice(2).join('\n')),
33+
};
34+
}
35+
36+
fs.writeFileSync(
37+
path.join(__dirname, folderName, 'suggestion.json'),
38+
JSON.stringify(functions, undefined, 2)
39+
);
40+
41+
const baseKeywords = readKeywordsFromFile(
42+
path.join(__dirname, 'keywords.txt')
43+
);
44+
const baseTypes = readKeywordsFromFile(path.join(__dirname, 'types.txt'));
45+
46+
const dialectKeywords = readKeywordsFromFile(
47+
path.join(__dirname, folderName, 'keywords.txt')
48+
);
49+
50+
const dialectTypes = readKeywordsFromFile(
51+
path.join(__dirname, folderName, 'types.txt')
52+
);
53+
54+
const combinedKeywords = Array.from(
55+
new Set([...baseKeywords, ...dialectKeywords, ...Object.keys(functions)])
56+
);
57+
58+
const combinedTypes = Array.from(new Set([...baseTypes, ...dialectTypes]));
59+
60+
const rules = JSON.parse(
61+
fs.readFileSync(path.join(__dirname, folderName, 'rules.json')).toString()
62+
);
63+
64+
const dialect = {
65+
...rules,
66+
keywords: combinedKeywords.join(' '),
67+
types: combinedTypes.join(' '),
68+
};
69+
70+
fs.writeFileSync(
71+
path.join(__dirname, folderName, 'dialect.json'),
72+
JSON.stringify(dialect, undefined, 2)
73+
);
74+
}
75+
76+
build_dialect('mysql');

src/dialects/keywords.txt

+264
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
absolute
2+
action
3+
add
4+
after
5+
all
6+
allocate
7+
alter
8+
and
9+
any
10+
are
11+
as
12+
asc
13+
assertion
14+
at
15+
authorization
16+
before
17+
begin
18+
between
19+
both
20+
breadth
21+
by
22+
call
23+
cascade
24+
cascaded
25+
case
26+
cast
27+
catalog
28+
check
29+
close
30+
collate
31+
collation
32+
column
33+
commit
34+
condition
35+
connect
36+
connection
37+
constraint
38+
constraints
39+
constructor
40+
continue
41+
corresponding
42+
count
43+
create
44+
cross
45+
cube
46+
current
47+
current_date
48+
current_default_transform_group
49+
current_transform_group_for_type
50+
current_path
51+
current_role
52+
current_time
53+
current_timestamp
54+
current_user
55+
cursor
56+
cycle
57+
data
58+
day
59+
deallocate
60+
declare
61+
default
62+
deferrable
63+
deferred
64+
delete
65+
depth
66+
deref
67+
desc
68+
describe
69+
descriptor
70+
deterministic
71+
diagnostics
72+
disconnect
73+
distinct
74+
do
75+
domain
76+
drop
77+
dynamic
78+
each
79+
else
80+
elseif
81+
end
82+
end-exec
83+
equals
84+
escape
85+
except
86+
exception
87+
exec
88+
execute
89+
exists
90+
exit
91+
external
92+
fetch
93+
first
94+
for
95+
foreign
96+
found
97+
from
98+
free
99+
full
100+
function
101+
general
102+
get
103+
global
104+
go
105+
goto
106+
grant
107+
group
108+
grouping
109+
handle
110+
having
111+
hold
112+
hour
113+
identity
114+
if
115+
immediate
116+
in
117+
indicator
118+
initially
119+
inner
120+
inout
121+
input
122+
insert
123+
intersect
124+
into
125+
is
126+
isolation
127+
join
128+
key
129+
language
130+
last
131+
lateral
132+
leading
133+
leave
134+
left
135+
level
136+
like
137+
limit
138+
local
139+
localtime
140+
localtimestamp
141+
locator
142+
loop
143+
map
144+
match
145+
method
146+
minute
147+
modifies
148+
module
149+
month
150+
names
151+
natural
152+
nesting
153+
new
154+
next
155+
no
156+
none
157+
not
158+
of
159+
old
160+
on
161+
only
162+
open
163+
option
164+
or
165+
order
166+
ordinality
167+
out
168+
outer
169+
output
170+
overlaps
171+
pad
172+
parameter
173+
partial
174+
path
175+
prepare
176+
preserve
177+
primary
178+
prior
179+
privileges
180+
procedure
181+
public
182+
read
183+
reads
184+
recursive
185+
redo
186+
ref
187+
references
188+
referencing
189+
relative
190+
release
191+
repeat
192+
resignal
193+
restrict
194+
result
195+
return
196+
returns
197+
revoke
198+
right
199+
role
200+
rollback
201+
rollup
202+
routine
203+
row
204+
rows
205+
savepoint
206+
schema
207+
scroll
208+
search
209+
second
210+
section
211+
select
212+
session
213+
session_user
214+
set
215+
sets
216+
signal
217+
similar
218+
size
219+
some
220+
space
221+
specific
222+
specifictype
223+
sql
224+
sqlexception
225+
sqlstate
226+
sqlwarning
227+
start
228+
state
229+
static
230+
system_user
231+
table
232+
temporary
233+
then
234+
timezone_hour
235+
timezone_minute
236+
to
237+
trailing
238+
transaction
239+
translation
240+
treat
241+
trigger
242+
under
243+
undo
244+
union
245+
unique
246+
unnest
247+
until
248+
update
249+
usage
250+
user
251+
using
252+
value
253+
values
254+
view
255+
when
256+
whenever
257+
where
258+
while
259+
with
260+
without
261+
work
262+
write
263+
year
264+
zone

src/dialects/mysql/builtin.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
charset
2+
clear
3+
edit
4+
ego
5+
help
6+
nopager
7+
notee
8+
nowarning
9+
pager
10+
print
11+
prompt
12+
quit
13+
rehash
14+
source
15+
status
16+
system
17+
tee

0 commit comments

Comments
 (0)