-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from smjonas/rewrite
Rewrite for better architecture, easier extensibility; add a general :Preview command
- Loading branch information
Showing
13 changed files
with
795 additions
and
475 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
local M = {} | ||
|
||
local differ = require("live-command.differ") | ||
local highlighter = require("live-command.highlighter") | ||
local logger = require("live-command.logger") | ||
|
||
---@type string | ||
local latest_cmd | ||
|
||
local running = false | ||
|
||
local refetch_lines = true | ||
|
||
---@tyle string[] | ||
local cached_lines | ||
|
||
---@type boolean | ||
local prev_lazyredraw | ||
|
||
local setup = function(bufnr) | ||
prev_lazyredraw = vim.o.lazyredraw | ||
vim.o.lazyredraw = true | ||
if refetch_lines then | ||
cached_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) | ||
refetch_lines = false | ||
else | ||
logger.trace("did not refetch for cmd " .. latest_cmd) | ||
end | ||
return cached_lines | ||
end | ||
|
||
M.teardown = function(do_refetch_lines) | ||
vim.o.lazyredraw = prev_lazyredraw | ||
refetch_lines = do_refetch_lines | ||
if vim.v.errmsg ~= "" then | ||
logger.error(("An error occurred in the preview function:\n%s"):format(vim.inspect(vim.v.errmsg))) | ||
end | ||
end | ||
|
||
local execute_command = function(cmd, bufnr) | ||
local old_buf_lines = setup(bufnr) | ||
local visible_line_range = { vim.fn.line("w0"), vim.fn.line("w$") } | ||
vim.api.nvim_buf_call(bufnr, function() | ||
vim.cmd(cmd) | ||
end) | ||
-- M.teardown(false) | ||
visible_line_range = { | ||
math.max(visible_line_range[1], vim.fn.line("w0")), | ||
math.max(visible_line_range[2], vim.fn.line("w$")), | ||
} | ||
local new_buf_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) | ||
return old_buf_lines, new_buf_lines, visible_line_range | ||
end | ||
|
||
---@param cmd string | ||
---@param opts livecmd.Config | ||
---@param bufnr number | ||
---@param update_buffer_cb fun(bufnr:number,updated_buffer_lines:string[],highlights:livecmd.Highlight[]?) | ||
M.submit_command = function(cmd, opts, bufnr, update_buffer_cb) | ||
if cmd == latest_cmd then | ||
return | ||
end | ||
latest_cmd = cmd | ||
if not running then | ||
running = true | ||
local old_buf_lines, new_buf_lines, line_range = execute_command(cmd, bufnr) | ||
if not opts.enable_highlighting then | ||
update_buffer_cb(bufnr, new_buf_lines, nil) | ||
running = false | ||
return | ||
end | ||
local diff = differ.get_diff(old_buf_lines, new_buf_lines) | ||
local highlights, updated_buf_lines = highlighter.get_highlights( | ||
diff, | ||
old_buf_lines, | ||
new_buf_lines, | ||
line_range, | ||
opts.inline_highlighting, | ||
opts.hl_groups.deletion ~= false | ||
) | ||
update_buffer_cb(bufnr, updated_buf_lines, highlights) | ||
running = false | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
local M = {} | ||
|
||
local user_command = require("live-command.user_command") | ||
|
||
---@class livecmd.Config.HlGroups | ||
---@field insertion string|false | ||
---@field deletion string|false | ||
---@field change string|false | ||
|
||
---@class livecmd.Config | ||
---@field command_name string? | ||
---@field enable_highlighting boolean? | ||
---@field inline_highlighting boolean? | ||
---@field hl_groups livecmd.Config.HlGroups? | ||
---@field commands table<string, livecmd.CommandSpec> | ||
|
||
local show_diagnostics_message = function(config) | ||
local message = [[ | ||
Version 2.0 of live-command.nvim has dropped support for the "args" and "range" keys in the command specification. | ||
The following commands in your configuration are affected: %s. Please remove or modify them. | ||
See the migration guide for more information: https://github.com/smjonas/live-command.nvim/blob/main/migrate_to_v2.md | ||
]] | ||
local affected_cmds = {} | ||
for cmd_name, cmd_spec in pairs(config.commands) do | ||
if cmd_spec.args ~= nil or cmd_spec.range ~= nil then | ||
table.insert(affected_cmds, '"' .. cmd_name .. '"') | ||
end | ||
end | ||
local cmd_names = table.concat(affected_cmds, ", ") | ||
local formatted_message = string.format(message, cmd_names) | ||
vim.notify(formatted_message, vim.log.levels.INFO) | ||
end | ||
|
||
---@param config livecmd.Config | ||
M.validate_config = function(config) | ||
vim.validate { | ||
command_name = { config.command_name, "string" }, | ||
enable_highlighting = { config.enable_highlighting, "boolean" }, | ||
inline_highlighting = { config.inline_highlighting, "boolean" }, | ||
hl_groups = { config.hl_groups, "table" }, | ||
commands = { config.commands, "table" }, | ||
} | ||
for cmd_name, cmd_spec in pairs(config.commands) do | ||
if cmd_spec.args ~= nil or cmd_spec.range ~= nil then | ||
vim.notify( | ||
'[live-command.nvim] Some unsupported features are used in your config. Please run ":LiveCommand diagnose" for details.', | ||
vim.log.levels.WARN | ||
) | ||
user_command.register_argument_handler("diagnose", function() | ||
show_diagnostics_message(config) | ||
end) | ||
end | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
local M = {} | ||
|
||
M.get_diff = function(old_lines, new_lines) | ||
return vim.diff(table.concat(old_lines, "\n"), table.concat(new_lines, "\n"), { | ||
result_type = "indices", | ||
}) | ||
end | ||
|
||
return M |
Oops, something went wrong.