Skip to content

Commit

Permalink
fix: confusion between character and byte offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon committed Dec 12, 2023
1 parent 33dc4e6 commit 2daa160
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 115 deletions.
17 changes: 7 additions & 10 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ impl Backend {
};
match (split_uri, params.language) {
(Some((_, "py")), _) | (_, Some(Language::Python)) => {
self.on_change_python(
&params.text,
&params.uri,
rope,
params.old_rope,
&mut diagnostics,
params.open,
)?;
self.on_change_python(&params.text, &params.uri, rope.clone(), params.old_rope)?;
if !self.capabilities.pull_diagnostics.load(Relaxed)
&& (params.open || rope.len_lines() < Self::LINE_LIMIT)
{
self.diagnose_python(&params.uri, rope.clone(), &mut diagnostics);
}
}
(Some((_, "xml")), _) | (_, Some(Language::Xml)) => {
self.on_change_xml(&params.text, &params.uri, rope).await?;
Expand Down Expand Up @@ -125,7 +123,6 @@ impl Backend {
let end =
position_to_offset(range.end, old_rope.clone()).ok_or_else(|| diagnostic!("delta end"))?;
let len_new = change.text.len();
let len_new_bytes = change.text.as_bytes().len();
let start_position = tree_sitter::Point {
row: range.start.line as usize,
column: range.start.character as usize,
Expand All @@ -146,7 +143,7 @@ impl Backend {
ast.edit(&tree_sitter::InputEdit {
start_byte: start.0,
old_end_byte: end.0,
new_end_byte: start.0 + len_new_bytes,
new_end_byte: start.0 + len_new,
start_position,
old_end_position,
new_end_position,
Expand Down
8 changes: 4 additions & 4 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use xmlparser::{Token, Tokenizer};

use crate::model::{Model, ModelIndex, ModelType};
use crate::record::Record;
use crate::utils::{ts_range_to_lsp_range, ByteOffset, RangeExt, Usage};
use crate::utils::{ts_range_to_lsp_range, ByteOffset, ByteRange, RangeExt, Usage};
use crate::{format_loc, ImStr};

mod record;
Expand Down Expand Up @@ -344,7 +344,7 @@ pub fn index_models(contents: &[u8]) -> miette::Result<Vec<Model>> {
let mut models = HashMap::new();
struct NewModel<'a> {
range: tree_sitter::Range,
byte_range: core::ops::Range<usize>,
byte_range: ByteRange,
name: Option<&'a [u8]>,
inherits: Vec<&'a [u8]>,
}
Expand All @@ -360,7 +360,7 @@ pub fn index_models(contents: &[u8]) -> miette::Result<Vec<Model>> {
let model = models.entry(model_node.byte_range()).or_insert_with(|| NewModel {
name: None,
range: model_node.range(),
byte_range: model_node.byte_range(),
byte_range: model_node.byte_range().map_unit(ByteOffset),
inherits: vec![],
});
match &contents[capture.byte_range()] {
Expand Down Expand Up @@ -440,7 +440,7 @@ pub fn index_models(contents: &[u8]) -> miette::Result<Vec<Model>> {
.map(|inherit| ImStr::from(String::from_utf8_lossy(inherit).as_ref()))
.collect::<Vec<_>>();
let range = ts_range_to_lsp_range(model.range);
let byte_range = model.byte_range.map_unit(ByteOffset);
let byte_range = model.byte_range;
match (inherits.is_empty(), model.name) {
(false, None) => Some(Model {
range,
Expand Down
Loading

0 comments on commit 2daa160

Please sign in to comment.