Skip to content

Commit 1178359

Browse files
authored
Merge pull request #731 from scop/docs/api-and-naming
docs: add API and naming
2 parents f02e282 + 89a239a commit 1178359

File tree

4 files changed

+104
-15
lines changed

4 files changed

+104
-15
lines changed

.pymarkdown.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"//": "Does not work with GitHub issue templates",
55
"enabled": false
66
},
7+
"line-length": {
8+
"//": "Disabled because necessary for tables, and no option to disable for them as of 0.9.6",
9+
"enabled": false
10+
},
711
"no-inline-html": {
812
"//": "!-- for issue template, kbd for README and in general",
913
"allowed_elements": "!--,kbd"

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ that. Don't be disappointed if it does or doesn't happen instantly.
4242

4343
Also, please bear the following coding guidelines in mind:
4444

45+
- See the [API and naming](doc/api-and-naming.md) document for information
46+
about conventions to follow related to those topics.
47+
4548
- Do not use Perl, Ruby, Python etc. to do text processing unless the
4649
command for which you are writing the completion code implies the
4750
presence of one of those languages.

doc/api-and-naming.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# API and naming
2+
3+
## General API conventions
4+
5+
Most of the functions in bash-completion generate completions and directly
6+
inject them to the `COMPREPLY` array variable, as required for completions to
7+
work.
8+
9+
Most other functions make use of "output" variables, i.e. assign values to
10+
them. The most common one of these is named `ret`. Consult the commentary
11+
before each function in the source to find out the specific names.
12+
`local`izing output variables before invoking a function that populates them
13+
is the caller's responsibility.
14+
Note that if calling multiple functions that assign output to the same variable
15+
during one completion function run, each result should be copied to another
16+
variable between the calls to avoid it possibly being overwritten and lost on
17+
the next call. Also, the variables should also be ensured to be clear before
18+
each call that references the value, variable name, or their existence,
19+
typically by `unset -v`ing them when multiple such calls are used,
20+
to avoid them interfering with each other.
21+
22+
## Naming
23+
24+
Due to its nature, bash-completion adds a number of functions and variables in
25+
the shell's environment.
26+
27+
| | `bash_completion` | `completions/*` |
28+
|:------------------------------------|:--------------------|:---------------------------------------------------------------------------|
29+
| public configuration variables | `BASH_COMPLETION_*` | `BASH_COMPLETION_CMD_${Command^^}_${Config^^}` |
30+
| private non-local variables | `_comp__*` | `_comp_cmd_${Command}__${Data}` |
31+
| private non-local mutable variables | `_comp__*_mut_*` | `_comp_cmd_${Command}__mut_${Data}` |
32+
| exporter function local variables | `_*` (not `_comp*`) | `_*` (not `_comp*`) |
33+
| public/exported functions | `_comp_*` | `_comp_cmd_${Command}` (functions for `complete -F`) |
34+
| | | `_comp_xfunc_${Command}_${Utility}` (functions for use with `_comp_xfunc`) |
35+
| private/internal functions | `_comp__*` | `_comp_cmd_${Command}__${Utility}` (utility functions) |
36+
37+
`${Command}` refers to a command name (with characters not allowed in POSIX
38+
function or variable names replaced by an underscore), `${Config}` the name of
39+
a configurable thing, `^^` means uppercase, `${Data}` is an identifier for the
40+
data contained in the variable, and `${Utility}` describes the typical usage of
41+
the function.
42+
43+
Variables and functions affecting multiple completions are usually defined
44+
in the main `bash_completion` file and do not require any additional files to
45+
be sourced. Variables and functions in command specific completion files in
46+
`completions/*` follow a slightly different naming scheme; they include
47+
`cmd` in their name as well as the name of the command.
48+
49+
Public configuration variables are shell ones that affect the runtime behavior
50+
of various completions. As a rule of thumb, we lean towards not providing
51+
customizability but rather strive to provide great completion behavior out of
52+
the box. But there are some, see [configuration](configuration.md).
53+
54+
Variables and functions whose name contains a double underscore (`__`) anywhere
55+
in their name are private implementation details, not part of the stable API,
56+
and not intended to be used outside of their defining context. Internally, the
57+
double underscores serve as privacy scope delimiters; there can be more than one
58+
pair of them in a name, and functions and variables are intended to reference
59+
and call other functions and variables within that scope, one level deep,
60+
sharing a common prefix. For example, a function named `_comp_foo` is "allowed"
61+
to access `_comp_foo__*` where `*` does not contain any double underscores,
62+
i.e. it should not access `_comp_foo__something__*` despite the common prefix.
63+
64+
Private non-local variables are considered readonly by default. When a
65+
completion function needs to change variables for e.g. caching purposes, the
66+
variables should contain the infix `*_mut_*` anywhere in their names. This is
67+
needed to tell the test framework to allow these variables changing.
68+
Nevertheless, the completion results should be consistent among different calls
69+
and unaffected by the state of the cache variables when it is called.
70+
71+
Internal local variables of functions that "export" their results using a
72+
variable name that is passed in start with an underscore and do not start with
73+
`_comp`. The variable names that are passed in for this purpose must not start
74+
with an underscore.
75+
76+
Functions with names prefixed with `_comp_xfunc_` are intended to be used
77+
through the `_comp_xfunc` function from files other than the one they are
78+
defined in. From the same file they can be used directly using their complete
79+
name.
80+
81+
Function names start with an underscore in order to avoid them being
82+
included in completions of command names. (Except naturally when a command
83+
starting with an underscore is being completed.) The underscore prefix does
84+
not have anything to do with whether the thing is considered public or
85+
private in the API, nor anything else really.
86+
87+
The `BASH_COMPLETION_` prefix provides a namespace and makes it clear what
88+
these variables relate to. The `_comp` in other names serves a similar purpose,
89+
but because these are used a lot in the code (unlike the public configuration
90+
variables), using something shorter is beneficial. We hope and believe this is
91+
distinctive and clash free enough.
92+
93+
It is known that a lot of functions and variables in the tree do not follow
94+
these naming rules yet. Things introduced after version 2.11 should, and we are
95+
evaluating our options for handling older ones.

doc/styleguide.md

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

122-
## Function names
122+
## Function and variable names
123123

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

0 commit comments

Comments
 (0)