feat: improved diff
This commit is contained in:
parent
85c2659ea3
commit
4f1834d34b
4 changed files with 51 additions and 65 deletions
|
|
@ -10,7 +10,7 @@ local function au(events, cb)
|
||||||
vim.api.nvim_create_autocmd(events, {
|
vim.api.nvim_create_autocmd(events, {
|
||||||
callback = function(opts)
|
callback = function(opts)
|
||||||
if is_ic_buf(opts.buf) then
|
if is_ic_buf(opts.buf) then
|
||||||
cb(opts)
|
cb(opts.buf)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
group = M.aug_id,
|
group = M.aug_id,
|
||||||
|
|
@ -25,8 +25,8 @@ M.config = {
|
||||||
'typescriptreact',
|
'typescriptreact',
|
||||||
},
|
},
|
||||||
format = {
|
format = {
|
||||||
byte_format = '%.2f b',
|
byte_format = '%.1fb',
|
||||||
kb_format = '%.2f kb',
|
kb_format = '%.1fk',
|
||||||
virtual_text = '%s (gzipped: %s)',
|
virtual_text = '%s (gzipped: %s)',
|
||||||
},
|
},
|
||||||
highlight = 'Comment',
|
highlight = 'Comment',
|
||||||
|
|
@ -52,12 +52,21 @@ M.setup = function(user_config)
|
||||||
|
|
||||||
local extmark = require 'import-cost.extmark'
|
local extmark = require 'import-cost.extmark'
|
||||||
|
|
||||||
au({ 'BufEnter', 'BufWritePost' }, function(opts)
|
au('BufEnter', function(bufnr)
|
||||||
extmark.set_missing_extmarks(opts.buf)
|
extmark.set_extmarks(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
au('TextChanged', function(opts)
|
au('InsertEnter', function(bufnr)
|
||||||
extmark.update_extmarks(opts.buf)
|
extmark.delete_extmarks(bufnr)
|
||||||
|
end)
|
||||||
|
|
||||||
|
au('InsertLeave', function(bufnr)
|
||||||
|
extmark.set_extmarks(bufnr)
|
||||||
|
end)
|
||||||
|
|
||||||
|
au('TextChanged', function(bufnr)
|
||||||
|
extmark.delete_extmarks(bufnr)
|
||||||
|
extmark.set_extmarks(bufnr)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,30 @@ local M = {}
|
||||||
local ic, job, util =
|
local ic, job, util =
|
||||||
require 'import-cost', require 'import-cost.job', require 'import-cost.util'
|
require 'import-cost', require 'import-cost.job', require 'import-cost.util'
|
||||||
|
|
||||||
local visible_strings = {}
|
local cache = {}
|
||||||
|
|
||||||
local function update_visible_strings(bufnr, data, extmark_id)
|
local function update_cache(bufnr, data, extmark_id)
|
||||||
if not visible_strings[bufnr] then
|
if not cache[bufnr] then
|
||||||
visible_strings[bufnr] = {}
|
cache[bufnr] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
visible_strings[bufnr][data.string] = data
|
cache[bufnr][data.string] = data
|
||||||
visible_strings[bufnr][data.string].extmark_id = extmark_id
|
cache[bufnr][data.string].extmark_id = extmark_id
|
||||||
end
|
end
|
||||||
|
|
||||||
local function string_visible(bufnr, string)
|
local function set_extmark(bufnr, data)
|
||||||
return visible_strings[bufnr] and visible_strings[bufnr][string]
|
local string = util.normalize_string(data.string)
|
||||||
|
|
||||||
|
if cache[bufnr] and cache[bufnr][string] then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
data.string = string
|
||||||
|
local extmark_id = job.render_extmark(bufnr, data)
|
||||||
|
update_cache(bufnr, data, extmark_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.set_missing_extmarks(bufnr)
|
function M.set_extmarks(bufnr)
|
||||||
local path = vim.api.nvim_buf_get_name(bufnr)
|
local path = vim.api.nvim_buf_get_name(bufnr)
|
||||||
local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
|
local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
|
||||||
|
|
||||||
|
|
@ -35,16 +43,8 @@ function M.set_missing_extmarks(bufnr)
|
||||||
for _, chunk in ipairs(chunks) do
|
for _, chunk in ipairs(chunks) do
|
||||||
local data = util.parse_data(chunk)
|
local data = util.parse_data(chunk)
|
||||||
|
|
||||||
if util.is_ok(data) then
|
if data and data.size then
|
||||||
local string = util.normalize_string(data.string)
|
set_extmark(bufnr, data)
|
||||||
|
|
||||||
if not string_visible(bufnr, string) then
|
|
||||||
data.string = string
|
|
||||||
|
|
||||||
local extmark_id = job.set_extmark(bufnr, data)
|
|
||||||
|
|
||||||
update_visible_strings(bufnr, data, extmark_id)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
@ -55,49 +55,30 @@ function M.set_missing_extmarks(bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.clear_extmarks(bufnr)
|
function M.clear_extmarks(bufnr)
|
||||||
if visible_strings[bufnr] then
|
if cache[bufnr] then
|
||||||
visible_strings[bufnr] = nil
|
cache[bufnr] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_buf_clear_namespace(bufnr, ic.ns_id, 0, -1)
|
vim.api.nvim_buf_clear_namespace(bufnr, ic.ns_id, 0, -1)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.update_extmarks(bufnr)
|
function M.delete_extmarks(bufnr)
|
||||||
local buffer_strings = M.move_existing_extmarks(bufnr)
|
if not cache[bufnr] then
|
||||||
M.delete_remaining_extmarks(bufnr, buffer_strings)
|
return
|
||||||
M.set_missing_extmarks(bufnr)
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.move_existing_extmarks(bufnr)
|
|
||||||
local buffer_strings = {}
|
|
||||||
|
|
||||||
for nr, raw_string in ipairs(vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)) do
|
|
||||||
if util.is_import_string(raw_string) then
|
|
||||||
local string = util.normalize_string(raw_string)
|
|
||||||
local data = visible_strings[bufnr][string]
|
|
||||||
|
|
||||||
buffer_strings[string] = true
|
|
||||||
|
|
||||||
if data and data.string == string then
|
|
||||||
if data.line ~= nr then
|
|
||||||
data.line = nr
|
|
||||||
|
|
||||||
job.set_extmark(bufnr, data, data.extmark_id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return buffer_strings
|
for string, data in pairs(cache[bufnr]) do
|
||||||
end
|
if
|
||||||
|
string
|
||||||
function M.delete_remaining_extmarks(bufnr, buffer_strings)
|
== util.normalize_string(vim.fn.getbufoneline(bufnr, data.line))
|
||||||
for string, data in pairs(visible_strings[bufnr]) do
|
then
|
||||||
if not buffer_strings[string] then
|
goto continue
|
||||||
|
else
|
||||||
vim.api.nvim_buf_del_extmark(bufnr, ic.ns_id, data.extmark_id)
|
vim.api.nvim_buf_del_extmark(bufnr, ic.ns_id, data.extmark_id)
|
||||||
|
cache[bufnr][string] = nil
|
||||||
visible_strings[bufnr][string] = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
::continue::
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ local format_bytes = function(bytes)
|
||||||
return string.format(format.kb_format, 0.0009765625 * bytes)
|
return string.format(format.kb_format, 0.0009765625 * bytes)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.set_extmark(bufnr, data, extmark_id)
|
function M.render_extmark(bufnr, data, extmark_id)
|
||||||
local line, size, gzip =
|
local line, size, gzip =
|
||||||
data.line - 1, format_bytes(data.size), format_bytes(data.gzip)
|
data.line - 1, format_bytes(data.size), format_bytes(data.gzip)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,4 @@ function M.parse_data(chunk)
|
||||||
return json.data
|
return json.data
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.is_ok(data)
|
|
||||||
return data and data.size
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue