Skip to content

Commit

Permalink
Add documentation for function and config variable order
Browse files Browse the repository at this point in the history
Closes #84
  • Loading branch information
LunarWatcher committed Feb 10, 2024
1 parent f7fb59a commit 52884e2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
22 changes: 22 additions & 0 deletions doc/AutoPairs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,21 @@ raw keys, which can and will cause conflicts with typed letters if given the
opportunity. Should you choose not to, though, you get to live with the
consequences, if any.

WARNING: This variable must be declared before calling any auto-pairs
functions. See |autopairs-config-changes-broken| for more details.

TL;DR:
Correct: >
let g:AutoPairsPrefix = '<some keybind here>'
let g:AutoPairs = autopairs#AutoPairsDefine({ ... some definitions here })
<
Incorrect: >
let g:AutoPairs = autopairs#AutoPairsDefine({ ... some definitions here })
let g:AutoPairsPrefix = '<some keybind here>'
<

Note that this applies to any of auto-pairs' functions.

------------------------------------------------------------------------------
*g:AutoPairsDefaultDisableKeybinds*

Expand Down Expand Up @@ -1713,6 +1728,9 @@ don't want to use the standards, go ahead and omit this function.
Additionally, if you want to add new types after loading, you need to use
|autopairs#AutoPairsMap()|

WARNING: Functions must be called after config variable declarations. See
|autopairs-config-changes-broken|

------------------------------------------------------------------------------
AutoPairsDefine() *autopairs#AutoPairsDefine()* *autopairs-copy-defaults*
Arguments: (pairs[, pairs_to_remove])
Expand All @@ -1739,6 +1757,10 @@ files are loaded when `autopair#` functions are called. This means using, for
an instance, `autopairs#AutoPairsDefine` loads the file and makes the function
valid.

WARNING: Functions must be called after config variable declarations. This
means g:AutoPairs has to be the variable defined last in many cases. See
|autopairs-config-changes-broken| for more information

------------------------------------------------------------------------------
AutoPairsMap() *autopairs#AutoPairsMap()* *auto-pairs-map-key*
Arguments: (key)
Expand Down
54 changes: 54 additions & 0 deletions doc/AutoPairsTrouble.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Table of Contents *autopairs-troubleshooting-contents*
7. Test failures (and related errors) ... |autopairs-test-failures|
1. Autocmd and syntax errors ........ |autopairs-typescript-errors|
8. Weird auto-pairs stuff on <CR> ....... |autopairs-bad-cr|
9. Config changes not taking effect ..... |autopairs-config-changes-broken|

==============================================================================
1. General troubleshooting *autopairs-troubleshooting-general*
Expand Down Expand Up @@ -373,4 +374,57 @@ the last straw for the other plugin, you can uninstall the other plugin plugin.

See also |autopairs-autocomplete-cr|

==============================================================================
9. Config changes not taking effect *autopairs-config-changes-broken*

TL;DR:~

Correct: >
let g:AutoPairsPrefix = '<some keybind here>'
let g:AutoPairs = autopairs#AutoPairsDefine({ ... some definitions here })
<
Incorrect: >
let g:AutoPairs = autopairs#AutoPairsDefine({ ... some definitions here })
let g:AutoPairsPrefix = '<some keybind here>'
<
This applies to any of auto-pairs' functions, generally identified by the
`autopairs#` prefix.

Long version~
If you're making changes to auto-pairs' config variables and seeing it not
take effect, you're probably running into an initialisation order problem.
This particularly applies to |g:AutoPairsPrefix|, but may exist in other
places as well. To avoid running into these problems, it's recommended to
initialise variables before calling any of auto-pairs' functions.

If any functions are called before variables are declared, this results in
certain internal variables being initialised in a way that makes it difficult
to change afterwards. Taking |g:AutoPairsPrefix| as an example, consider: >
let g:AutoPairs = autopairs#AutoPairsDefine({ ... some definitions here })
let g:AutoPairsPrefix = '<some keybind here>'
<

In this case, all the defaults are initialised after the g:AutoPairsDefine
call. While this usually isn't a problem, all the keybind initialisations
depend on the value of |g:AutoPairsPrefix|. Because it isn't defined prior to
the function call, the function defines it as the default value. When
|g:AutoPairsPrefix| then is defined after the function call, the variable
initialisation function is not called again (and even if it was, it would
not change anything, as the dependent variables are assumed to be initialised
by the user, and won't be reinitialised).

Simply put, if you're seeing this problem, it's caused by variables depending
on other variables during initialisation, and the value of the variable is
copied at the time of initialisation.

This means that, as the TL;DR: stated, you need to call `autopairs#` functions
after initialising the variables you're interested in, and not before. That
way, when the variable initialisation happens, dependent variables are
initialised correctly.

See also~
* https://github.com/LunarWatcher/auto-pairs/issues/84
The issue also contains a practical demonstration of this happening in
practice.

vim:ft=help

0 comments on commit 52884e2

Please sign in to comment.