diff --git a/doc/please.txt b/doc/please.txt index c6aecaa..9d51790 100644 --- a/doc/please.txt +++ b/doc/please.txt @@ -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* @@ -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 diff --git a/lua/please.lua b/lua/please.lua index f01b092..339715e 100644 --- a/lua/please.lua +++ b/lua/please.lua @@ -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 @@ -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] = {} diff --git a/tests/please_spec.lua b/tests/please_spec.lua index b4d91e0..0d83fd0 100644 --- a/tests/please_spec.lua +++ b/tests/please_spec.lua @@ -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() @@ -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()