Skip to content

Autocompletion

Mathieu Guindon edited this page Sep 4, 2019 · 2 revisions

Autocompletion

Configuration

Autocompletion is disabled by default. The 'Autocompletion Settings' tab of the 'Settings' dialog lets you enable each sub-feature individually:

Autocompletion Settings
image as of v2.2.0.4100; subject to change.

Enable autocompletion features
This checkbox determines whether Rubberduck responds to a keypress in the active code pane.

Enable self-closing pairs
Determines whether Rubberduck swallows a keypress and takes over the VBE, depending on context-sensitive information from the current logical line of code.

Enable smart-concatenation
Determines whether Rubberduck handles the ENTER key when the caret is inside a string literal.

  • Concatenate 'vbNewLine' on Ctrl+Enter
    Determines whether Rubberduck handles the CTRL+ENTER key combination when the caret is inside a string literal.

Self-Closing Pairs

When the self-closing pairs feature (SCP) is enabled, Rubberduck will capture a keypress in specific conditions. Pairs are defined by an opening and a closing single-character token - the list is hard-coded for now:

Opening Closing
" "
( )
[ ]
{ }

A self-closing pair is said to be symetric when the opening and closing characters are the same; SCP handling has special rules for symetric pairs, that don't necessarily presume the pair delimits a string literal.

Note that SCP will generally not be handled for a keypress of either the opening or closing character when the caret position is inside a comment or string literal.

Opening a Pair

The base behavior is that when the keypress matches the opening character of a self-closing pair, Rubberduck completes the pair by inserting both the opening and closing characters, placing the caret between the two, replacing the entire current logical line of code, and then preventing the keypress from reaching the code pane.

For example, given " for input on this code (where | denotes the caret position):

Original Input Result
msgbox |
"
MsgBox "|"

Closing a Pair

Self-closing pairs close themselves, so when the caret is immediately to the left of a pair-closing character, typing that pair-closing character will simply move the caret one position to the right - resulting in exactly the same outcome as if you would have typed it:

Original Input Result
MsgBox "|"
"
MsgBox ""|

Nesting Pairs

If the next character isn't the pair-closing character being input, and that input matches a SCP opening character, pairs get nested:

Original Input Result
msgbox (|)
"
MsgBox ("|")

Backspace

The BACK key is handled by a SCP when the caret immediately follows a pair-opening character... and that the pair is closed. If handled, both the pair-opening and pair-closing characters are removed from the logical line, i.e. irrespective of line continuations.

Original Input Result
MsgBox "|"
{BACK}
MsgBox|
Debug.Print ((|2 + 2) + _
             (2 - 2) - _
             (2 + 2))
{BACK}
Debug.Print (|2 + 2 + _
             (2 - 2) - _
             (2 + 2))
Debug.Print (|(2 + 2) + _
             (2 - 2) - _
             (2 + 2))
{BACK}
Debug.Print |(2 + 2) + _
             (2 - 2) - _
             (2 + 2)

Smart-Concat

When typing a multi-line string literal, Rubberduck assists by automating the " & _ on the caret line, and the {indent}"|" on the next line... keeping in mind that VBA will not compile an instruction that spans more than 25 lines.

Original Input Result
MsgBox "Lorem ipsum dolor sit amet, |"
{ENTER}
MsgBox "Lorem ipsum dolor sit amet, " & _
       "|"
MsgBox "Lorem ipsum dolor sit amet, |"
{CTRL}+{ENTER}
MsgBox "Lorem ipsum dolor sit amet, " & vbNewLine & _
       "|"

In both cases, when the caret line contains only a pair of double quotes, the smart-concatenation can be undone by a single BACK keypress:

Original Input Result
MsgBox "Lorem ipsum dolor sit amet, " & vbNewLine & _
       "|"
{BACK}
MsgBox "Lorem ipsum dolor sit amet, |"

Block Completion

[coming soon...ish]

Clone this wiki locally