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
Show file tree
Hide file tree
Changes from all 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
20 changes: 16 additions & 4 deletions ghcide/src/Development/IDE/GHC/Compat/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ module Development.IDE.GHC.Compat.Core (
SrcLoc.RealSrcSpan,
pattern RealSrcSpan,
SrcLoc.RealSrcLoc,
SrcLoc.SrcLoc(..),
pattern RealSrcLoc,
SrcLoc.SrcLoc(SrcLoc.UnhelpfulLoc),
BufSpan,
SrcLoc.leftmost_smallest,
SrcLoc.containsSpan,
Expand Down Expand Up @@ -511,7 +512,7 @@ import GHC.Types.TyThing.Ppr
#else
import GHC.Types.Name.Set
#endif
import GHC.Types.SrcLoc (BufSpan, SrcSpan (UnhelpfulSpan))
import GHC.Types.SrcLoc (BufPos, BufSpan, SrcSpan (UnhelpfulSpan), SrcLoc(UnhelpfulLoc))
import qualified GHC.Types.SrcLoc as SrcLoc
import GHC.Types.Unique.Supply
import GHC.Types.Var (Var (varName), setTyVarUnique,
Expand Down Expand Up @@ -637,10 +638,11 @@ import Var (Var (varName), setTyVarUnique,
#if MIN_VERSION_ghc(8,10,0)
import Coercion (coercionKind)
import Predicate
import SrcLoc (SrcSpan (UnhelpfulSpan))
import SrcLoc (SrcSpan (UnhelpfulSpan), SrcLoc (UnhelpfulLoc))
#else
import SrcLoc (RealLocated,
SrcSpan (UnhelpfulSpan))
SrcSpan (UnhelpfulSpan),
SrcLoc (UnhelpfulLoc))
#endif
#endif

Expand All @@ -651,6 +653,7 @@ import System.FilePath

#if !MIN_VERSION_ghc(9,0,0)
type BufSpan = ()
type BufPos = ()
#endif

pattern RealSrcSpan :: SrcLoc.RealSrcSpan -> Maybe BufSpan -> SrcLoc.SrcSpan
Expand All @@ -662,6 +665,15 @@ pattern RealSrcSpan x y <- ((,Nothing) -> (SrcLoc.RealSrcSpan x, y)) where
#endif
{-# COMPLETE RealSrcSpan, UnhelpfulSpan #-}

pattern RealSrcLoc :: SrcLoc.RealSrcLoc -> Maybe BufPos-> SrcLoc.SrcLoc
#if MIN_VERSION_ghc(9,0,0)
pattern RealSrcLoc x y = SrcLoc.RealSrcLoc x y
#else
pattern RealSrcLoc x y <- ((,Nothing) -> (SrcLoc.RealSrcLoc x, y)) where
RealSrcLoc x _ = SrcLoc.RealSrcLoc x
#endif
{-# COMPLETE RealSrcLoc, UnhelpfulLoc #-}


pattern AvailTC :: Name -> [Name] -> [FieldLabel] -> Avail.AvailInfo
#if __GLASGOW_HASKELL__ >= 902
Expand Down
42 changes: 38 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 All @@ -47,6 +47,7 @@ import Ide.Types
import qualified Language.LSP.Server as LSP
import Language.LSP.Types
import qualified Language.LSP.VFS as VFS
import Text.Fuzzy.Parallel (Scored (..))

descriptor :: PluginId -> PluginDescriptor IdeState
descriptor plId = (defaultPluginDescriptor plId)
Expand Down Expand Up @@ -156,17 +157,50 @@ 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.

Moreover, the order prioritizes qualifiers, for instance, given:

import qualified MyModule
foo = MyModule.<complete>

The identifiers defined in MyModule will be listed first, followed by other
identifiers in importable modules.

According to the LSP specification, if no sortText is provided, the label is used
to sort alphabetically. Alphabetical ordering is almost never what we want,
so we force the LSP client to respect our ordering by using a numbered sequence.
-}

orderedCompletions :: [Scored CompletionItem] -> [CompletionItem]
orderedCompletions [] = []
orderedCompletions xx = zipWith addOrder [0..] xx
where
lxx = digits $ Prelude.length xx
digits = Prelude.length . show

addOrder :: Int -> Scored CompletionItem -> CompletionItem
addOrder n Scored{original = it@CompletionItem{_label,_sortText}} =
it{_sortText = Just $
T.pack(pad lxx n)
}

pad n x = let sx = show x in replicate (n - Prelude.length sx) '0' <> sx

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

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
Loading