Skip to content

Commit b7c4a1b

Browse files
committed
Correctly interpret offsets in on_bytes handler
When new_end_row is not equal to start_row, the new_end_col is just the column within that row, rather than an offset from start_col. This fixes the changes that are sent to parinfer, which fixes a couple of bugs.
1 parent 72f5f3d commit b7c4a1b

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

fnl/parinfer/setup.fnl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
:forceBalance (true? (get-option :force_balance))}
105105
response ((. modes (get-option :mode)) text request)]
106106
(log "request" request)
107-
(tset state bufnr :changes nil)
107+
(tset state bufnr :changes [])
108108
response))
109109

110110
(fn update-buffer [bufnr lines]
@@ -163,15 +163,15 @@
163163
(fn slice [lines start-row start-col end-row end-col]
164164
(let [start-row (+ start-row 1)
165165
start-col (+ start-col 1)
166-
end-row (+ end-row 1)
167-
end-col (+ end-col 1)
168-
first-line (string.sub (. lines start-row) start-col (if (= start-row end-row) (- end-col 1) -1))
166+
first-line (string.sub (. lines start-row) start-col (if (= 0 end-row) (- (+ start-col end-col) 1) -1))
169167
out [first-line]]
170-
(for [i (+ start-row 1) (- end-row 1)]
168+
(for [i (+ start-row 1) (- (+ start-row end-row) 1)]
171169
(let [line (. lines i)]
172170
(table.insert out line)))
173-
(when (and (not= start-row end-row) (< 1 end-col) (. lines end-row))
174-
(table.insert out (string.sub (. lines end-row) 1 (- end-col 1))))
171+
(when (not= 0 end-row)
172+
(table.insert out (if (and (< 0 end-col) (. lines (+ start-row end-row)))
173+
(string.sub (. lines (+ start-row end-row)) 1 end-col)
174+
"")))
175175
out))
176176

177177
(fn on-bytes [_ bufnr _ start-row start-col _ old-end-row old-end-col _ new-end-row new-end-col]
@@ -182,10 +182,6 @@
182182
; "changes"
183183
(when (< start-row (length prev-contents))
184184
(let [contents (vim.api.nvim_buf_get_lines bufnr 0 -1 true)
185-
old-end-row (+ start-row old-end-row)
186-
new-end-row (+ start-row new-end-row)
187-
old-end-col (+ start-col old-end-col)
188-
new-end-col (+ start-col new-end-col)
189185
old-text (slice prev-contents start-row start-col old-end-row old-end-col)
190186
new-text (if (< start-row (length contents))
191187
(slice contents start-row start-col new-end-row new-end-col)
@@ -195,10 +191,12 @@
195191
; empty string
196192
[""])]
197193
(tset state bufnr :prev-contents contents)
198-
(tset state bufnr :changes [{:oldText (table.concat old-text "\n")
199-
:newText (table.concat new-text "\n")
200-
:lineNo (+ start-row 1)
201-
:x (+ start-col 1)}]))))))
194+
(when (not (. state bufnr :changes))
195+
(tset state bufnr :changes []))
196+
(table.insert (. state bufnr :changes) {:oldText (table.concat old-text "\n")
197+
:newText (table.concat new-text "\n")
198+
:lineNo (+ start-row 1)
199+
:x (+ start-col 1)}))))))
202200

203201
(fn enter-buffer []
204202
(let [bufnr (vim.api.nvim_get_current_buf)

lua/parinfer/setup.lua

Lines changed: 27 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)