@@ -19,10 +19,67 @@ export interface SqlLinterConfig {
1919/**
2020 * Converts a SQL parse error to a CodeMirror diagnostic
2121 */
22- function convertToCodeMirrorDiagnostic ( error : SqlParseError , doc : Text ) : Diagnostic {
23- const lineStart = doc . line ( error . line ) . from ;
22+ export function convertToCodeMirrorDiagnostic ( error : SqlParseError , doc : Text ) : Diagnostic {
23+ const line = doc . line ( error . line ) ;
24+ const lineStart = line . from ;
25+
26+ // Calculate the start position of the error
2427 const from = lineStart + Math . max ( 0 , error . column - 1 ) ;
25- const to = from + 1 ;
28+
29+ // For column errors, try to span the entire column name
30+ let to = from + 1 ; // Default to single character
31+
32+ // If this is a column error, try to find the column name length
33+ if ( error . message . includes ( "Column" ) && error . message . includes ( "does not exist" ) ) {
34+ // Extract column name from error message
35+ const columnMatch = error . message . match ( / C o l u m n ' ( [ ^ ' ] + ) ' d o e s n o t e x i s t / ) ;
36+ if ( columnMatch ?. [ 1 ] ) {
37+ const columnName = columnMatch [ 1 ] ;
38+ // Find the column name in the line text starting from the error column
39+ const lineText = line . text ;
40+ const startSearchPos = Math . max ( 0 , error . column - 1 ) ;
41+ const columnIndex = lineText . indexOf ( columnName , startSearchPos ) ;
42+ if ( columnIndex !== - 1 ) {
43+ to = lineStart + columnIndex + columnName . length ;
44+ } else {
45+ // If not found from the error column, try to find it anywhere in the line
46+ const globalIndex = lineText . indexOf ( columnName ) ;
47+ if ( globalIndex !== - 1 ) {
48+ to = lineStart + globalIndex + columnName . length ;
49+ }
50+ }
51+ }
52+ }
53+
54+ // Handle the case where the error is reported on a comment line but should be on the SQL line
55+ // This happens when the parser reports the wrong line number due to comments
56+ if ( line . text . trim ( ) . startsWith ( "--" ) ) {
57+ // Find the next non-comment line that contains SQL
58+ for ( let i = error . line ; i < doc . lines ; i ++ ) {
59+ const nextLine = doc . line ( i + 1 ) ;
60+ const nextLineText = nextLine . text . trim ( ) ;
61+ if ( nextLineText && ! nextLineText . startsWith ( "--" ) ) {
62+ // Found a non-comment line, check if it contains the error
63+ if ( error . message . includes ( "Column" ) && error . message . includes ( "does not exist" ) ) {
64+ const columnMatch = error . message . match ( / C o l u m n ' ( [ ^ ' ] + ) ' d o e s n o t e x i s t / ) ;
65+ if ( columnMatch ?. [ 1 ] ) {
66+ const columnName = columnMatch [ 1 ] ;
67+ const columnIndex = nextLineText . indexOf ( columnName ) ;
68+ if ( columnIndex !== - 1 ) {
69+ return {
70+ from : nextLine . from + columnIndex ,
71+ to : nextLine . from + columnIndex + columnName . length ,
72+ severity : error . severity ,
73+ message : error . message ,
74+ source : "sql-parser" ,
75+ } ;
76+ }
77+ }
78+ }
79+ break ;
80+ }
81+ }
82+ }
2683
2784 return {
2885 from,
0 commit comments