Skip to content

Commit b0048d0

Browse files
author
VeryMilkyJoe
committed
Fix subdirectory completion
1 parent 87a265d commit b0048d0

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

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

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ contextToCompleter :: Context -> Completer
9292
-- if we are in the top level of the cabal file and not in a keyword context,
9393
-- we can write any top level keywords or a stanza declaration
9494
contextToCompleter (TopLevel, None) =
95-
constantCompleter $ Map.keys (cabalKeywords <> cabalVersionKeyword) ++ Map.keys stanzaKeywordMap
95+
constantCompleter $ Map.keys (cabalVersionKeyword <> cabalKeywords) ++ Map.keys stanzaKeywordMap
9696
-- if we are in a keyword context in the top level,
9797
-- we look up that keyword in the top level context and can complete its possible values
9898
contextToCompleter (TopLevel, KeyWord kw) =
99-
case Map.lookup kw (cabalKeywords <> cabalVersionKeyword) of
99+
case Map.lookup kw (cabalVersionKeyword <> cabalKeywords) of
100100
Nothing -> noopCompleter
101101
Just l -> l
102102
-- if we are in a stanza and not in a keyword context,
@@ -290,16 +290,20 @@ filePathCompleter ctx = do
290290
fullFilePath <- makeFullFilePath suffix compl complInfo
291291
pure $ makeCabalCompletionItem (completionRange ctx) fullFilePath fullFilePath
292292
)
293-
294-
where
295-
-- | Takes a suffix and a text and returns filepath with the suffix
296-
makeFullFilePath :: T.Text -> T.Text -> PathCompletionInfo -> IO T.Text
297-
makeFullFilePath suffix' completion' complInfo = do
298-
let fullPath' = prefixPathInfo complInfo Posix.</> T.unpack completion'
299-
isFilePath <- doesFileExist fullPath'
300-
let fullPath = if isFilePath then fullPath' ++ T.unpack suffix' else fullPath'
301-
pure $ T.pack fullPath
302-
293+
where
294+
-- | Takes a suffix, a completed path and a pathCompletionInfo and
295+
-- generates the whole filepath including the already written prefix
296+
-- and the suffix in case the completed path is a filepath
297+
makeFullFilePath :: T.Text -> T.Text -> PathCompletionInfo -> IO T.Text
298+
makeFullFilePath suffix' completion' complInfo = do
299+
let fullPath' = prefixPathInfo complInfo Posix.</> T.unpack completion'
300+
isFilePath <- doesFileExist fullPath'
301+
let fullPath = if isFilePath then fullPath' ++ T.unpack suffix' else fullPath'
302+
pure $ T.pack fullPath
303+
304+
{- | Takes a path completion info and returns the list of files
305+
in the directory the path completion info describes
306+
-}
303307
listFileCompletions :: PathCompletionInfo -> IO [FilePath]
304308
listFileCompletions complInfo = do
305309
try (evaluate =<< listDirectory (mkCompletionDirectory complInfo)) >>= \case
@@ -313,6 +317,9 @@ listFileCompletions complInfo = do
313317
pure fixedDirs
314318
Left (_::IOError) -> pure []
315319

320+
{- | Returns a list of all directories in the directory
321+
described by path completion info
322+
-}
316323
listDirectoryCompletions :: PathCompletionInfo -> IO [FilePath]
317324
listDirectoryCompletions complInfo = do
318325
filepaths <- listFileCompletions complInfo
@@ -327,7 +334,21 @@ directoryCompleter ctx = do
327334
let complInfo = pathCompletionInfoFromCompletionContext ctx
328335
directoryCompletions <- listDirectoryCompletions complInfo
329336
let scored = Fuzzy.simpleFilter 1000 10 (prefixLeftOver complInfo) (map T.pack directoryCompletions)
330-
pure $ map (makeSimpleCabalCompletionItem (completionRange ctx) . Fuzzy.original) scored
337+
forM
338+
scored
339+
( \compl' -> do
340+
let compl = Fuzzy.original compl'
341+
fullDirPath <- makeFullDirPath compl complInfo
342+
pure $ makeCabalCompletionItem (completionRange ctx) fullDirPath fullDirPath
343+
)
344+
where
345+
-- | Takes a directory and PathCompletionInfo and
346+
-- returns the whole path including the prefix that was already written
347+
makeFullDirPath :: T.Text -> PathCompletionInfo -> IO T.Text
348+
makeFullDirPath completion' complInfo = do
349+
let fullPath = prefixPathInfo complInfo Posix.</> T.unpack completion'
350+
pure $ T.pack fullPath
351+
331352

332353
pathCompletionInfoFromCompletionContext :: CabalCompletionContext -> PathCompletionInfo
333354
pathCompletionInfoFromCompletionContext ctx = PathCompletionInfo
@@ -339,6 +360,8 @@ pathCompletionInfoFromCompletionContext ctx = PathCompletionInfo
339360
dirNamePrefix = T.pack $ Posix.takeFileName prefix
340361
fp = Posix.takeDirectory $ completionCabalFilePath ctx
341362

363+
{- | Returns
364+
-}
342365
mkCompletionDirectory :: PathCompletionInfo -> FilePath
343366
mkCompletionDirectory complInfo = FP.addTrailingPathSeparator $ cabalFilePathInfo complInfo FP.</> (FP.normalise $ prefixPathInfo complInfo)
344367

0 commit comments

Comments
 (0)