Diagnostics to correct length. Using doc versioning where available

This commit is contained in:
Noah Santschi-Cooney 2020-07-15 10:09:31 +01:00
parent c9d2285ece
commit 17c4f8aa93
No known key found for this signature in database
GPG key ID: 3B22282472C8AE48
2 changed files with 28 additions and 12 deletions

View file

@ -7,7 +7,7 @@ use super::IncludePosition;
/// Wraps a `StableDiGraph` with caching behaviour for node search by maintaining
/// an index for node value to node index and a reverse index.
/// This allows for O(1) lookup for a value after the initial lookup.
/// This allows for **O(1)** lookup for a value after the initial lookup.
pub struct CachedStableGraph {
// StableDiGraph is used as it allows for String node values, essential for
// generating the GraphViz DOT render.
@ -28,8 +28,9 @@ impl CachedStableGraph {
}
/// Returns the `NodeIndex` for a given graph node with the value of `name`
/// and caches the result in the `HashMap`. Complexity is O(1) if the value
/// is cached, else O(n) as an exhaustive search must be done.
/// and caches the result in the `HashMap`. Complexity is **O(1)** if the value
/// is cached (which should always be the case), else **O(n)** where **n** is
/// the number of node indices, as an exhaustive search must be done.
pub fn find_node(&mut self, name: impl Into<String>) -> Option<NodeIndex> {
let name_str = name.into();
match self.cache.get(&name_str) {

View file

@ -43,6 +43,8 @@ lazy_static! {
#[allow(dead_code)]
static INCLUDE_STR: &'static str = "#extension GL_GOOGLE_include_directive : require";
static SOURCE: &'static str = "mc-glsl";
fn main() {
let stdin = std::io::stdin();
@ -249,7 +251,7 @@ impl MinecraftShaderLanguageServer {
return includes;
}
pub fn lint(&self, uri: impl Into<Url>, source: impl Into<String>) {
pub fn lint(&self, source: impl Into<String>) -> Vec<Diagnostic> {
let source: String = source.into();
eprintln!("validator bin path: {}", self.config.glslang_validator_path);
let cmd = process::Command::new(&self.config.glslang_validator_path)
@ -284,26 +286,34 @@ impl MinecraftShaderLanguageServer {
return
}
let line = NonZeroU64::from_str(diagnostic_capture.get(3).unwrap().as_str()).unwrap();
let line = NonZeroU64::from_str(
diagnostic_capture
.get(3)
.expect("third capture group was None")
.as_str()
).expect("got non-numeric string value");
let line: u64 = line.into();
let line = line - 1;
let line_text = source_lines.get(line as usize).unwrap();
let line_text = source_lines[line as usize];
let leading_whitespace = line_text.len() - line_text.trim_start().len();
let severity = match diagnostic_capture.get(0).unwrap().as_str() {
"ERROR" => DiagnosticSeverity::Error,
"WARNING" => DiagnosticSeverity::Warning,
_ => DiagnosticSeverity::Error,
_ => DiagnosticSeverity::Information,
};
let diagnostic = Diagnostic {
range: Range::new(Position::new(line, leading_whitespace as u64), Position::new(line, 1000)),
range: Range::new(
Position::new(line, leading_whitespace as u64),
Position::new(line, line_text.len() as u64)
),
code: None,
severity: Some(severity),
source: Some(String::from("mc-glsl")),
source: Some(String::from(SOURCE)),
message: String::from(msg),
related_information: None,
tags: None,
@ -311,11 +321,14 @@ impl MinecraftShaderLanguageServer {
diagnostics.push(diagnostic);
});
diagnostics
}
pub fn publish_diagnostic(&self, diagnostics: Vec<Diagnostic>, uri: impl Into<Url>, document_version: Option<i64>) {
self.endpoint.send_notification(PublishDiagnostics::METHOD, PublishDiagnosticsParams {
uri: uri.into(),
diagnostics,
version: None,
version: document_version,
}).unwrap();
}
}
@ -394,7 +407,8 @@ impl LanguageServerHandling for MinecraftShaderLanguageServer {
fn did_open_text_document(&mut self, params: DidOpenTextDocumentParams) {
eprintln!("opened doc {}", params.text_document.uri);
self.lint(params.text_document.uri, params.text_document.text);
let diagnostics = self.lint(params.text_document.text);
self.publish_diagnostic(diagnostics, params.text_document.uri, Some(params.text_document.version));
}
fn did_change_text_document(&mut self, params: DidChangeTextDocumentParams) {
@ -413,7 +427,8 @@ impl LanguageServerHandling for MinecraftShaderLanguageServer {
let path: String = percent_encoding::percent_decode_str(params.text_document.uri.path()).decode_utf8().unwrap().into();
let file_content = std::fs::read(path).unwrap();
self.lint(params.text_document.uri, String::from_utf8(file_content).unwrap());
let diagnostics = self.lint(String::from_utf8(file_content).unwrap());
self.publish_diagnostic(diagnostics, params.text_document.uri, None);
}
fn did_change_watched_files(&mut self, _: DidChangeWatchedFilesParams) {}