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

chore: remove logs
  • Loading branch information
jellydn committed Oct 29, 2024
1 parent 5127e52 commit 8b8f0f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
31 changes: 21 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,22 @@ 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()
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 8b8f0f8

Please sign in to comment.