Skip to content

Commit

Permalink
feat(inlay): show virtual variable text
Browse files Browse the repository at this point in the history
  • Loading branch information
iamxiaojianzheng committed Sep 3, 2024
1 parent a18bdff commit c788803
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
9 changes: 7 additions & 2 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 Expand Up @@ -106,7 +111,7 @@ provide autocompletion and type checking.

### get_selected_env

::: warning :::
:::warning

This function is only available if you are using a `http-client.env.json` file.

Expand All @@ -117,7 +122,7 @@ returns the selected environment.

### set_selected_env

::: warning :::
:::warning

This function is only available if you are using a `http-client.env.json` file.

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
71 changes: 70 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,66 @@ 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(),
ENV_PARSER.get_env(),
DB.data.http_client_env_base["DEFAULT_HEADERS"]
)

for lineno, line in ipairs(lines) do
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

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",
callback = add_virtual_variable_text,
})
else
M.clear_virtual_variable_text()
end
end

return M

0 comments on commit c788803

Please sign in to comment.