@@ -6,9 +6,10 @@ module Development.IDE.Plugin.HLS
6
6
) where
7
7
8
8
import Control.Exception (SomeException , catch )
9
- import Control.Lens ( (^.) )
9
+ import Control.Lens ((^.) )
10
10
import Control.Monad
11
11
import qualified Data.Aeson as J
12
+ import qualified Data.DList as DList
12
13
import Data.Either
13
14
import qualified Data.List as List
14
15
import qualified Data.Map as Map
@@ -436,35 +437,55 @@ makeCompletions :: [(PluginId, CompletionProvider IdeState)]
436
437
makeCompletions sps lf ideState params@ (CompletionParams (TextDocumentIdentifier doc) pos _context _mt)
437
438
= do
438
439
mprefix <- getPrefixAtPos lf doc pos
439
- _snippets <- WithSnippets . completionSnippetsOn <$> getClientConfig lf
440
+ config <- getClientConfig lf
440
441
441
442
let
442
443
combine :: [CompletionResponseResult ] -> CompletionResponseResult
443
- combine cs = go (Completions $ List [] ) cs
444
- where
445
- go acc [] = acc
446
- go (Completions (List ls)) (Completions (List ls2): rest)
447
- = go (Completions (List (ls <> ls2))) rest
448
- go (Completions (List ls)) (CompletionList (CompletionListType complete (List ls2)): rest)
449
- = go (CompletionList $ CompletionListType complete (List (ls <> ls2))) rest
450
- go (CompletionList (CompletionListType complete (List ls))) (CompletionList (CompletionListType complete2 (List ls2)): rest)
451
- = go (CompletionList $ CompletionListType (complete || complete2) (List (ls <> ls2))) rest
452
- go (CompletionList (CompletionListType complete (List ls))) (Completions (List ls2): rest)
453
- = go (CompletionList $ CompletionListType complete (List (ls <> ls2))) rest
454
- makeAction (pid,p) = do
444
+ combine cs = go True mempty cs
445
+
446
+ go ! comp acc [] =
447
+ CompletionList (CompletionListType comp (List $ DList. toList acc))
448
+ go comp acc (Completions (List ls) : rest) =
449
+ go comp (acc <> DList. fromList ls) rest
450
+ go comp acc (CompletionList (CompletionListType comp' (List ls)) : rest) =
451
+ go (comp && comp') (acc <> DList. fromList ls) rest
452
+
453
+ -- | Process a list of completion providers until we reach a max number of results
454
+ makeAction ::
455
+ Int ->
456
+ [(PluginId , CompletionProvider IdeState )] ->
457
+ IO [Either ResponseError CompletionResponseResult ]
458
+ makeAction 0 _ = return []
459
+ makeAction _ [] = return []
460
+ makeAction n ((pid, p) : rest) = do
455
461
pluginConfig <- getPluginConfig lf pid
456
- if pluginEnabled pluginConfig plcCompletionOn
462
+ results <- if pluginEnabled pluginConfig plcCompletionOn
457
463
then otTracedProvider pid " completions" $ p lf ideState params
458
464
else return $ Right $ Completions $ List []
465
+ case results of
466
+ Right resp -> do
467
+ let (n', results') = consumeCompletionResponse n resp
468
+ (Right results' : ) <$> makeAction n' rest
469
+ Left err ->
470
+ (Left err : ) <$> makeAction n rest
459
471
460
472
case mprefix of
461
473
Nothing -> return $ Right $ Completions $ List []
462
474
Just _prefix -> do
463
- mhs <- mapM makeAction sps
475
+ mhs <- makeAction (maxCompletions config) sps
464
476
case rights mhs of
465
477
[] -> return $ Left $ responseError $ T. pack $ show $ lefts mhs
466
478
hs -> return $ Right $ combine hs
467
479
480
+ -- | Crops a completion response. Returns the final number of completions and the cropped response
481
+ consumeCompletionResponse :: Int -> CompletionResponseResult -> (Int , CompletionResponseResult )
482
+ consumeCompletionResponse n it@ (CompletionList (CompletionListType _ (List xx))) =
483
+ case splitAt n xx of
484
+ (_, [] ) -> (n - length xx, it)
485
+ (xx', _) -> (0 , CompletionList (CompletionListType False (List xx')))
486
+ consumeCompletionResponse n (Completions (List xx)) =
487
+ consumeCompletionResponse n (CompletionList (CompletionListType False (List xx)))
488
+
468
489
getPrefixAtPos :: LSP. LspFuncs Config -> Uri -> Position -> IO (Maybe VFS. PosPrefixInfo )
469
490
getPrefixAtPos lf uri pos = do
470
491
mvf <- LSP. getVirtualFileFunc lf (J. toNormalizedUri uri)
0 commit comments