6

Recently I am using clang_complete to do C++ code completion. It is good and fast for small program but too slow for my case (I am working on large code base and usually one file takes several seconds to compile), even if I used libclang, which can cache some parsed results to speedup later parsing, if I understand correctly.

Currently clang_complete will block in ClangComplete until libclang finishes parsing. Even though it starts a worker thread, main thread still repeatedly checks if user pressed CTRLC or the worker thread completes successfully. During this period, vim becomes irresponsive and thus makes this plugin hard to use.

I want to make some improvement to this behavior, for example, ClangComplete will not block, but return empty results if it takes longer than 0.2 seconds, while the thread is still running. When libclang finishes its parsing, and it detects that I am still typing the same completion word, it will popup a completion menu.

The difficulties for this is:

  1. how to popup a menu at that time, without causing some subtle race conditions between different threads,
  2. how does it know whether I am still typing the same completion word? I think vim itself keep track of this, because when I type something wrong, for example, std::strang instead of std::string, then I type backspace to delete the wrong ang, the completion menu will show up again. So how do I access to this internal flag?
Peeter Joot
  • 7,848
  • 7
  • 48
  • 82
Kan Li
  • 8,557
  • 8
  • 53
  • 93

1 Answers1

0
  1. Vimscript is single-threaded; you won't have to worry about races.

  2. Vim will pass the base (i.e. the part of the completion word already typed / completed) into your function. Check out :help complete-functions for details and an example.

In general, your approach (assuming you're using an embedded language like Python or Perl for the multi-threading) should be feasible; however, I haven't seen something like that attempted yet.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324