Skip to content

Commit a04e0c1

Browse files
committed
feat: add color to table name for sql editor
1 parent 21c1d93 commit a04e0c1

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

src/renderer/App.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,15 @@ hr {
209209
.cm-tooltip-autocomplete .cm-completionDetail {
210210
padding-left: 15px;
211211
}
212+
213+
.cm-table-name {
214+
color: #e84393;
215+
}
216+
217+
.darl .cm-table-name {
218+
color: #fd79a8;
219+
}
220+
221+
.cm-table-name > span {
222+
color: inherit !important;
223+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {
2+
EditorView,
3+
ViewPlugin,
4+
Decoration,
5+
DecorationSet,
6+
ViewUpdate,
7+
} from '@codemirror/view';
8+
import { Range } from '@codemirror/state';
9+
import { syntaxTree } from '@codemirror/language';
10+
11+
const underlineMark = Decoration.mark({ class: 'cm-table-name' });
12+
13+
export default function createSQLTableNameHighlightPlugin(
14+
tableNameList: string[]
15+
) {
16+
const tableNameSet = new Set(
17+
tableNameList.map((table) => table.toLowerCase())
18+
);
19+
20+
function highlightTableName(view: EditorView) {
21+
const decorationList: Range<Decoration>[] = [];
22+
23+
for (const { from, to } of view.visibleRanges) {
24+
syntaxTree(view.state).iterate({
25+
from,
26+
to,
27+
enter: (node) => {
28+
if (node.name == 'Identifier') {
29+
const word = view.state.doc
30+
.sliceString(node.from, node.to)
31+
.toLowerCase();
32+
33+
const lastChar = node.node.prevSibling
34+
? view.state.doc
35+
.sliceString(
36+
node.node.prevSibling.from,
37+
node.node.prevSibling.to
38+
)
39+
.toLowerCase()
40+
: '';
41+
42+
if (tableNameSet.has(word) && lastChar !== '.') {
43+
decorationList.push(underlineMark.range(node.from, node.to));
44+
}
45+
}
46+
},
47+
});
48+
}
49+
50+
return Decoration.set(decorationList);
51+
}
52+
53+
return ViewPlugin.fromClass(
54+
class {
55+
decorations: DecorationSet;
56+
57+
constructor(view: EditorView) {
58+
this.decorations = highlightTableName(view);
59+
}
60+
61+
update(update: ViewUpdate) {
62+
this.decorations = highlightTableName(update.view);
63+
}
64+
},
65+
{ decorations: (v) => v.decorations }
66+
);
67+
}

src/renderer/components/CodeEditor/SqlCodeEditor.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { indentUnit } from '@codemirror/language';
1414
import { defaultKeymap, insertTab } from '@codemirror/commands';
1515
import { keymap } from '@codemirror/view';
16-
import { Ref, forwardRef, useCallback } from 'react';
16+
import { Ref, forwardRef, useCallback, useMemo } from 'react';
1717
import useCodeEditorTheme from './useCodeEditorTheme';
1818
import { SyntaxNode } from '@lezer/common';
1919
import {
@@ -22,8 +22,10 @@ import {
2222
genericCompletion,
2323
keywordCompletionSource,
2424
} from '../../../language/dist/';
25+
2526
import handleCustomSqlAutoComplete from './handleCustomSqlAutoComplete';
2627
import { DatabaseSchemas } from 'types/SqlSchema';
28+
import createSQLTableNameHighlightPlugin from './SQLTableNameHightlight';
2729

2830
const SqlCodeEditor = forwardRef(function SqlCodeEditor(
2931
props: ReactCodeMirrorProps & {
@@ -47,6 +49,15 @@ const SqlCodeEditor = forwardRef(function SqlCodeEditor(
4749
[schema, currentDatabase]
4850
);
4951

52+
const tableNameHighlightPlugin = useMemo(() => {
53+
if (schema && currentDatabase && schema[currentDatabase]) {
54+
return createSQLTableNameHighlightPlugin(
55+
Object.keys(schema[currentDatabase].tables)
56+
);
57+
}
58+
return createSQLTableNameHighlightPlugin([]);
59+
}, [schema, currentDatabase]);
60+
5061
return (
5162
<CodeMirror
5263
ref={ref}
@@ -89,6 +100,7 @@ const SqlCodeEditor = forwardRef(function SqlCodeEditor(
89100
],
90101
}),
91102
indentUnit.of(' '),
103+
tableNameHighlightPlugin,
92104
]}
93105
{...codeMirrorProps}
94106
/>

src/renderer/components/CodeEditor/useCodeEditorTheme.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export default function useCodeEditorTheme() {
2424
'Menlo, Monaco, Consolas, "Andale Mono", "Ubuntu Mono", "Courier New", monospace',
2525
},
2626
styles: [
27-
{ tag: [t.meta, t.comment, t.name], color: '#804000' },
27+
{ tag: [t.meta, t.comment], color: '#804000' },
2828
{ tag: [t.keyword, t.strong], color: '#0000FF' },
2929
{ tag: [t.number], color: '#FF0080' },
30-
{ tag: [t.string], color: '#FF0080' },
30+
{ tag: [t.string], color: '#e17055' },
3131
{ tag: [t.variableName], color: '#006600' },
3232
{ tag: [t.escape], color: '#33CC33' },
3333
{ tag: [t.tagName], color: '#1C02FF' },
@@ -56,6 +56,7 @@ export default function useCodeEditorTheme() {
5656
'Menlo, Monaco, Consolas, "Andale Mono", "Ubuntu Mono", "Courier New", monospace',
5757
},
5858
styles: [
59+
{ tag: [t.number], color: '#fbc531' },
5960
{ tag: t.keyword, color: '#3498db' },
6061
{ tag: t.comment, color: '#27ae60' },
6162
{ tag: t.definition(t.typeName), color: '#27ae60' },

0 commit comments

Comments
 (0)