@@ -13,6 +13,7 @@ import Data.Maybe (fromMaybe)
13
13
import qualified Data.Text as T
14
14
import Data.Text.Utf16.Rope (Rope )
15
15
import qualified Data.Text.Utf16.Rope as Rope
16
+ import Debug.Trace (traceShowM )
16
17
import Development.IDE as D
17
18
import Distribution.CabalSpecVersion (CabalSpecVersion (CabalSpecV2_2 ),
18
19
showCabalSpecVersion )
@@ -25,6 +26,7 @@ import qualified Language.LSP.Protocol.Types as Compls (CompletionItem
25
26
import qualified Language.LSP.Protocol.Types as LSP
26
27
import qualified Language.LSP.VFS as VFS
27
28
import qualified Text.Fuzzy.Parallel as Fuzzy
29
+ import Data.Ord (Down (.. ))
28
30
29
31
-- ----------------------------------------------------------------
30
32
-- Public API for Completions
@@ -265,8 +267,29 @@ constantCompleter completions _ ctxInfo = do
265
267
let range = completionRange ctxInfo
266
268
pure $ map (makeSimpleCabalCompletionItem range . Fuzzy. original) scored
267
269
270
+ weightedConstantCompleter :: [T. Text ] -> Map T. Text Double -> Completer
271
+ weightedConstantCompleter completions weights _ ctxInfo = do
272
+ let scored = if perfectScore > 0
273
+ then fmap Fuzzy. original $ Fuzzy. simpleFilter' 1000 10 prefix completions customMatch
274
+ else topTenByWeight
275
+ let range = completionRange ctxInfo
276
+ pure $ map (makeSimpleCabalCompletionItem range) scored
277
+ where
278
+ prefix = completionPrefix ctxInfo
279
+ perfectScore = fromMaybe (error " match is broken" ) $ Fuzzy. match prefix prefix
280
+ customMatch :: (T. Text -> T. Text -> Maybe Int )
281
+ customMatch toSearch searchSpace = do
282
+ matched <- Fuzzy. match toSearch searchSpace
283
+ let weight = fromMaybe 0 $ Map. lookup searchSpace weights
284
+ let score = min
285
+ perfectScore
286
+ (round (fromIntegral matched * (1 + weight)))
287
+ pure score
288
+ topTenByWeight :: [T. Text ]
289
+ topTenByWeight = take 10 $ map fst $ List. sortOn (Down . snd ) $ Map. assocs weights
290
+
268
291
{- | Completer to be used when a file path can be
269
- completed for a field, takes the file path of the directory to start from.
292
+ completed for a field, takes the file path of the directory to start from.
270
293
Completes file paths as well as directories.
271
294
-}
272
295
filePathCompleter :: Completer
@@ -327,7 +350,7 @@ cabalKeywords =
327
350
[ (" name:" , noopCompleter) -- TODO: should complete to filename, needs meta info
328
351
, (" version:" , noopCompleter)
329
352
, (" build-type:" , constantCompleter [" Simple" , " Custom" , " Configure" , " Make" ])
330
- , (" license:" , constantCompleter licenseNames)
353
+ , (" license:" , weightedConstantCompleter licenseNames weightedLicenseNames )
331
354
, (" license-file:" , filePathCompleter)
332
355
, (" license-files:" , filePathCompleter) -- list of filenames
333
356
, (" copyright:" , noopCompleter)
@@ -411,7 +434,7 @@ stanzaKeywordMap =
411
434
)
412
435
,
413
436
( " source-repository"
414
- , Map. fromList $
437
+ , Map. fromList
415
438
[
416
439
( " type:"
417
440
, constantCompleter
@@ -476,6 +499,56 @@ stanzaKeywordMap =
476
499
, (" mixins:" , noopCompleter)
477
500
]
478
501
502
+ weightedLicenseNames :: Map T. Text Double
503
+ weightedLicenseNames = fmap statisticsToWeight $ Map. fromList
504
+ [(" BSD-3-Clause" ,9955 )
505
+ , (" MIT" ,3336 )
506
+ , (" GPL-3.0-only" ,679 )
507
+ , (" LicenseRef-OtherLicense" ,521 )
508
+ , (" Apache-2.0" ,514 )
509
+ , (" LicenseRef-GPL" ,443 )
510
+ , (" LicenseRef-PublicDomain" ,318 )
511
+ , (" MPL-2.0" ,288 )
512
+ , (" BSD-2-Clause" ,174 )
513
+ , (" GPL-2.0-only" ,160 )
514
+ , (" LicenseRef-LGPL" ,146 )
515
+ , (" LGPL-2.1-only" ,112 )
516
+ , (" LGPL-3.0-only" ,100 )
517
+ , (" AGPL-3.0-only" ,96 )
518
+ , (" ISC" ,89 )
519
+ , (" LicenseRef-Apache" ,45 )
520
+ , (" GPL-3.0-or-later" ,43 )
521
+ , (" BSD-2-Clause-Patent" ,33 )
522
+ , (" GPL-2.0-or-later" ,21 )
523
+ , (" CC0-1.0" ,16 )
524
+ , (" AGPL-3.0-or-later" ,15 )
525
+ , (" LGPL-2.1-or-later" ,12 )
526
+ , (" (BSD-2-Clause OR Apache-2.0)" ,10 )
527
+ , (" (Apache-2.0 OR MPL-2.0)" ,8 )
528
+ , (" LicenseRef-AGPL" ,6 )
529
+ , (" (BSD-3-Clause OR Apache-2.0)" ,4 )
530
+ , (" 0BSD" ,3 )
531
+ , (" BSD-4-Clause" ,3 )
532
+ , (" LGPL-3.0-or-later" ,3 )
533
+ , (" LicenseRef-LGPL-2" ,2 )
534
+ , (" GPL-2.0-or-later AND BSD-3-Clause" ,2 )
535
+ , (" NONE" ,2 )
536
+ , (" Zlib" ,2 )
537
+ , (" (Apache-2.0 OR BSD-3-Clause)" ,2 )
538
+ , (" BSD-3-Clause AND GPL-2.0-or-later" ,2 )
539
+ , (" BSD-3-Clause AND GPL-3.0-or-later" ,2 )
540
+ ]
541
+ where
542
+ statisticsToWeight :: Int -> Double
543
+ statisticsToWeight stat
544
+ | stat < 10 = 0.1
545
+ | stat < 20 = 0.3
546
+ | stat < 50 = 0.4
547
+ | stat < 100 = 0.5
548
+ | stat < 500 = 0.6
549
+ | stat < 650 = 0.7
550
+ | otherwise = 0.9
551
+
479
552
-- cabalFlagKeywords :: [(T.Text, T.Text)]
480
553
-- cabalFlagKeywords =
481
554
-- [
0 commit comments