Code suggestions are not as good as they used to be

I use my own custom editor for Swift development, and I started to have a feeling that code suggestions in later versions of Swift have lost their telepathic magic of older versions. To check if that is only a false feeling or a fact, I found an old screenshot of code suggestions for a simple example code snippet (I believe it was made with some Swift 5.x or maybe early Swift 6.x), and reproduced the same with Swift 6.2. And indeed, code suggestions are sorted differently, and not in favor of newer Swift:

Before (Swift 5.x)

After (Swift 6.2)

I think this needs some attention from the sourcekit-lsp developers.

Hi @dima_kozhinov,

I just tried reproducing this and seeing results that are closer to you Before screenshot. Could you file an issue for this at GitHub - swiftlang/sourcekit-lsp: Language Server Protocol implementation for Swift and C-based languages. From your screenshots, it seems like you’re reproducing this in a very small project, if you could attach that as well as a diagnose bundle you can generate by running sourcekit-lsp diagnose in Terminal, that would greatly help us investigate what’s going on.

Filed an issue: Code suggestions are not sorted as expected · Issue #2325 · swiftlang/sourcekit-lsp · GitHub

I have a guess: Maybe the sourcekit-lsp language server now returns code suggestions unsorted (in no particular order), and it is now a client responsibility to sort them according to some client-guessed rules?

Since this is only a guess, I leave the github issue open for now.

Do we have sourcekit-lsp documentation?

Edit: I found the documentation: sourcekit-lsp/Documentation at main · swiftlang/sourcekit-lsp · GitHub
It is quite rudimentary TBH. The Editor Integration -> Building a new SourceKit-LSP Client section is just one paragraph (half a page of text).

Edit2: We also have Microsoft's documentation: Specification and I must admit it says

usually the client is responsible for filtering and sorting

But in earlier versions of Swift sourcekit-lsp returned nicely sorted code suggestions, and now it does not. Instead it provides mysterious key.semantic_score and key.text_match_score that are not documented anywhere, and I can only guess that a client (editor) should use one of them (or both somehow) to sort the code suggestions.

Edit3: The key.semantic_score and key.text_match_score appear in sourcekit-lsp diagnostoc log, not its actual response to the client. In its response to the client there is Microsoft-documented sortText property, which apparently should be used by the client to sort code suggestions.

The bug was on the client side (in my editor). It failed to sort the code suggestions received from sourcekit-lsp, and instead relied on receiving code suggestions already sorted on the server side (which worked in earlier versions of Swift). I closed the github issue, sorry for the noise. :sweat_smile:

The magic is back:

2 Likes

How did you fix that?

1 Like

I found out that the latest version of Swift (the sourcekit-lsp component of it to be correct) does not provide code suggestions in a sorted order anymore (how earlier versions of Swift did), and instead it provides a sortText property for every code suggestion item that can be used at client side (my editor) to sort code suggestions so that more relevant code suggestions come first. My editor now sorts them, and they appear in perfect order.

1 Like

Is this editor a product, you'd like to plug?

Yes, it's a product. It is closed source and commercial (sorry for that, but it has free trial period). https://www.plainswift.com/

1 Like