-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdiagnostics.ts
More file actions
67 lines (59 loc) · 1.67 KB
/
Copy pathdiagnostics.ts
File metadata and controls
67 lines (59 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { type Diagnostic, linter } from "@codemirror/lint";
import type { Text } from "@codemirror/state";
import type { EditorView } from "@codemirror/view";
import type { SqlParseError, SqlParser } from "./parser.js";
const DEFAULT_DELAY = 750;
/**
* Configuration options for the SQL linter
*/
export interface SqlLinterConfig {
/** Delay in milliseconds before running validation (default: 750) */
delay?: number;
}
/**
* Converts a SQL parse error to a CodeMirror diagnostic
*/
function convertToCodeMirrorDiagnostic(error: SqlParseError, doc: Text): Diagnostic {
const lineStart = doc.line(error.line).from;
const from = lineStart + Math.max(0, error.column - 1);
const to = from + 1;
return {
from,
to,
severity: error.severity,
message: error.message,
source: "sql-parser",
};
}
/**
* Creates a SQL linter extension that validates SQL syntax and reports errors
*
* @param config Configuration options for the linter
* @returns A CodeMirror linter extension
*
* @example
* ```ts
* import { sqlLinter } from '@marimo-team/codemirror-sql';
*
* const linter = sqlLinter({
* delay: 500, // 500ms delay before validation
* parser: new SqlParser() // custom parser instance
* });
* ```
*/
export function sqlLinter(parser: SqlParser, config: SqlLinterConfig = {}) {
return linter(
(view: EditorView): Diagnostic[] => {
const doc = view.state.doc;
const sql = doc.toString();
if (!sql.trim()) {
return [];
}
const errors = parser.validateSql(sql);
return errors.map((error) => convertToCodeMirrorDiagnostic(error, doc));
},
{
delay: config.delay ?? DEFAULT_DELAY,
},
);
}