-
Notifications
You must be signed in to change notification settings - Fork 401
docs: add API and naming #731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3d235f8
docs(api-and-naming): first cut
scop 53549b0
test(pymarkdown): disable line-length check for now
scop ed168f0
docs(api-and-naming): document `_comp_xfunc` considerations
scop d92af15
docs(api-and-naming): elaborate on double underscores as scope markers
scop c2097d5
docs(api-and-naming): double underscore wording tweaks
scop bd77d1a
docs(api-and-naming): start exporter local var names with underscores
scop 4f6e424
docs(api-and-naming): introduce infix `*_mut_*` for mutable varnames
akinomyoga 5435b7c
docs(api-and-naming): reword "internal => private" for symmetry
akinomyoga 260a118
docs(api-and-naming): add general conventions/output variables section
scop 1fb425f
docs(api-and-naming): elaborate on clearing output-assigned variables
scop 89a239a
docs(CONTRIBUTING): link to API and naming
scop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # API and naming | ||
|
|
||
| ## General API conventions | ||
|
|
||
| Most of the functions in bash-completion generate completions and directly | ||
| inject them to the `COMPREPLY` array variable, as required for completions to | ||
| work. | ||
|
|
||
| Most other functions make use of "output" variables, i.e. assign values to | ||
| them. The most common one of these is named `ret`. Consult the commentary | ||
| before each function in the source to find out the specific names. | ||
| `local`izing output variables before invoking a function that populates them | ||
| is the caller's responsibility. | ||
| Note that if calling multiple functions that assign output to the same variable | ||
| during one completion function run, each result should be copied to another | ||
| variable between the calls to avoid it possibly being overwritten and lost on | ||
| the next call. Also, the variables should also be ensured to be clear before | ||
| each call, typically by `unset -v`ing them when multiple such calls are used, | ||
| to avoid them interfering with each other. | ||
|
|
||
| ## Naming | ||
|
|
||
| Due to its nature, bash-completion adds a number of functions and variables in | ||
| the shell's environment. | ||
|
|
||
| | | `bash_completion` | `completions/*` | | ||
| |:------------------------------------|:--------------------|:---------------------------------------------------------------------------| | ||
| | public configuration variables | `BASH_COMPLETION_*` | `BASH_COMPLETION_CMD_${Command^^}_${Config^^}` | | ||
| | private non-local variables | `_comp__*` | `_comp_cmd_${Command}__${Data}` | | ||
| | private non-local mutable variables | `_comp__*_mut_*` | `_comp_cmd_${Command}__mut_${Data}` | | ||
| | exporter function local variables | `_*` (not `_comp*`) | `_*` (not `_comp*`) | | ||
| | public/exported functions | `_comp_*` | `_comp_cmd_${Command}` (functions for `complete -F`) | | ||
| | | | `_comp_xfunc_${Command}_${Utility}` (functions for use with `_comp_xfunc`) | | ||
| | private/internal functions | `_comp__*` | `_comp_cmd_${Command}__${Utility}` (utility functions) | | ||
|
|
||
| `${Command}` refers to a command name (with characters not allowed in POSIX | ||
| function or variable names replaced by an underscore), `${Config}` the name of | ||
| a configurable thing, `^^` means uppercase, `${Data}` is an identifier for the | ||
| data contained in the variable, and `${Utility}` describes the typical usage of | ||
| the function. | ||
|
|
||
| Variables and functions affecting multiple completions are usually defined | ||
| in the main `bash_completion` file and do not require any additional files to | ||
| be sourced. Variables and functions in command specific completion files in | ||
| `completions/*` follow a slightly different naming scheme; they include | ||
| `cmd` in their name as well as the name of the command. | ||
|
|
||
| Public configuration variables are shell ones that affect the runtime behavior | ||
| of various completions. As a rule of thumb, we lean towards not providing | ||
| customizability but rather strive to provide great completion behavior out of | ||
| the box. But there are some, see [configuration](configuration.md). | ||
|
|
||
| Variables and functions whose name contains a double underscore (`__`) anywhere | ||
| in their name are private implementation details, not part of the stable API, | ||
| and not intended to be used outside of their defining context. Internally, the | ||
| double underscores serve as privacy scope delimiters; there can be more than one | ||
| pair of them in a name, and functions and variables are intended to reference | ||
| and call other functions and variables within that scope, one level deep, | ||
| sharing a common prefix. For example, a function named `_comp_foo` is "allowed" | ||
| to access `_comp_foo__*` where `*` does not contain any double underscores, | ||
| i.e. it should not access `_comp_foo__something__*` despite the common prefix. | ||
|
|
||
| Private non-local variables are considered readonly by default. When a | ||
| completion function needs to change variables for e.g. caching purposes, the | ||
| variables should contain the infix `*_mut_*` anywhere in their names. This is | ||
| needed to tell the test framework to allow these variables changing. | ||
| Nevertheless, the completion results should be consistent among different calls | ||
| and unaffected by the state of the cache variables when it is called. | ||
|
|
||
| Internal local variables of functions that "export" their results using a | ||
| variable name that is passed in start with an underscore and do not start with | ||
| `_comp`. The variable names that are passed in for this purpose must not start | ||
| with an underscore. | ||
|
|
||
| Functions with names prefixed with `_comp_xfunc_` are intended to be used | ||
| through the `_comp_xfunc` function from files other than the one they are | ||
| defined in. From the same file they can be used directly using their complete | ||
| name. | ||
|
|
||
| Function names start with an underscore in order to avoid them being | ||
| included in completions of command names. (Except naturally when a command | ||
| starting with an underscore is being completed.) The underscore prefix does | ||
| not have anything to do with whether the thing is considered public or | ||
| private in the API, nor anything else really. | ||
|
|
||
| The `BASH_COMPLETION_` prefix provides a namespace and makes it clear what | ||
| these variables relate to. The `_comp` in other names serves a similar purpose, | ||
| but because these are used a lot in the code (unlike the public configuration | ||
| variables), using something shorter is beneficial. We hope and believe this is | ||
| distinctive and clash free enough. | ||
|
|
||
| It is known that a lot of functions and variables in the tree do not follow | ||
| these naming rules yet. Things introduced after version 2.11 should, and we are | ||
| evaluating our options for handling older ones. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.