Skip to content

Commit

Permalink
Fix: broken highlight reset on delete cancel due to regression in #25
Browse files Browse the repository at this point in the history
  • Loading branch information
SidOfc committed May 21, 2022
1 parent 8faca0d commit d7876d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
15 changes: 10 additions & 5 deletions doc/carbon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1345,20 +1345,23 @@ BUFFER *carbon-buffe

Makes the Carbon buffer |modifiable| and calls |nvim_buf_set_lines| like this:

`vim.api.nvim_buf_set_lines(`0, {from}, {to}, 1, {lines}`)`
`vim.api.nvim_buf_set_lines(`<bufnr>, {from}, {to}, 1, {lines}`)`

After lines have been inserted Carbon sets |modified| to `false`.
Will also set |nomodifiable| to `true` except when called in |insert-mode|.

<bufnr> will be set to the |bufnr| of the Carbon buffer.

`------------------------------------------------------------------------------`
add_highlight *carbon-buffer-add-highlight*

Signature: `require('carbon.buffer').add_highlight(`{hl}, {lnum}, {from}, {to}`)`

Calls |nvim_buf_add_highlight| like this:

`vim.api.nvim_buf_add_highlight(`0, <ns_id>, {hl}, {lnum}, {from}, {to}`)`
`vim.api.nvim_buf_add_highlight(`<bufnr>, <ns_id>, {hl}, {lnum}, {from}, {to}`)`

<bufnr> will be set to the |bufnr| of the Carbon buffer.
<ns_id> will be set to the namespace stored internally by Carbon.

`------------------------------------------------------------------------------`
Expand All @@ -1368,8 +1371,9 @@ BUFFER *carbon-buffe

Calls |nvim_buf_clear_namespace| like this:

`vim.api.nvim_buf_clear_namespace(`0, <ns_id>, {from}, {to}`)`
`vim.api.nvim_buf_clear_namespace(`<bufnr>, <ns_id>, {from}, {to}`)`

<bufnr> will be set to the |bufnr| of the Carbon buffer.
<ns_id> will be set to the namespace stored internally by Carbon.

`------------------------------------------------------------------------------`
Expand All @@ -1380,13 +1384,14 @@ BUFFER *carbon-buffe
Wraps |nvim_buf_get_extmarks| and |nvim_buf_del_extmark|. This function
first calls |nvim_buf_get_extmarks| like this:

`vim.api.nvim_buf_get_extmarks(`0, <ns_id>, {from}, {to}, {opts}`)`
`vim.api.nvim_buf_get_extmarks(`<bufnr>, <ns_id>, {from}, {to}, {opts}`)`

The resulting list of |extmarks| will all be cleared by calling
|nvim_buf_del_extmark| like this:

`vim.api.nvim_buf_del_extmark(`0, <ns_id>, <extmark_id>`)`
`vim.api.nvim_buf_del_extmark(`<bufnr>, <ns_id>, <extmark_id>`)`

<bufnr> will be set to the |bufnr| of the Carbon buffer.
<ns_id> will be set to the namespace stored internally by Carbon.

`------------------------------------------------------------------------------`
Expand Down
22 changes: 13 additions & 9 deletions lua/carbon/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -416,35 +416,39 @@ function buffer.create()
util.map('<cr>', handle_create_confirm(ctx), { buffer = 0, mode = 'i' })
util.map('<esc>', handle_create_cancel(ctx), { buffer = 0, mode = 'i' })
util.cursor(ctx.edit_lnum + 1, #ctx.edit_indent - 1)
vim.api.nvim_buf_set_option(0, 'modifiable', true)
vim.api.nvim_buf_set_option(data.handle, 'modifiable', true)
vim.cmd('startinsert!')
end

function buffer.clear_extmarks(...)
local extmarks = vim.api.nvim_buf_get_extmarks(0, data.namespace, ...)
local extmarks = vim.api.nvim_buf_get_extmarks(
data.handle,
data.namespace,
...
)

for _, extmark in ipairs(extmarks) do
vim.api.nvim_buf_del_extmark(0, data.namespace, extmark[1])
vim.api.nvim_buf_del_extmark(data.handle, data.namespace, extmark[1])
end
end

function buffer.clear_namespace(...)
vim.api.nvim_buf_clear_namespace(0, data.namespace, ...)
vim.api.nvim_buf_clear_namespace(data.handle, data.namespace, ...)
end

function buffer.add_highlight(...)
vim.api.nvim_buf_add_highlight(0, data.namespace, ...)
vim.api.nvim_buf_add_highlight(data.handle, data.namespace, ...)
end

function buffer.set_lines(start_lnum, end_lnum, lines)
local current_mode = string.lower(vim.api.nvim_get_mode().mode)

vim.api.nvim_buf_set_option(0, 'modifiable', true)
vim.api.nvim_buf_set_lines(0, start_lnum, end_lnum, 1, lines)
vim.api.nvim_buf_set_option(0, 'modified', false)
vim.api.nvim_buf_set_option(data.handle, 'modifiable', true)
vim.api.nvim_buf_set_lines(data.handle, start_lnum, end_lnum, 1, lines)
vim.api.nvim_buf_set_option(data.handle, 'modified', false)

if not string.find(current_mode, 'i') then
vim.api.nvim_buf_set_option(0, 'modifiable', false)
vim.api.nvim_buf_set_option(data.handle, 'modifiable', false)
end
end

Expand Down

0 comments on commit d7876d5

Please sign in to comment.