aboutsummaryrefslogtreecommitdiff
path: root/lua/colorizer/utils.lua
diff options
context:
space:
mode:
authorAkianonymus <anonymus.aki@gmail.com>2022-09-09 23:55:43 +0530
committerAki <anonymus.aki@gmail.com>2022-09-14 11:47:08 +0530
commitf986d9a0ea1feff1388b5c7d297ec3faa19036a7 (patch)
tree501de10a0709883b9facfb26960f59a181b58d01 /lua/colorizer/utils.lua
parentbuffer_utils: Do not error when lsp info cannot be fetched (diff)
[FEATURE] Add support for sass variables
Import support import using use and import keyword multiple imports in single line multiple imports in multiple lines ( using commas ) recursive imports watch imports for changes and automatically rehighlight buffer buffer variables recursive variables support multiple variables in single line parse imported files only when they are changed Loosely based on https://github.com/norcalli/nvim-colorizer.lua/pull/22/files
Diffstat (limited to 'lua/colorizer/utils.lua')
-rw-r--r--lua/colorizer/utils.lua66
1 files changed, 66 insertions, 0 deletions
diff --git a/lua/colorizer/utils.lua b/lua/colorizer/utils.lua
index bfffb60..9dad9d8 100644
--- a/lua/colorizer/utils.lua
+++ b/lua/colorizer/utils.lua
@@ -3,6 +3,8 @@
local bit, ffi = require "bit", require "ffi"
local band, bor, rshift, lshift = bit.band, bit.bor, bit.rshift, bit.lshift
+local uv = vim.loop
+
-- -- TODO use rgb as the return value from the matcher functions
-- -- instead of the rgb_hex. Can be the highlight key as well
-- -- when you shift it left 8 bits. Use the lower 8 bits for
@@ -101,6 +103,68 @@ local function percent_or_hex(v)
return x
end
+--- Get last modified time of a file
+---@param path string: file path
+---@return number|nil: modified time
+local function get_last_modified(path)
+ local fd = uv.fs_open(path, "r", 438)
+ if not fd then
+ return
+ end
+
+ local stat = uv.fs_fstat(fd)
+ uv.fs_close(fd)
+ if stat then
+ return stat.mtime.nsec
+ else
+ return
+ end
+end
+
+--- Watch a file for changes and execute callback
+---@param path string: File path
+---@param callback function: Callback to execute
+---@param ... array: params for callback
+---@return function|nil
+local function watch_file(path, callback, ...)
+ if not path or type(callback) ~= "function" then
+ return
+ end
+
+ local fullpath = uv.fs_realpath(path)
+ if not fullpath then
+ return
+ end
+
+ local start
+ local args = { ... }
+
+ local handle = uv.new_fs_event()
+ local function on_change(err, filename, _)
+ -- Do work...
+ callback(filename, unpack(args))
+ -- Debounce: stop/start.
+ handle:stop()
+ if not err or not get_last_modified(filename) then
+ start()
+ end
+ end
+
+ function start()
+ uv.fs_event_start(
+ handle,
+ fullpath,
+ {},
+ vim.schedule_wrap(function(...)
+ on_change(...)
+ end)
+ )
+ end
+
+ start()
+ return handle
+end
+
--- @export
return {
byte_is_alphanumeric = byte_is_alphanumeric,
@@ -108,4 +172,6 @@ return {
merge = merge,
parse_hex = parse_hex,
percent_or_hex = percent_or_hex,
+ get_last_modified = get_last_modified,
+ watch_file = watch_file,
}