1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 11:20:06 +08:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Eric Wong
e5cf9021dc fix(cpicker): init data_dir 2024-08-01 00:15:22 +08:00
Eric Wong
d0d48b7f78 feat(cpicker): change cursor highlight 2024-08-01 00:06:33 +08:00
6 changed files with 126 additions and 4 deletions

View File

@ -48,7 +48,7 @@ let s:default_spaces = ['rgb', 'hsl']
function! SpaceVim#layers#tools#cpicker#plugins() abort
return [
\ [g:_spacevim_root_dir . 'bundle/cpicker.nvim', {'merged' : 0, 'loadconf' : 1, 'on_cmd' : ['Cpicker', 'CpickerCursorForeground', 'CpickerColorMix']}],
\ [g:_spacevim_root_dir . 'bundle/cpicker.nvim', {'merged' : 0, 'loadconf' : 1}],
\ ]
endfunction

View File

@ -16,5 +16,6 @@ context_filetype-variables context_filetype.jax /*context_filetype-variables*
context_filetype.txt context_filetype.jax /*context_filetype.txt*
g:context_filetype#filetypes context_filetype.jax /*g:context_filetype#filetypes*
g:context_filetype#ignore_composite_filetypes context_filetype.jax /*g:context_filetype#ignore_composite_filetypes*
g:context_filetype#ignore_patterns context_filetype.jax /*g:context_filetype#ignore_patterns*
g:context_filetype#same_filetypes context_filetype.jax /*g:context_filetype#same_filetypes*
g:context_filetype#search_offset context_filetype.jax /*g:context_filetype#search_offset*

View File

@ -27,6 +27,8 @@ Plug 'wsdjeg/cpicker.nvim'
1. `:Cpicker`: open the color converter
2. `:CpickerCursorForeground`: open the color converter with cursor highlight
3. `:CpickerColorMix`: open the color mixer
4. `:CpickerCursorChangeHighlight`: change the highlight of cursor word
5. `:CpickerClearColorPatch`: clear colorscheme patch
## Feedback

View File

@ -21,6 +21,8 @@ local enabled_formats = {}
local increase_keys = {}
local reduce_keys = {}
local color_code_regex = {}
local cursor_hl
local cursor_hl_name
local function update_buf_text()
local rst = {}
@ -112,6 +114,11 @@ local function increase()
end
end
update_buf_text()
if cursor_hl and cursor_hl_name then
cursor_hl.fg = color_hi
vim.api.nvim_set_hl(0, cursor_hl_name, cursor_hl)
util.update_color_patch(cursor_hl_name, cursor_hl)
end
end
local function reduce()
@ -126,6 +133,11 @@ local function reduce()
end
end
update_buf_text()
if cursor_hl and cursor_hl_name then
cursor_hl.fg = color_hi
vim.api.nvim_set_hl(0, cursor_hl_name, cursor_hl)
util.update_color_patch(cursor_hl_name, cursor_hl)
end
end
M.picker = function(formats)
@ -148,9 +160,6 @@ M.picker = function(formats)
vim.api.nvim_set_option_value('bufhidden', 'wipe', {
buf = bufnr,
})
vim.api.nvim_set_option_value('number', false, {
buf = bufnr,
})
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'l', '', {
callback = increase,
})
@ -201,4 +210,9 @@ M.set_default_color = function(hex)
color_hi = hex
end
function M.change_cursor_highlight(name, hl, formats)
cursor_hl_name = name
cursor_hl = hl
M.picker(formats)
end
return M

View File

@ -125,4 +125,83 @@ function M.set_default_color(formats)
end
end
function M.get_cursor_hl()
local inspect = vim.inspect_pos()
local name, hl
if #inspect.semantic_tokens > 0 then
local token, priority = {}, 0
for _, semantic_token in ipairs(inspect.semantic_tokens) do
if semantic_token.opts.priority > priority then
priority = semantic_token.opts.priority
token = semantic_token
end
end
if token then
name = token.opts.hl_group_link
hl = vim.api.nvim_get_hl(0, { name = token.opts.hl_group_link })
end
elseif #inspect.treesitter > 0 then
for i = #inspect.treesitter, 1, -1 do
name = inspect.treesitter[i].hl_group_link
hl = vim.api.nvim_get_hl(0, { name = name })
if hl.fg then
break
end
end
else
name = vim.fn.synIDattr(vim.fn.synID(vim.fn.line('.'), vim.fn.col('.'), 1), 'name', 'gui')
hl = get_color(name)
end
return name, hl
end
function M.read_color_patch()
if
vim.fn.filereadable(vim.fn.expand(vim.g.spacevim_data_dir .. 'SpaceVim/cpicker_color_patch'))
== 1
then
local _his = vim.fn.json_decode( vim.fn.join( vim.fn.readfile(vim.fn.expand(vim.g.spacevim_data_dir .. 'SpaceVim/cpicker_color_patch'), ''), '' ) )
if type(_his) == 'table' then
return _his or {}
else
return {}
end
else
return {}
end
end
function M.update_color_patch(name, hl)
if vim.fn.isdirectory(vim.fn.expand(vim.g.spacevim_data_dir .. 'SpaceVim')) == 0 then
vim.fn.mkdir(vim.fn.expand(vim.g.spacevim_data_dir .. 'SpaceVim'))
end
local patch = M.read_color_patch()
if not patch[vim.g.colors_name] then
patch[vim.g.colors_name] = {}
end
patch[vim.g.colors_name][name] = hl
vim.fn.writefile(
{ vim.fn.json_encode(patch) },
vim.fn.expand(vim.g.spacevim_data_dir .. 'SpaceVim/cpicker_color_patch')
)
end
function M.patch_color(c)
local patch = M.read_color_patch()
if patch[c] then
for name, hl in pairs(patch[c]) do
vim.api.nvim_set_hl(0, name, hl)
end
end
end
function M.clear_color_patch()
local patch = M.read_color_patch()
patch[vim.g.colors_name] = nil
vim.fn.writefile(
{ vim.fn.json_encode(patch) },
vim.fn.expand(vim.g.spacevim_data_dir .. 'SpaceVim/cpicker_color_patch')
)
vim.cmd('colorscheme ' .. vim.g.colors_name)
end
return M

View File

@ -6,6 +6,11 @@
-- License: GPLv3
--=============================================================================
-- detached cpicker need this var
if not vim.g.spacevim_data_dir then
vim.g.spacevim_data_dir = '~/.cache/'
end
if vim.api.nvim_create_user_command then
local function complete()
return { 'rgb', 'hsl', 'hsv', 'cmyk', 'hwb', 'linear', 'lab' }
@ -20,4 +25,25 @@ if vim.api.nvim_create_user_command then
vim.api.nvim_create_user_command('CpickerColorMix', function(opt)
require('cpicker.mix').color_mix(unpack(opt.fargs))
end, { nargs = '*', complete = complete })
vim.api.nvim_create_user_command('CpickerCursorChangeHighlight', function(opt)
local name, hl = require('cpicker.util').get_cursor_hl()
require('cpicker.util').set_default_color(opt.fargs)
require('cpicker').change_cursor_highlight(name, hl, opt.fargs)
end, { nargs = '*', complete = complete })
-- if vim.g.cpicker_enable_color_patch then
local group = vim.api.nvim_create_augroup('cpicker', { clear = true })
vim.api.nvim_create_autocmd({ 'ColorScheme' }, {
group = group,
pattern = { '*' },
callback = function(ev)
require('cpicker.util').patch_color(ev.match)
end,
})
require('cpicker.util').patch_color(vim.g.colors_name)
vim.api.nvim_create_user_command('CpickerClearColorPatch', function(opt)
require('cpicker.util').clear_color_patch()
end, { nargs = '*', complete = complete })
-- end
end