Skip to content

Commit aafaa91

Browse files
committed
refactor: rename smart_open() to sbuffer()
Trying to reduce the number of entities that have "open" in the name, to reduce confusion.
1 parent 187cef5 commit aafaa91

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

ARCHITECTURE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ There are two generic classes of "finders", that are paired with corresponding g
77
1. **"List"-based finders and scanners.** These take a list of candidate haystacks, like a list of buffers, a list of help tags, or a list of search patterns. These are accessed by commands like `:CommandTBuffer`, `:CommandTHelp`, and `:CommandTSearch`.
88
2. **"Command"-based finders and scanners.** These run a command to generate the list of candidate haystacks. These are accessed by commands like `:CommandTGit` and `:CommandTRipgrep`.
99

10-
Each instance of a list or command-based finder is defined using a Lua table. Finders definitions are found under `finders`, and define either `candidates` (a list of candidates, or a function that returns such a list) or `command` (a command string, or a function that returns such a string).
10+
Each instance of a list or command-based finder is defined using a Lua table. Finder definitions are found under `finders`, and specify either `candidates` (a list of candidates, or a function that returns such a list) or `command` (a command string, or a function that returns such a string).
1111

1212
There are also two specialised "finder" and "scanner" pairs:
1313

@@ -18,7 +18,7 @@ These finders are hard-coded in the Command-T source code as functions.
1818

1919
In the following discussion, note that there are a few sets of confusing, overlapping, or overloading terms that would make a good target for refactoring:
2020

21-
- "open", "on_open", "opener", "smart_open": used at various layers of abstraction for configuration key names, callbacks, parameters, and so on.
21+
- "open", "on_open", "opener": used at various layers of abstraction for configuration key names, callbacks, parameters, and so on.
2222
- "config" vs "options": used to describe user-supplied settings, defaults, and also function-level parameters.
2323

2424
## List-based finder life-cycle
@@ -39,7 +39,7 @@ In the following discussion, note that there are a few sets of confusing, overla
3939
1. It looks up the finder config under the `name` key (`'buffer'`, for example) of the user's settings (obtained via a call to `commandt.options()`, which returns a _copy_ of the user's settings, or falls back to the defaults if there are no explicit user settings).
4040
2. If the config defines an `options` callback, it calls the callback with the existing options so that it has an opportunity to transform those options, and then sanitizes them. This is used by several finders to force the display of dotfiles (ie. by unconditionally setting `always_show_dot_files` to `true`, and `never_show_dot_files` to `false`); specifically, the `'buffer'`, `'help'`, `'history'`, `'jump'`, `'line'`, `'search'`, and `'tag'` finders.
4141
3. If the config defines an `on_directory` callback, it calls the callback with the directory to give it the opportunity to transform the directory. This is used by finders which change their root directory based on the `commandt.traverse` setting; if no directory is provided, and the user settings require it, the callback will attempt to infer an appropriate directory, using the current working directory as a fallback. Finders which use `on_directory` include `'fd'`, `'find'`, `'git'`, and `'rg'`.
42-
4. It prepares an `open` callback with signature `open(item, ex_command)` and assigns it back to the `options` object that will be passed into the `finders.list` or `finders.command` implementation, and also into `ui.show()`. This `open` callback will in turn forward to the `open` implementation specified in the config, if provided, otherwise falling back to `commandt.open(item, ex_command)` which is otherwise known as `smart_open(buffer, command)` (it's called "smart" because it tries to intelligently pick the right place to open the requested buffer). Finders which specify a custom `'open'` in their config include `'fd'`, `'find'`, `'git'`, and `'rg'`; which all pass in an `on_open(item, ex_command, directory, _options, opener, _context)` implementation which uses the `opener` to open a relativized path (ie. `opener(relativize(directory, item), ex_command)`); note that the `opener` here is actually `commandt.open` (ie. `smart_open()`). In contrast, the `'help'`, `'history'`, `'line'`, `'search'`, and `'tag'` finders define totally custom open callbacks. Finally, the `'buffer'` and `'jump'` finders define nothing, which means they use the fallback.
42+
4. It prepares an `open` callback with signature `open(item, ex_command)` and assigns it back to the `options` object that will be passed into the `finders.list` or `finders.command` implementation, and also into `ui.show()`. This `open` callback will in turn forward to the `open` implementation specified in the config, if provided, otherwise falling back to `commandt.open(item, ex_command)` which is otherwise known as `sbuffer(buffer, command)` (it's called "smart" because it tries to intelligently pick the right place to open the requested buffer). Finders which specify a custom `'open'` in their config include `'fd'`, `'find'`, `'git'`, and `'rg'`; which all pass in an `on_open(item, ex_command, directory, _options, opener, _context)` implementation which uses the `opener` to open a relativized path (ie. `opener(relativize(directory, item), ex_command)`); note that the `opener` here is actually `commandt.open` (ie. `sbuffer()`). In contrast, the `'help'`, `'history'`, `'line'`, `'search'`, and `'tag'` finders define totally custom open callbacks. Finally, the `'buffer'` and `'jump'` finders define nothing, which means they use the fallback.
4343
5. If the finder config provides a `candidates` field, it obtains the actual list finder and context by passing in the `directory`, `config.candidates`, and `options`. Otherwise, it must contain a `command` field and it obtains a command finder passing in `directory`, `config.command`, `options`, and `name` (the `name` is used to look up finder-specific settings in the `options`).
4444
6. If the finder config provides a `fallback` field set to `true`, it adds the fallback finder to the `finder` object, passing in the original `finder`, `directory`, and `options` (this fallback finder is a lazy wrapper around the built-in file finder, which gets invoked if the primary finder fails.
4545
7. It invokes `ui.show()`, passing in the `finder` and `options`, merging in three additional settings: `name`, `mode` (`mode` is `'virtual'`, `'file'` or `nil`), and an `on_close` callback set to `config.on_close`. Finders which actually specify an `on_close` are `'fd'`, `'find'`, `'rg'`, and the built-in file finders; they all use `popd` for this purpose, to reset to the original working directory in case the user specified a temporary directory as an argument to those finders (the watchman finder doesn't need this as it does not change into a temporary directory even when the user specifies a directory argument).

lua/wincent/commandt/init.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ local commandt = {}
1919
--- @param buffer string
2020
--- @param command 'edit' | 'split' | 'tabedit' | 'vsplit'
2121
--- @return nil
22-
local function smart_open(buffer, command)
22+
local function sbuffer(buffer, command)
2323
buffer = vim.fn.fnameescape(buffer)
2424
local is_visible = require('wincent.commandt.private.buffer_visible')(buffer)
2525
if is_visible then
@@ -733,7 +733,7 @@ local default_options = {
733733
prompt = {
734734
border = { '', '', '', '', '', '', '', '' }, -- 'double', 'none', 'rounded', 'shadow', 'single', 'solid', 'winborder', or a list of strings.
735735
},
736-
open = smart_open,
736+
open = sbuffer,
737737
root_markers = { '.git', '.hg', '.svn', '.bzr', '_darcs' },
738738
scanners = {
739739
fd = {
@@ -908,8 +908,8 @@ commandt.pushd = pushd
908908
commandt.on_directory = on_directory
909909
-- TODO: maybe rename this
910910
commandt.on_open = on_open
911-
-- TODO: probably rename this to smart_open
912-
commandt.open = smart_open
911+
-- TODO: rename `commandt.open` to `commandt.sbuffer`?
912+
commandt.open = sbuffer
913913

914914
commandt.options = function()
915915
return copy(_options or commandt.default_options())

0 commit comments

Comments
 (0)