Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pglt_workspace): emit scan errors #219

Merged
merged 8 commits into from
Feb 25, 2025
Merged

fix(pglt_workspace): emit scan errors #219

merged 8 commits into from
Feb 25, 2025

Conversation

psteinroe
Copy link
Collaborator

@psteinroe psteinroe commented Feb 22, 2025

emitting scan errors from the lexer. we ignored them before because I thought they never happen. but they do!

  • added specialised diagnostic to both the lexer and the statement splitter
  • bubble up error from lexer
  • store parse diagnostics on document
  • if any diagnostics are fatal, we remove all statements and only process text changes until they are gone
  • expose document diagnostics

also:

  • removed the leftover base_db crate

@psteinroe psteinroe marked this pull request as ready for review February 24, 2025 08:38
///
/// Scan diagnostics are always **fatal errors**.
#[derive(Clone, Debug, Diagnostic, PartialEq)]
#[diagnostic(category = "syntax", severity = Fatal)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

severity = Fatal is important here -> its what we use higher up to determine whether the document can be used or not

Comment on lines +19 to +38
pub fn from_pg_query_err(err: pg_query::Error, input: &str) -> Vec<Self> {
let err_msg = err.to_string();
let re = regex::Regex::new(r#"at or near "(.*?)""#).unwrap();
let mut diagnostics = Vec::new();

for captures in re.captures_iter(&err_msg) {
if let Some(matched) = captures.get(1) {
let search_term = matched.as_str();
for (idx, _) in input.match_indices(search_term) {
let from = idx;
let to = from + search_term.len();
diagnostics.push(ScanError {
span: Some(TextRange::new(
from.try_into().unwrap(),
to.try_into().unwrap(),
)),
message: MessageAndDescription::from(err_msg.clone()),
});
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we try to find the problematic text in the input to put a range on the diagnostics. this is not safe in any way but I find it nicer to show too many diagnostics where one is for sure valid than none and have the user look through the text.

@psteinroe psteinroe changed the title fix: emit scan errors fix(pglt_workspace): emit scan errors Feb 24, 2025
Copy link
Collaborator

@juleswritescode juleswritescode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work :)

@psteinroe psteinroe merged commit c77e7cf into main Feb 25, 2025
6 checks passed
@psteinroe psteinroe deleted the fix/scan-errors branch February 25, 2025 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants