Skip to content

Commit d05b88c

Browse files
committed
fix: better syntax
1 parent e1257f4 commit d05b88c

File tree

3 files changed

+176
-32
lines changed

3 files changed

+176
-32
lines changed

src/components/SqlHighlighter/SqlHighlighter.tsx

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,13 @@
11
import {useThemeValue} from '@gravity-ui/uikit';
22
import {PrismLight as SyntaxHighlighter} from 'react-syntax-highlighter';
3-
import plsql from 'react-syntax-highlighter/dist/esm/languages/prism/plsql';
4-
import {
5-
vscDarkPlus as darkTheme,
6-
materialLight as lightTheme,
7-
} from 'react-syntax-highlighter/dist/esm/styles/prism';
8-
9-
// Create custom themes with transparent backgrounds
10-
const light = {
11-
...lightTheme,
12-
'pre[class*="language-"]': {
13-
...lightTheme['pre[class*="language-"]'],
14-
background: 'transparent',
15-
},
16-
'code[class*="language-"]': {
17-
...lightTheme['code[class*="language-"]'],
18-
background: 'transparent',
19-
},
20-
};
21-
22-
const dark = {
23-
...darkTheme,
24-
'pre[class*="language-"]': {
25-
...darkTheme['pre[class*="language-"]'],
26-
background: 'transparent',
27-
},
28-
'code[class*="language-"]': {
29-
...darkTheme['code[class*="language-"]'],
30-
background: 'transparent',
31-
},
32-
};
333

344
import {cn} from '../../utils/cn';
355

6+
import {dark, light, yql} from './yql';
7+
368
import './SqlHighlighter.scss';
379

38-
SyntaxHighlighter.registerLanguage('plsql', plsql);
10+
SyntaxHighlighter.registerLanguage('yql', yql);
3911

4012
const b = cn('sql-highlighter');
4113

@@ -50,7 +22,7 @@ export const SqlHighlighter = ({children, className}: SqlHighlighterProps) => {
5022

5123
return (
5224
<div className={b(null, className)}>
53-
<SyntaxHighlighter language="sql" style={isDark ? dark : light}>
25+
<SyntaxHighlighter language="yql" style={isDark ? dark : light}>
5426
{children}
5527
</SyntaxHighlighter>
5628
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const keywords =
2+
'$row|$rows|action|add|all|alter|and|any|as|asc|assume|async|bigserial|serial|smallserial|serial8|serial4|serial2|begin|bernoulli|between|by|case|changefeed|column|columns|commit|compact|create|cross|cube|declare|define|delete|desc|dict|discard|distinct|do|drop|else|empty_action|end|erase|evaluate|exclusion|exists|export|family|flatten|for|from|full|global|group|grouping|having|if|ignore|ilike|import|in|index|inner|insert|into|is|join|key|left|like|limit|list|match|not|null|nulls|offset|on|only|optional|or|order|over|partition|pragma|presort|primary|process|reduce|regexp|repeatable|replace|respect|result|return|right|rlike|rollup|sample|schema|select|semi|set|sets|stream|subquery|sync|table|tablesample|then|truncate|union|intersect|except|update|upsert|use|using|values|view|when|where|window|with|without|xor|callable|resource|tagged|generic|unit|void|emptylist|emptydict|flow|callable|resource|tagged|generic|unit|void|emptylist|emptydict|flow'.split(
3+
'|',
4+
);
5+
export const typeKeywords =
6+
'bool|date|datetime|decimal|double|float|int16|int32|int64|int8|interval|json|jsondocument|string|timestamp|tzdate|tzdatetime|tztimestamp|uint16|uint32|uint64|uint8|utf8|uuid|yson|text|bytes'.split(
7+
'|',
8+
);
9+
10+
export const builtinFunctions =
11+
'abs|aggregate_by|aggregate_list|aggregate_list_distinct|agg_list|agg_list_distinct|as_table|avg|avg_if|adaptivedistancehistogram|adaptivewardhistogram|adaptiveweighthistogram|addmember|addtimezone|aggregateflatten|aggregatetransforminput|aggregatetransformoutput|aggregationfactory|asatom|asdict|asdictstrict|asenum|aslist|asliststrict|asset|assetstrict|asstruct|astagged|astuple|asvariant|atomcode|bitcast|bit_and|bit_or|bit_xor|bool_and|bool_or|bool_xor|bottom|bottom_by|blockwardhistogram|blockweighthistogram|cast|coalesce|concat|concat_strict|correlation|count|count_if|covariance|covariance_population|covariance_sample|callableargument|callableargumenttype|callableresulttype|callabletype|callabletypecomponents|callabletypehandle|choosemembers|combinemembers|countdistinctestimate|currentauthenticateduser|currentoperationid|currentoperationsharedid|currenttzdate|currenttzdatetime|currenttztimestamp|currentutcdate|currentutcdatetime|currentutctimestamp|dense_rank|datatype|datatypecomponents|datatypehandle|dictaggregate|dictcontains|dictcreate|dicthasitems|dictitems|dictkeytype|dictkeys|dictlength|dictlookup|dictpayloadtype|dictpayloads|dicttype|dicttypecomponents|dicttypehandle|each|each_strict|emptydicttype|emptydicttypehandle|emptylisttype|emptylisttypehandle|endswith|ensure|ensureconvertibleto|ensuretype|enum|evaluateatom|evaluatecode|evaluateexpr|evaluatetype|expandstruct|filter|filter_strict|find|first_value|folder|filecontent|filepath|flattenmembers|forceremovemember|forceremovemembers|forcerenamemembers|forcespreadmembers|formatcode|formattype|frombytes|frompg|funccode|greatest|grouping|gathermembers|generictype|histogram|hll|hoppingwindowpgcast|hyperloglog|if|if_strict|instanceof|json_exists|json_query|json_value|jointablerow|just|lag|last_value|lead|least|len|length|like|likely|like_strict|lambdaargumentscount|lambdacode|lambdaoptionalargumentscount|linearhistogram|listaggregate|listall|listany|listavg|listcode|listcollect|listconcat|listcreate|listdistinct|listenumerate|listextend|listextendstrict|listextract|listfilter|listflatmap|listflatten|listfold|listfold1|listfold1map|listfoldmap|listfromrange|listfromtuple|listhas|listhasitems|listhead|listindexof|listitemtype|listlast|listlength|listmap|listmax|listmin|listnotnull|listreplicate|listreverse|listskip|listskipwhile|listskipwhileinclusive|listsort|listsortasc|listsortdesc|listsum|listtake|listtakewhile|listtakewhileinclusive|listtop|listtopsort|listtopasc|listtopdesc|listtopsortasc|listtopsortdesc|listtotuple|listtype|listtypehandle|listunionall|listuniq|listzip|listzipall|loghistogram|logarithmichistogram|max|max_by|max_of|median|min|min_by|min_of|mode|multi_aggregate_by|nanvl|nvl|nothing|nulltype|nulltypehandle|optionalitemtype|optionaltype|optionaltypehandle|percentile|parsefile|parsetype|parsetypehandle|pgand|pgarray|pgcall|pgconst|pgnot|pgop|pgor|pickle|quotecode|range|range_strict|rank|regexp|regexp_strict|rfind|row_number|random|randomnumber|randomuuid|removemember|removemembers|removetimezone|renamemembers|replacemember|reprcode|resourcetype|resourcetypehandle|resourcetypetag|some|stddev|stddev_population|stddev_sample|substring|sum|sum_if|sessionstart|sessionwindow|setcreate|setdifference|setincludes|setintersection|setisdisjoint|setsymmetricdifference|setunion|spreadmembers|stablepickle|startswith|staticmap|staticzip|streamitemtype|streamtype|streamtypehandle|structmembertype|structmembers|structtypecomponents|structtypehandle|subqueryextend|subqueryextendfor|subquerymerge|subquerymergefor|subqueryunionall|subqueryunionallfor|subqueryunionmerge|subqueryunionmergefor|top|topfreq|top_by|tablename|tablepath|tablerecordindex|tablerow|tablerows|taggedtype|taggedtypecomponents|taggedtypehandle|tobytes|todict|tomultidict|topg|toset|tosorteddict|tosortedmultidict|trymember|tupleelementtype|tupletype|tupletypecomponents|tupletypehandle|typehandle|typekind|typeof|udaf|udf|unittype|unpickle|untag|unwrap|variance|variance_population|variance_sample|variant|varianttype|varianttypehandle|variantunderlyingtype|voidtype|voidtypehandle|way|worldcode|weakfield'.split(
12+
'|',
13+
);
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import {
2+
vscDarkPlus as darkTheme,
3+
materialLight as lightTheme,
4+
} from 'react-syntax-highlighter/dist/esm/styles/prism';
5+
6+
import {builtinFunctions, keywords, typeKeywords} from './keywords';
7+
8+
export const light = {
9+
...lightTheme,
10+
'pre[class*="language-"]': {
11+
...lightTheme['pre[class*="language-"]'],
12+
background: 'transparent',
13+
},
14+
'code[class*="language-"]': {
15+
...lightTheme['code[class*="language-"]'],
16+
background: 'transparent',
17+
},
18+
comment: {
19+
color: '#969896',
20+
},
21+
string: {
22+
color: '#a31515',
23+
},
24+
tablepath: {
25+
color: '#338186',
26+
},
27+
function: {
28+
color: '#7a3e9d',
29+
},
30+
udf: {
31+
color: '#7a3e9d',
32+
},
33+
type: {
34+
color: '#4d932d',
35+
},
36+
boolean: {
37+
color: '#608b4e',
38+
},
39+
constant: {
40+
color: '#608b4e',
41+
},
42+
variable: {
43+
color: '#001188',
44+
},
45+
};
46+
47+
export const dark = {
48+
...darkTheme,
49+
'pre[class*="language-"]': {
50+
...darkTheme['pre[class*="language-"]'],
51+
background: 'transparent',
52+
},
53+
'code[class*="language-"]': {
54+
...darkTheme['code[class*="language-"]'],
55+
background: 'transparent',
56+
},
57+
comment: {
58+
color: '#969896',
59+
},
60+
string: {
61+
color: '#ce9178',
62+
},
63+
tablepath: {
64+
color: '#338186',
65+
},
66+
function: {
67+
color: '#9e7bb0',
68+
},
69+
udf: {
70+
color: '#9e7bb0',
71+
},
72+
type: {
73+
color: '#6A8759',
74+
},
75+
boolean: {
76+
color: '#608b4e',
77+
},
78+
constant: {
79+
color: '#608b4e',
80+
},
81+
variable: {
82+
color: '#74b0df',
83+
},
84+
};
85+
86+
export function yql(Prism: any) {
87+
// Define YQL language
88+
Prism.languages.yql = {
89+
comment: [
90+
{
91+
pattern: /--.*$/m,
92+
greedy: true,
93+
},
94+
{
95+
pattern: /\/\*[\s\S]*?(?:\*\/|$)/,
96+
greedy: true,
97+
},
98+
],
99+
tablepath: {
100+
pattern: /(`[\w/]+`\s*\.\s*)?`[^`]+`/,
101+
greedy: true,
102+
},
103+
string: [
104+
{
105+
pattern: /'(?:\\[\s\S]|[^\\'])*'/,
106+
greedy: true,
107+
},
108+
{
109+
pattern: /"(?:\\[\s\S]|[^\\"])*"/,
110+
greedy: true,
111+
},
112+
{
113+
pattern: /@@(?:[^@]|@(?!@))*@@/,
114+
greedy: true,
115+
},
116+
],
117+
variable: [
118+
{
119+
pattern: /\$[a-zA-Z_]\w*/,
120+
greedy: true,
121+
},
122+
],
123+
function: {
124+
pattern: new RegExp(`\\b(?:${builtinFunctions.join('|')})\\b`, 'i'),
125+
greedy: true,
126+
},
127+
keyword: {
128+
pattern: new RegExp(`\\b(?:${keywords.join('|')})\\b`, 'i'),
129+
greedy: true,
130+
},
131+
udf: {
132+
pattern: /[A-Za-z_]\w*::[A-Za-z_]\w*/,
133+
greedy: true,
134+
},
135+
type: {
136+
pattern: new RegExp(`\\b(?:${typeKeywords.join('|')})\\b`, 'i'),
137+
greedy: true,
138+
},
139+
boolean: {
140+
pattern: /\b(?:true|false|null)\b/i,
141+
greedy: true,
142+
},
143+
number: {
144+
pattern: /[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?/i,
145+
greedy: true,
146+
},
147+
operator: {
148+
pattern: /[-+*/%<>!=&|^~]+|\b(?:and|or|not|is|like|ilike|rlike|in|between)\b/i,
149+
greedy: true,
150+
},
151+
punctuation: {
152+
pattern: /[;[\](){}.,]/,
153+
greedy: true,
154+
},
155+
};
156+
}
157+
158+
yql.displayName = 'yql';
159+
yql.aliases = ['yql'] as string[];

0 commit comments

Comments
 (0)