Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ telescope.setup({
})
```

### File-type specific picker option

- LaTeX
- `use_section_number`: display section number with section title

## Usage

```viml
Expand Down
8 changes: 4 additions & 4 deletions lua/telescope/_extensions/heading.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ local function support_treesitter(ft)
return false
end

local function get_headings()
local function get_headings(opts)
local ft = filetype()
local mod_path =
string.format('telescope._extensions.heading.format.%s', ft)
Expand Down Expand Up @@ -77,13 +77,13 @@ local function get_headings()
end
end

return mod.get_headings(filepath, index, total)
return mod.get_headings(filepath, index, total, opts)
end

local function pick_headings(opts)
opts = vim.tbl_deep_extend('keep', opts or {}, heading_config.picker_opts)

local headings = get_headings()
local headings = get_headings(opts)
if headings == nil then
return
end
Expand All @@ -96,7 +96,7 @@ local function pick_headings(opts)
entry_maker = function(entry)
return {
value = entry.line,
display = entry.heading,
display = entry.display or entry.heading,
ordinal = entry.heading,
filename = entry.path,
lnum = entry.line,
Expand Down
56 changes: 54 additions & 2 deletions lua/telescope/_extensions/heading/format/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,49 @@ local function isDisallowedEnv(lnum, col)
return false
end

function Latex.get_headings(filepath, start, total)
local ArticleHeadingMaker = {}
ArticleHeadingMaker.__index = ArticleHeadingMaker

function ArticleHeadingMaker:new (matches)
local heading_counts = {}
for _, m in ipairs(matches) do
heading_counts[m] = 0
end
return setmetatable({
heading_counts = heading_counts,
}, ArticleHeadingMaker)
end

function ArticleHeadingMaker:update_counter(heading_name)
if heading_name == "section" then
self.heading_counts["subsection"] = 0
self.heading_counts["subsubsection"] = 0
elseif heading_name == "subsection" then
self.heading_counts["subsubsection"] = 0
end
self.heading_counts[heading_name] = self.heading_counts[heading_name] + 1
end

function ArticleHeadingMaker:make_display_name(heading_name, section_title)
local section_num = nil
if heading_name == "section" then
section_num = self.heading_counts["section"]
elseif heading_name == "subsection" then
section_num = self.heading_counts["section"] .. "."
.. self.heading_counts["subsection"]
elseif heading_name == "subsubsection" then
section_num = self.heading_counts["section"] .. "."
.. self.heading_counts["subsection"] .. "."
.. self.heading_counts["subsubsection"]
end
if section_title == nil then
return section_title
else
return section_num .. " " .. section_title
end
end

function Latex.get_headings(filepath, start, total, opts)
local headings = {}
local index = start
local matchtable = {}
Expand All @@ -36,10 +78,11 @@ function Latex.get_headings(filepath, start, total)
'paragraph',
'subparagraph',
}
local article_heading_maker = ArticleHeadingMaker:new(matches)

-- allows syntax of matchtable[headingname]
for _, m in ipairs(matches) do
matchtable[m] = ''
matchtable[m] = true
end

while index <= total do
Expand All @@ -57,8 +100,17 @@ function Latex.get_headings(filepath, start, total)
end

if not skip then
local pattern = "\\" .. headingname .. "{([^}]*)}"
local section_title = string.match(vim.trim(line), pattern)
local display_name = nil
if opts.use_section_number then
article_heading_maker:update_counter(headingname)
display_name = article_heading_maker:make_display_name(headingname, section_title
)
end
table.insert(headings, {
heading = vim.trim(line),
display = display_name,
line = index,
path = filepath,
})
Expand Down