Git Product home page Git Product logo

deadcolumn.nvim's Introduction

deadcolumn.nvim

luarocks

Don't across that column in Neovim

Deadcolumn is a neovim plugin to assist users in maintaining a specific column width in their code. This plugin operates by gradually displaying the colorcolumn as the user approaches it. It is useful for people who wish to keep their code aligned within a specific column range.

Table of Contents

Features

  • Gradually display the colorcolumn as the user approaches it:

  • Display the column in warning color if current line exceeds colorcolumn:

  • Handle multiple values of colorcolumn properly:

    • :set colorcolumn=-10,25,+2 textwidth=20:
  • Show the colored column only when you need it

    • Show the colored column in insert mode only:

    • Show the colored column only when current line is longer than colorcolumn:

    • and more...

Installation

  • Using lazy.nvim

    lua require('lazy').setup({
        { 'Bekaboo/deadcolumn.nvim' }
    })
  • Using packer.nvim

    require('packer').startup(function(use)
        use 'Bekaboo/deadcolumn.nvim'
    end)
  • Using rocks.nvim

    :Rocks install deadcolumn.nvim

Options

⚠️ Notice

You don't need to call the setup() function if you don't want to change the default options, the plugin should work out of the box if you set colorcolumn to a value greater than 0.

The following is the default options, you can pass a table to the setup() function to override the default options.

local opts = {
    scope = 'line', ---@type string|fun(): integer
    ---@type string[]|fun(mode: string): boolean
    modes = function(mode)
        return mode:find('^[ictRss\x13]') ~= nil
    end,
    blending = {
        threshold = 0.75,
        colorcode = '#000000',
        hlgroup = { 'Normal', 'bg' },
    },
    warning = {
        alpha = 0.4,
        offset = 0,
        colorcode = '#FF0000',
        hlgroup = { 'Error', 'bg' },
    },
    extra = {
        ---@type string?
        follow_tw = nil,
    },
}

require('deadcolumn').setup(opts) -- Call the setup function
  • scope (string|function): The scope for showing the colored column, there are several possible values:

    • 'line': colored column will be shown based on the length of the current line.

    • 'buffer': colored column will be shown based on the length of the longest line in current buffer (up to 1000 lines around current line).

    • 'visible': colored column will be shown based on the length of the longest line in the visible area.

    • 'cursor': colored column will be shown based on current cursor position.

    • function() -> number: callback function that returns a number as the length of the row. For example, to show the colored column based on the longest line in the nearby 100 lines:

        require('deadcolumn').setup({
            scope = function()
                local max = 0
                for i = -50, 50 do
                    local len = vim.fn.strdisplaywidth(vim.fn.getline(vim.fn.line('.') + i))
                    if len > max then
                        max = len
                    end
                end
                return max
            end
        })
  • modes (table|function): In which modes to show the colored column.

    • If modes is a table, it should contain a list of mode names

    • If modes is a function, it should accept a string as the mode name and return a boolean value indicating whether to show the colored column in that mode.

  • blending (table): Blending options.

    • threshold (number): The threshold for showing the colored column.

      • If threshold is a number between 0 and 1, it will be treated as a relative threshold, the colored column will be shown when the current line is longer than threshold times the colorcolumn.

      • If threshold is a number greater than 1, it will be treated as a fixed threshold, the colored column will be shown when the current line is longer than threshold characters.

    • colorcode (string): The color code to be used as the background color for blending.

    • hlgroup (table): The highlight group to be used as the background color for blending.

      • If the highlight group is not found, colorcode will be used.
  • warning (table): Warning color options.

    • alpha (number): The alpha value for the warning color, blended with the background color.

    • offset (number): The offset for the warning color, the warning color will be shown when the length of the line exceeds colorcolumn by offset characters.

    • colorcode (string): The color code for the warning color.

    • hlgroup (table): The highlight group for the warning color.

      • If the highlight group is not found, colorcode will be used.
  • extra (table): Extra functionalities.

    • follow_tw (nil|string):

      • If follow_tw is nil: the functionalities is disabled.

      • If follow_tw is string: colorcolumn will be set to this value when textwidth is set, and will be restored to the original value when textwidth is unset.

        Suggested value for this option is '+1'.

FAQ

Why can't I see the colored column?

This can have several reasons:

  1. If you are using the default config, it is expected that you can't see the colored column in normal mode, because the colored column is only shown in insert mode and replace mode by default. You can change the modes option to show the colored column in normal mode.

  2. Please make sure you have set colorcolumn to a value greater than 0 in your config. Also, make sure that you have termguicolors set using :set termguicolors

  3. If you set colorcolumn to a relative value (e.g. '-10'), make sure textwidth is set to a value greater than 0.

How to set different colorcolumn for different filetypes?

This plugin does not set colorcolumn for you, it only reads and uses the value of colorcolumn of the current buffer to show the colored column when needed.

It leaves to you to set colorcolumn for different filetypes, under different conditions, which is more flexible compared to setting colorcolumn in the plugin setup function.

There are mainly two ways to set colorcolumn for different filetypes:

  1. Using autocmd:

    You can use the autocmd command to set colorcolumn for different filetypes.

    For example, you can set colorcolumn to 80 for markdown files:

    autocmd FileType markdown setlocal colorcolumn=80
  2. Using ftplugin:

    You can also use the ftplugin directory to set colorcolumn for different filetypes.

    For example, you can create a file named markdown.vim in the ftplugin directory under your config directory, and set colorcolumn to 80 for markdown files:

    setlocal colorcolumn=80

Known Issues

Transparent Background

If you are using a transparent background, the colored column may not be displayed properly, since the background color of the colored column dynamically changed based on the blending of 'Normal' background color and the orignial 'ColorColumn' background color.

If Deadcolumn cannot find the 'Normal' background color, it will use '#000000' (pure black) as the default background color for blending.

There is no way to fix this, since terminal emulators do not support setting a transparent background color for a specific character.

Workarounds:

  1. You can set opts.threshold to 1 to disable blending when the length is smaller than colorcolumn and show the colored column only when it is greater than colorcolumn, OR

  2. You can assign a different highlight group or a fixed colorcode to be used for blending with the original 'ColorColumn' background color, for example:

    require('deadcolumn').setup({
        blending = {
            colorcode = '#1F2430',
            hlgroup = { 'NonText', 'bg' },
        },
    })

Similar Projects

deadcolumn.nvim's People

Contributors

bekaboo avatar github-actions[bot] avatar ntbbloodbath avatar ribru17 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

deadcolumn.nvim's Issues

Feature request: show column as usual when it would otherwise not show

Thanks for writing and releasing deadcolumn. I have a feature request if it fits in with your plans.

I like the concept of deadcolumn but I want to see the columns permanently. Do you think there could be an option such that when the columns aren't currently shown they could be shown is the default fashion?

Setup error

I get the following error, when scope and modes are setup, and theme is changed programatically afterwards:

Error detected while processing ./minimal.lua:
E5113: Error while calling lua chunk: ./minimal.lua:28: /Users/simon.mandlik/Downloads/test_deadcolumn/minimal.lua..ColorScheme Autocommands for "*": Vim(append):Erro
r executing lua callback: ...column/.repro/plugins/deadcolumn.nvim/lua/deadcolumn.lua:154: bad argument #1 to 'scope' (number expected, got no value)
stack traceback:
        [C]: in function 'scope'
        ...column/.repro/plugins/deadcolumn.nvim/lua/deadcolumn.lua:154: in function <...column/.repro/plugins/deadcolumn.nvim/lua/deadcolumn.lua:140>
        [C]: in function 'nvim_command'
        ./minimal.lua:28: in main chunk
stack traceback:
        [C]: in function 'nvim_command'
        ./minimal.lua:28: in main chunk

minimal.lua:

local root = vim.fn.fnamemodify("./.repro", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    "Bekaboo/deadcolumn.nvim",
    opts = {
        scope = "buffer",
        modes = function(mode)
            return mode:find('^[nictRss\x13]') ~= nil
        end,
    }
}

vim.cmd[[set termguicolors]]
vim.cmd[[set colorcolumn=20]]

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.api.nvim_command("colorscheme default")

Feature request: allow text on the column

It looks like the way you use columns is that the text must finish before the column. I like to allow text on the column but not beyond it. Do you think there could be an option to allow text on the column?

Setting winhighlight conflicts with other plugins

In a nutshell winhighlight conflicts with vim.api.nvim_win_set_hl_ns. The documentation says that either way of setting highlights takes precedence over another way. In practice, it means that if a highlight is set by winhighlight, it completely disables all highlights that were set by nvim_win_set_hl_ns.

winhighlight help
					*'winhighlight'* *'winhl'*
'winhighlight' 'winhl'	string	(default "")
			local to window
	Window-local highlights.  Comma-delimited list of highlight
	|group-name| pairs "{hl-from}:{hl-to},..." where each {hl-from} is
	a |highlight-groups| item to be overridden by {hl-to} group in
	the window.

	Note: highlight namespaces take precedence over 'winhighlight'.
	See |nvim_win_set_hl_ns()| and |nvim_set_hl()|.
nvim_win_set_hl_ns help
nvim_win_set_hl_ns({window}, {ns_id})                   *nvim_win_set_hl_ns()*
    Set highlight namespace for a window. This will use highlights defined
    with |nvim_set_hl()| for this namespace, but fall back to global
    highlights (ns=0) when missing.

    This takes precedence over the 'winhighlight' option.

    Parameters: ~
      • {ns_id}  the namespace to use

Snippet to test:

local ns = vim.api.nvim_create_namespace("UserTestHighlight")
vim.api.nvim_set_hl(ns, "Function", { fg = "#00FF66" })

vim.keymap.set("n", "<C-w>", function()
  vim.api.nvim_win_set_hl_ns(0, ns)
end)

vim.api.nvim_create_autocmd({ 'CursorMoved', 'TextChanged', }, {
  callback = function()
    vim.wo[0].winhl = "String:"
  end
})
issue.mp4

ccc.nvim is one of the plugin that is in conflict with the current plugin.

I think that sticking to the lua api is a good way to solve the problem

Cursor Losing Column Position Issue in Nightly Version

Firstly, I want to express my sincere thanks for this plugin.

Description:
I've encountered an issue where the cursor loses its column position when scrolling down with the plugin active. This problem persists in nightly builds (I've tried multiple recent versions), but it doesn't occur in stable builds.

While I understand nightly builds may have issues, I wanted to report this problem for consideration.

To Reproduce:

  1. Open a Lua file filled with long lines (exceeding 80 columns).
  2. Start editing the file.
  3. Scroll down using the 'j' key down a long line, and the cursor will lose its position and jump to column 0.
Code to Reproduce
mkdir ~/.config/testcolumn
cd ~/.config/testcolumn
touch init.lua
NVIM_APPNAME=testcolumn nvim

init.lua file

vim.opt.number = true
vim.opt.relativenumber = true

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"https://github.com/folke/lazy.nvim.git",
		"--branch=stable",
		lazypath,
	})
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
	"Bekaboo/deadcolumn.nvim",
	event = { "InsertEnter" },
	config = function()
		vim.opt.colorcolumn = "80"
		local opts = {
			modes = { "n", "i" },
			blending = {
				threshold = 0.75,
			},
			warning = {
				alpha = 0.1,
			},
		}
		require("deadcolumn").setup(opts)
	end,
})

The video demonstrates that the issue does not exist before activating the plugin. However, after inserting and saving the file, the problem occurs when scrolling down a long line.
Video:

Kapture.2024-02-13.at.11.03.30.mp4

OS: MacOS
term: wezterm
neovim version:

NVIM v0.10.0-dev-2355+g1c7b0b9d5
Build type: RelWithDebInfo
LuaJIT 2.1.1707061634

Thank you

Plugin doesn't work out of the box

Hi, thanks for working on this cool plugin. I decided to give it a try, because it seemed to have the most features, but can't make it work, even in vanilla vim.

repro.lua:

local root = vim.fn.fnamemodify("./.repro", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    -- "Bekaboo/deadcolumn.nvim"
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd[[hi ColorColumn guibg=red]]
vim.cmd[[set textwidth=20]]
vim.cmd[[set colorcolumn=-10,25,+2]]

When I run nvim -u repro.lua test.txt, it works as expected

image

but when I uncomment the line with the plugin, install it and start typing, nothing happens

image

Normal mode when used with scope buffer works but throws below error while opening a file!

It throws below error:
Error detected while processing /Users/username/.config/nvim/init.lua:
E5113: Error while calling lua chunk: vim/_editor.lua:0: /Users/username/.config/nvim/init.lua..nvim_exec2() called at /Users/username/.config/nvim/init.lua:0..ColorScheme Autocommands for "*": Vim(append):Error executing lua callback: ...re/nvim/lazy/deadcolumn.nvim/lua/deadcolumn/autocmds.lua:73: bad argument 1 to '?' (number expected, got no value)
stack traceback:
[builtin#61]: at 0x0103a18aa4
...re/nvim/lazy/deadcolumn.nvim/lua/deadcolumn/autocmds.lua:73: in function <...re/nvim/lazy/deadcolumn.nvim/lua/deadcolumn/autocmds.lua:58>
[C]: in function 'nvim_exec2'
vim/_editor.lua: in function 'cmd'
/Users/username/.config/nvim/init.lua:57: in main chunk
stack traceback:
[C]: in function 'nvim_exec2'
vim/_editor.lua: in function 'cmd'
/Users/username/.config/nvim/init.lua:57: in main chunk
Press ENTER or type command to continue

Horizontal cursor position clobbered

Hi and thanks for the great plugin! One small bug I noticed: when using this plugin, my horizontal cursor position is not saved in normal mode, same problem found here or here. I really like this plugin but this issue bugs me so hopefully it can be fixed, thank you!! (P.S. when disabling the plugin, horizontal cursor position is again saved correctly)

Always show colorcolumn if a line crosses it

This plugin works really freaking well, I'm always a fan of reducing visual noise, but one this I miss is that I can no longer tell of a line in the current view crosses the column.

If I set scope to "visible", then the point of the plugin is largely lost, I don't want to see the colorcolumn as long as nothing crosses it.

What would be perfect is if the plugin did exactly what it currently does, but if a currently visible line crosses the colorcolumn, it should be displayed no matter what.

Unless I've misunderstood the documentation, this isn't currently possible. Is there a elegant way this can be implemented?


Maybe something like:

warningscope = "buffer" | "visible"

Works like scope, but overrides it if a line actually crosses the colorcolumn. My ideal behavior would be scope = "line" and warningscope = "visible". I would also probably like warningscope = "buffer" so I would immediately know if any line in the current file was too long.

How exactly blending works?

Hi, I wanted to achieve a simple colorcolumn, that would behave like vanilla colorcolumn, but would start appearing once the cursor is getting closer to it.

What I need from deadcolumn is to show "standard" colorcolumn when the text is longer than textwidth, and only slowly "fade in" and "fade out".

But I can't even make colorcolumn have the same background color as ColorColumn highlight group.

This is my minimal.lua:

local root = vim.fn.fnamemodify("./.repro", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    {
        "Bekaboo/deadcolumn.nvim",
        init = function()
            require('deadcolumn').setup {
                blend = { hlgroup = { "ColorColumn", "bg" }, },
                warning = { hlgroup = { "ColorColumn", "bg" }, },
            }
        end
    }
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd[[set termguicolors]]
vim.cmd[[hi ColorColumn guibg=blue]]
vim.cmd[[set colorcolumn=20]]

And this is the behavior:

deadcolumn_2.mov

Color column has red color instead of blue, and once the text length exceeds the column, it turns into brown.

Can I somehow make the colorcolumn have the same color as the ColorColumn highlight group and basically "turn off" the warning functionality, only display fully opaque color column?

Thanks!

[Bug] deadcolumn works only in the first file

Hi, thanks for this nice plugin.

Describe the bug

  • Context

    When I open a file, it just works. But after moving to other files (eg. when opening multiple files, moving to a definition of a function in another file, etc.) it doesn't work in them. It works again when I come back to the first one.

  • Expected behavior

    I expect it to work on any file, regardless of the number or sequence of file opens.

  • Actual behavior

    It works only in the first file.

To Reproduce

  • Minimal init.lua

    -- Lazy
    local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'
    if not vim.loop.fs_stat(lazypath) then
      vim.fn.system({
        'git',
        'clone',
        '--filter=blob:none',
        'https://github.com/folke/lazy.nvim.git',
        '--branch=stable', -- latest stable release
        lazypath,
      })
    end
    vim.opt.rtp:prepend(lazypath)
    require'lazy'.setup({
      { 'Bekaboo/deadcolumn.nvim' },
    }, {})
    
    -- options
    local opt = vim.opt
    opt.colorcolumn = '80'
    opt.termguicolors = true
  • Steps to reproduce the behavior

    1. Save the above config as minimal.lua
    2. Start Neovim using nvim --clean -u minimal.lua
    3. Open multiple files and check if it works in them.

Environment

  • Neovim version: NVIM v0.9.0-dev-1338+g9e7426718

  • Operating system: Ubuntu 22.04

Additional context

Add any other context about the problem here.

Errors on colors.lua:41: invalid key: create

After upgrading my nvim packages, I got this:

Error detected while processing /home/mort/.config/nvim/init.lua:
E5113: Error while calling lua chunk: /home/mort/.config/nvim/lua/theme.lua:3: /home/mort/.config/nvim/init.lua..ColorScheme Autocommands for "*": Vim(append):Error executing lua callback:
...hare/nvim/lazy/deadcolumn.nvim/lua/deadcolumn/colors.lua:41: invalid key: create
stack traceback:
        [C]: in function 'nvim_get_hl'
        ...hare/nvim/lazy/deadcolumn.nvim/lua/deadcolumn/colors.lua:41: in function 'get'
        ...local/share/nvim/lazy/deadcolumn.nvim/lua/deadcolumn.lua:17: in function 'get_hl_hex'
        ...local/share/nvim/lazy/deadcolumn.nvim/lua/deadcolumn.lua:24: in function <...local/share/nvim/lazy/deadcolumn.nvim/lua/deadcolumn.lua:23>
        [C]: in function 'colorscheme'
        /home/mort/.config/nvim/lua/theme.lua:3: in main chunk
        [C]: in function 'require'
        /home/mort/.config/nvim/init.lua:8: in main chunk
stack traceback:
        [C]: in function 'colorscheme'
        /home/mort/.config/nvim/lua/theme.lua:3: in main chunk
        [C]: in function 'require'
        /home/mort/.config/nvim/init.lua:8: in main chunk

Just installing the package without any config.

My system:

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1693350652

This plugin doesn't work sometimes

Sorry for the low-quality bug report, but sometimes this plugin just... doesn't work. I enter insert mode and see no color column, despite there being a long line visible on my screen (I'm using scope 'visible'). Restarting vim fixes the issue.

I peeked at the implementation, and I see it's incredibly involved and complicated. So thanks for packaging this up into a plugin and trying to maintain all of the complexity behind an interface, but unfortunately, it seems something so far was missed.

Has anyone else experienced this issue? I'm afraid I don't have a reliable repro.

Repect

can shihua gg teach me how to install linux and use nvim?

Plugin un-setting colorcolumn setting

I have the setting vim.o.colorcolumn = '100', which normally works and sets the value.

When running the plugin with lazy.nvim, and then running :set colorcolumn? to verify the value, the setting appears empty. Setting the value to 100 in the buffer makes the plugin work normally afterwards.

Uninstalling and then verifying the value of the setting, the setting has the correct value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.