WIP: Lint on FocusGained/VimResume#2494
WIP: Lint on FocusGained/VimResume#2494andreypopp wants to merge 1 commit intodense-analysis:masterfrom
Conversation
This commits allow to configure ALE to lint when vim gains focus or is resumed after being suspended (C-z and then `fg` command). Not that for this to work with tmux one must set ``` set -g focus-events on ``` in their `tmux.conf`.
w0rp
left a comment
There was a problem hiding this comment.
Add tests for the new event, and update the tests that check which autocmd commands are set.
| endfunction | ||
|
|
||
| function! ale#events#FocusGained(buffer) abort | ||
| call setbufvar(a:buffer, 'force_changedtick', 1) |
There was a problem hiding this comment.
We shouldn't force updates to be sent to language servers here and below. We should only send updates if the buffer has actually changed. If b:changedtick isn't updated because some other process updated the buffer and Vim didn't increment b:changedtick, we can do something that causes b:changedtick to increment in response to the buffer being changed, maybe comparing the text before and after Vim lost focus. If the buffer hasn't been changed, no messages should be sent to the language servers.
There was a problem hiding this comment.
This is done so we can request server to recompute diagnostics. Unfortunate LSP lacks such method and I see you’ve also noted that — microsoft/language-server-protocol#737
There was a problem hiding this comment.
I understand. I think we shouldn't request diagnostics again if the buffer hasn't changed.
There was a problem hiding this comment.
Do you think we can add such feature (requesting diagnostics via sending didChange events) configurable for specific LSP linters? In my case I'm working on ocamlmerlin-lsp which only works per-file — and if I run a build system — I want to re-request diagnostics from the linter no matter if buffer was changed or not (there could be new/no errors just because of the build process finished).
There was a problem hiding this comment.
Btw such behaviour can be also useful for non-LSP linters — they don't work with "push" model (I think LSP is designed around this) also, instead you ask / "pull" diagnostics from them.
There was a problem hiding this comment.
It won't work for all language servers. Languages servers don't always respond to textDocument/didChange with diagnostics. The Language Server Protocol needs to be changed to support explicitly requesting diagnostics for such a thing to work. The protocol doesn't offer any guarantees about what will trigger diagnostics being sent, when they will arrive, or if they will ever be sent. I think it's one of the flaws of the protocol.
tsserver does have an explicit command for requesting diagnostics and pretty much guarantees a response, so we could add a command to request for diagnostics for tsserver.
There was a problem hiding this comment.
Right, I understand that.
That's why I'm asking if we can make this configurable per-linter so it works with ocamlmerlin-lsp (either via textDocument/didChange or I can add some new type of request to query for diagnostics as a protocol extension).
There was a problem hiding this comment.
I think we should follow the protocol and only send a message for a document being changed when it has changed, and push for the protocol to be extended to allow user to request diagnostics explicitly, like you can with tsserver.
There was a problem hiding this comment.
Unfortunately I don't think I can wait till it's standardised in LSP — standard bodies move slowly. I prefer standards instead be informed by real world usage. I think I'll be able to patch it up on top of ALE, not optimal but doable.
|
|
||
| function! ale#events#FocusGained(buffer) abort | ||
| call setbufvar(a:buffer, 'force_changedtick', 1) | ||
| call ale#Queue(0, 'lint_file', a:buffer) |
There was a problem hiding this comment.
Check g:ale_lint_on_focus_gained here again, and probably ale_enabled like we do above. Otherwise it will be difficult to turn this event off after the autocmd command has been set.
| let g:ale_lint_on_filetype_changed = get(g:, 'ale_lint_on_filetype_changed', 1) | ||
|
|
||
| " This flag can be set to 1 to enable linting when vim gains focus. | ||
| let g:ale_lint_on_focus_gained = get(g:, 'ale_lint_on_focus_gained', 1) |
There was a problem hiding this comment.
Document the option and turn it off by default. There are thousands of people using ALE, and many of them will have told ALE not to lint their buffers when they enter a file. They will be surprised if it starts linting their buffers when Vim regains focus after they update ALE.
There was a problem hiding this comment.
Hm... maybe we should use an existent check g:ale_lint_on_enter, what do you think? Not sure adding an extra option is worth it considering the semantics of "entering" is so poorly defined.
There was a problem hiding this comment.
Also — maybe we can make such behaviour configurable per-linter?
There was a problem hiding this comment.
I think a new option is appropriate, but it should be off by default.
|
You can get support for linting buffers on |
|
Yeah, good point. |
|
I'm closing old pull requests that didn't go anywhere, or I didn't have time to look at. Re-open the pull request or submit a new one if you have something you think can be merged. |
This PR allows to configure ALE to lint when vim gains focus or is resumed after
being suspended (C-z and then
fgcommand).The motivation for this is that some linters do not watch all needed resources
(build artifacts) and thus they require to be nudged after those are changed.
The good way I found is to do that when you switch back to an editor.
Example workflow:
artifacts
diagnostics for the buffer but right now I have to save buffer manually so it
triggers the lint (and didSave event for an LSP). With with PR vim sends
didChange event to LSP / requests lints automatically when I switch to back
to it.
Not that for this to work with tmux one must set
in their
tmux.conf.Note also that I've added
b:force_changedtickwhich forces to send LSP didChange event even if there were no changes.