Skip to content

Commit

Permalink
feat(codelens): add setup function to handle buffer events for Hurl e…
Browse files Browse the repository at this point in the history
…ntries
  • Loading branch information
jellydn committed Oct 29, 2024
1 parent 5127e52 commit 94955bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
33 changes: 23 additions & 10 deletions lua/hurl/codelens.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ local M = {}
-- Add virtual text for Hurl entries by finding HTTP verbs
function M.add_virtual_text_for_hurl_entries()
local bufnr = vim.api.nvim_get_current_buf()
local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')

-- Only add virtual text if the filetype is 'hurl'
if filetype ~= 'hurl' then
return
end

local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

-- Create a dedicated namespace for Hurl entry markers
local ns_id = vim.api.nvim_create_namespace('hurl_entries')

-- Clear existing virtual text before adding new ones
vim.api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1)

-- Define all supported HTTP methods
local http_methods = {
'GET',
Expand All @@ -26,9 +22,6 @@ function M.add_virtual_text_for_hurl_entries()
'OPTIONS',
}

-- Clear existing virtual text
vim.api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1)

local entry_number = 1
for i, line in ipairs(lines) do
-- Match any HTTP method, ignoring preceding whitespace and comments
Expand All @@ -42,4 +35,24 @@ function M.add_virtual_text_for_hurl_entries()
end
end

-- Setup function to attach buffer autocmd
function M.setup()
local group = vim.api.nvim_create_augroup('HurlEntryMarkers', { clear = true })

-- Handle buffer events
vim.api.nvim_create_autocmd({
'BufEnter',
'TextChanged',
'InsertLeave',
}, {
group = group,
pattern = '*.hurl',
callback = function(ev)
-- Debug log
vim.notify(string.format('Event %s triggered for file %s', ev.event, ev.file))
M.add_virtual_text_for_hurl_entries()
end,
})
end

return M
2 changes: 1 addition & 1 deletion lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ end

function M.setup()
-- Show virtual text for Hurl entries
codelens.add_virtual_text_for_hurl_entries()
codelens.setup()

-- Run request for a range of lines or the entire file
utils.create_cmd('HurlRunner', function(opts)
Expand Down

0 comments on commit 94955bd

Please sign in to comment.