From 460719c460937d069e3e4d988acf99ac73fc4268 Mon Sep 17 00:00:00 2001 From: Christopher Gagner Date: Tue, 9 Apr 2024 22:53:02 -0400 Subject: [PATCH] Fixed comments between ProcessConfig braces. --- moos-ivp-language-server/src/parsers/mod.rs | 26 +++++++++ moos-ivp-language-server/src/parsers/moos.rs | 10 +++- .../src/parsers/nsplug.rs | 53 +++++++------------ moos-parser/src/moos/moos.lalrpop | 7 +++ moos-parser/src/moos/tree.rs | 2 +- 5 files changed, 63 insertions(+), 35 deletions(-) diff --git a/moos-ivp-language-server/src/parsers/mod.rs b/moos-ivp-language-server/src/parsers/mod.rs index e09f026..f0ea244 100644 --- a/moos-ivp-language-server/src/parsers/mod.rs +++ b/moos-ivp-language-server/src/parsers/mod.rs @@ -1,2 +1,28 @@ +use lsp_types::Url; + pub mod moos; pub mod nsplug; + +/// Find a file relative to a parent URL. +/// +/// TODO: This should really search the workspace, but for now we will +/// assume this can search the local file system. +fn find_relative_file(parent_url: &Url, file_name: &str) -> Option { + if parent_url.scheme() == "file" { + let parent_path = std::path::Path::new(parent_url.path()); + if parent_path.exists() && parent_path.is_file() { + if let Some(parent_dir) = parent_path.parent() { + let include_path = parent_dir.join(file_name.to_string()); + if include_path.exists() && include_path.is_file() { + let mut new_url = parent_url.clone(); + if let Some(path_str) = include_path.to_str() { + new_url.set_path(path_str); + return Some(new_url); + } + } + } + } + } + + return None; +} diff --git a/moos-ivp-language-server/src/parsers/moos.rs b/moos-ivp-language-server/src/parsers/moos.rs index 20c1039..168cc07 100644 --- a/moos-ivp-language-server/src/parsers/moos.rs +++ b/moos-ivp-language-server/src/parsers/moos.rs @@ -44,7 +44,7 @@ pub fn new_error_diagnostic(start: &Location, end: &Location, message: String) - } pub fn new_warning_diagnostic(start: &Location, end: &Location, message: String) -> Diagnostic { - new_diagnostic(DiagnosticSeverity::ERROR, start, end, message) + new_diagnostic(DiagnosticSeverity::WARNING, start, end, message) } pub fn parse(document: &mut Document) { @@ -356,6 +356,14 @@ fn handle_process_config( handle_comment(document, line, &comment); } + // Prelude Comments + if !process_config.prelude_comments.is_empty() { + // NOTE: Invalid lines are handled by the parser. This should just + // add comments. + handle_lines(document, &process_config.prelude_comments); + } + + // Open Curly Comment if let Some(comment) = &process_config.open_curly_comment { handle_comment(document, process_config.open_curly_line, &comment); } diff --git a/moos-ivp-language-server/src/parsers/nsplug.rs b/moos-ivp-language-server/src/parsers/nsplug.rs index d662c18..2bfbff3 100644 --- a/moos-ivp-language-server/src/parsers/nsplug.rs +++ b/moos-ivp-language-server/src/parsers/nsplug.rs @@ -18,6 +18,8 @@ use moos_parser::{ }; use tracing::{debug, error, info, trace, warn}; +use super::find_relative_file; + pub fn new_diagnostic( severity: DiagnosticSeverity, start: &Location, @@ -44,7 +46,7 @@ pub fn new_error_diagnostic(start: &Location, end: &Location, message: String) - } pub fn new_warning_diagnostic(start: &Location, end: &Location, message: String) -> Diagnostic { - new_diagnostic(DiagnosticSeverity::ERROR, start, end, message) + new_diagnostic(DiagnosticSeverity::WARNING, start, end, message) } pub fn parse(document: &mut Document) { @@ -259,39 +261,24 @@ fn handle_include( // TODO: This should really use the workspace, but for now we'll just // handle this using the local file system. - if document.uri.scheme() == "file" { - let document_path = std::path::Path::new(document.uri.path()); - if document_path.exists() && document_path.is_file() { - if let Some(parent_dir) = document_path.parent() { - let include_path = parent_dir.join(path.to_string()); - if include_path.exists() && include_path.is_file() { - let mut include_uri = document.uri.clone(); - if let Some(path_str) = include_path.to_str() { - include_uri.set_path(path_str); - - // Finally, we can add the document link. - let include_range = path.get_range(); - document.document_links.push(DocumentLink { - range: lsp_types::Range { - start: Position { - line, - character: include_range.start, - }, - end: Position { - line, - character: include_range.end, - }, - }, - target: Some(include_uri), - tooltip: None, - data: None, - }); - } - } - } - } + if let Some(include_url) = find_relative_file(&document.uri, path.to_string().as_str()) { + let include_range = path.get_range(); + document.document_links.push(DocumentLink { + range: lsp_types::Range { + start: Position { + line, + character: include_range.start, + }, + end: Position { + line, + character: include_range.end, + }, + }, + target: Some(include_url), + tooltip: None, + data: None, + }); } - if let Some(tag) = tag { document.semantic_tokens.insert( line, diff --git a/moos-parser/src/moos/moos.lalrpop b/moos-parser/src/moos/moos.lalrpop index c3bb864..8ca5625 100644 --- a/moos-parser/src/moos/moos.lalrpop +++ b/moos-parser/src/moos/moos.lalrpop @@ -92,6 +92,11 @@ Define: Line<'input> = { "define:" => Line::Define{assignment, line: l.line, range: TokenRange::new_line(l,r).expect("Invalid token range while parsing `define:`")}, } +CommentOrEmptyLine: Line<'input> = { + "EOL" => Line::EndOfLine, + "EOL" => Line::Comment{comment, line: l.line}, +} + ProcessConfigLine: Line<'input> = { "EOL" => Line::EndOfLine, "EOL" => Line::Comment{comment, line: l.line}, @@ -125,6 +130,7 @@ ProcessConfigLine: Line<'input> = { ProcessConfig: Line<'input> = { "ProcessConfig" "=" "EOL" + "{" "EOL" "}" "EOL" @@ -132,6 +138,7 @@ ProcessConfig: Line<'input> = { process_config: tree::ProcessConfig{ process_config_comment, process_name, + prelude_comments: prelude_comments.into(), open_curly_line: ocl.line, open_curly_index: ocl.index, open_curly_comment, diff --git a/moos-parser/src/moos/tree.rs b/moos-parser/src/moos/tree.rs index 900a53a..6a01ddc 100644 --- a/moos-parser/src/moos/tree.rs +++ b/moos-parser/src/moos/tree.rs @@ -177,7 +177,7 @@ pub struct ProcessConfig<'input> { /// Name of the process pub process_name: VariableStrings<'input>, /// Comments between ProcessConfig line and curly brace - //pub prelude_comments: Vec>, + pub prelude_comments: Lines<'input>, /// Line number for the opening curly brace pub open_curly_line: u32, /// Line number for the opening curly brace