Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit 58239a5

Browse files
fix: preview splitting (#59)
* improved splitting: sort entries fixes #57 * improved splitting: review comments
1 parent 7414d20 commit 58239a5

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

lua/nvim-devdocs/build.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local transpiler = require("nvim-devdocs.transpiler")
77
local function build_docs(entry, index, docs)
88
local alias = entry.slug:gsub("~", "-")
99
local current_doc_dir = DOCS_DIR:joinpath(alias)
10+
local sort_lookup = {}
11+
local sort_lookup_last_index = 1
1012

1113
notify.log("Building " .. alias .. " documentation...")
1214

@@ -34,15 +36,22 @@ local function build_docs(entry, index, docs)
3436
local markdown, md_sections = transpiler.html_to_md(doc, sections)
3537
local file_path = current_doc_dir:joinpath(tostring(count) .. ".md")
3638

37-
for id, md_path in pairs(md_sections) do
38-
path_map[key .. "#" .. id] = count .. "," .. md_path
39+
for _, section in ipairs(md_sections) do
40+
path_map[key .. "#" .. section.id] = count .. "," .. section.md_path
41+
sort_lookup[key .. "#" .. section.id] = sort_lookup_last_index
42+
sort_lookup_last_index = sort_lookup_last_index + 1
3943
end
4044

4145
path_map[key] = tostring(count)
4246
file_path:write(markdown, "w")
4347
count = count + 1
4448
end
4549

50+
table.sort(index.entries, function(a, b)
51+
local index_a = sort_lookup[a.path] or -1
52+
local index_b = sort_lookup[b.path] or -1
53+
return index_a < index_b
54+
end)
4655
for i, index_entry in ipairs(index.entries) do
4756
local main = vim.split(index_entry.path, "#")[1]
4857
index.entries[i].link = index.entries[i].path

lua/nvim-devdocs/operations.lua

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,17 @@ M.get_all_entries = function()
174174
local index_parsed = vim.fn.json_decode(INDEX_PATH:read())
175175

176176
for alias, index in pairs(index_parsed) do
177-
for _, doc_entry in ipairs(index.entries) do
177+
local entries_count = #index.entries
178+
for idx, doc_entry in ipairs(index.entries) do
179+
local next_path = nil
180+
if idx < entries_count then
181+
next_path = index.entries[idx+1].path
182+
end
178183
local entry = {
179184
name = string.format("[%s] %s", alias, doc_entry.name),
180185
alias = alias,
181186
path = doc_entry.path,
187+
next_path = next_path,
182188
link = doc_entry.link,
183189
}
184190
table.insert(entries, entry)
@@ -197,8 +203,12 @@ M.read_entry = function(entry, callback)
197203

198204
file_path:_read_async(vim.schedule_wrap(function(content)
199205
local pattern = splited_path[2]
206+
local next_pattern = nil
207+
if entry.next_path ~= nil then
208+
next_pattern = vim.split(entry.next_path, ",")[2]
209+
end
200210
local lines = vim.split(content, "\n")
201-
local filtered_lines = M.filter_doc(lines, pattern)
211+
local filtered_lines = M.filter_doc(lines, pattern, next_pattern)
202212

203213
callback(filtered_lines)
204214
end))
@@ -207,8 +217,9 @@ end
207217
---if we have a pattern to search for, only consider lines after the pattern
208218
---@param lines string[]
209219
---@param pattern? string
220+
---@param next_pattern? string
210221
---@return string[]
211-
M.filter_doc = function(lines, pattern)
222+
M.filter_doc = function(lines, pattern, next_pattern)
212223
if not pattern then return lines end
213224

214225
-- https://stackoverflow.com/a/34953646/516188
@@ -218,15 +229,15 @@ M.filter_doc = function(lines, pattern)
218229
local found = false
219230
local pattern_lines = vim.split(pattern, "\n")
220231
local search_pattern = create_pattern(pattern_lines[1]) -- only search the first line
221-
local split = vim.split(pattern, " ")
222-
local header = split[1]
223-
local top_header = header and header:sub(1, #header - 1)
232+
local next_search_pattern = nil
233+
if next_pattern then
234+
local next_pattern_lines = vim.split(next_pattern, "\n")
235+
next_search_pattern = create_pattern(next_pattern_lines[1]) -- only search the first line
236+
end
224237

225238
for _, line in ipairs(lines) do
226-
if found then
227-
local line_split = vim.split(line, " ")
228-
local first = line_split[1]
229-
if first == header or first == top_header then break end
239+
if found and next_search_pattern then
240+
if line:match(next_search_pattern) then break end
230241
end
231242
if line:match(search_pattern) then found = true end
232243
if found then table.insert(filtered_lines, line) end

lua/nvim-devdocs/transpiler.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function transpiler:eval(node)
323323
local id = attributes.id
324324

325325
if id and self.section_map and vim.tbl_contains(self.section_map, id) then
326-
self.sections[id] = vim.trim(result)
326+
table.insert(self.sections, {id = id, md_path = vim.trim(result)})
327327
end
328328

329329
return result

0 commit comments

Comments
 (0)