diff options
author | Daniil Rozanov <daniilrozzanov@gmail.com> | 2024-03-29 04:37:56 +0300 |
---|---|---|
committer | Daniil Rozanov <daniilrozzanov@gmail.com> | 2024-03-29 04:37:56 +0300 |
commit | 672f0d32e322b79661b5d7959887adaa9e41ad98 (patch) | |
tree | c2c3f4e2157d47c7e3f8dfd2f3229e37e0919b3e /lua/cmake-explorer/telescope | |
parent | d453f54d98536eb3a52daebfe279c4e624979c31 (diff) |
feat: build from current variant
Diffstat (limited to 'lua/cmake-explorer/telescope')
-rw-r--r-- | lua/cmake-explorer/telescope/make_entry.lua | 57 | ||||
-rw-r--r-- | lua/cmake-explorer/telescope/pickers.lua | 103 | ||||
-rw-r--r-- | lua/cmake-explorer/telescope/previewers.lua | 40 | ||||
-rw-r--r-- | lua/cmake-explorer/telescope/test.lua | 46 |
4 files changed, 246 insertions, 0 deletions
diff --git a/lua/cmake-explorer/telescope/make_entry.lua b/lua/cmake-explorer/telescope/make_entry.lua new file mode 100644 index 0000000..cc919bd --- /dev/null +++ b/lua/cmake-explorer/telescope/make_entry.lua @@ -0,0 +1,57 @@ +local make_entry = require("telescope.make_entry") +local entry_display = require("telescope.pickers.entry_display") +local config = require("cmake-explorer.config") + +local M = {} + +M.gen_from_configure = function(opts) + local project = require("cmake-explorer").project + local displayer = entry_display.create({ + separator = " ", + items = { + { width = project.display.short_len + 5 }, + { remaining = true }, + }, + }) + local make_display = function(entry) + vim.print(entry) + return displayer({ + { entry.value.display.short, "TelescopeResultsIdentifier" }, + { entry.value.display.long, "TelescopeResultsComment" }, + }) + end + return function(entry) + return make_entry.set_default_entry_mt({ + value = entry, + ordinal = table.concat(entry.short, config.variants_display.short_sep), + display = make_display, + }, opts) + end +end + +M.gen_from_build = function(opts) + local project = require("cmake-explorer").project + local displayer = entry_display.create({ + separator = " ", + items = { + { width = project.display.short_len + 5 }, + { remaining = true }, + }, + }) + local make_display = function(entry) + vim.print(entry) + return displayer({ + { entry.value.display.short, "TelescopeResultsIdentifier" }, + { entry.value.display.long, "TelescopeResultsComment" }, + }) + end + return function(entry) + return make_entry.set_default_entry_mt({ + value = entry, + ordinal = table.concat(entry.short, config.variants_display.short_sep), + display = make_display, + }, opts) + end +end + +return M diff --git a/lua/cmake-explorer/telescope/pickers.lua b/lua/cmake-explorer/telescope/pickers.lua new file mode 100644 index 0000000..b2c7f56 --- /dev/null +++ b/lua/cmake-explorer/telescope/pickers.lua @@ -0,0 +1,103 @@ +local pickers = require("telescope.pickers") +local finders = require("telescope.finders") +local conf = require("telescope.config").values +local actions = require("telescope.actions") +local action_state = require("telescope.actions.state") +local cmake_make_entry = require("cmake-explorer.telescope.make_entry") +local notif = require("cmake-explorer.notification") +local previewers = require("cmake-explorer.telescope.previewers") + +local M = {} + +M.build_dirs = function(opts) + local cmake = require("cmake-explorer") + pickers + .new(opts, { + prompt_title = "CMake Builds", + finder = finders.new_table({ + results = cmake.project.fileapis, + -- entry_maker = cmake_make_entry.gen_from_fileapi(opts), + entry_maker = function(entry) + return { + value = entry, + display = entry.path, + ordinal = entry.path, + } + end, + sorter = conf.generic_sorter(opts), + -- attach_mappings = function(prompt_bufnr, map) + -- actions.select_default:replace(function() end) + -- return true + -- end, + }), + }) + :find() +end + +M.configure = function(opts) + local cmake = require("cmake-explorer") + local runner = require("cmake-explorer.runner") + opts.layout_strategy = "vertical" + opts.layout_config = { + prompt_position = "top", + preview_cutoff = 0, + preview_height = 5, + mirror = true, + } + pickers + .new(opts, { + default_selection_index = cmake.project:current_configure_index(), + prompt_title = "CMake Configure Options", + finder = finders.new_table({ + results = cmake.project:list_configs(), + entry_maker = cmake_make_entry.gen_from_configure(opts), + }), + sorter = conf.generic_sorter(opts), + previewer = previewers.configure_previewer(), + attach_mappings = function(prompt_bufnr, map) + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + cmake.project.current_config = selection.value + runner.start(selection.value.configure_command) + end) + return true + end, + }) + :find() +end + +M.build = function(opts) + local cmake = require("cmake-explorer") + local runner = require("cmake-explorer.runner") + opts.layout_strategy = "vertical" + opts.layout_config = { + prompt_position = "top", + preview_cutoff = 0, + preview_height = 5, + mirror = true, + } + pickers + .new(opts, { + default_selection_index = cmake.project:current_build_index(), + prompt_title = "CMake Build Options", + finder = finders.new_table({ + results = cmake.project:list_builds(), + entry_maker = cmake_make_entry.gen_from_configure(opts), + }), + sorter = conf.generic_sorter(opts), + previewer = previewers.build_previewer(), + attach_mappings = function(prompt_bufnr, map) + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + cmake.project.current_config = selection.value + runner.start(selection.value.build_command) + end) + return true + end, + }) + :find() +end + +return M diff --git a/lua/cmake-explorer/telescope/previewers.lua b/lua/cmake-explorer/telescope/previewers.lua new file mode 100644 index 0000000..39fea4a --- /dev/null +++ b/lua/cmake-explorer/telescope/previewers.lua @@ -0,0 +1,40 @@ +local previewers = require("telescope.previewers") +local config = require("cmake-explorer.config") + +local M = {} + +M.configure_previewer = function(opts) + return previewers.new_buffer_previewer({ + title = "Configure Details", + + define_preview = function(self, entry) + if self.state.bufname then + return + end + local entries = { + "Command:", + config.cmake_path .. " " .. table.concat(entry.value.configure_args, " "), + } + vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, entries) + end, + }) +end + +M.build_previewer = function(opts) + return previewers.new_buffer_previewer({ + title = "Build Details", + + define_preview = function(self, entry) + if self.state.bufname then + return + end + local entries = { + "Command:", + config.cmake_path .. " " .. table.concat(entry.value.build_args, " "), + } + vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, entries) + end, + }) +end + +return M diff --git a/lua/cmake-explorer/telescope/test.lua b/lua/cmake-explorer/telescope/test.lua new file mode 100644 index 0000000..7b8bb00 --- /dev/null +++ b/lua/cmake-explorer/telescope/test.lua @@ -0,0 +1,46 @@ +local pickers = require("telescope.pickers") +local finders = require("telescope.finders") +local conf = require("telescope.config").values +local actions = require("telescope.actions") +local action_state = require("telescope.actions.state") + +-- our picker function: colors +local colors = function(opts) + opts = opts or {} + pickers + .new(opts, { + prompt_title = "colors", + finder = finders.new_table({ + results = { + { "red", "#ff0000" }, + { "green", "#00ff00" }, + { "blue", "#0000ff" }, + }, + entry_maker = function(entry) + return { + value = entry, + display = entry[1], + ordinal = entry[1], + } + end, + }), + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr, map) + map({ "i", "n" }, "<C-r>", function(_prompt_bufnr) + print("You typed <C-r>") + end) + + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + -- print(vim.inspect(selection)) + vim.api.nvim_put({ selection[1] }, "", false, true) + end) + return true + end, + }) + :find() +end + +-- to execute the function +colors() |