As for the occasion, I think it was seeing Microsoft recently open-source a new static analysis tool pyright — a static analysis tool for Python; such tools are mainly used for syntax checking and completion. I have been using YouCompleteMe all along, so spending some time to see how it works is worthwhile. This post will be updated often; I’ll share some thoughts from my debugging process.
Basic Principle
When using YouCompleteMe, a ycmd process auto-starts in the background — this is the server side.
When completing, the relevant code snippet is sent to the server, which returns possible completions.
Under the plugin directory (~/.vim/plugged/YouCompleteMe):
- the server side is in
third_party/ycmd - the client side is in
python/ycm
The two communicate via the HTTP protocol.
A Few Patches
Since YouCompleteMe starts ycmd, I’ll just modify that part of the code; we manually start ycmd
to debug the server side.
Client-side patch
This patch mainly does 4 things:
- Fixes the
hmac_secretso it doesn’t need regenerating each time when debugging; - Fixes the local HTTP port (30827 in this post);
- Comments out
utils.SafePopenbelow soycmdisn’t started directly whenvimstarts; - Doesn’t generate a temporary config file
1 | diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py |
Server-side patch
This patch does 2 things:
- Prevents ycmd from deleting the options_file on exit — using our own file, it must not be deleted.
- On receiving the shutdown signal, don’t shut down, to avoid constantly restarting the server side.
1 | diff --git a/ycmd/__main__.py b/ycmd/__main__.py |
Manually starting ycmd
When starting, specify the options_file and our pre-assigned port:
1 | > /usr/bin/python \ |
ycmd.json — this file is generated by ycm; I saved a separate copy here that can be used directly
TL; DR click to expand
1 | { |
Debugging with Tornado
ycmd uses the bottle framework; it can also be debugged with tornado. Everyone is welcome to try it yourselves.