[relay-lsp] Suggestions from wrong context
Last week, while working on a rather large file, I noticed that the LSP would often not suggest any fields or fields from a different level in the selection set.
The issue is easily reproduced (more so in larger files) if you accept the completion item for a field returning an object type.
In response to that the LSP will insert a snippet like the following:
newObjectField {
$cursor
}
Afterwards it triggers the editor.action.triggerSuggest command to reopen the completion menu at the new cursor position.
What's happening is that the textdocument/didchange and textdocument/completion commands from the client are not necessarily sent in that order and if they are, the LSP might still be processing the document update while completion items are already being calculated at the new cursor position. And of course, if the LSP is computing completion items for the new location, while still working with the old document, weird things can happen like no suggetions because the position is outside the GraphQL document or the position being in a different selection set.
I think there are two things at play here that both need to be addressed in order to correct the behavior:
- Relay should wait to calculate completion items until all sync operations are completed - this can be easily done and I will send a PR for it in a follow-up. EDIT: https://github.com/facebook/relay/pull/4473
~~2. There seems to be a bug in the LSP client (VS Code in this case) where
textdocument/didchangeandtextdocument/completionare not sent in this order if a completion item triggerseditor.action.triggerSuggest- I will raise an issue for this in their repository.~~
There actually doesn't appear to be an issue with the LSP client (VSCode). textdocument/didchange and textdocument/completion actually arrive in the correct order at the server. They just aren't processed in the correct order:
I'm still investigating why...
This would be fantastic to fix indeed.
Got another report from a team struggling with this today. Would be awesome if my fix in https://github.com/facebook/relay/pull/4473 could be considered to be merged. Honestly surprised this isn't a problem at Meta or other big companies...
I'm out this week but Cc @tyao1