Skip to content

Commit

Permalink
feat: implement first config option
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Aug 31, 2024
1 parent 3d93240 commit d0ef1ae
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 33 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
- **WIP**: Show hints highlighting the effect of `nindent` and `indent` functions
![demo for indent hints](https://raw.githubusercontent.com/qvalentin/helm-ls.nvim/main/doc/gifs/indent-hints.gif)


## Installing

### Using lazy
Expand All @@ -20,9 +19,37 @@
{
"qvalentin/helm-ls.nvim",
ft = "helm",
opts = {},
opts = {
-- leave emtpy or see below
},
}
```

If you are not using lazy make sure to call `require("helm-ls").setup()` in your lua config.

### Requirments

The plugin requires helm-ls and the helm tree-sitter grammar.

```
:TSInstall helm
```

## Configuration

Default config:

```lua
{
conceal_templates = {
-- enable the replacement of templates with virtual text of their current values
enabled = true, -- this might change to false in the future
},
indent_hints = {
-- enable hints for indent and nindent functions
enabled = true,
-- show the hints only for the line the cursor is on
only_for_current_line = true,
},
}
```
File renamed without changes.
73 changes: 56 additions & 17 deletions lua/helm-ls.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-- main module file

---@class Config
---@field conceal_templates table
---@field indent_hints table
---@field opt string Your config option
local config = {
conceal_templates = {
Expand All @@ -18,38 +20,75 @@ local M = {}
---@type Config
M.config = config

-- Function to handle indent hints
local function handle_indent_hints(indent_hints)
if M.config.indent_hints.enabled then
indent_hints.add_indent_hints()
end
end

-- Function to handle conceal templates
local function handle_conceal_templates(conceal)
if M.config.conceal_templates.enabled then
local clients = vim.lsp.get_clients({ bufnr = vim.api.nvim_get_current_buf(), name = "helm_ls" })
if vim.tbl_isempty(clients) then
return
end
conceal.conceal_templates_with_hover()
conceal.clear_extmark_if_cursor_on_line()
end
end

---@param args Config?
-- you can define your setup function here. Usually configurations can be merged, accepting outside params and
-- you can also put some validation here for those.
M.setup = function(args)
-- Validate and merge configuration
if args then
if args.conceal_templates and type(args.conceal_templates) ~= "table" then
error("Invalid type for conceal_templates")
end
if args.indent_hints and type(args.indent_hints) ~= "table" then
error("Invalid type for indent_hints")
end
end

M.config = vim.tbl_deep_extend("force", M.config, args or {})
--
-- Create the autocommand group "ConcealWithLsp" if it doesn't already exist

local conceal = nil
local indent_hints = nil

if M.config.conceal_templates.enabled then
conceal = require("helm-ls.conceal")
conceal.set_config(M.config.conceal_templates)
end

if M.config.indent_hints.enabled then
indent_hints = require("helm-ls.indent-hints")
indent_hints.set_config(M.config.indent_hints)
end

-- Create the autocommand group "ConcealWithLsp"
local group_id = vim.api.nvim_create_augroup("ConcealWithLsp", { clear = true })

-- Define file patterns as constants
local file_patterns = { "*.yaml", "*.yml", "*.helm", "*.tpl" }

-- Define the autocommand
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
group = group_id,
pattern = { "*.yaml", "*.yml", "*.helm", "*.tpl" },
pattern = file_patterns,
callback = function()
if vim.bo.filetype ~= "helm" then
return
end
if M.config.indent_hints then
local indent_hints = require("helm-ls.indent-hints")

indent_hints.add_indent_hints()
if indent_hints then
handle_indent_hints(indent_hints)
end
if M.config.conceal_templates then
local conceal = require("helm-ls.conceal")
local clients = vim.lsp.get_clients({ bufnr = vim.api.nvim_get_current_buf(), name = "helm_ls" })
if vim.tbl_isempty(clients) then
return
end
conceal.conceal_templates_with_hover()
conceal.clear_extmark_if_cursor_on_line()
if conceal then
handle_conceal_templates(conceal)
end
end,
})
end


return M
6 changes: 6 additions & 0 deletions lua/helm-ls/conceal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ local clear_extmark_if_cursor_on_line = function()
api.nvim_buf_clear_namespace(bufnr, ns_id, cursor_row - 1, cursor_row)
end


local function set_config(config)
M.config = config
end

M.conceal_templates_with_hover = debounce_conceal_templates_with_hover
M.clear_extmark_if_cursor_on_line = clear_extmark_if_cursor_on_line
M.set_config = set_config
return M
36 changes: 22 additions & 14 deletions lua/helm-ls/indent-hints.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
---@class CustomModule
local M = {}

local util = require("helm-ls.utils")

local ns_id = vim.api.nvim_create_namespace("helm-ls-indent-hints") -- Create a unique namespace

-- Debounce timer
local debounce_timer = nil

-- Function to replace leading indentation with virtual underlines
local function replace_indentation_with_underline(bufnr, line, indent_count)
local function replace_indentation_with_marker(bufnr, line, indent_count)
local line_content = vim.api.nvim_buf_get_lines(bufnr, line, line + 1, false)[1]

if line_content == nil then
print("Line content is nil")
return
end

if #line_content == 0 then
-- Add a virtual underline for the full width of the indent_count
local underline = string.rep(".", indent_count)
local marker_string = string.rep(".", indent_count)

vim.api.nvim_buf_set_extmark(bufnr, ns_id, line, 0, {
virt_text = { { underline, "Underlined" } },
virt_text = { { marker_string, "Underlined" } },
virt_text_pos = "overlay",
hl_mode = "combine",
virt_text_hide = true,
Expand All @@ -46,11 +42,9 @@ local function replace_indentation_with_underline(bufnr, line, indent_count)
end

local show_hint = function(row, indent_count)
vim.api.nvim_buf_clear_namespace(0, ns_id, row, row + 1)
vim.api.nvim_buf_add_highlight(0, ns_id, "Underlined", row, 0, indent_count)

-- Replace leading indentation with virtual underlines
replace_indentation_with_underline(0, row, indent_count)
replace_indentation_with_marker(0, row, indent_count)
end

local add_indent_hints = function()
Expand All @@ -67,10 +61,18 @@ local add_indent_hints = function()
]]
)

-- Get the range of visible lines in the current window
local start_line = vim.fn.line("w0") - 1
local end_line = vim.fn.line("w$") - 1
local start_line, end_line
if M.config.only_for_current_line then
start_line = vim.fn.line(".") - 1
end_line = vim.fn.line(".")
else
-- Get the range of visible lines in the current window
start_line = vim.fn.line("w0") - 1
end_line = vim.fn.line("w$") - 1
end


vim.api.nvim_buf_clear_namespace(0, ns_id, start_line - 1, end_line + 2)
for _, match in query:iter_matches(root, bufnr, start_line, end_line) do
local new_line_indent = false
for id, node in pairs(match) do
Expand Down Expand Up @@ -99,8 +101,14 @@ local function debounce_add_indent_hints()
end
debounce_timer = vim.defer_fn(function()
add_indent_hints()
end, 100) -- 200ms debounce time
end, 100) -- 100ms debounce time
end

local function set_config(config)
print("Setting config", config.only_for_current_line)
M.config = config
end

M.add_indent_hints = debounce_add_indent_hints
M.set_config = set_config
return M

0 comments on commit d0ef1ae

Please sign in to comment.