Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Off by one for row and column in diagnostic generation #20

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions ruffd-core/src/server_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ruffd_types::{
CreateLocksFn, RpcNotification, RwGuarded, RwReq, ScheduledTask, ServerNotification,
ServerNotificationExec, ServerState, ServerStateHandles, ServerStateLocks,
};
use std::path::Path;
use std::sync::Arc;

// TODO macro the create locks fn
Expand All @@ -17,10 +18,10 @@ fn message_into_diagnostic(msg: Message) -> lsp_types::Diagnostic {
// only have it span a single character
let range = {
// diagnostic is zero indexed, but message rows are 1-indexed
let row_start = msg.location.row() as u32;
let col_start = msg.location.column() as u32 - 1;
let row_end = msg.end_location.row() as u32;
let col_end = msg.end_location.column() as u32 - 1;
let row_start = msg.location.row() as u32 - 1;
let col_start = msg.location.column() as u32;
let row_end = msg.end_location.row() as u32 - 1;
let col_end = msg.end_location.column() as u32;
let start = lsp_types::Position {
line: row_start,
character: col_start,
Expand Down Expand Up @@ -48,6 +49,14 @@ fn message_into_diagnostic(msg: Message) -> lsp_types::Diagnostic {
}
}

fn diagnostics_from_doc(path: &Path, doc: &str) -> Vec<lsp_types::Diagnostic> {
check(path, doc)
.unwrap_or_default()
.into_iter()
.map(message_into_diagnostic)
.collect()
}

// NOTE require interface from ruff before checks can be run
pub fn run_diagnostic_op(document_uri: lsp_types::Url) -> ServerNotification {
let exec: ServerNotificationExec = Box::new(
Expand All @@ -57,22 +66,18 @@ pub fn run_diagnostic_op(document_uri: lsp_types::Url) -> ServerNotification {
RwGuarded::Read(x) => x,
_ => unreachable!(),
};
let messages: Vec<Message> = {
let diagnostics = {
if let Some(buffer) = open_buffers.get(&document_uri) {
let doc = buffer.iter().collect::<String>();
if let Ok(path) = document_uri.to_file_path() {
check(&path, &doc).unwrap_or_default()
diagnostics_from_doc(&path, doc.as_str())
} else {
vec![]
}
} else {
vec![]
}
};
let diagnostics = messages
.into_iter()
.map(message_into_diagnostic)
.collect::<Vec<_>>();
RpcNotification::new(
"textDocument/publishDiagnostics".to_string(),
Some(
Expand Down Expand Up @@ -100,3 +105,34 @@ pub fn run_diagnostic_op(document_uri: lsp_types::Url) -> ServerNotification {
});
ServerNotification { exec, create_locks }
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_diagnostic_gen_position() {
let doc = r#"
def bar():
x = 0
print('does this now work?')
"#;
let path = lsp_types::Url::parse("file:///tmp/dummy.py")
.unwrap()
.to_file_path()
.unwrap();
let diagnostics = diagnostics_from_doc(&path, doc);
let expected_range = lsp_types::Range {
start: lsp_types::Position {
line: 2,
character: 4,
},
end: lsp_types::Position {
line: 2,
character: 5,
},
};
assert_eq!(diagnostics.len(), 1);
assert_eq!(diagnostics[0].range, expected_range);
}
}