Skip to content

Commit

Permalink
refactor(service): add diagnostic code for syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Dec 24, 2024
1 parent 5b69940 commit 7be16f9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/service/src/files.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::InternUri;
use line_index::{LineIndex, TextSize};
use lsp_types::{Diagnostic, DiagnosticSeverity, Position, Range, Uri};
use lsp_types::{Diagnostic, DiagnosticSeverity, NumberOrString, Position, Range, Uri};
use rowan::GreenNode;
use std::rc::Rc;
use wat_parser::Parser;
use wat_parser::{Message, Parser};

#[salsa::query_group(Files)]
pub(crate) trait FilesCtx: salsa::Database {
Expand Down Expand Up @@ -47,6 +47,14 @@ fn parse(db: &dyn FilesCtx, uri: InternUri) -> (GreenNode, Vec<Diagnostic>) {
),
severity: Some(DiagnosticSeverity::ERROR),
source: Some("wat".into()),
code: if let Message::Name(name) = error.message {
Some(NumberOrString::String(format!(
"syntax/{}",
name.replace(' ', "-")
)))
} else {
None
},
message: format!("syntax error: {}", error.message),
..Default::default()
}
Expand Down
2 changes: 2 additions & 0 deletions crates/service/tests/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod multi_modules;
#[cfg(test)]
mod shadow;
#[cfg(test)]
mod syntax;
#[cfg(test)]
mod typeck;
#[cfg(test)]
mod undef;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/service/tests/diagnostics/syntax.rs
expression: response
---
{
"kind": "full",
"items": [
{
"range": {
"start": {
"line": 2,
"character": 2
},
"end": {
"line": 2,
"character": 8
}
},
"severity": 1,
"code": "syntax/module-field",
"source": "wat",
"message": "syntax error: expected module field"
}
]
}
18 changes: 18 additions & 0 deletions crates/service/tests/diagnostics/syntax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::*;
use insta::assert_json_snapshot;
use lsp_types::Uri;
use wat_service::LanguageService;

#[test]
fn blocks() {
let uri = "untitled:test".parse::<Uri>().unwrap();
let source = "
(module
(fnuc))
";
let mut service = LanguageService::default();
service.commit(uri.clone(), source.into());
allow_unused(&mut service, uri.clone());
let response = service.pull_diagnostics(create_params(uri));
assert_json_snapshot!(response);
}

0 comments on commit 7be16f9

Please sign in to comment.