Skip to content

Commit c00a9da

Browse files
committed
refactor(analysis): share diagnostic location and the catalog's options view
1 parent c03cec2 commit c00a9da

4 files changed

Lines changed: 31 additions & 24 deletions

File tree

‎crates/sqlbuild-analysis/src/semantic_validation/_helpers/catalog.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,9 @@ impl ProjectCatalog {
175175

176176
pub fn with_relations(&self, relations: Relations) -> Self {
177177
let mut catalog = Self {
178-
dialect: self.dialect,
179-
options: self.options.clone(),
180-
quoted_ignore_case: self.quoted_ignore_case,
181178
tables: self.tables.clone(),
182-
overrides: Vec::new(),
183179
analysis_tables: self.analysis_tables.clone(),
184-
analysis_pool: Arc::clone(&self.analysis_pool),
185-
running_analyses: Arc::clone(&self.running_analyses),
180+
..self.analysis_view()
186181
};
187182
catalog.update_relations(relations);
188183
catalog

‎crates/sqlbuild-analysis/src/semantic_validation/_helpers/function_names.rs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ impl Cursor {
104104
}
105105
self.byte = byte;
106106
}
107+
108+
/// `error` located at the byte range `start..end` of `sql`, with its character span.
109+
pub(crate) fn locate(
110+
&mut self,
111+
sql: &str,
112+
(start, end): (usize, usize),
113+
error: ValidationError,
114+
) -> ValidationError {
115+
self.advance(sql, start);
116+
let first: usize = self.characters;
117+
let last: usize = first + sql[start..end].chars().count();
118+
error
119+
.with_location(self.line, self.column)
120+
.with_span(Some(first), Some(last))
121+
}
107122
}
108123

109124
/// Read `dialect SPELLING arguments ACCEPTED` rows; `#` starts a comment.
@@ -156,12 +171,11 @@ pub(crate) fn unsupported_function_errors(sql: &str, dialect: DialectType) -> Ve
156171
"Unknown function '{}' for dialect {dialect:?}; did you mean {}?",
157172
call.name, call.suggestion
158173
);
159-
cursor.advance(sql, call.start);
160-
let start: usize = cursor.characters;
161-
let end: usize = start + sql[call.start..call.end].chars().count();
162-
ValidationError::error(message, UNKNOWN_FUNCTION)
163-
.with_location(cursor.line, cursor.column)
164-
.with_span(Some(start), Some(end))
174+
cursor.locate(
175+
sql,
176+
(call.start, call.end),
177+
ValidationError::error(message, UNKNOWN_FUNCTION),
178+
)
165179
})
166180
.collect()
167181
}

‎crates/sqlbuild-analysis/src/semantic_validation/_helpers/sqlite_temporal_casts.rs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ pub(crate) fn sqlite_temporal_cast_warnings(
3535
'2026-01-01 10:00:00' becomes 2026",
3636
cast.type_name
3737
);
38-
cursor.advance(sql, cast.start);
39-
let start: usize = cursor.characters;
40-
let end: usize = start + sql[cast.start..cast.end].chars().count();
41-
ValidationError::warning(message, SQLITE_TEMPORAL_CAST_CODE)
42-
.with_location(cursor.line, cursor.column)
43-
.with_span(Some(start), Some(end))
38+
cursor.locate(
39+
sql,
40+
(cast.start, cast.end),
41+
ValidationError::warning(message, SQLITE_TEMPORAL_CAST_CODE),
42+
)
4443
})
4544
.collect()
4645
}

‎crates/sqlbuild-analysis/src/semantic_validation/_helpers/truth_tests.rs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ pub(crate) fn unsupported_truth_test_errors(
3737
spelling(&test),
3838
rewrite(dialect, &test)
3939
);
40-
cursor.advance(sql, test.start);
41-
let start: usize = cursor.characters;
42-
let end: usize = start + sql[test.start..test.end].chars().count();
43-
ValidationError::error(message, UNSUPPORTED_TRUTH_TEST_CODE)
44-
.with_location(cursor.line, cursor.column)
45-
.with_span(Some(start), Some(end))
40+
cursor.locate(
41+
sql,
42+
(test.start, test.end),
43+
ValidationError::error(message, UNSUPPORTED_TRUTH_TEST_CODE),
44+
)
4645
})
4746
.collect()
4847
}

0 commit comments

Comments
 (0)