implement LSP-enum augend
Closes #104, #106.
Problems
- Not working until language server is up and running
- it is hard to solve essentially, but there is room for UX improvement, e.g. warning when server is not running
- Not intuitive what values are incremented to
- Slow with some language servers and condition
- it might be better to assign this function to a key other than
<C-a>/<C-x>
- it might be better to assign this function to a key other than
I tested this, the enum cycling worked for me! I wouldnt expect to be able to use this without an lsp running
I have an issue though, it slows down any non-enum cycling, so if I hover over a normal number and ctrl-a it will just go incredibly slow; about 1sec delay. probably because it defers from the lsp way too slowly but im not aware of these mechanisms. anyway you seem to be aware!
note im testing in rust with this dial.nvim config
return {
{
"monaqa/dial.nvim",
branch = "feat-augend-lsp",
keys = {
{ "<C-a>", function() require("dial.map").manipulate("increment", "normal") end, },
{ "<C-x>", function() require("dial.map").manipulate("decrement", "normal") end, },
{ "g<C-a>", function() require("dial.map").manipulate("increment", "gnormal") end, },
{ "g<C-x>", function() require("dial.map").manipulate("decrement", "gnormal") end, },
{ mode={"v"}, "<C-a>", function() require("dial.map").manipulate("increment", "visual") end, },
{ mode={"v"}, "<C-x>", function() require("dial.map").manipulate("decrement", "visual") end, },
{ mode={"v"}, "g<C-a>", function() require("dial.map").manipulate("increment", "gvisual") end, },
{ mode={"v"}, "g<C-x>", function() require("dial.map").manipulate("decrement", "gvisual") end, },
},
config = function()
local custom_lists = {
{"foo", "bar"},
{"tic", "tac", "toe"},
{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"},
{"mon", "tue", "wed", "thu", "fri", "sat", "sun"},
{"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"},
{"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"},
{"spring", "summer", "fall", "winter"},
{"north", "northeast", "east", "southeast", "south", "southwest", "west", "northwest"},
{"true", "false"},
{"yes", "no"},
{"on", "off"},
{"enable", "disable"},
{"enabled", "disabled"},
{"null", "undefined"},
{"public", "private", "protected"},
{"let", "const", "var"},
{"and", "or"},
{"&&", "||", opts = {word=false}, },
{"if", "else", "elif"},
{"min", "max"},
{"minimum", "maximum"},
{"start", "end"},
{"first", "last"},
{"open", "close"},
{"read", "write"},
{"input", "output"},
{"in", "out"},
{"up", "down"},
{"left", "right"},
{"top", "bottom"},
{"width", "height"},
{"row", "column"},
{"div", "span"},
{"block", "inline", "flex", "grid"},
{"margin", "padding"},
{"absolute", "relative", "fixed", "sticky"},
{"seconds", "minutes", "hours", "days", "weeks", "months", "years"},
{"second", "minute", "hour", "day", "week", "month", "year"},
{"secs", "mins", "hrs", "days", "wks", "mos", "yrs"},
{"sec", "min", "hr", "day", "wk", "mo", "yr"},
}
local augend = require("dial.augend")
local augends = {
augend.lsp_enum.new({}),
augend.integer.alias.decimal_int,
augend.integer.alias.hex,
augend.integer.alias.octal,
augend.integer.alias.binary,
augend.date.alias["%Y/%m/%d"],
augend.date.alias["%H:%M:%S"],
augend.date.alias["%H:%M"],
augend.semver.alias.semver,
augend.constant.alias.alpha,
augend.constant.alias.Alpha,
augend.constant.alias.ja_weekday,
augend.constant.alias.ja_weekday_full,
augend.constant.alias.bool,
}
for _, list in ipairs(custom_lists) do
local options = {
word = true, -- Match whole words only by default
cyclic = true, -- Cycle through the list (e.g., "sunday" to "monday")
preserve_case = true, -- Preserve case (e.g., "True" to "False")
}
local elements = list
if list.opts then
local custom_opts = list.opts
list.opts = nil
for k, v in pairs(custom_opts) do
options[k] = v
end
end
options.elements = list
table.insert(augends, augend.constant.new(options))
end
require("dial.config").augends:register_group({
default = augends,
})
end,
},
}
<details>
just realized that you have a working draft.
There's no point in providing warning I think, since if there's no lsp, we don't know where should that warning be provided.
As for intuitive values, I think if users can just use lsp.hover first to get the available values, and then do the inc/dec.
for the delay problem, it seems a bit hard to solve, my first idea is to use treesitter, for lua, enum values can only be in strings, can other languages, we only trigger when cursor is on a valid treesitter node type, just an idea.
@monaqa
I implemented this using treesitter instead of lsp for zig, feel free to grab whatever you need if this is applicable to other languages
local function get_enums()
local bufnr = vim.api.nvim_get_current_buf()
local ts = vim.treesitter
local parser = ts.get_parser(bufnr, 'zig')
if not parser then
return {}
end
local tree = parser:parse()[1]
if not tree then
return {}
end
local root = tree:root()
-- Your corrected query
local query = ts.query.parse(
'zig',
[[
(variable_declaration (identifier)@enumName (enum_declaration) )
((enum_declaration (container_field (identifier)@enumId)))
]]
)
local sets = {}
local current_set = nil
for id, node, _ in query:iter_captures(root, bufnr, 0, -1) do
local cap = query.captures[id]
local text = vim.treesitter.get_node_text(node, bufnr)
if cap == 'enumName' then
current_set = text
sets[current_set] = sets[current_set] or {}
elseif cap == 'enumId' and current_set then
table.insert(sets[current_set], text)
end
end
-- print(vim.inspect(sets))
local conf = require 'dial.config'
local augend = require 'dial.augend'
conf.augends:register_group(sets)
for i, v in pairs(sets) do
table.insert(
conf.augends.group['default'],
augend.constant.new {
elements = v,
word = true,
cyclic = true,
}
)
end
end
local function get_error_sets()
local bufnr = vim.api.nvim_get_current_buf()
local ts = vim.treesitter
local parser = ts.get_parser(bufnr, 'zig')
if not parser then
return {}
end
local tree = parser:parse()[1]
if not tree then
return {}
end
local root = tree:root()
local query = ts.query.parse(
'zig',
[[
(variable_declaration
(identifier) @errSet
(error_set_declaration
(identifier) @err_name))
]]
)
local sets = {}
local current_set = nil
for id, node, _ in query:iter_captures(root, bufnr, 0, -1) do
local cap = query.captures[id]
local text = vim.treesitter.get_node_text(node, bufnr)
if cap == 'errSet' then
current_set = text
sets[current_set] = sets[current_set] or {}
elseif cap == 'err_name' and current_set then
table.insert(sets[current_set], text)
end
end
local conf = require 'dial.config'
local augend = require 'dial.augend'
conf.augends:register_group(sets)
for i, v in pairs(sets) do
table.insert(
conf.augends.group['default'],
augend.constant.new {
elements = v,
word = true,
cyclic = true,
}
)
end
end
only works when the sets/enums are declared in the current file but hey