Skip to content

Commit

Permalink
feat(cmdline): added cmdline support for :lua= and :=
Browse files Browse the repository at this point in the history
* feat(cmdline): Add format for := lua expr

* style: formatting

* feat: added support for multiple patterns for cmldine

---------

Co-authored-by: Folke Lemaitre <[email protected]>
  • Loading branch information
mikesmithgh and folke authored Mar 23, 2023
1 parent 92b058a commit acfa513
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
search_down = { kind = "search", pattern = "^/", icon = " ", lang = "regex" },
search_up = { kind = "search", pattern = "^%?", icon = " ", lang = "regex" },
filter = { pattern = "^:%s*!", icon = "$", lang = "bash" },
lua = { pattern = "^:%s*lua%s+", icon = "", lang = "lua" },
lua = { pattern = { "^:%s*lua%s+", "^:%s*lua%s*=%s*", "^:%s*=%s*" }, icon = "", lang = "lua" },
help = { pattern = "^:%s*he?l?p?%s+", icon = "" },
input = {}, -- Used by input()
-- lua = false, -- to disable a format, set to `false`
Expand Down
2 changes: 1 addition & 1 deletion lua/noice/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function M.defaults()
search_down = { kind = "search", pattern = "^/", icon = " ", lang = "regex" },
search_up = { kind = "search", pattern = "^%?", icon = " ", lang = "regex" },
filter = { pattern = "^:%s*!", icon = "$", lang = "bash" },
lua = { pattern = "^:%s*lua%s+", icon = "", lang = "lua" },
lua = { pattern = { "^:%s*lua%s+", "^:%s*lua%s*=%s*", "^:%s*=%s*" }, icon = "", lang = "lua" },
help = { pattern = "^:%s*he?l?p?%s+", icon = "" },
calculator = { pattern = "^=", icon = "", lang = "vimnormal" },
input = {}, -- Used by input()
Expand Down
39 changes: 24 additions & 15 deletions lua/noice/ui/cmdline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ M.active = nil

---@class CmdlineFormat
---@field kind string
---@field pattern? string
---@field pattern? string|string[]
---@field view string
---@field conceal? boolean
---@field icon? string
Expand Down Expand Up @@ -75,22 +75,31 @@ function Cmdline:get_format()
end
local line = self.state.firstc .. self:get()

---@type table<string, CmdlineFormat>
local formats = vim.tbl_values(vim.tbl_filter(function(f)
return f.pattern
end, Config.options.cmdline.format))
table.sort(formats, function(a, b)
return #a.pattern > #b.pattern
end)

for _, format in pairs(formats) do
local from, to = line:find(format.pattern)
-- if match and cmdline pos is visible
if from and self.state.pos >= to - 1 then
self.offset = format.conceal and to or 0
return format
---@type {offset:number, format: CmdlineFormat}[]
local ret = {}

for _, format in pairs(Config.options.cmdline.format) do
local patterns = type(format.pattern) == "table" and format.pattern or { format.pattern }
---@cast patterns string[]
for _, pattern in ipairs(patterns) do
local from, to = line:find(pattern)
-- if match and cmdline pos is visible
if from and self.state.pos >= to - 1 then
ret[#ret + 1] = {
offset = format.conceal and to or 0,
format = format,
}
end
end
end
table.sort(ret, function(a, b)
return a.offset > b.offset
end)
local format = ret[1]
if format then
self.offset = format.offset
return format.format
end
self.offset = 0
return {
kind = self.state.firstc,
Expand Down

0 comments on commit acfa513

Please sign in to comment.