Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include sortText in completions and improve suggestions #2332

Merged
merged 20 commits into from
Nov 10, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions ghcide/src/Development/IDE/Plugin/Completions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import Development.IDE.GHC.ExactPrint (Annotated (annsA)
import Development.IDE.GHC.Util (prettyPrint)
import Development.IDE.Graph
import Development.IDE.Graph.Classes
import qualified Development.IDE.Types.KnownTargets as KT
import Development.IDE.Plugin.CodeAction (newImport,
newImportToEdit)
import Development.IDE.Plugin.CodeAction.ExactPrint
Expand All @@ -39,6 +38,7 @@ import Development.IDE.Plugin.Completions.Types
import Development.IDE.Types.Exports
import Development.IDE.Types.HscEnvEq (HscEnvEq (envPackageExports),
hscEnv)
import qualified Development.IDE.Types.KnownTargets as KT
import Development.IDE.Types.Location
import GHC.Exts (fromList, toList)
import GHC.Generics
Expand Down Expand Up @@ -156,17 +156,54 @@ getCompletionsLSP ide plId
let clientCaps = clientCapabilities $ shakeExtras ide
config <- getCompletionsConfig plId
allCompletions <- liftIO $ getCompletions plId ideOpts cci' parsedMod bindMap pfix' clientCaps config moduleExports
pure $ InL (List allCompletions)
pure $ InL (List $ orderedCompletions allCompletions)
_ -> return (InL $ List [])
_ -> return (InL $ List [])
_ -> return (InL $ List [])

{- COMPLETION SORTING
We return an ordered set of completions (local -> nonlocal -> global).
Ordering is important because local/nonlocal are import aware, whereas
global are not and will always insert import statements, potentially redundant.

On the other hand the fuzzy sort algorithm doesn't always sort in the optimal way,
there is room for the LSP client to improve.

According to the LSP specification, if no sortText is provided, the label is used.
To allow the LSP client to reorder identifiers while preserving the relative ordering
of repeated occurrences we generate sortText values that include both the label and
an index denoting the relative order
pepeiborra marked this conversation as resolved.
Show resolved Hide resolved

EXAMPLE
We produce completions:
x -- local
y -- local
x -- global
y -- global

The LSP client decides to present:
y -- local
y -- global
x -- local
x -- global

This is fine if the LSP client thinks that 'y' is more relevant than 'x'.
We are OK with that choice since the local options are presented before the global ones

-}
orderedCompletions :: [CompletionItem] -> [CompletionItem]
orderedCompletions = zipWith addOrder [0..]
where
addOrder :: Int -> CompletionItem -> CompletionItem
addOrder n it@CompletionItem{_label} =
it{_sortText = Just $ _label <> T.pack(show n)}

----------------------------------------------------------------------------------------------------

toModueNameText :: KT.Target -> T.Text
toModueNameText target = case target of
KT.TargetModule m -> T.pack $ moduleNameString m
_ -> T.empty
KT.TargetModule m -> T.pack $ moduleNameString m
_ -> T.empty

extendImportCommand :: PluginCommand IdeState
extendImportCommand =
Expand Down