m4xshen/smartcolumn.nvim

Inability to use more than one colorcolumn

junio256 opened this issue · 8 comments

Ins't common to see user using more than one limit to specify the "acceptable" column length and "You really should break this right now" column line
So would be an good enhancement to allow this :)

Just found plugin and thought about the exact same thing.

For example, in Python, I stick to cc=72,80 as suggested. The first is for documentation(comments) and the second is for code.

zamlz commented

+1 for this feature too! Except the semantics for how this should function might be up for debate. Given the situation that @dinko-pehar mentioned here, I would only want to see it on doc strings if and only if it exceeds 72, but not if my code lines are exceeding 72.

Maybe some integration with treesitter is needed for that?

I have a much more simple use case though. In my personal codebases, I use 80 as the character limit, but some other codes bases, I need to use 128 (for the same filetype). Not sure what the best solution is here, but with the in-built colorcolumn, you can do vim.opt.colorcolumn = {80, 128} which draws two at the same time. Not sure how'd this'd work either.

@zamlz
If you need different settings for similar projects (such as Python projects), you can use direnv to source your project.vimrc for example. Lot of projects have some files that are related to most popular IDEs/Editors so you can set their convention for that specific project. VSCode has .vscode folder, Jetbrains has .idea folder and so on. Each of these set up configuration for that particular project.
In (Neo)Vim case, you can just create .vimrc at project level and source it.

Or you create an autocmd for a specific filetype, in this case you would need to create two of them. One to make it the custom size on opening the file(BufWinEnter), and another to set the default when exiting (BufWinLeave)
Maybe it's possible to set this on the plugin automatically, but would need an specific issue for this

A brief example:
-- -- file: ../nvim/lua/autocmd.lua

-- Require the plugin
local _, smartcolumn = pcall(require, "smartcolumn")

-- Is good practice to use augroups on autocmds for perfomance 
local smartcolumngroup = vim.api.nvim_create_augroup("SmartColumn", { clear = true })

autocmd("BufWinEnter", { -- declare the event and after it an table with your desired config
	group = smartcolumngroup, -- The group we just created
	pattern = "*.py", -- File type specification (the wildcard `*.` is necessary)
	callback = function()
		smartcolumn.setup {colorcolumn = 120} -- plugin specifications
	end,
})

-- Set default values when leaving the python buffer
autocmd("BufWinLeave", {
	group = smartcolumngroup,
	pattern = "*", -- This glob isn't necessary since it applies on all files when no pattern is declared
	callback = function()
		smartcolumn.setup {colorcolumn = 80}
	end,
})

-- -- file: ../nvim/lua/init.lua
...

require('autocmd')

...

The feature of multiple colorcolumns is added in a383e95!

zamlz commented

Thank you @m4xshen for the quick feature implementation. I had a quick question about usage and maybe it's more of a lua question really.

In an earlier part of the code (in the option setting phase of my init script), I do the following

vim.opt.colorcolumn = {80, 128}

I'm still really new to lua but I would have expected that the following would work for the plugin.

opts = {
    colorcolumn = vim.opt.column
}

However, it doesn't work, as it turns out, when I run :lua print(vim.opt.colorcolumn[0]) in nvim after everything is loaded up, this command returns nil!!!

This is clearly NOT a problem with the plugin, but definitely my own understanding of lua. Why does this not work in this scenario?

The following does work, but I would prefer not having to redefine the values.

opts = {
    colorcolumn = {80, 128}
}

vim.opt returns an Option object, not the value of the option, which is accessed through vim.opt:get().

In this case, you should use:

opts = {
   colorcolumn = vim.opt.colorcolumn:get(),
}

You can learn more with :h vim.opt.

zamlz commented

Thank you @m4xshen! I appreciate the explanation, I did not realize it wasn't a table!!