Skip to content

Commit 3d235f8

Browse files
committed
docs(api-and-naming): first cut
#537 #539
1 parent 7492a42 commit 3d235f8

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

doc/api-and-naming.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# API and naming
2+
3+
Due to its nature, bash-completion adds a number of functions and variables in
4+
the shell's environment.
5+
6+
| | `bash_completion` | `completions/*` |
7+
| :----------------------------- | :------------------ | :----------------------------------------------------- |
8+
| public configuration variables | `BASH_COMPLETION_*` | `BASH_COMPLETION_CMD_${Command^^}_${Config^^}` |
9+
| non-local internal variables | `_comp__*` | `_comp_cmd_${Command}__${Data}` |
10+
| public/exported functions | `_comp_*` | `_comp_cmd_${Command}` (functions for `complete -F`) |
11+
| private/internal functions | `_comp__*` | `_comp_cmd_${Command}__${Utility}` (utility functions) |
12+
13+
`${Command}` refers to a command name (with characters not allowed in POSIX
14+
function or variable names replaced by an underscore), `${Config}` the name of
15+
a configurable thing, `^^` means uppercase, `${Data}` is an identifier for the
16+
data contained in the variable, and `${Utility}` describes the typical usage of
17+
the function.
18+
19+
Variables and functions affecting multiple completions are usually defined
20+
in the main `bash_completion` file and do not require any additional files to
21+
be sourced. Variables and functions in command specific completion files in
22+
`completions/*` follow a slightly different naming scheme; they include
23+
`cmd` in their name as well as the name of the command.
24+
25+
Public configuration variables are shell ones that affect the runtime behavior
26+
of various completions. As a rule of thumb, we lean towards not providing
27+
customizability but rather strive to provide great completion behavior out of
28+
the box. But there are some, see [configuration](configuration.md).
29+
30+
Variables and functions whose name contains a double underscore (`__`) anywhere
31+
in their name are private implementation details, not part of the stable API,
32+
and not intended to be used outside of their defining context.
33+
34+
Function names start with an underscore in order to avoid them being
35+
included in completions of command names. (Except naturally when a command
36+
starting with an underscore is being completed.) The underscore prefix does
37+
not have anything to do with whether the thing is considered public or
38+
private in the API, nor anything else really.
39+
40+
The `BASH_COMPLETION_` prefix provides a namespace and makes it clear what
41+
these variables relate to. The `_comp` in other names serves a similar purpose,
42+
but because these are used a lot in the code (unlike the public configuration
43+
variables), using something shorter is beneficial. We hope and believe this is
44+
distinctive and clash free enough.
45+
46+
It is known that a lot of functions and variables in the tree do not follow
47+
these naming rules yet. Things introduced after version 2.11 should, and we are
48+
evaluating our options for handling older ones.

doc/styleguide.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,6 @@ over actual values. If an index or value is to be accessed later on instead of
111111
being just locally for looping, use a more descriptive and specific name for
112112
it.
113113

114-
## Function names
114+
## Function and variable names
115115

116-
Use the `_comp_` prefix for all function names, and `_comp_cmd_` for functions
117-
defined in per command completion files and not anywhere else. Prefixing with
118-
an underscore helps keep the functions out of the way for most command name
119-
completions (except obviously ones starting with an underscore or ones that have
120-
nothing typed in yet), and having a consistent prefix helps avoid some clashes
121-
and gives a hint where a function originates from.
122-
123-
It is known that a lot of functions in the tree do not follow this practice.
124-
This is due to backwards compatibility reasons, but all functions introduced
125-
after version 2.11 should follow this name prefix rule.
126-
127-
## Variable naming
128-
129-
To be written.
116+
See [API and naming](api-and-naming.md).

0 commit comments

Comments
 (0)