|
| 1 | +import { |
| 2 | + Decoration, |
| 3 | + EditorState, |
| 4 | + EditorView, |
| 5 | + StateField, |
| 6 | + Range, |
| 7 | +} from "@uiw/react-codemirror"; |
| 8 | +import { syntaxTree } from "@codemirror/language"; |
| 9 | +import { SyntaxNode } from "@lezer/common"; |
| 10 | + |
| 11 | +const statementLineHighlight = Decoration.line({ |
| 12 | + class: "cm-highlight-statement", |
| 13 | +}); |
| 14 | + |
| 15 | +export interface StatementSegment { |
| 16 | + from: number; |
| 17 | + to: number; |
| 18 | + text: string; |
| 19 | +} |
| 20 | + |
| 21 | +function toNodeString(state: EditorState, node: SyntaxNode) { |
| 22 | + return state.doc.sliceString(node.from, node.to); |
| 23 | +} |
| 24 | + |
| 25 | +function isRequireEndStatement(state: EditorState, node: SyntaxNode): number { |
| 26 | + const ptr = node.firstChild; |
| 27 | + if (!ptr) return 0; |
| 28 | + |
| 29 | + // Majority of the query will fall in SELECT, INSERT, UPDATE, DELETE |
| 30 | + const firstKeyword = toNodeString(state, ptr).toLowerCase(); |
| 31 | + if (firstKeyword === "select") return 0; |
| 32 | + if (firstKeyword === "insert") return 0; |
| 33 | + if (firstKeyword === "update") return 0; |
| 34 | + if (firstKeyword === "delete") return 0; |
| 35 | + |
| 36 | + const keywords = node.getChildren("Keyword"); |
| 37 | + if (keywords.length === 0) return 0; |
| 38 | + |
| 39 | + return keywords.filter( |
| 40 | + (k) => toNodeString(state, k).toLowerCase() === "begin" |
| 41 | + ).length; |
| 42 | +} |
| 43 | + |
| 44 | +function isEndStatement(state: EditorState, node: SyntaxNode) { |
| 45 | + let ptr = node.firstChild; |
| 46 | + if (!ptr) return false; |
| 47 | + if (toNodeString(state, ptr).toLowerCase() !== "end") return false; |
| 48 | + |
| 49 | + ptr = ptr.nextSibling; |
| 50 | + if (!ptr) return false; |
| 51 | + if (toNodeString(state, ptr) !== ";") return false; |
| 52 | + |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +export function splitSqlQuery( |
| 57 | + state: EditorState, |
| 58 | + generateText: boolean = true |
| 59 | +): StatementSegment[] { |
| 60 | + const topNode = syntaxTree(state).topNode; |
| 61 | + |
| 62 | + // Get all the statements |
| 63 | + let needEndStatementCounter = 0; |
| 64 | + const statements = topNode.getChildren("Statement"); |
| 65 | + |
| 66 | + if (statements.length === 0) return []; |
| 67 | + |
| 68 | + const statementGroups: SyntaxNode[][] = []; |
| 69 | + let accumulateNodes: SyntaxNode[] = []; |
| 70 | + let i = 0; |
| 71 | + |
| 72 | + for (; i < statements.length; i++) { |
| 73 | + const statement = statements[i]; |
| 74 | + needEndStatementCounter += isRequireEndStatement(state, statement); |
| 75 | + |
| 76 | + if (needEndStatementCounter) { |
| 77 | + accumulateNodes.push(statement); |
| 78 | + } else { |
| 79 | + statementGroups.push([statement]); |
| 80 | + } |
| 81 | + |
| 82 | + if (needEndStatementCounter && isEndStatement(state, statement)) { |
| 83 | + needEndStatementCounter--; |
| 84 | + if (needEndStatementCounter === 0) { |
| 85 | + statementGroups.push(accumulateNodes); |
| 86 | + accumulateNodes = []; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (accumulateNodes.length > 0) { |
| 92 | + statementGroups.push(accumulateNodes); |
| 93 | + } |
| 94 | + |
| 95 | + return statementGroups.map((r) => ({ |
| 96 | + from: r[0].from, |
| 97 | + to: r[r.length - 1].to, |
| 98 | + text: generateText |
| 99 | + ? state.doc.sliceString(r[0].from, r[r.length - 1].to) |
| 100 | + : "", |
| 101 | + })); |
| 102 | +} |
| 103 | + |
| 104 | +export function resolveToNearestStatement( |
| 105 | + state: EditorState |
| 106 | +): { from: number; to: number } | null { |
| 107 | + // Breakdown and grouping the statement |
| 108 | + const cursor = state.selection.main.from; |
| 109 | + const statements = splitSqlQuery(state, false); |
| 110 | + |
| 111 | + if (statements.length === 0) return null; |
| 112 | + |
| 113 | + // Check if our current cursor is within any statement |
| 114 | + let i = 0; |
| 115 | + for (; i < statements.length; i++) { |
| 116 | + const statement = statements[i]; |
| 117 | + if (cursor < statement.from) break; |
| 118 | + if (cursor > statement.to) continue; |
| 119 | + if (cursor >= statement.from && cursor <= statement.to) return statement; |
| 120 | + } |
| 121 | + |
| 122 | + if (i === 0) return statements[0]; |
| 123 | + if (i === statements.length) return statements[i - 1]; |
| 124 | + |
| 125 | + const cursorLine = state.doc.lineAt(cursor).number; |
| 126 | + const topLine = state.doc.lineAt(statements[i - 1].to).number; |
| 127 | + const bottomLine = state.doc.lineAt(statements[i].from).number; |
| 128 | + |
| 129 | + if (cursorLine - topLine >= bottomLine - cursorLine) { |
| 130 | + return statements[i]; |
| 131 | + } else { |
| 132 | + return statements[i - 1]; |
| 133 | + } |
| 134 | +} |
| 135 | +function getDecorationFromState(state: EditorState) { |
| 136 | + const statement = resolveToNearestStatement(state); |
| 137 | + |
| 138 | + if (!statement) return Decoration.none; |
| 139 | + |
| 140 | + // Get the line of the node |
| 141 | + const fromLineNumber = state.doc.lineAt(statement.from).number; |
| 142 | + const toLineNumber = state.doc.lineAt(statement.to).number; |
| 143 | + |
| 144 | + const d: Range<Decoration>[] = []; |
| 145 | + for (let i = fromLineNumber; i <= toLineNumber; i++) { |
| 146 | + d.push(statementLineHighlight.range(state.doc.line(i).from)); |
| 147 | + } |
| 148 | + |
| 149 | + return Decoration.set(d); |
| 150 | +} |
| 151 | + |
| 152 | +const SqlStatementStateField = StateField.define({ |
| 153 | + create(state) { |
| 154 | + return getDecorationFromState(state); |
| 155 | + }, |
| 156 | + |
| 157 | + update(_, tr) { |
| 158 | + return getDecorationFromState(tr.state); |
| 159 | + }, |
| 160 | + |
| 161 | + provide: (f) => EditorView.decorations.from(f), |
| 162 | +}); |
| 163 | + |
| 164 | +const SqlStatementTheme = EditorView.baseTheme({ |
| 165 | + ".cm-highlight-statement": { |
| 166 | + borderLeft: "3px solid #ff9ff3 !important", |
| 167 | + }, |
| 168 | +}); |
| 169 | + |
| 170 | +const SqlStatementHighlightPlugin = [SqlStatementStateField, SqlStatementTheme]; |
| 171 | + |
| 172 | +export default SqlStatementHighlightPlugin; |
0 commit comments