Skip to content

Commit 17f0ddd

Browse files
committed
Add documentation
1 parent d5db58a commit 17f0ddd

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import qualified Text.Fuzzy.Parallel as Fuzzy
4646
-- ----------------------------------------------------------------
4747

4848
{- | Takes information about the completion status within the file
49-
and finds the correct completer to be applied
49+
and finds the correct completer to be applied.
5050
-}
5151
contextToCompleter :: Context -> Completer
5252
-- if we are in the top level of the cabal file and not in a keyword context,
@@ -169,6 +169,9 @@ currentLevel (cur : xs)
169169
| otherwise -> Just (t, Just $ T.strip n)
170170
Nothing -> Nothing
171171

172+
{- | Creates a CompletionItem with the given text as the label
173+
where the completion item kind is keyword.
174+
-}
172175
mkDefaultCompletionItem :: T.Text -> LSP.CompletionItem
173176
mkDefaultCompletionItem label = LSP.CompletionItem
174177
{ Compls._label = label
@@ -223,7 +226,7 @@ splitAtPosition pos ls = do
223226
}
224227

225228
{- | Takes a line of text and removes the last partially
226-
written word to be completed
229+
written word to be completed.
227230
-}
228231
stripPartiallyWritten :: T.Text -> T.Text
229232
stripPartiallyWritten = T.dropWhileEnd (\y -> (y /= ' ') && (y /= ':'))
@@ -267,6 +270,7 @@ getCabalPrefixInfo fp prefixInfo =
267270
-- otherwise we parse until a space occurs
268271
stopConditionChars = apostropheOrSpaceSeparator : [',', ':']
269272

273+
-- | Calculates how many spaces the currently completed item is indented.
270274
completionIndentation :: CabalPrefixInfo -> Int
271275
completionIndentation prefInfo = fromIntegral (pos ^. JL.character) - (T.length $ completionPrefix prefInfo)
272276
where
@@ -293,14 +297,15 @@ constantCompleter completions _ cData = do
293297
range = completionRange prefInfo
294298
pure $ map (mkSimpleCompletionItem range . Fuzzy.original) scored
295299

300+
-- | Completer to be used for the name field's value
296301
nameCompleter :: Completer
297302
nameCompleter _ cData = do
298303
let scored = Fuzzy.simpleFilter 1000 10 (completionPrefix prefInfo) [completionFileName prefInfo]
299304
prefInfo = cabalPrefixInfo cData
300305
range = completionRange prefInfo
301306
pure $ map (mkSimpleCompletionItem range . Fuzzy.original) scored
302307

303-
-- maps snippet triggerwords to match on with their completers
308+
-- | Maps snippet triggerwords with their completers
304309
snippetCompleter :: Completer
305310
snippetCompleter _ cData = do
306311
let scored = Fuzzy.simpleFilter 1000 10 (completionPrefix prefInfo) $ Map.keys snippetMap
@@ -408,6 +413,9 @@ filePathCompleter recorder cData = do
408413
)
409414

410415
{- | Completer to be used when module paths can be completed for the field.
416+
417+
Takes an extraction function which extracts the source directories
418+
to be used by the completer.
411419
-}
412420
modulesCompleter :: (GenericPackageDescription -> [FilePath]) -> Completer
413421
modulesCompleter extractionFunction recorder cData = do
@@ -425,8 +433,10 @@ modulesCompleter extractionFunction recorder cData = do
425433
extras = shakeExtras (ideState cData)
426434
prefInfo = cabalPrefixInfo cData
427435

428-
exposedModuleExtraction :: GenericPackageDescription -> [FilePath]
429-
exposedModuleExtraction gpd =
436+
{- | Extracts the source directories of the library stanza.
437+
-}
438+
sourceDirsExtractionLibrary :: GenericPackageDescription -> [FilePath]
439+
sourceDirsExtractionLibrary gpd =
430440
-- we use condLibrary to get the information contained in the library stanza
431441
-- since the library in PackageDescription is not populated by us
432442
case libM of
@@ -436,9 +446,11 @@ exposedModuleExtraction gpd =
436446
where
437447
libM = condLibrary gpd
438448

439-
otherModulesExtractionExecutable :: Maybe T.Text -> GenericPackageDescription -> [FilePath]
440-
otherModulesExtractionExecutable Nothing _ = []
441-
otherModulesExtractionExecutable (Just name) gpd
449+
{- | Extracts the source directories of the executable stanza with the given name.
450+
-}
451+
sourceDirsExtractionExecutable :: Maybe T.Text -> GenericPackageDescription -> [FilePath]
452+
sourceDirsExtractionExecutable Nothing _ = []
453+
sourceDirsExtractionExecutable (Just name) gpd
442454
| exeName executable == (mkUnqualComponentName $ T.unpack name) = map getSymbolicPath $ hsSourceDirs $ buildInfo executable
443455
| otherwise = []
444456
where
@@ -452,9 +464,11 @@ otherModulesExtractionExecutable (Just name) gpd
452464
)
453465
execsM
454466

455-
otherModulesExtractionTestSuite :: Maybe T.Text -> GenericPackageDescription -> [FilePath]
456-
otherModulesExtractionTestSuite Nothing _ = []
457-
otherModulesExtractionTestSuite (Just name) gpd
467+
{- | Extracts the source directories of the test suite stanza with the given name.
468+
-}
469+
sourceDirsExtractionTestSuite :: Maybe T.Text -> GenericPackageDescription -> [FilePath]
470+
sourceDirsExtractionTestSuite Nothing _ = []
471+
sourceDirsExtractionTestSuite (Just name) gpd
458472
| testName testSuite == (mkUnqualComponentName $ T.unpack name) = map getSymbolicPath $ hsSourceDirs $ testBuildInfo testSuite
459473
| otherwise = []
460474
where
@@ -468,9 +482,11 @@ otherModulesExtractionTestSuite (Just name) gpd
468482
)
469483
testSuitesM
470484

471-
otherModulesExtractionBenchmark :: Maybe T.Text -> GenericPackageDescription -> [FilePath]
472-
otherModulesExtractionBenchmark Nothing _ = []
473-
otherModulesExtractionBenchmark (Just name) gpd
485+
{- | Extracts the source directories of benchmark stanza with the given name.
486+
-}
487+
sourceDirsExtractionBenchmark :: Maybe T.Text -> GenericPackageDescription -> [FilePath]
488+
sourceDirsExtractionBenchmark Nothing _ = []
489+
sourceDirsExtractionBenchmark (Just name) gpd
474490
| benchmarkName bMark == (mkUnqualComponentName $ T.unpack name) = map getSymbolicPath $ hsSourceDirs $ benchmarkBuildInfo bMark
475491
| otherwise = []
476492
where
@@ -526,7 +542,7 @@ cabalVersionKeyword =
526542
cabalKeywords :: Map KeyWordName Completer
527543
cabalKeywords =
528544
Map.fromList
529-
[ ("name:", nameCompleter) -- TODO: should complete to filename, needs meta info
545+
[ ("name:", nameCompleter)
530546
, ("version:", noopCompleter)
531547
, ("build-type:", constantCompleter ["Simple", "Custom", "Configure", "Make"])
532548
, ("license:", weightedConstantCompleter licenseNames weightedLicenseNames)
@@ -557,13 +573,13 @@ stanzaKeywordMap =
557573
[
558574
( "library"
559575
, \_ -> Map.fromList $
560-
[ ("exposed-modules:", modulesCompleter exposedModuleExtraction) -- identifier list
576+
[ ("exposed-modules:", modulesCompleter sourceDirsExtractionLibrary) -- identifier list
561577
, ("virtual-modules:", noopCompleter)
562578
, ("exposed:", constantCompleter ["True", "False"])
563579
, ("visibility:", constantCompleter ["private", "public"])
564580
, ("reexported-modules:", noopCompleter) -- export list, i.e. "orig-okg:Name as NewName"
565581
, ("signatures:", noopCompleter) -- list of signatures
566-
, ("other-modules:", modulesCompleter exposedModuleExtraction)
582+
, ("other-modules:", modulesCompleter sourceDirsExtractionLibrary)
567583
]
568584
++ libExecTestBenchCommons
569585
)
@@ -572,7 +588,7 @@ stanzaKeywordMap =
572588
, \n -> Map.fromList $
573589
[ ("main-is:", filePathCompleter)
574590
, ("scope:", constantCompleter ["public", "private"])
575-
, ("other-modules:", modulesCompleter (otherModulesExtractionExecutable n))
591+
, ("other-modules:", modulesCompleter (sourceDirsExtractionExecutable n))
576592
]
577593
++ libExecTestBenchCommons
578594
)
@@ -581,7 +597,7 @@ stanzaKeywordMap =
581597
, \n -> Map.fromList $
582598
[ ("type:", constantCompleter ["exitcode-stdio-1.0", "detailed-0.9"])
583599
, ("main-is:", filePathCompleter)
584-
, ("other-modules:", modulesCompleter (otherModulesExtractionTestSuite n))
600+
, ("other-modules:", modulesCompleter (sourceDirsExtractionTestSuite n))
585601
]
586602
++ libExecTestBenchCommons
587603
)
@@ -590,7 +606,7 @@ stanzaKeywordMap =
590606
, \n -> Map.fromList $
591607
[ ("type:", noopCompleter)
592608
, ("main-is:", filePathCompleter)
593-
, ("other-modules:", modulesCompleter (otherModulesExtractionBenchmark n))
609+
, ("other-modules:", modulesCompleter (sourceDirsExtractionBenchmark n))
594610
]
595611
++ libExecTestBenchCommons
596612
)

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ listFileCompletions recorder complInfo = do
7878
pure []
7979

8080
{- | Returns a list of all (and only) directories in the
81-
directory described by path completion info
81+
directory described by path completion info.
8282
-}
8383
listDirectoryCompletions :: Recorder (WithPriority Log) -> PathCompletionInfo -> IO [FilePath]
8484
listDirectoryCompletions recorder complInfo = do
@@ -134,16 +134,19 @@ filePathsForExposedModules srcDirs recorder prefInfo = do
134134
fileExists <- doesFileExist (dir FP.</> path)
135135
pure $ not fileExists || FP.isExtensionOf ".hs" path
136136

137-
137+
{- | Takes a source dir path and a cabal file path and returns the complete source dir
138+
path in exposed module syntax where the separators are '.' and the file ending is removed.
139+
-}
138140
fpToExposedModulePath :: FilePath -> FilePath -> FilePath
139-
fpToExposedModulePath srcDir fp' = T.unpack $ T.intercalate "." $ fmap T.pack $ FP.splitDirectories fp
141+
fpToExposedModulePath srcDir cabalDir = T.unpack $ T.intercalate "." $ fmap T.pack $ FP.splitDirectories fp
140142
where
141-
fp = fromMaybe fp' $ stripPrefix srcDir fp'
143+
fp = fromMaybe cabalDir $ stripPrefix srcDir cabalDir
142144

145+
{- | Takes a path in the exposed module field and translates it to a filepath.
146+
-}
143147
exposedModulePathToFp :: T.Text -> FilePath
144148
exposedModulePathToFp fp = T.unpack $ T.replace "." (T.singleton FP.pathSeparator) fp
145149

146-
147150
{- | Returns the directory, the currently handled cabal file is in.
148151
149152
We let System.FilePath handle the separator syntax since this is used

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ and returns the list of possible completion items
4747
-}
4848
type Completer = Recorder (WithPriority Log) -> CompleterData -> IO [CompletionItem]
4949

50+
{- | Contains information to be used by completers.
51+
-}
5052
data CompleterData = CompleterData
5153
{ ideState :: IdeState
54+
-- ^ The ideState, which can be used to call the cabal parser results
5255
, cabalPrefixInfo :: CabalPrefixInfo
56+
-- ^ Prefix info to be used for constructing completion items
5357
, stanzaName :: Maybe StanzaName
58+
-- ^ The name of the stanza in which the completer is applied
5459
}
5560

5661
{- | The context a cursor can be in within a cabal file,

0 commit comments

Comments
 (0)