Asynchronous completion contributor
已回答
Hi, our team wrote a completion contributor that fetches candidates from a web service. The web service is kind of slow (200 ~ 1000 ms) and we don't want to block the default completion list from showing up. Is it possible to let the default completion popup list show up first and dynamically inject the fetched results later? Or what solution do you suggest if the completion contributor generally takes more time to compute?
Thanks.
请先登录再写评论。
Completion contributors are already run in background, so if your contributor produces results after 1 second of waiting, the user will just see that result in 1 second, and an empty "Loading..." popup before that.
But there's an important prerequisite: to avoid UI freezes your completion thread should be cancellable at any moment. So it's a bad idea to do blocking web requests from it directly, since it then wouldn't be able to do "checkCanceled". A common solution is to start another thread for web request, and wait for its results in completion thread. You can use ApplicationUtil#runWithCheckCanceled for that.
Thanks for the response!
We are now following the example here:
https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/toml/CargoTomlDependencyCompletionProvider.kt#L49
https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/toml/CratesIoApi.kt#L42
https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/toml/CratesIoApi.kt#L62
However the completion popup only shows up after the web request completes. Is it possible to let the default completion popup list show up first and dynamically inject the fetched results later?
Please try registering this completion contributor with `order="last"` in plugin.xml