Skip to content

Commit f226e02

Browse files
xiyaowongfolke
andauthored
feat(prompt): add flash.prompt() to get the prompt text (folke#347)
- Resolve folke#346 --------- Co-authored-by: Folke Lemaitre <[email protected]>
1 parent 2f2256e commit f226e02

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ Install the plugin with your preferred package manager:
324324
},
325325
-- options for the floating window that shows the prompt,
326326
-- for regular jumps
327+
-- `require("flash").prompt()` is always available to get the prompt text
327328
prompt = {
328329
enabled = true,
329330
prefix = { { "", "FlashPromptIcon" } },
@@ -410,7 +411,8 @@ Install the plugin with your preferred package manager:
410411
- **jump**: `require("flash").jump(opts?)` opens **flash** with the given options
411412
- type any number of characters before typing a jump label
412413
- **VS Code**: some functionality is changed/disabled when running **flash** in **VS Code**:
413-
- `prompt` is disabled
414+
- `prompt` is disabled. You can use `require("flash").prompt()` to get the
415+
prompt text and integrate it into the statusline.
414416
- `highlights` are set to different defaults that will actually work in VS Code
415417

416418
## 📡 API

lua/flash/commands.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ function M.toggle(enabled)
3333
return Search.toggle(enabled)
3434
end
3535

36+
---@return string
37+
function M.prompt()
38+
return require("flash.prompt").prompt or ""
39+
end
40+
3641
return M

lua/flash/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ local defaults = {
237237
},
238238
-- options for the floating window that shows the prompt,
239239
-- for regular jumps
240+
-- `require("flash").prompt()` is always available to get the prompt text
240241
prompt = {
241242
enabled = true,
242243
prefix = { { "", "FlashPromptIcon" } },

lua/flash/prompt.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
local Config = require("flash.config")
22

33
---@class Flash.Prompt
4-
---@field win window
5-
---@field buf buffer
4+
---@field win number
5+
---@field buf number
6+
---@field prompt string
67
local M = {}
78

89
local ns = vim.api.nvim_create_namespace("flash_prompt")
@@ -47,6 +48,8 @@ function M.show()
4748
end
4849

4950
function M.hide()
51+
M.prompt = ""
52+
5053
if M.win and vim.api.nvim_win_is_valid(M.win) then
5154
vim.api.nvim_win_close(M.win, true)
5255
M.win = nil
@@ -58,15 +61,24 @@ function M.hide()
5861
end
5962

6063
---@param pattern string
61-
function M.set(pattern)
62-
M.show()
64+
---@param show boolean
65+
function M.set(pattern, show)
6366
local text = vim.deepcopy(Config.prompt.prefix)
6467
text[#text + 1] = { pattern }
6568

6669
local str = ""
6770
for _, item in ipairs(text) do
6871
str = str .. item[1]
6972
end
73+
74+
M.prompt = str
75+
76+
if not show then
77+
return
78+
end
79+
80+
M.show()
81+
7082
vim.api.nvim_buf_set_lines(M.buf, 0, -1, false, { str })
7183
vim.api.nvim_buf_clear_namespace(M.buf, ns, 0, -1)
7284
local col = 0

lua/flash/state.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ end
343343
---@param opts? Flash.Step.Options
344344
function M:step(opts)
345345
opts = opts or {}
346-
if self.opts.prompt.enabled and not M.is_search() then
347-
Prompt.set(self.pattern())
346+
if not M.is_search() then
347+
Prompt.set(self.pattern(), self.opts.prompt.enabled)
348348
end
349349
local actions = opts.actions or self.opts.actions or {}
350350
local c = self:get_char()

0 commit comments

Comments
 (0)