mirror of https://github.com/astral-sh/ruff
JSON Emitter: Use one indexed column numbers for edits (#4007)
I noticed in the byte-offsets refactor that the `JsonEmitter` uses one indexed column numbers for the diagnostic start and end locations but not for `edits`.
This PR changes the `JsonEmitter` to emit one-indexed column numbers for edits, as we already do for `Message::location` and `Message::end_location`.
## Open questions
~We'll need to change the LSP to subtract 1 from the columns in `_parse_fix`~
6e44fadf8a/ruff_lsp/server.py (L129-L150)
~@charliermarsh is there a way to get the ruff version in that method? If not, then I recommend adding a `version` that we increment whenever we make incompatible changes to the serialized message. We can then use it in the LSP to correctly compute the column offset.~
I'll use the presence of the `Fix::applicability` field to detect if the Ruff version uses one or zero-based column indices.
See https://github.com/charliermarsh/ruff-lsp/pull/103
This commit is contained in:
parent
5f64d2346f
commit
853d8354cb
|
|
@ -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)]
|
||||
|
|
@ -79,12 +79,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)?;
|
||||
|
|
@ -94,13 +92,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};
|
||||
|
|
|
|||
|
|
@ -14,11 +14,11 @@ expression: content
|
|||
"content": "",
|
||||
"location": {
|
||||
"row": 1,
|
||||
"column": 0
|
||||
"column": 1
|
||||
},
|
||||
"end_location": {
|
||||
"row": 2,
|
||||
"column": 0
|
||||
"column": 1
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
@ -45,11 +45,11 @@ expression: content
|
|||
"content": "",
|
||||
"location": {
|
||||
"row": 6,
|
||||
"column": 4
|
||||
"column": 5
|
||||
},
|
||||
"end_location": {
|
||||
"row": 6,
|
||||
"column": 9
|
||||
"column": 10
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
|||
|
|
@ -100,11 +100,11 @@ fn test_stdin_json() -> Result<()> {
|
|||
"content": "",
|
||||
"location": {{
|
||||
"row": 1,
|
||||
"column": 0
|
||||
"column": 1
|
||||
}},
|
||||
"end_location": {{
|
||||
"row": 2,
|
||||
"column": 0
|
||||
"column": 1
|
||||
}}
|
||||
}}
|
||||
]
|
||||
|
|
|
|||
Loading…
Reference in New Issue