From f986d9a0ea1feff1388b5c7d297ec3faa19036a7 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Fri, 9 Sep 2022 23:55:43 +0530 Subject: [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 --- lua/colorizer/utils.lua | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'lua/colorizer/utils.lua') 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, } -- cgit v1.2.3-70-g09d2