Skip to content

Commit

Permalink
fix: Added option to change the indent size of list items
Browse files Browse the repository at this point in the history
WARNING: This does not affect tree-sitter. Tree-sitter only considers
list items with 2n number of spaces.

Ref: #96
  • Loading branch information
OXY2DEV committed Aug 10, 2024
1 parent 6d0bcce commit d54039e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lua/definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@
--- Enable/Disable custom list items
---@field enable boolean?
---
--- Indent size of list items
---@field indent_size number?
---
--- Number of characters to shift per level
---@field shift_width number?
---
Expand Down
3 changes: 3 additions & 0 deletions lua/markview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,9 @@ markview.configuration = {
},

list_items = {
indent_size = 2,
shift_width = 4,

marker_minus = {
add_padding = true,

Expand Down
6 changes: 6 additions & 0 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local parser = {};
-- local renderer = require("markview/renderer");

parser.cached_conf = {};

parser.fiter_lines = function (buffer, from, to)
local captured_lines = vim.api.nvim_buf_get_lines(buffer, from, to, false);
local filtered_lines = {};
Expand Down Expand Up @@ -709,6 +711,10 @@ parser.parse_range = function (buffer, config_table, from, to)
local root_parser = vim.treesitter.get_parser(buffer);
root_parser:parse(true);

if config_table then
parser.cached_conf = config_table;
end

-- Clear the previous contents
parser.parsed_content = {};

Expand Down
10 changes: 7 additions & 3 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ renderer.render_lists = function (buffer, content, config_table)
ls_conf = config_table.marker_dot or {};
end

-- Do not render list types with no configuraton
-- Do not render list types with no configuration
if not ls_conf then
return;
end
Expand All @@ -1518,10 +1518,12 @@ renderer.render_lists = function (buffer, content, config_table)
use_text = "";
end

local level = math.floor(before / (config_table.indent_size or 2)) + 1;

vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, line_num, 0, {
virt_text_pos = "inline",
virt_text = {
{ string.rep(" ", (math.floor(before / 2) + 1) * shift) },
{ string.rep(" ", level * shift) },
{ vim.trim(use_text), set_hl(ls_conf.hl) or "Special" }
},

Expand All @@ -1531,10 +1533,12 @@ renderer.render_lists = function (buffer, content, config_table)
elseif vim.list_contains(content.list_candidates, l) then
local line_len = vim.fn.strchars(line);

local level = math.floor(before / (config_table.indent_size or 2)) + 1;

vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, line_num, 0, {
virt_text_pos = "inline",
virt_text = {
{ string.rep(" ", (math.floor(before / 2) + 1) * shift) }
{ string.rep(" ", level * shift) }
},

end_col = line_len < before and line_len or before,
Expand Down
2 changes: 1 addition & 1 deletion markview.nvim.wiki

0 comments on commit d54039e

Please sign in to comment.