Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go Comment can be colorful #505

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
debounce the highlight requests
  • Loading branch information
ray-x committed Sep 24, 2024
commit 09dfc002c137141375281381ca6465f9fefe2cc3
1 change: 1 addition & 0 deletions lua/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ _GO_NVIM_CFG = {
highlight = false, -- set to true to disable
queries = nil, -- set to a table of queries to use for comment highlight see comment.lua
highlight_groups = nil,
highlight_debounce = 1000, -- 1000ms
},
}

Expand Down
16 changes: 13 additions & 3 deletions lua/go/comment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,15 @@ local function highlight_go_code_in_comments()
end
end

---@diagnostic disable-next-line: unused-local
local function toggle_go_comment_highlight()
_GO_NVIM_CFG.comment.highlight = not _GO_NVIM_CFG.comment.highlight
if _GO_NVIM_CFG.comment.highlight then
highlight_go_code_in_comments()
highlight = require('go.utils').throttle(
highlight_go_code_in_comments,
_GO_NVIM_CFG.comment.highlight_debounce
)
highlight()
else
vim.api.nvim_buf_clear_namespace(0, ns_id, 0, -1)
end
Expand All @@ -327,12 +332,17 @@ vim.api.nvim_create_user_command('ToggleGoCommentHighlight', function()
require('go.comment').toggle_highlight()
end, {})

local group = vim.api.nvim_create_augroup('gonvim__comment_hl', {})
vim.api.nvim_create_autocmd(
{ 'BufEnter', 'BufWritePost', 'WinEnter', 'TextChanged', 'InsertLeave' },
{ 'BufEnter', 'BufWritePost', 'WinEnter', 'InsertLeave', 'CursorHold' },
{
pattern = { '*.go' },
callback = function()
group = group,
callback = function(opts)
if _GO_NVIM_CFG.comment.highlight then
if opts.event == 'CursorHold' then
-- can be slow
end
require('go.comment').highlight()
end
end,
Expand Down
17 changes: 1 addition & 16 deletions lua/go/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -863,21 +863,6 @@ utils.throttle = function(func, duration)
return inner, timer
end

-- function M.debounce_trailing(ms, fn)
-- local timer = uv.new_timer()
-- return function(...)
-- local argv = { ... }
-- if timer:is_active() then
-- timer:stop()
-- return
-- end
-- timer:start(ms, 0, function()
-- timer:stop()
-- fn(unpack(argv))
-- end)
-- end
-- end
--
utils.debounce = function(func, ms)
local timer = uv.new_timer()
local function inner(...)
Expand All @@ -898,7 +883,7 @@ utils.extract_filepath = function(msg, pkg_path)
msg = msg or ''
-- util.log(msg)
--[[ or [[ findAllSubStr_test.go:234: Error inserting caseResult1: operation error DynamoDB: PutItem, exceeded maximum number of attempts]]
-- or 'path/path2/filename.go:50:11: Error invaild
-- or 'path/path2/filename.go:50:11: Error invalid
-- or /home/ray/go/src/github/sample/app/driver.go:342 +0x19e5
local ma = fn.matchlist(msg, [[\v\s*(\w+.+\.go):(\d+):]])
ma = ma or fn.matchlist(msg, [[\v\s*(\w+.+\.go):(\d+)]])
Expand Down