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

feat(inlay): show virtual variable text #211

Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ the `DEFAULT_HEADERS` will be merged with the headers from the HTTP requests.
"DEFAULT_HEADERS": {
"Content-Type": "application/json",
"Accept": "application/json"
},
},
"dev": {
"API_KEY": "your-api-key"
Expand Down
5 changes: 5 additions & 0 deletions docs/docs/usage/public-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ the `body` and `headers` view of the last run request.

Persists across restarts.

### toggle_virtual_variable

`require('kulala').toggle_virtual_variable()` toggles between
the `variable` and `virtual text`.

### search

`require('kulala').search()` searches for all `.http` and `.rest` files
Expand Down
5 changes: 5 additions & 0 deletions lua/kulala/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local UI = require("kulala.ui")
local INLAY = require("kulala.inlay")
local SELECTOR = require("kulala.ui.selector")
local ENV = require("kulala.parser.env")
local GLOBALS = require("kulala.globals")
Expand Down Expand Up @@ -101,4 +102,8 @@ M.set_selected_env = function(env)
vim.g.kulala_selected_env = env
end

M.toggle_virtual_variable = function()
INLAY.toggle_virtual_variable()
end

return M
73 changes: 72 additions & 1 deletion lua/kulala/inlay/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
local VV_GROUP_NAME = "kulala_virtual_variable"
local VV_NS_NAME = "virtual_variable_text_namespace"
local NS = vim.api.nvim_create_namespace("kulala_inlay_hints")
local TS = require("kulala.parser.treesitter")
local DB = require("kulala.db")
local ENV_PARSER = require("kulala.parser.env")
local CONFIG = require("kulala.config")

local M = {}
local M = {
show_virtual_variable_text = CONFIG.get().show_virtual_variable_text,
}

M.get_current_line_number = function()
local linenr = vim.api.nvim_win_get_cursor(0)[1]
Expand Down Expand Up @@ -44,4 +51,68 @@ M.show = function(t, linenr)
})
end

-- Function to add virtual text to patterns like {{host}}
local add_virtual_variable_text = function()
if (M.show_virtual_variable_text or false) == false then
return
end

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

local ns_id = vim.api.nvim_create_namespace(VV_NS_NAME)
vim.api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1)

local pattern = "{{(.-)}}"

local variables = vim.tbl_extend(
"force",
TS.get_document_variables() or {},
ENV_PARSER.get_env() or {},
DB.data.http_client_env_base["DEFAULT_HEADERS"] or {}
)

for lineno, line in ipairs(lines) do
if line ~= nil and line:match("%S") ~= nil then
for start_idx, match in line:gmatch("()(" .. pattern .. ")()") do
local label = match:gsub("{{", ""):gsub("}}", "")
local value = variables[label]
if value ~= nil then
-- Calculate the position to place virtual text
local end_idx = start_idx + #match - 1

-- Add virtual text before the closing braces of the match
vim.api.nvim_buf_set_extmark(bufnr, ns_id, lineno - 1, end_idx - 2, {
virt_text = { { ":" .. value, "Comment" } }, -- You can change the highlight group "Comment" as needed
virt_text_pos = "inline",
})
end
end
end
end
end

M.clear_virtual_variable_text = function()
local ns_id = vim.api.nvim_create_namespace(VV_NS_NAME)
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1)
vim.api.nvim_clear_autocmds({ group = VV_GROUP_NAME })
end

M.toggle_virtual_variable = function()
M.show_virtual_variable_text = not M.show_virtual_variable_text

if M.show_virtual_variable_text then
add_virtual_variable_text()
vim.api.nvim_create_augroup(VV_GROUP_NAME, { clear = true })
vim.api.nvim_create_autocmd({ "BufEnter", "TextChanged", "TextChangedI" }, {
group = VV_GROUP_NAME,
pattern = "*.(http|rest)",
callback = add_virtual_variable_text,
})
else
M.clear_virtual_variable_text()
end
end

return M