Skip to content

Commit

Permalink
fix(#13): misaligned code sections
Browse files Browse the repository at this point in the history
The hover keeps the indentation from the native hover call.

Signed-off-by: Filip Lobpreis <[email protected]>
  • Loading branch information
Filip Lobpreis committed Apr 14, 2024
1 parent ac5687a commit 4c6e6f2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lua/pretty_hover/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@ local M = {}
M.winnr = 0
M.bufnr = 0

--- Returns an empty string of size of the indentation of the given string.
---@param str string String on which should the identation be calculated.
---@return string Indentation of the given string.
local function getIndentation(str)
local indentation = ""
for i = 1, #str do
if str:sub(i, i) == " " then
indentation = indentation .. " "
else
break
end
end
return indentation
end

--- Splits a string into a table of strings.
---@param toSplit string String to be split.
---@param separator string|nil The separator. If not defined, the separator is set to "%S+".
---@return table Table of strings split by the separator.
function M.split(toSplit, separator)
local indentation = nil
if separator == nil then
indentation = getIndentation(toSplit)
separator = "%S+"
end

Expand All @@ -21,6 +38,10 @@ function M.split(toSplit, separator)
end

local chunks = {}
if indentation ~= nil and indentation:len() > 0 then
table.insert(chunks, indentation)
end

for substring in toSplit:gmatch(separator) do
table.insert(chunks, substring)
end
Expand Down

0 comments on commit 4c6e6f2

Please sign in to comment.