Shopify / ruby-lsp

An opinionated language server for Ruby
https://shopify.github.io/ruby-lsp/
MIT License
1.52k stars 144 forks source link

Avoid mutating completion item in resolve #2009

Closed vinistock closed 4 months ago

vinistock commented 4 months ago

Motivation

Closes #1983

Like the author of the issue, I had also noticed that we stopped using short names for constants in completion. Which puzzled me a lot given that we explicitly handle that and have unit tests to verify the behaviour.

After reading the specification again, I finally realized the issue.

When we split completion into completion + resolve, there was a mistake in the original PR. The resolve request should never mutate any existing fields of the completion item other than label details and documentation. But instead, we were creating a new completion item from scratch without including all of the original fields.

That means that the insertText we were specifying in completion was getting erased during resolve, which forces the editor to use the label instead, resulting in the behaviour of showing the fully qualified name.

Implementation

We never want to mutate anything in resolve that isn't label details and documentation. We should take the original item as a hash, change only those fields and then return it.

This fixes the issue of using the wrong sorting text and the wrong insertion text, which we're currently seeing in main.

Automated Tests

Improved the resolve test a bit.