August Feng

lsp-mode and web-mode hanging on css completion

About

I'm using Spacemacs, and the Emacs UI hangs for a few seconds when I select a completion value while authoring css properties.

Studying the completion framework

The completion framework provided by Spacemacs is I'm using is company and the completion backend selected when I edit html is completion-capf which is bridge to the native completion system in Emacs.

We can see which completion backend is being used by using the M-x: company-diag command.

The company-dig command will even tell you the string prefix being used for completion.

In my case, the completion-at-point-functions variable has lsp-completion-at-point as the first value, so the buffer will use lsp in order to get completion values.

Back on the right track

The list of completions pop up relatively quickly, so I'm assuming the problem can't be the lsp client/server.

When the completion finally ends, I see this message:

[yas] Error running #[128 \302\301\303\300""\207 [(yas-after-exit-snippet-hook) run-hooks apply append] 6 (subr.elc . 20473)]: Timeout while waiting for response. Method: textDocument/rangeFormatting

This is when I learned that the yasnippet system has hooks. If I remove the hook, then the UI no longer hangs:

  (set 'yas-after-exit-snippet-hook nil)

For what it's worth, the variable is added by the web-mode package:

  (add-hook 'yas-after-exit-snippet-hook
            'web-mode-yasnippet-exit-hook
            t t)

It's also buffer local, so I'll just use a .dir-locals.el file to override it per project:

  ((web-mode . ((yas-after-exit-snippet-hook . nil))))

A proper solution would be to investigate the textDocument/rangeFormatting call and see if that's what causing the hang, but I have things to do.