Skip to content

Commit

Permalink
I noticed in the byte-offsets refactor that the JsonEmitter uses on…
Browse files Browse the repository at this point in the history
…e indexed

column numbers for the the diagnostic start and end locations but not for `edits`.

This PR changes the `JsonEmitter` to emit one-indexed column numbers for edits,
the same as we already to for `Message::location` and `Message::end_location`.
  • Loading branch information
MichaReiser committed May 10, 2023
1 parent a2b8487 commit 85f8156
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
17 changes: 4 additions & 13 deletions crates/ruff/src/message/json.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::message::{Emitter, EmitterContext, Message};
use crate::registry::AsRule;
use ruff_diagnostics::Edit;
use ruff_python_ast::source_code::{SourceCode, SourceLocation};
use ruff_python_ast::source_code::SourceCode;
use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};
use serde_json::{json, Value};
use serde_json::json;
use std::io::Write;

#[derive(Default)]
Expand Down Expand Up @@ -78,12 +78,10 @@ impl Serialize for ExpandedEdits<'_> {
let mut s = serializer.serialize_seq(Some(self.edits.len()))?;

for edit in self.edits {
let start_location = self.source_code.source_location(edit.start());
let end_location = self.source_code.source_location(edit.end());
let value = json!({
"content": edit.content().unwrap_or_default(),
"location": to_zero_indexed_column(&start_location),
"end_location": to_zero_indexed_column(&end_location)
"location": self.source_code.source_location(edit.start()),
"end_location": self.source_code.source_location(edit.end())
});

s.serialize_element(&value)?;
Expand All @@ -93,13 +91,6 @@ impl Serialize for ExpandedEdits<'_> {
}
}

fn to_zero_indexed_column(location: &SourceLocation) -> Value {
json!({
"row": location.row,
"column": location.column.to_zero_indexed()
})
}

#[cfg(test)]
mod tests {
use crate::message::tests::{capture_emitter_output, create_messages};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ expression: content
"content": "",
"location": {
"row": 1,
"column": 0
"column": 1
},
"end_location": {
"row": 2,
"column": 0
"column": 1
}
}
]
Expand All @@ -43,11 +43,11 @@ expression: content
"content": "",
"location": {
"row": 6,
"column": 4
"column": 5
},
"end_location": {
"row": 6,
"column": 9
"column": 10
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ fn test_stdin_json() -> Result<()> {
"content": "",
"location": {{
"row": 1,
"column": 0
"column": 1
}},
"end_location": {{
"row": 2,
"column": 0
"column": 1
}}
}}
]
Expand Down

0 comments on commit 85f8156

Please sign in to comment.