Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
glepnir committed Apr 27, 2024
1 parent 9cdbe25 commit b99e0f3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 69 deletions.
97 changes: 36 additions & 61 deletions lua/indentmini/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local api = vim.api
local nvim_create_autocmd, nvim_buf_set_extmark = api.nvim_create_autocmd, api.nvim_buf_set_extmark
local au, nvim_buf_set_extmark = api.nvim_create_autocmd, api.nvim_buf_set_extmark
local mini = {}

local ns = api.nvim_create_namespace('IndentLine')

local function col_in_screen(col)
Expand All @@ -11,40 +10,49 @@ end

local function can_set(row, col)
local text = api.nvim_buf_get_text(0, row, col, row, col + 1, {})[1]
return text ~= '\t' and text:find('%s')
return text ~= '\t' and not not text:find('%s')
end

local function indentline(group)
local function on_win(_, _, bufnr, _)
if bufnr ~= vim.api.nvim_get_current_buf() then
return false
end
local function on_win(_, _, bufnr, _)
if bufnr ~= vim.api.nvim_get_current_buf() then
return false
end
end

local ctx = {}
vim.api.nvim_create_autocmd('BufDelete', {
group = group,
callback = function()
ctx = {}
end,
})
local function on_start(_, _)
local bufnr = api.nvim_get_current_buf()
local exclude_buftype = { 'nofile', 'terminal' }
if
vim.tbl_contains(exclude_buftype, vim.bo[bufnr].buftype)
or not vim.bo[bufnr].expandtab
or vim.tbl_contains(mini.exclude, vim.bo[bufnr].ft)
then
return false
end
end

local function indentline()
local config = {
virt_text_pos = 'overlay',
hl_mode = 'combine',
ephemeral = true,
}
local function on_line(_, _, bufnr, row)
local indent = vim.fn.indent(row + 1)
local ok, lines = pcall(api.nvim_buf_get_text, bufnr, row, 0, row, -1, {})
if not ok then
return
end
local text = lines[1]
local prev = ctx[row - 1] or 0
local prev = vim.fn.indent(row)
if indent == 0 and #text == 0 and prev > 0 then
indent = prev > 20 and 4 or prev
end

ctx[row] = indent

local shiftw = vim.fn.shiftwidth()
local last_defined_level = 0
for i = 1, indent - 1, shiftw do
local col = i - 1
local indent_level = math.floor((i - 1) / shiftw) + 1
local hi_name = ('IndentLine%d'):format(indent_level)
-- Attempt to fetch highlight details for the current level
Expand All @@ -66,67 +74,34 @@ local function indentline(group)
end
api.nvim_set_hl(0, hi_name, { link = 'IndentLine', default = true })

if col_in_screen(i - 1) then
local param, col = {}, 0
if #text == 0 and i - 1 > 0 then
param = {
virt_text = { { mini.char, hi_name } },
virt_text_pos = 'overlay',
virt_text_win_col = i - 1,
hl_mode = 'combine',
ephemeral = true,
}
if col_in_screen(col) and can_set(row, col) then
config.virt_text = { { mini.char, hi_name } }
if #text == 0 and col > 0 then
config.virt_text_win_col = i - 1
nvim_buf_set_extmark(bufnr, ns, row, col, config)
else
param = {
virt_text = { { mini.char, hi_name } },
virt_text_pos = 'overlay',
hl_mode = 'combine',
ephemeral = true,
}
col = i - 1
end

if can_set(row, col) then
nvim_buf_set_extmark(bufnr, ns, row, col, param)
nvim_buf_set_extmark(bufnr, ns, row, col, config)
end
end
end
end

local function on_start(_, _)
local bufnr = api.nvim_get_current_buf()
local exclude_buftype = { 'nofile', 'terminal' }
if
vim.tbl_contains(exclude_buftype, vim.bo[bufnr].buftype)
or not vim.bo[bufnr].expandtab
or vim.tbl_contains(mini.exclude, vim.bo[bufnr].ft)
then
return false
end
end

api.nvim_set_decoration_provider(ns, {
on_win = on_win,
on_start = on_start,
on_line = on_line,
})
end

local function default_exclude()
return { 'dashboard', 'lazy', 'help', 'markdown' }
end

local function setup(opt)
mini = vim.tbl_extend('force', {
char = '',
exclude = default_exclude(),
exclude = { 'dashboard', 'lazy', 'help', 'markdown' },
}, opt or {})

local group = api.nvim_create_augroup('IndentMini', { clear = true })
nvim_create_autocmd('BufEnter', {
group = group,
au('BufEnter', {
group = api.nvim_create_augroup('IndentMini', { clear = true }),
callback = function()
indentline(group)
indentline()
end,
})
end
Expand Down
11 changes: 11 additions & 0 deletions test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local function test()
local a = 10
local b = 20
while true do
if a > b then
if b < a then
print('test')
end
end
end
end
36 changes: 28 additions & 8 deletions test/indentmini_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ local function screen(lines)
local channel = vim.fn.sockconnect('pipe', address, { rpc = true })

local current_dir = vim.fn.getcwd()

vim.rpcrequest(channel, 'nvim_set_option_value', 'rtp', current_dir, { scope = 'global' })
vim.rpcrequest(channel, 'nvim_exec_lua', 'require("indentmini").setup({})', {})
vim.rpcrequest(channel, 'nvim_set_option_value', 'columns', 26, { scope = 'global' })
vim.rpcrequest(channel, 'nvim_set_option_value', 'lines', #lines + 2, { scope = 'global' })
vim.rpcrequest(channel, 'nvim_exec_autocmds', 'BufEnter', { group = 'IndentMini' })
--indent set
vim.rpcrequest(channel, 'nvim_set_option_value', 'expandtab', true, { scope = 'global' })
vim.rpcrequest(channel, 'nvim_set_option_value', 'shiftwidth', 2, { scope = 'global' })
vim.rpcrequest(channel, 'nvim_set_option_value', 'tabstop', 2, { scope = 'global' })
vim.rpcrequest(channel, 'nvim_set_option_value', 'softtabstop', 2, { scope = 'global' })

vim.rpcrequest(channel, 'nvim_exec_autocmds', 'BufEnter', { group = 'IndentMini' })
vim.rpcrequest(channel, 'nvim_set_option_value', 'shiftwidth', 2, { buf = 0 })
vim.rpcrequest(channel, 'nvim_set_option_value', 'expandtab', true, { buf = 0 })

Expand Down Expand Up @@ -65,11 +69,10 @@ describe('indent mini', function()
}
local char = ''
local screenstr = screen(lines)
for _, line in ipairs(screenstr) do
print(vim.inspect(line))
end

local expect = {
-- for _, line in ipairs(screenstr) do
-- print(vim.inspect(line))
-- end
local expected = {
'local function test() ',
'┇ local a = 10 ',
'┇ local b = 20 ',
Expand All @@ -83,6 +86,23 @@ describe('indent mini', function()
'end ',
}

assert.same(expect, screenstr)
assert.same(expected, screenstr)
end)

it('not work when line has tab character', function ()
local lines = {
'functio test_tab()',
'\tprint("hello")',
'\tprint("world")',
'end'
}
local screenstr = screen(lines)
local expected = {
'functio test_tab() ',
' print("hello") ',
' print("world") ',
'end ',
}
assert.same(expected, screenstr)
end)
end)

0 comments on commit b99e0f3

Please sign in to comment.