From 5efafb539ab0b09fea582334d094b3271fd96a3c Mon Sep 17 00:00:00 2001 From: Lyz Date: Thu, 8 Feb 2024 01:09:12 +0100 Subject: [PATCH] style: format code --- lua/telescope-orgmode/utils.lua | 119 +++++++------- lua/telescope/_extensions/orgmode/init.lua | 12 +- .../_extensions/orgmode/refile_heading.lua | 153 +++++++++--------- .../_extensions/orgmode/search_headings.lua | 24 +-- 4 files changed, 154 insertions(+), 154 deletions(-) diff --git a/lua/telescope-orgmode/utils.lua b/lua/telescope-orgmode/utils.lua index e014db4..43af337 100644 --- a/lua/telescope-orgmode/utils.lua +++ b/lua/telescope-orgmode/utils.lua @@ -1,82 +1,79 @@ local entry_display = require("telescope.pickers.entry_display") -local orgmode = require('orgmode.api') +local orgmode = require("orgmode.api") local utils = {} utils.get_entries = function(opts) + local file_results = vim.tbl_map(function(file) + return { file = file, filename = file.filename } + end, orgmode.load()) - local file_results = vim.tbl_map(function(file) - return { file = file, filename = file.filename } - end, orgmode.load()) + if not opts.archived then + file_results = vim.tbl_filter(function(entry) + return not entry.file.is_archive_file + end, file_results) + end - if not opts.archived then - file_results = vim.tbl_filter(function(entry) - return not entry.file.is_archive_file - end, file_results) - end + if opts.max_depth == 0 then + return file_results + end - if opts.max_depth == 0 then - return file_results - end + local results = {} + for _, file_entry in ipairs(file_results) do + for _, headline in ipairs(file_entry.file.headlines) do + local allowed_depth = opts.max_depth == nil or headline.level <= opts.max_depth + local allowed_archive = opts.archived or not headline.is_archived + if allowed_depth and allowed_archive then + local entry = { + file = file_entry.file, + filename = file_entry.filename, + headline = headline, + } + table.insert(results, entry) + end + end + end - local results = {} - for _, file_entry in ipairs(file_results) do - for _, headline in ipairs(file_entry.file.headlines) do - - local allowed_depth = opts.max_depth == nil or headline.level <= opts.max_depth - local allowed_archive = opts.archived or not headline.is_archived - if allowed_depth and allowed_archive then - local entry = { - file = file_entry.file, - filename = file_entry.filename, - headline = headline - } - table.insert(results, entry) - end - end - end - - return results + return results end utils.make_entry = function(opts) + local displayer = entry_display.create({ + separator = " ", + items = { + { width = vim.F.if_nil(opts.location_width, 20) }, + { remaining = true }, + }, + }) - local displayer = entry_display.create({ - separator = ' ', - items = { - { width = vim.F.if_nil(opts.location_width, 20) }, - { remaining = true } - } - }) + local function make_display(entry) + return displayer({ entry.location, entry.line }) + end - local function make_display(entry) - return displayer({ entry.location, entry.line }) - end + return function(entry) + local headline = entry.headline - return function(entry) - local headline = entry.headline + local lnum = nil + local location = vim.fn.fnamemodify(entry.filename, ":t") + local line = "" - local lnum = nil - local location = vim.fn.fnamemodify(entry.filename, ':t') - local line = "" + if headline then + lnum = headline.position.start_line + location = string.format("%s:%i", location, lnum) + line = string.format("%s %s", string.rep("*", headline.level), headline.title) + end - if headline then - lnum = headline.position.start_line - location = string.format('%s:%i', location, lnum) - line = string.format('%s %s', string.rep('*', headline.level), headline.title) - end - - return { - value = entry, - ordinal = location .. ' ' .. line, - filename = entry.filename, - lnum = lnum, - display = make_display, - location = location, - line = line - } - end + return { + value = entry, + ordinal = location .. " " .. line, + filename = entry.filename, + lnum = lnum, + display = make_display, + location = location, + line = line, + } + end end return utils diff --git a/lua/telescope/_extensions/orgmode/init.lua b/lua/telescope/_extensions/orgmode/init.lua index 9ae10f1..1526e2b 100644 --- a/lua/telescope/_extensions/orgmode/init.lua +++ b/lua/telescope/_extensions/orgmode/init.lua @@ -2,9 +2,9 @@ -- public orgmode api -- TODO: add highlight groups -return require("telescope").register_extension { - exports = { - search_headings = require("telescope._extensions.orgmode.search_headings"), - refile_heading = require("telescope._extensions.orgmode.refile_heading") - }, -} +return require("telescope").register_extension({ + exports = { + search_headings = require("telescope._extensions.orgmode.search_headings"), + refile_heading = require("telescope._extensions.orgmode.refile_heading"), + }, +}) diff --git a/lua/telescope/_extensions/orgmode/refile_heading.lua b/lua/telescope/_extensions/orgmode/refile_heading.lua index 09bc641..ad74770 100644 --- a/lua/telescope/_extensions/orgmode/refile_heading.lua +++ b/lua/telescope/_extensions/orgmode/refile_heading.lua @@ -6,94 +6,95 @@ local actions = require("telescope.actions") local action_state = require("telescope.actions.state") local state = require("telescope.state") -local utils = require('telescope-orgmode.utils') +local utils = require("telescope-orgmode.utils") -local orgmode = require('orgmode') -local Files = require('orgmode.parser.files') -local Capture = require('orgmode.capture') -local Range = require('orgmode.parser.range') +local orgmode = require("orgmode") +local Files = require("orgmode.parser.files") +local Capture = require("orgmode.capture") +local Range = require("orgmode.parser.range") return function(opts) - opts = opts or {} + opts = opts or {} - -- TODO: this should be included in return from Files.get_current_file - local is_capture = vim.F.npcall(vim.api.nvim_buf_get_var, 0, 'org_capture') + -- TODO: this should be included in return from Files.get_current_file + local is_capture = vim.F.npcall(vim.api.nvim_buf_get_var, 0, "org_capture") - local src_file = Files.get_current_file() - -- In capture, refile top level heading even if cursor closer to a subheading - local src_item = is_capture and src_file:get_headlines()[1] or src_file:get_closest_headline() - local src_lines = src_file:get_headline_lines(src_item) + local src_file = Files.get_current_file() + -- In capture, refile top level heading even if cursor closer to a subheading + local src_item = is_capture and src_file:get_headlines()[1] or src_file:get_closest_headline() + local src_lines = src_file:get_headline_lines(src_item) - local function refile(prompt_bufnr) - local entry = action_state.get_selected_entry() - actions.close(prompt_bufnr) + local function refile(prompt_bufnr) + local entry = action_state.get_selected_entry() + actions.close(prompt_bufnr) - local dst_file = entry.value.file - local dst_headline = entry.value.headline - if dst_headline then - -- NOTE: adapted from Capture:refile_to_headline - local is_same_file = dst_file.filename == src_item.root.filename - src_lines = Capture:_adapt_headline_level(src_item, dst_headline.level, is_same_file) - local refile_opts = { - file = dst_file.filename, - lines = src_lines, - item = src_item, - range = Range.from_line(dst_headline.position.end_line), - headline = dst_headline.title, - } - local refiled = Capture:_refile_to(refile_opts) - if not refiled then - return false - end - else - Capture:_refile_to_end(dst_file.filename, src_lines, src_item) - end + local dst_file = entry.value.file + local dst_headline = entry.value.headline + if dst_headline then + -- NOTE: adapted from Capture:refile_to_headline + local is_same_file = dst_file.filename == src_item.root.filename + src_lines = Capture:_adapt_headline_level(src_item, dst_headline.level, is_same_file) + local refile_opts = { + file = dst_file.filename, + lines = src_lines, + item = src_item, + range = Range.from_line(dst_headline.position.end_line), + headline = dst_headline.title, + } + local refiled = Capture:_refile_to(refile_opts) + if not refiled then + return false + end + else + Capture:_refile_to_end(dst_file.filename, src_lines, src_item) + end - if is_capture then - orgmode.action('capture.kill') - end - end + if is_capture then + orgmode.action("capture.kill") + end + end - local function gen_depth_toggle(opts, prompt_bufnr) + local function gen_depth_toggle(opts, prompt_bufnr) + local status = state.get_status(prompt_bufnr) + status._ot_current_depth = opts.max_depth + status._ot_next_depth = nil + if status._ot_current_depth ~= 0 then + status._ot_next_depth = 0 + end - local status = state.get_status(prompt_bufnr) - status._ot_current_depth = opts.max_depth - status._ot_next_depth = nil - if status._ot_current_depth ~= 0 then - status._ot_next_depth = 0 - end + return function() + local current_picker = action_state.get_current_picker(prompt_bufnr) - return function() - local current_picker = action_state.get_current_picker(prompt_bufnr) + local aux = status._ot_current_depth + status._ot_current_depth = status._ot_next_depth + status._ot_next_depth = aux - local aux = status._ot_current_depth - status._ot_current_depth = status._ot_next_depth - status._ot_next_depth = aux + opts.max_depth = status._ot_current_depth + local new_finder = finders.new_table({ + results = utils.get_entries(opts), + entry_maker = opts.entry_maker or utils.make_entry(opts), + }) - opts.max_depth = status._ot_current_depth - local new_finder = finders.new_table { - results = utils.get_entries(opts), - entry_maker = opts.entry_maker or utils.make_entry(opts), - } + current_picker:refresh(new_finder, opts) + end + end - current_picker:refresh(new_finder, opts) - end - end - - pickers.new(opts, { - -- TODO: alter prompt title when depth is 0: Refile under file, Refile - -- under Headline - prompt_title = "Refile Destination", - finder = finders.new_table { - results = utils.get_entries(opts), - entry_maker = opts.entry_maker or utils.make_entry(opts), - }, - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - attach_mappings = function(prompt_bufnr, map) - action_set.select:replace(refile) - map("i", "", gen_depth_toggle(opts, prompt_bufnr)) - return true - end, - }):find() + pickers + .new(opts, { + -- TODO: alter prompt title when depth is 0: Refile under file, Refile + -- under Headline + prompt_title = "Refile Destination", + finder = finders.new_table({ + results = utils.get_entries(opts), + entry_maker = opts.entry_maker or utils.make_entry(opts), + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + attach_mappings = function(prompt_bufnr, map) + action_set.select:replace(refile) + map("i", "", gen_depth_toggle(opts, prompt_bufnr)) + return true + end, + }) + :find() end diff --git a/lua/telescope/_extensions/orgmode/search_headings.lua b/lua/telescope/_extensions/orgmode/search_headings.lua index a2783c5..c946fe7 100644 --- a/lua/telescope/_extensions/orgmode/search_headings.lua +++ b/lua/telescope/_extensions/orgmode/search_headings.lua @@ -2,18 +2,20 @@ local pickers = require("telescope.pickers") local finders = require("telescope.finders") local conf = require("telescope.config").values -local utils = require('telescope-orgmode.utils') +local utils = require("telescope-orgmode.utils") return function(opts) - opts = opts or {} + opts = opts or {} - pickers.new(opts, { - prompt_title = "Search Headings", - finder = finders.new_table { - results = utils.get_entries(opts), - entry_maker = opts.entry_maker or utils.make_entry(opts), - }, - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - }):find() + pickers + .new(opts, { + prompt_title = "Search Headings", + finder = finders.new_table({ + results = utils.get_entries(opts), + entry_maker = opts.entry_maker or utils.make_entry(opts), + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() end