Skip to content

Commit

Permalink
Fixed comments between ProcessConfig braces.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgagner committed Apr 10, 2024
1 parent 1de9c79 commit 460719c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
26 changes: 26 additions & 0 deletions moos-ivp-language-server/src/parsers/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Url> {
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;
}
10 changes: 9 additions & 1 deletion moos-ivp-language-server/src/parsers/moos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
53 changes: 20 additions & 33 deletions moos-ivp-language-server/src/parsers/nsplug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions moos-parser/src/moos/moos.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ Define: Line<'input> = {
<l:@L> "define:" <r:@R> <assignment: Assignment> => 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,
<l:@L> <comment:Comment> <r:@R> "EOL" => Line::Comment{comment, line: l.line},
}

ProcessConfigLine: Line<'input> = {
"EOL" => Line::EndOfLine,
<l:@L> <comment:Comment> <r:@R> "EOL" => Line::Comment{comment, line: l.line},
Expand Down Expand Up @@ -125,13 +130,15 @@ ProcessConfigLine: Line<'input> = {

ProcessConfig: Line<'input> = {
<pl:@L> "ProcessConfig" <pr:@R> "=" <process_name: VariableStrings> <process_config_comment: Comment?> "EOL"
<prelude_comments: CommentOrEmptyLine*>
<ocl:@L> "{" <open_curly_comment: Comment?> "EOL"
<body: ProcessConfigLine*>
<ccl:@L> "}" <close_curly_comment: Comment?> "EOL"
=> Line::ProcessConfig {
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,
Expand Down
2 changes: 1 addition & 1 deletion moos-parser/src/moos/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment<'input>>,
pub prelude_comments: Lines<'input>,
/// Line number for the opening curly brace
pub open_curly_line: u32,
/// Line number for the opening curly brace
Expand Down

0 comments on commit 460719c

Please sign in to comment.