nvimdev / indentmini.nvim

A minimal and blazing fast indentline plugin
MIT License
171 stars 12 forks source link

Overlapping content in tab indented files #8

Closed antonk52 closed 4 months ago

antonk52 commented 11 months ago

If a file that uses tabs for indentation the indent char seem to overlap some content. Please see attached screenshot

Screenshot 2023-10-14 at 00 01 35
config
require('indentmini').setup({
    char = '│',
    exclude = {
        'markdown',
    },
})

Example file from the screenshot

Expected behaviour: indent chars do not overlay file content

glepnir commented 11 months ago

if using tab as indent . indentmini shouldn't work . expandtab value ?

antonk52 commented 11 months ago

Thank you for the reply. expandtab is set to true globally.

vim.opt.expandtab = true

turning it off for a specific buffer fixes the issue. I can add a BufEnter autocommand that would scan the buffer if it uses tabs for indentation and set exapndtab to false for this buffer only. I wonder if you would be okay adding this to your plugin? I am happy to send a PR.

glepnir commented 11 months ago

I check expandtab value in there. https://github.com/nvimdev/indentmini.nvim/blob/a58129ae424fd6d8e0e2e7f6ce06c0443101e370/lua/indentmini/init.lua#L88

for some filetype it use tab as indent . in this situation i think this value shouldn't set to true. for me I usually config indent in after/ftplugin/xxx file like go use indent i config it like this

https://github.com/glepnir/nvim/blob/main/after/ftplugin/go.lua

antonk52 commented 11 months ago

Unfortunately this is not up to me, personally I use spaces for lua files, the example from the issue post is another lua plugin that I use. Also not everyone uses .editorconfig files. This is why dynamic detection can be a good thing.

For now I settled back to use leadmultispace listchar option to display indentation levels. And dynamically detect what is used for indentation in the file. This solution is not bulletproof but good enough.

local function update_listchars_for_spaces(space_count)
    local leadmultispace = space_count == 2 and '│ ' or '│   '
    vim.opt.listchars = vim.tbl_extend('force', LIST_CHARS, { leadmultispace = leadmultispace })
    vim.bo.tabstop = space_count
    vim.bo.shiftwidth = space_count
end

vim.api.nvim_create_autocmd('BufReadPost', {
    pattern = { '*' },
    callback = function()
        local lines = vim.api.nvim_buf_get_lines(0, 0, 100, false)
        local tab = '\t'

        -- check if there are indented lines
        -- and set listchars accordingly
        for lnum, line in ipairs(lines) do
            if line:find('^%s') ~= nil then
                local first_char = line:sub(1, 1)
                if first_char == tab then
                    -- do not use spaces in this buffer
                    vim.bo.expandtab = false
                    return
                end
                local indent_level = vim.fn.indent(lnum)
                if indent_level == 2 then
                    return update_listchars_for_spaces(2)
                elseif indent_level == 4 then
                    return update_listchars_for_spaces(4)
                end
            end
        end

        update_listchars_for_spaces(2)
    end,
})
glepnir commented 11 months ago

the example from the issue post is another lua plugin that I use.

sry i don't understand this. neovim also supports editorconfig :help editorconfig. except some project (like neovim script file which generate runtime/doc) need lua use space and tab. dynamic also better ...

antonk52 commented 11 months ago

No worries. The scenario where I hit a problem that motivated me to create this issue is

  1. I am editing my own dot files, all files use spaces for indentations, the plugin works as expected
  2. Within the same session I open one of the plugins source code that uses tabs for indentation and does not have an.editorconfig file

Does this help?

glepnir commented 4 months ago

maybe now is fixed.