Skip to content

Commit 28069f0

Browse files
committed
feat: use vim.ui.input() for compatibility issues.
This changes was from Zeioth#77.
1 parent c09ab4e commit 28069f0

4 files changed

Lines changed: 233 additions & 102 deletions

File tree

lua/compiler/init.lua

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,40 @@ local cmd = vim.api.nvim_create_user_command
77
local M = {}
88

99
M.setup = function(opts)
10-
cmd("CompilerOpen", function()
11-
require("compiler.telescope").show()
12-
end, { desc = "Open the compiler" })
10+
cmd(
11+
"CompilerOpen",
12+
function() require("compiler.picker").show() end,
13+
{ desc = "Open the compiler" }
14+
)
1315

14-
cmd("CompilerToggleResults", function()
15-
vim.cmd("OverseerToggle")
16-
end, { desc = "Toggle the compiler results" })
16+
cmd(
17+
"CompilerToggleResults",
18+
function() vim.cmd("OverseerToggle") end,
19+
{ desc = "Toggle the compiler results" }
20+
)
1721

1822
cmd("CompilerRedo", function()
1923
local current_filetype = vim.bo.filetype
2024

2125
-- If the user didn't select an option yet, send a notification.
22-
if _G.compiler_redo_selection == nil and _G.compiler_redo_bau_selection == nil then
23-
vim.notify("Open the compiler and select an option before doing redo.",
24-
vim.log.levels.INFO, { title = "Compiler.nvim" }
26+
if
27+
_G.compiler_redo_selection == nil
28+
and _G.compiler_redo_bau_selection == nil
29+
then
30+
vim.notify(
31+
"Open the compiler and select an option before doing redo.",
32+
vim.log.levels.INFO,
33+
{ title = "Compiler.nvim" }
2534
)
2635
return
2736
end
2837
if _G.compiler_redo_filetype then
2938
-- If filetype is not the same as when the option was selected, send a notification.
3039
if _G.compiler_redo_filetype ~= current_filetype then
31-
vim.notify("You are on a different language now. Open the compiler and select an option before doing redo.",
32-
vim.log.levels.INFO, { title = "Compiler.nvim" }
40+
vim.notify(
41+
"You are on a different language now. Open the compiler and select an option before doing redo.",
42+
vim.log.levels.INFO,
43+
{ title = "Compiler.nvim" }
3344
)
3445
return
3546
end
@@ -40,17 +51,21 @@ M.setup = function(opts)
4051
local bau_selection = _G.compiler_redo_bau_selection
4152
if bau_selection then bau.action(bau_selection) end
4253
else
43-
local language = require('compiler.utils').require_language(current_filetype)
54+
local language =
55+
require("compiler.utils").require_language(current_filetype)
4456
if not language then language = require("compiler.languages.make") end
4557
language.action(_G.compiler_redo_selection)
4658
end
47-
4859
end, { desc = "Redo the last selected compiler option" })
4960

5061
cmd("CompilerStop", function()
51-
vim.notify("SUCCESS - All tasks have been disposed.", vim.log.levels.INFO, {
52-
title = "Compiler.nvim"
53-
})
62+
vim.notify(
63+
"SUCCESS - All tasks have been disposed.",
64+
vim.log.levels.INFO,
65+
{
66+
title = "Compiler.nvim",
67+
}
68+
)
5469
local overseer = require("overseer")
5570
local tasks = overseer.list_tasks({ unique = false })
5671
for _, task in ipairs(tasks) do

lua/compiler/picker-util.lua

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

lua/compiler/picker.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--- ### Picker frontend for compiler.nvim
2+
-- Automatically detects and uses telescope.nvim or falls back to vim.ui.select
3+
4+
local M = {}
5+
6+
function M.show()
7+
local telescope_ok = pcall(require, "telescope")
8+
9+
if telescope_ok then
10+
require("compiler.telescope").show()
11+
else
12+
-- Fallback to vim.ui.select implementation
13+
local picker_util = require("compiler.picker-util")
14+
15+
-- Validate working directory
16+
if not picker_util.validate_working_directory() then return end
17+
18+
-- Prepare compiler options
19+
local compiler_data = picker_util.prepare_compiler_options()
20+
local language = compiler_data.language
21+
local options = compiler_data.options
22+
local filetype = compiler_data.filetype
23+
24+
-- Create items for vim.ui.select
25+
local select_data = picker_util.create_select_items(options)
26+
local items = select_data.items
27+
local item_map = select_data.item_map
28+
29+
-- Show vim.ui.select
30+
vim.ui.select(items, {
31+
prompt = "Compiler: ",
32+
}, function(choice)
33+
if not choice then return end
34+
local selected = item_map[choice]
35+
if not selected then return end
36+
37+
picker_util.execute_selection(
38+
selected.value,
39+
choice,
40+
options,
41+
language,
42+
filetype
43+
)
44+
end)
45+
end
46+
end
47+
48+
return M

0 commit comments

Comments
 (0)