0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00
Girish Palya af9a7a04f1
patch 9.1.1590: cannot perform autocompletion
Problem:  cannot perform autocompletion
Solution: Add the 'autocomplete' option value
          (Girish Palya)

This change introduces the 'autocomplete' ('ac') boolean option to
enable automatic popup menu completion during insert mode. When enabled,
Vim shows a completion menu as you type, similar to pressing |i\_CTRL-N|
manually. The items are collected from sources defined in the
'complete' option.

To ensure responsiveness, this feature uses a time-sliced strategy:

- Sources earlier in the 'complete' list are given more time.
- If a source exceeds its allocated timeout, it is interrupted.
- The next source is then started with a reduced timeout (exponentially
  decayed).
- A small minimum ensures every source still gets a brief chance to
  contribute.

The feature is fully compatible with other |i_CTRL-X| completion modes,
which can temporarily suspend automatic completion when triggered.

See :help 'autocomplete' and :help ins-autocompletion for more details.

To try it out, use :set ac

You should see a popup menu appear automatically with suggestions. This
works seamlessly across:

- Large files (multi-gigabyte size)
- Massive codebases (:argadd thousands of .c or .h files)
- Large dictionaries via the `k` option
- Slow or blocking LSP servers or user-defined 'completefunc'

Despite potential slowness in sources, the menu remains fast,
responsive, and useful.

Compatibility: This mode is fully compatible with existing completion
methods. You can still invoke any CTRL-X based completion (e.g.,
CTRL-X CTRL-F for filenames) at any time (CTRL-X temporarily
suspends 'autocomplete'). To specifically use i_CTRL-N, dismiss the
current popup by pressing CTRL-E first.

---

How it works

To keep completion snappy under all conditions, autocompletion uses a
decaying time-sliced algorithm:

- Starts with an initial timeout (80ms).
- If a source does not complete within the timeout, it's interrupted and
  the timeout is halved for the next source.
- This continues recursively until a minimum timeout (5ms) is reached.
- All sources are given a chance, but slower ones are de-prioritized
  quickly.

Most of the time, matches are computed well within the initial window.

---

Implementation details

- Completion logic is mostly triggered in `edit.c` and handled in
  insexpand.c.

- Uses existing inc_compl_check_keys() mechanism, so no new polling
  hooks are needed.

- The completion system already checks for user input periodically; it
  now also checks for timer expiry.

---

Design notes

- The menu doesn't continuously update after it's shown to prevent
  visual distraction (due to resizing) and ensure the internal list
  stays synchronized with the displayed menu.

- The 'complete' option determines priority—sources listed earlier get
  more time.

- The exponential time-decay mechanism prevents indefinite collection,
  contributing to low CPU usage and a minimal memory footprint.

- Timeout values are intentionally not configurable—this system is
  optimized to "just work" out of the box. If autocompletion feels slow,
  it typically indicates a deeper performance bottleneck (e.g., a slow
  custom function not using `complete_check()`) rather than a
  configuration issue.

---

Performance

Based on testing, the total roundtrip time for completion is generally
under 200ms. For common usage, it often responds in under 50ms on an
average laptop, which falls within the "feels instantaneous" category
(sub-100ms) for perceived user experience.

| Upper Bound (ms) | Perceived UX
|----------------- |-------------
| <100 ms          | Excellent; instantaneous
| <200 ms          | Good; snappy
| >300 ms          | Noticeable lag
| >500 ms          | Sluggish/Broken

---

Why this belongs in core:

- Minimal and focused implementation, tightly integrated with existing
  Insert-mode completion logic.
- Zero reliance on autocommands and external scripting.
- Makes full use of Vim’s highly composable 'complete' infrastructure
  while avoiding the complexity of plugin-based solutions.
- Gives users C native autocompletion with excellent responsiveness and
  no configuration overhead.
- Adds a key UX functionality in a simple, performant, and Vim-like way.

closes: #17812

Signed-off-by: Girish Palya <girishji@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
2025-07-25 18:57:04 +02:00
..
2024-01-02 16:51:11 +01:00
2025-07-15 20:06:49 +02:00
2024-07-17 20:04:22 +02:00
2024-01-02 16:51:11 +01:00
2018-07-21 05:56:22 +02:00
2024-08-20 19:57:00 +02:00
2024-01-02 16:51:11 +01:00
2025-07-15 20:06:49 +02:00
2024-09-29 10:10:51 +02:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2021-06-27 15:18:56 +02:00
2022-02-26 12:25:45 +00:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2025-07-15 20:06:49 +02:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-12-17 20:24:24 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2024-01-02 16:51:11 +01:00
2020-07-26 17:00:44 +02:00
2020-07-26 17:00:44 +02:00
2020-07-26 17:00:44 +02:00
2020-07-26 17:00:44 +02:00
2020-07-26 17:00:44 +02:00
2004-06-13 20:20:40 +00:00
2018-07-15 20:20:18 +02:00
2018-07-15 20:20:18 +02:00
2012-05-18 13:46:39 +02:00
2012-05-18 13:46:39 +02:00
2022-01-16 14:46:06 +00:00
2010-01-06 20:52:26 +01:00
2024-08-20 19:57:00 +02:00
2018-07-15 20:20:18 +02:00
2018-07-15 20:20:18 +02:00
2010-01-06 20:52:26 +01:00
2024-01-02 16:51:11 +01:00
2024-09-17 19:12:03 +02:00
2024-09-17 19:03:16 +02:00