Skip to content

Commit

Permalink
feat(history): add configurable limit to number of commands stored
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuscaisey committed Nov 19, 2024
1 parent 4c4db2c commit d32daf0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
22 changes: 22 additions & 0 deletions doc/please.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Input and selection prompts are provided by |vim.ui.input()| and
appearance of them to your taste. See |vim.ui| and the fantastic
https://github.com/stevearc/dressing.nvim for more information.

==============================================================================
SETUP *please-setup*

please.nvim does not require any setup to be used. However, you can customise
the behaviour of the plugin by calling |please.setup()|.

==============================================================================
MAPPINGS *please-mappings*

Expand All @@ -83,6 +89,22 @@ command to get you started.
==============================================================================
Lua module: please *please*

setup({opts}) *please.setup()*
Updates the configuration with the provided {opts}. Should only be called
if you want to change the defaults which are shown below.

Example: >lua
local please = require('please')
please.setup({
max_history_items = 20,
})
<

Parameters: ~
{opts} (`table`) A table with the following fields:
• {max_history_items} (`integer?`) The maximum number of
history items to store for each repository.

build() *please.build()*
If the current file is a `BUILD` file, builds the target which is under
the cursor. Otherwise, builds the target which takes the current file as
Expand Down
32 changes: 32 additions & 0 deletions lua/please.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ local debug = require('please.debug')

local please = {}

---@nodoc
---@class please.Config
---@field max_history_items integer The maximum number of history items to store for each repository.

---@type please.Config
local config = {
max_history_items = 20,
}

---@inlinedoc
---@class please.Opts
---@field max_history_items integer? The maximum number of history items to store for each repository.

---Updates the configuration with the provided {opts}. Should only be called if you want to change the defaults which
---are shown below.
---
---Example:
---```lua
---local please = require('please')
---please.setup({
--- max_history_items = 20,
---})
---```
---@param opts please.Opts
function please.setup(opts)
vim.validate({
max_history_items = { opts.max_history_items, 'number', true },
})
config = vim.tbl_deep_extend('force', config, opts)
end

local default_profile = os.getenv('PLZ_CONFIG_PROFILE')

---@type table<string, string?>
Expand Down Expand Up @@ -77,6 +108,7 @@ local function save_command(root, command)
:filter(function(history_item)
return history_item.description ~= command.description
end)
:take(config.max_history_items-1)
:totable()
else
history[root] = {}
Expand Down
22 changes: 21 additions & 1 deletion tests/please_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ describe('history', function()
local root = create_temp_tree()
local select_fake = SelectFake:new()

-- GIVEN we've yanked the label of three targets, one after the other
-- GIVEN we've built three targets, one after the other
for _, filename in ipairs({ 'foo1.txt', 'foo2.txt', 'foo3.txt' }) do
vim.cmd('edit ' .. root .. '/' .. filename)
please.build()
Expand Down Expand Up @@ -895,6 +895,26 @@ describe('history', function()
please.history()
select_fake:assert_items({ 'plz build //:foo2', 'plz build //:foo3', 'plz build //:foo1' })
end)

it('should display the n most recent items', function()
local root = create_temp_tree()
local select_fake = SelectFake:new()

-- GIVEN we've built three targets, one after the other
please.setup({ max_history_items = 2 })
for _, filename in ipairs({ 'foo1.txt', 'foo2.txt', 'foo3.txt' }) do
vim.cmd('edit ' .. root .. '/' .. filename)
print('TEST')
please.build()
end
please.setup({ max_history_items = 20 })
-- WHEN we call history
please.history()
-- THEN the commands to build the two most recently built target are ordered from most to least recent
select_fake:assert_items({ 'plz build //:foo3', 'plz build //:foo2' })

please.setup({ max_history_items = 20 })
end)
end)

describe('jump_to_target', function()
Expand Down

0 comments on commit d32daf0

Please sign in to comment.