-
Notifications
You must be signed in to change notification settings - Fork 8
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
Fix "copy" operations #30
Conversation
I used this opportunity to heavily comment how copy works because coming back to this code after months was pretty hard. |
}; | ||
|
||
let copied_lines = old_lines.drain(range).map(|mut line| { | ||
line.line_num = line.line_num.map(|line_num| (line_num as i64 + diff) as u64); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The juggling between i64 and u64 is pretty ugly, and could panic in theory, but it'll be a while before we see files longer than 9,223,372,036,854,775,807 lines.
Copy operations include an "ln" field that is used to update the line number of the lines being copied. If we ignore it, we end up with inconsistent line numbers in the cache. For instance, consider the following file (`|` indicates the cursor position): ``` a| b ``` On pressing "Enter" to insert a newline, the following update is being sent: {"update":{"annotations":[{"n":1,"payloads":null,"ranges":[[1,0,1,0]],"type":"selection"}],"ops":[{"lines":[{"ln":1,"styles":[0,2,8],"text":"a\n"},{"cursor":[0],"ln":2,"styles":[0,1,8],"text":"\n"}],"n":2,"op":"ins"},{"n":1,"op":"skip"},{"ln":3,"n":1,"op":"copy"}],"pristine":false},"view_id":"view-id-2"} The intersting operation is {"ln":3,"n":1,"op":"copy"}. Before the update we have the following line cache: LineCache { invalid_before: 0, lines: [Line { text: "a", cursor: [1], styles: [StyleDef { offset: 0, length: 2, style_id: 8 }], line_num: Some(1) }, Line { text: "b", cursor: [], styles: [StyleDef { offset: 0, length: 1, style_id: 8 }], line_num: Some(2) }], invalid_after: 0 } If we ignore the "ln" field, we end up with the following cache where we have to lines number "2" ("" and "b"): LineCache { invalid_before: 0, lines: [Line { text: "a", cursor: [], styles: [StyleDef { offset: 0, length: 2, style_id: 8 }], line_num: Some(1) }, Line { text: "", cursor: [0], styles: [StyleDef { offset: 0, length: 1, style_id: 8 }], line_num: Some(2) }, Line { text: "b", cursor: [], styles: [StyleDef { offset: 0, length: 1, style_id: 8 }], line_num: Some(2) }], invalid_after: 0 } because the "b" line is copied as is. With this commit, we end up with the following line cache: LineCache { invalid_before: 0, lines: [Line { text: "a", cursor: [], styles: [StyleDef { offset: 0, length: 2, style_id: 8 }], line_num: Some(1) }, Line { text: "", cursor: [0], styles: [StyleDef { offset: 0, length: 1, style_id: 8 }], line_num: Some(2) }, Line { text: "b", cursor: [], styles: [StyleDef { offset: 0, length: 1, style_id: 8 }], line_num: Some(3) }], invalid_after: 0 }
While trying to debug #29 I noticed that we were not handling copy operations correctly. This probably does not fix #29 but should make debugging easier.
cc @Cogitri