Skip to content

Commit

Permalink
update server capabilities + react! to different arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLee18 committed Oct 31, 2024
1 parent 3d2f2f9 commit fe4907c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 52 deletions.
99 changes: 58 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,59 @@ pub mod norminette_msg;
pub mod parser;

use lsp_server::{Connection, ExtractError, Message, Notification, Request, RequestId, Response};
use lsp_types::notification::{DidChangeTextDocument, DidOpenTextDocument, DidSaveTextDocument};
use lsp_types::notification::{DidOpenTextDocument, DidSaveTextDocument};
use lsp_types::request::DocumentDiagnosticRequest;
use lsp_types::{
Diagnostic, DiagnosticOptions, DiagnosticServerCapabilities,
InitializeParams, LogMessageParams, MessageType, PublishDiagnosticsParams, ServerCapabilities,
WorkDoneProgressOptions,
Diagnostic, DiagnosticOptions, DiagnosticServerCapabilities, DidOpenTextDocumentParams,
DidSaveTextDocumentParams, InitializeParams, PublishDiagnosticsParams, SaveOptions,
ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncOptions,
TextDocumentSyncSaveOptions, WorkDoneProgressOptions,
};
use parser::parse_norminette;
use std::error::Error;
use std::io;
use std::path::Path;

macro_rules! diag_on_event {
($conn: expr, $noti: expr, $t: ident) => {
($conn: expr, $noti: expr, $t: ident, $f: expr) => {
match cast_noti::<$t>($noti) {
Ok(params) => {
eprintln!("got doc document open notification: {params:?}");
notify_diagnostics!($conn, params);
notify_diagnostics!($conn, &params, $f);
}
Err(_) => {}
}
};
}

macro_rules! notify_diagnostics {
($conn: expr, $params: expr) => {
match read_norminette(&Path::new($params.text_document.uri.path().as_str())) {
($conn: expr, $params: expr, $f: expr) => {
let text = $f.map(|f_| f_($params));
match read_norminette(&Path::new($params.text_document.uri.path().as_str()), text) {
Ok(diags) => {
$conn.sender.send(Message::Notification(Notification {
method: String::from("textDocument/publishDiagnostics"),
params: serde_json::to_value(&PublishDiagnosticsParams {
uri: $params.text_document.uri,
uri: $params.text_document.uri.clone(),
diagnostics: diags,
version: None,
})?,
}))?;
}
Err(e) => {
$conn.sender.send(Message::Notification(Notification {
method: String::from("window/logMessage"),
params: serde_json::to_value(&LogMessageParams {
typ: MessageType::ERROR,
message: format!(
"norminette read of {} failed: {}",
$params.text_document.uri.path(),
e
),
})?,
}))?;
eprintln!(
"norminette read of {} failed: {}",
$params.text_document.uri.path(),
e
);
}
}
};
}

macro_rules! send_diagnostics {
($conn: expr, $id: expr, $params: expr) => {
match read_norminette(&Path::new($params.text_document.uri.path().as_str())) {
match read_norminette(&Path::new($params.text_document.uri.path().as_str()), None) {
Ok(diags) => {
$conn.sender.send(Message::Response(Response {
id: $id,
Expand All @@ -71,29 +67,28 @@ macro_rules! send_diagnostics {
}))?;
}
Err(e) => {
$conn.sender.send(Message::Response(Response {
id: $id,
result: Some(serde_json::to_value(&LogMessageParams {
typ: MessageType::ERROR,
message: format!(
"norminette read of {} failed: {}",
$params.text_document.uri.path(),
e
),
})?),
error: None,
}))?;
eprintln!(
"norminette read of {} failed: {}",
$params.text_document.uri.path(),
e
);
}
}
};
}

fn read_norminette(path: &Path) -> io::Result<Vec<Diagnostic>> {
let output = std::process::Command::new("norminette")
.arg(path)
.output()?;
fn read_norminette(path: &Path, text: Option<String>) -> io::Result<Vec<Diagnostic>> {
let mut cmd = std::process::Command::new("norminette");
match text {
Some(text) => {
cmd.args(["--cfile", &text, "--filename", path.to_str().unwrap()]);
}
None => {
cmd.arg(path);
}
}
let (_, diags) = parse_norminette(
&String::from_utf8(output.stdout)
&String::from_utf8(cmd.output()?.stdout)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?,
)
.map_err(|err| {
Expand Down Expand Up @@ -123,6 +118,16 @@ fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
workspace_diagnostics: false,
work_done_progress_options: WorkDoneProgressOptions::default(),
})),
text_document_sync: Some(TextDocumentSyncCapability::Options(
TextDocumentSyncOptions {
open_close: Some(true),
change: None,
save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
include_text: Some(true),
})),
..Default::default()
},
)),
..Default::default()
})?;
let initialization_params = match connection.initialize(server_capabilities) {
Expand Down Expand Up @@ -171,9 +176,21 @@ fn main_loop(
}
Message::Notification(not) => {
eprintln!("got notification: {not:?}");
diag_on_event!(connection, not.clone(), DidOpenTextDocument);
diag_on_event!(connection, not.clone(), DidChangeTextDocument);
diag_on_event!(connection, not, DidSaveTextDocument);
diag_on_event!(
connection,
not.clone(),
DidOpenTextDocument,
Some(|p: &DidOpenTextDocumentParams| p.text_document.text.clone())
);
diag_on_event!(
connection,
not,
DidSaveTextDocument,
Some(|p: &DidSaveTextDocumentParams| p
.text
.clone()
.expect("includeText set to true yet text was None"))
);
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions src/norminette_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@ pub enum NorminetteMsg {
column: i32,
message: String,
},
Ok
Ok,
}

impl NorminetteMsg {
pub fn find_range(&self) -> Option<Range> {
match self {
NorminetteMsg::Error {
error_type: _,
line,
column,
message: _,
} => Some(Range {
NorminetteMsg::Error { line, column, .. } => Some(Range {
start: Position::new((line - 1) as u32, *column as u32),
end: Position::new(*line as u32, u32::MAX),
end: Position::new(*line as u32, (column + 1) as u32),
}),
NorminetteMsg::Ok => None
NorminetteMsg::Ok => None,
}
}

pub fn to_diagnostic(self) -> Option<Diagnostic> {
let range = self.find_range()?;
match self {
NorminetteMsg::Error { error_type, message, .. } => Some(Diagnostic {
NorminetteMsg::Error {
error_type,
message,
..
} => Some(Diagnostic {
range,
severity: Some(DiagnosticSeverity::ERROR),
code: Some(NumberOrString::String(error_type)),
Expand All @@ -43,7 +42,7 @@ impl NorminetteMsg {
tags: None,
data: None,
}),
NorminetteMsg::Ok => None
NorminetteMsg::Ok => None,
}
}
}

0 comments on commit fe4907c

Please sign in to comment.