|
| 1 | +--- ### Shared utilities for compiler.nvim pickers |
| 2 | + |
| 3 | +local M = {} |
| 4 | + |
| 5 | +--- Validates that the current working directory is not the home directory |
| 6 | +--- @return boolean true if valid, false if invalid (also shows notification) |
| 7 | +function M.validate_working_directory() |
| 8 | + if vim.loop.os_homedir() == vim.loop.cwd() then |
| 9 | + vim.notify( |
| 10 | + "You must :cd your project dir first.\nHome is not allowed as working dir.", |
| 11 | + vim.log.levels.WARN, |
| 12 | + { |
| 13 | + title = "Compiler.nvim", |
| 14 | + } |
| 15 | + ) |
| 16 | + return false |
| 17 | + end |
| 18 | + return true |
| 19 | +end |
| 20 | + |
| 21 | +--- Prepares compiler options by gathering language and BAU options |
| 22 | +--- @return table { language, options, filetype } |
| 23 | +function M.prepare_compiler_options() |
| 24 | + local utils = require("compiler.utils") |
| 25 | + local utils_bau = require("compiler.utils-bau") |
| 26 | + |
| 27 | + local buffer = vim.api.nvim_get_current_buf() |
| 28 | + local filetype = vim.api.nvim_get_option_value("filetype", { buf = buffer }) |
| 29 | + |
| 30 | + -- Programatically require the backend for the current language. |
| 31 | + local language = utils.require_language(filetype) |
| 32 | + |
| 33 | + -- On unsupported languages, default to make. |
| 34 | + if not language then language = utils.require_language("make") or {} end |
| 35 | + |
| 36 | + -- Also show options discovered on Makefile, Cmake... and other bau. |
| 37 | + if not language.bau_added then |
| 38 | + language.bau_added = true |
| 39 | + local bau_opts = utils_bau.get_bau_opts() |
| 40 | + |
| 41 | + -- Insert a separator for every bau. |
| 42 | + local last_bau_value = nil |
| 43 | + for _, item in ipairs(bau_opts) do |
| 44 | + if last_bau_value ~= item.bau then |
| 45 | + table.insert(language.options, { text = "", value = "separator" }) |
| 46 | + last_bau_value = item.bau |
| 47 | + end |
| 48 | + table.insert(language.options, item) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + -- Add numbers in front of the options to display. |
| 53 | + local index_counter = 0 |
| 54 | + for _, option in ipairs(language.options) do |
| 55 | + if option.value ~= "separator" then |
| 56 | + index_counter = index_counter + 1 |
| 57 | + option.text = index_counter .. " - " .. option.text |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + return { |
| 62 | + language = language, |
| 63 | + options = language.options, |
| 64 | + filetype = filetype, |
| 65 | + } |
| 66 | +end |
| 67 | + |
| 68 | +--- Executes the selected compiler option |
| 69 | +--- @param selected_value string The value of the selected option |
| 70 | +--- @param selected_text string The display text of the selected option |
| 71 | +--- @param language_options table The full options table |
| 72 | +--- @param filetype string The current filetype |
| 73 | +function M.execute_selection( |
| 74 | + selected_value, |
| 75 | + selected_text, |
| 76 | + language_options, |
| 77 | + language, |
| 78 | + filetype |
| 79 | +) |
| 80 | + if selected_value == "" or selected_value == "separator" then return end |
| 81 | + |
| 82 | + local utils_bau = require("compiler.utils-bau") |
| 83 | + |
| 84 | + -- Do the selected option belong to a build automation utility? |
| 85 | + local bau = nil |
| 86 | + for _, value in ipairs(language_options) do |
| 87 | + if value.text == selected_text or value.value == selected_value then |
| 88 | + bau = value.bau |
| 89 | + break |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + if bau then -- call the bau backend. |
| 94 | + bau = utils_bau.require_bau(bau) |
| 95 | + if bau then bau.action(selected_value) end |
| 96 | + -- then |
| 97 | + -- clean redo (language) |
| 98 | + _G.compiler_redo_selection = nil |
| 99 | + -- save redo (bau) |
| 100 | + _G.compiler_redo_bau_selection = selected_value |
| 101 | + _G.compiler_redo_bau = bau |
| 102 | + else -- call the language backend. |
| 103 | + language.action(selected_value) |
| 104 | + -- then |
| 105 | + -- save redo (language) |
| 106 | + _G.compiler_redo_selection = selected_value |
| 107 | + _G.compiler_redo_filetype = filetype |
| 108 | + -- clean redo (bau) |
| 109 | + _G.compiler_redo_bau_selection = nil |
| 110 | + _G.compiler_redo_bau = nil |
| 111 | + end |
| 112 | +end |
| 113 | + |
| 114 | +--- Creates items and mapping for vim.ui.select (filters out separators) |
| 115 | +--- @param language_options table The full options table |
| 116 | +--- @return table { items, item_map } |
| 117 | +function M.create_select_items(language_options) |
| 118 | + local items = {} |
| 119 | + local item_map = {} |
| 120 | + |
| 121 | + for _, option in ipairs(language_options) do |
| 122 | + if option.value ~= "separator" then |
| 123 | + table.insert(items, option.text) |
| 124 | + item_map[option.text] = { value = option.value, bau = option.bau } |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + return { items = items, item_map = item_map } |
| 129 | +end |
| 130 | + |
| 131 | +return M |
0 commit comments