clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
629 stars 106 forks source link

There is no (configurable) delay while typing. #262

Open strpbrk opened 2 years ago

strpbrk commented 2 years ago

Allow me to ask for this feature, even though I fear that this feature would need to be implemented in the language client project. Nevertheless the configuration option would need to be supported here...

There doesn't seem to be a configurable, or at least significant delay while typing. I would expect that semantic information, or at the very least the list of found problems would not jump around while I'm typing. If the former is necessary, at least the latter should not be. The subjective feeling is that I'm working on an old computer with unnecessary distractions jumping all over the screen. (I have auto suggestions and all auto stuff turned off).

If there is nothing you can do, could you please point me to other resources, such as open issues on the language client project, a missing feature elsewhere, or at least an explanation why this would not be possible any time soon. I have tried, but my search has not found anything of significance.

Thanks!

sam-mccall commented 2 years ago

The server is in charge of publishing diagnostics. The client could suppress updates to diagnostics while typing - this seems likely to be a vscode-level feature rather than even the vscode-languageclient library. I don't have any particular insight there.

But the server can also delay computing/pushing diagnostics while it's seeing updates from the client. We actually do have this code in clangd (a debounce delay before reparsing for diagnostics on the server side), but it's tuned for responsiveness: the delay is equal to the parse time for the file, but never less than 50 or more than 500ms.

Before exposing this it would probably be nice to know whether it actually does what you want: these things are about "feel" and hard to predict. If you're able to build clangd locally, you could add this line to clang-tools-extra/clangd/tool/ClangdMain.cpp, near where other options are set:

Opts.UpdateDebounce = DebouncePolicy::fixed(std::chrono::seconds(5));
strpbrk commented 2 years ago

I have built and tested clangd. Verified that the option is correctly used in "TUScheduler.cpp" to create a Deadline. This had no effect on the output, certainly no delay of 5s.

However, even if this worked, I would have to disagree that it is a correct solution. The server should not be concerned about things that alter the user experience. It simply provides a service, and it is just fine that it is tuned for responsiveness. It is up to the client to do one of two things (or both):

  1. Send text changes after a short delay, or after the user stops typing.
  2. Apply updates from the server with a delay, or after the user stops typing. Microsoft's plugin has "intelliSenseUpdateDelay" option to control one of these (although I didn't try their plugin).

I have poked around the code, and have found that the first feature could potentially be implemented by providing a "middleware.didChange" handler that would delay before invoking "next". I got a simple delay working as a proof-of-concept, and it indeed holds before sending changes to the server. The code in the language client library seems dirty and not well suited for a proper implementation that should group changes and flush them on a completion request, but newer versions seem to have improved this.

I have to say that I find it very weird that such a great plugin would generate and send one event per key press to the language server (unless you are cutting or pasting selections), although it does indeed seem it is an issue for the language client library.

strpbrk commented 2 years ago

I have implemented this feature, and it seems to be working properly, at least for my environment. It uses latest dependencies, so I cannot create a pull request.

Issue #151 is similar to this one, but it only complains about flashing status bar, not for the reason behind it. The status bar will be silent with this change... as it should.

Please find changes in my forked repository: https://github.com/strpbrk/vscode-clangd

HighCommander4 commented 2 years ago

It uses latest dependencies, so I cannot create a pull request.

We do update dependencies from time to time, so you could create a pull request that also updates dependencies.

strpbrk commented 2 years ago

Let me use the plugin for a while, and I will do that. I just made a significant simplification related to my initial version anyway. In the meantime, it is there for anyone to try...

timmy-newtron commented 1 year ago

Hey guys, I just ran into the same issue: on a rather old notebook the constant LSP calls cause quite some heavy load. I did a similar implementation to that of @strpbrk but ditched it for his version as that one looked cleaner. On the current main branch @strpbrk's changes don't need dependency updates anymore. I did however notice some jumping inline decorations and breaking syntax highlighting. @strpbrk can you confirm this?

strpbrk commented 1 year ago

Thank you for the kind words, @edding4500. So far, I have been using plugin v0.1.17 with my modifications.

There is an issue with my changes - if an edit is made through an operation, such as refactoring or using a code fix, while there are pending edits in affected lines, it might mess up the code in those lines. Simply editing a file, including copy/paste of blocks of text, works great for me. Before refactoring, I just save the file, which flushes pending changes.

To fix this, the same thing "didSave" does should be done before an operation is performed, but I didn't find an appropriate extension point, and piercing a few frameworks would just make this pull request even less desirable. But, I don't think this is what you are describing...

When I find a bit of time, I will sync up to the master and see what happens, and will also look at changes in dependencies. Feel free to describe your issues in more detail, or maybe make a video or a gif. What I have been using has no distracting features, as long as I save before refactoring.

isgursoy commented 4 months ago

image

Qt creator handles this situation quite well. Analysis does not start as long as there is another modification in next n msecs, and you just wait for single last pass.

Without fixing this issue, VS Code is not an option for Clangd.