Skip to content

Commit 50d447d

Browse files
committed
Allow varying the matcher used in completions
1 parent 76ccaf3 commit 50d447d

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import qualified Language.LSP.Protocol.Lens as JL
5555
import qualified Language.LSP.Protocol.Message as LSP
5656
import Language.LSP.Protocol.Types
5757
import qualified Language.LSP.VFS as VFS
58+
import qualified Text.Fuzzy.Levenshtein as Fuzzy
59+
import qualified Text.Fuzzy.Parallel as Fuzzy
5860
import Text.Regex.TDFA
5961

6062
data Log
@@ -234,7 +236,9 @@ fieldSuggestCodeAction recorder ide _ (CodeActionParams _ _ (TextDocumentIdentif
234236
fakeLspCursorPosition = Position lineNr (col + fromIntegral (T.length fieldName))
235237
lspPrefixInfo = Ghcide.getCompletionPrefixFromRope fakeLspCursorPosition fileContents
236238
cabalPrefixInfo = Completions.getCabalPrefixInfo fp lspPrefixInfo
237-
completions <- liftIO $ computeCompletionsAt recorder ide cabalPrefixInfo fp cabalFields
239+
completions <- liftIO $ computeCompletionsAt recorder ide cabalPrefixInfo fp cabalFields $
240+
Fuzzy.Matcher $
241+
Fuzzy.levenshteinScored Fuzzy.defChunkSize
238242
let completionTexts = fmap (^. JL.label) completions
239243
pure $ FieldSuggest.fieldErrorAction uri fieldName completionTexts _range
240244

@@ -365,12 +369,21 @@ completion recorder ide _ complParams = do
365369
Just (fields, _) -> do
366370
let lspPrefInfo = Ghcide.getCompletionPrefixFromRope position cnts
367371
cabalPrefInfo = Completions.getCabalPrefixInfo path lspPrefInfo
368-
let res = computeCompletionsAt recorder ide cabalPrefInfo path fields
372+
res = computeCompletionsAt recorder ide cabalPrefInfo path fields $
373+
Fuzzy.Matcher $
374+
Fuzzy.simpleFilter Fuzzy.defChunkSize Fuzzy.defMaxResults
369375
liftIO $ fmap InL res
370376
Nothing -> pure . InR $ InR Null
371377

372-
computeCompletionsAt :: Recorder (WithPriority Log) -> IdeState -> Types.CabalPrefixInfo -> FilePath -> [Syntax.Field Syntax.Position] -> IO [CompletionItem]
373-
computeCompletionsAt recorder ide prefInfo fp fields = do
378+
computeCompletionsAt
379+
:: Recorder (WithPriority Log)
380+
-> IdeState
381+
-> Types.CabalPrefixInfo
382+
-> FilePath
383+
-> [Syntax.Field Syntax.Position]
384+
-> Fuzzy.Matcher T.Text
385+
-> IO [CompletionItem]
386+
computeCompletionsAt recorder ide prefInfo fp fields matcher = do
374387
runMaybeT (context fields) >>= \case
375388
Nothing -> pure []
376389
Just ctx -> do
@@ -390,6 +403,7 @@ computeCompletionsAt recorder ide prefInfo fp fields = do
390403
case fst ctx of
391404
Types.Stanza _ name -> name
392405
_ -> Nothing
406+
, matcher = matcher
393407
}
394408
completions <- completer completerRecorder completerData
395409
pure completions

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completer/Module.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ modulesCompleter extractionFunction recorder cData = do
3333
case mGPD of
3434
Just gpd -> do
3535
let sourceDirs = extractionFunction sName gpd
36-
filePathCompletions <-
37-
filePathsForExposedModules recorder sourceDirs prefInfo
36+
filePathCompletions <- filePathsForExposedModules recorder sourceDirs prefInfo (matcher cData)
3837
pure $ map (\compl -> mkSimpleCompletionItem (completionRange prefInfo) compl) filePathCompletions
3938
Nothing -> do
4039
logWith recorder Debug LogUseWithStaleFastNoResult
@@ -45,8 +44,13 @@ modulesCompleter extractionFunction recorder cData = do
4544

4645
-- | Takes a list of source directories and returns a list of path completions
4746
-- relative to any of the passed source directories which fit the passed prefix info.
48-
filePathsForExposedModules :: Recorder (WithPriority Log) -> [FilePath] -> CabalPrefixInfo -> IO [T.Text]
49-
filePathsForExposedModules recorder srcDirs prefInfo = do
47+
filePathsForExposedModules
48+
:: Recorder (WithPriority Log)
49+
-> [FilePath]
50+
-> CabalPrefixInfo
51+
-> Fuzzy.Matcher T.Text
52+
-> IO [T.Text]
53+
filePathsForExposedModules recorder srcDirs prefInfo matcher = do
5054
concatForM
5155
srcDirs
5256
( \dir' -> do
@@ -55,9 +59,8 @@ filePathsForExposedModules recorder srcDirs prefInfo = do
5559
completions <- listFileCompletions recorder pathInfo
5660
validExposedCompletions <- filterM (isValidExposedModulePath pathInfo) completions
5761
let toMatch = pathSegment pathInfo
58-
scored = Fuzzy.simpleFilter
59-
Fuzzy.defChunkSize
60-
Fuzzy.defMaxResults
62+
scored = Fuzzy.runMatcher
63+
matcher
6164
toMatch
6265
(map T.pack validExposedCompletions)
6366
forM

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completer/Simple.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ errorNoopCompleter l recorder _ = do
4141
constantCompleter :: [T.Text] -> Completer
4242
constantCompleter completions _ cData = do
4343
let prefInfo = cabalPrefixInfo cData
44-
scored = Fuzzy.simpleFilter Fuzzy.defChunkSize Fuzzy.defMaxResults (completionPrefix prefInfo) completions
44+
scored = Fuzzy.runMatcher (matcher cData) (completionPrefix prefInfo) completions
4545
range = completionRange prefInfo
4646
pure $ map (mkSimpleCompletionItem range . Fuzzy.original) scored
4747

@@ -68,7 +68,7 @@ importCompleter l cData = do
6868
-- it is just forbidden on hackage.
6969
nameCompleter :: Completer
7070
nameCompleter _ cData = do
71-
let scored = Fuzzy.simpleFilter Fuzzy.defChunkSize Fuzzy.defMaxResults (completionPrefix prefInfo) [completionFileName prefInfo]
71+
let scored = Fuzzy.runMatcher (matcher cData) (completionPrefix prefInfo) [completionFileName prefInfo]
7272
prefInfo = cabalPrefixInfo cData
7373
range = completionRange prefInfo
7474
pure $ map (mkSimpleCompletionItem range . Fuzzy.original) scored
@@ -85,6 +85,7 @@ weightedConstantCompleter completions weights _ cData = do
8585
let scored =
8686
if perfectScore > 0
8787
then
88+
-- TODO: Would be nice to use to be able to use the matcher in `cData`
8889
fmap Fuzzy.original $
8990
Fuzzy.simpleFilter' Fuzzy.defChunkSize Fuzzy.defMaxResults prefix completions customMatch
9091
else topTenByWeight

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completer/Types.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
module Ide.Plugin.Cabal.Completion.Completer.Types where
44

5+
import Data.Text (Text)
56
import Development.IDE as D
67
import qualified Distribution.Fields as Syntax
78
import Distribution.PackageDescription (GenericPackageDescription)
89
import qualified Distribution.Parsec.Position as Syntax
910
import Ide.Plugin.Cabal.Completion.Types
1011
import Language.LSP.Protocol.Types (CompletionItem)
12+
import qualified Text.Fuzzy.Parallel as Fuzzy
1113

1214
-- | Takes information needed to build possible completion items
1315
-- and returns the list of possible completion items
@@ -24,5 +26,7 @@ data CompleterData = CompleterData
2426
-- | Prefix info to be used for constructing completion items
2527
cabalPrefixInfo :: CabalPrefixInfo,
2628
-- | The name of the stanza in which the completer is applied
27-
stanzaName :: Maybe StanzaName
29+
stanzaName :: Maybe StanzaName,
30+
-- | The matcher that'll be used to rank the texts against the pattern.
31+
matcher :: Fuzzy.Matcher Text
2832
}

0 commit comments

Comments
 (0)