@@ -6,26 +6,16 @@ import Control.Monad (filterM)
66import Control.Monad.Extra (concatForM ,
77 forM )
88import Data.List (stripPrefix )
9- import qualified Data.List as List
109import Data.Maybe (fromMaybe )
1110import qualified Data.Text as T
12- import Distribution.PackageDescription (Benchmark (.. ),
13- BuildInfo (.. ),
14- CondTree (condTreeData ),
15- Executable (.. ),
16- GenericPackageDescription (.. ),
17- Library (.. ),
18- UnqualComponentName ,
19- mkUnqualComponentName ,
20- testBuildInfo )
21- import Distribution.Utils.Path (getSymbolicPath )
11+ import Distribution.PackageDescription (GenericPackageDescription )
2212import Ide.Logger (Priority (.. ),
2313 Recorder ,
2414 WithPriority ,
2515 logWith )
26- import Ide.Plugin.Cabal.Completion.Completer.FilePath (PathCompletionInfo (.. ),
27- listFileCompletions ,
16+ import Ide.Plugin.Cabal.Completion.Completer.FilePath (listFileCompletions ,
2817 mkCompletionDirectory )
18+ import Ide.Plugin.Cabal.Completion.Completer.Paths
2919import Ide.Plugin.Cabal.Completion.Completer.Simple
3020import Ide.Plugin.Cabal.Completion.Completer.Types
3121import Ide.Plugin.Cabal.Completion.Types
@@ -53,56 +43,6 @@ modulesCompleter extractionFunction recorder cData = do
5343 sName = stanzaName cData
5444 prefInfo = cabalPrefixInfo cData
5545
56- -- | Extracts the source directories of the library stanza.
57- sourceDirsExtractionLibrary :: Maybe StanzaName -> GenericPackageDescription -> [FilePath ]
58- sourceDirsExtractionLibrary Nothing gpd =
59- -- we use condLibrary to get the information contained in the library stanza
60- -- since the library in PackageDescription is not populated by us
61- case libM of
62- Just lib -> do
63- map getSymbolicPath $ hsSourceDirs $ libBuildInfo $ condTreeData lib
64- Nothing -> []
65- where
66- libM = condLibrary gpd
67- sourceDirsExtractionLibrary name gpd = extractRelativeDirsFromStanza name gpd condSubLibraries libBuildInfo
68-
69- -- | Extracts the source directories of the executable stanza with the given name.
70- sourceDirsExtractionExecutable :: Maybe StanzaName -> GenericPackageDescription -> [FilePath ]
71- sourceDirsExtractionExecutable name gpd = extractRelativeDirsFromStanza name gpd condExecutables buildInfo
72-
73- -- | Extracts the source directories of the test suite stanza with the given name.
74- sourceDirsExtractionTestSuite :: Maybe StanzaName -> GenericPackageDescription -> [FilePath ]
75- sourceDirsExtractionTestSuite name gpd = extractRelativeDirsFromStanza name gpd condTestSuites testBuildInfo
76-
77- -- | Extracts the source directories of benchmark stanza with the given name.
78- sourceDirsExtractionBenchmark :: Maybe StanzaName -> GenericPackageDescription -> [FilePath ]
79- sourceDirsExtractionBenchmark name gpd = extractRelativeDirsFromStanza name gpd condBenchmarks benchmarkBuildInfo
80-
81- -- | Takes a possible stanza name, a GenericPackageDescription,
82- -- a function to access the stanza information we are interested in
83- -- and a function to access the build info from the specific stanza.
84- --
85- -- Returns a list of relative source directory paths specified for the extracted stanza.
86- extractRelativeDirsFromStanza ::
87- Maybe StanzaName ->
88- GenericPackageDescription ->
89- (GenericPackageDescription -> [(UnqualComponentName , CondTree b c a )]) ->
90- (a -> BuildInfo ) ->
91- [FilePath ]
92- extractRelativeDirsFromStanza Nothing _ _ _ = []
93- extractRelativeDirsFromStanza (Just name) gpd getStanza getBuildInfo
94- | Just stanza <- stanzaM = map getSymbolicPath $ hsSourceDirs $ getBuildInfo stanza
95- | otherwise = []
96- where
97- stanzaM = fmap (condTreeData . snd ) res
98- allStanzasM = getStanza gpd
99- res =
100- List. find
101- ( \ (n, _) ->
102- n == mkUnqualComponentName (T. unpack name)
103- )
104- allStanzasM
105-
10646-- | Takes a list of source directories and returns a list of path completions
10747-- relative to any of the passed source directories which fit the passed prefix info.
10848filePathsForExposedModules :: Recorder (WithPriority Log ) -> [FilePath ] -> CabalPrefixInfo -> IO [T. Text ]
@@ -111,34 +51,36 @@ filePathsForExposedModules recorder srcDirs prefInfo = do
11151 srcDirs
11252 ( \ dir' -> do
11353 let dir = FP. normalise dir'
114- let pInfo =
115- PathCompletionInfo
116- { isStringNotationPath = Nothing ,
117- pathSegment = T. pack $ FP. takeFileName prefix,
118- queryDirectory = FP. addTrailingPathSeparator $ FP. takeDirectory prefix,
119- workingDirectory = completionWorkingDir prefInfo FP. </> dir
120- }
121- completions <- listFileCompletions recorder pInfo
122- validExposedCompletions <- filterM (isValidExposedModulePath pInfo) completions
123- let toMatch = pathSegment pInfo
124- scored = Fuzzy. simpleFilter Fuzzy. defChunkSize Fuzzy. defMaxResults toMatch (map T. pack validExposedCompletions)
54+ pathInfo = pathCompletionInfoFromCabalPrefixInfo dir modPrefInfo
55+ completions <- listFileCompletions recorder pathInfo
56+ validExposedCompletions <- filterM (isValidExposedModulePath pathInfo) completions
57+ let toMatch = pathSegment pathInfo
58+ scored = Fuzzy. simpleFilter
59+ Fuzzy. defChunkSize
60+ Fuzzy. defMaxResults
61+ toMatch
62+ (map T. pack validExposedCompletions)
12563 forM
12664 scored
12765 ( \ compl' -> do
12866 let compl = Fuzzy. original compl'
129- fullFilePath <- mkExposedModulePathCompletion pInfo $ T. unpack compl
67+ fullFilePath <- mkExposedModulePathCompletion pathInfo $ T. unpack compl
13068 pure fullFilePath
13169 )
13270 )
13371 where
13472 prefix =
135- exposedModulePathToFp $
73+ T. pack $ exposedModulePathToFp $
13674 completionPrefix prefInfo
137- -- \| Takes a PathCompletionInfo and a path segment and checks whether
75+ -- build completion info relative to the source dir,
76+ -- we overwrite the prefix written in the cabal file with its translation
77+ -- to filepath syntax, since it is in exposed module syntax
78+ modPrefInfo = prefInfo{completionPrefix= prefix}
79+
80+ -- Takes a PathCompletionInfo and a path segment and checks whether
13881 -- the path segment can be completed for an exposed module.
13982 --
14083 -- This is the case if the segment represents either a directory or a Haskell file.
141- --
14284 isValidExposedModulePath :: PathCompletionInfo -> FilePath -> IO Bool
14385 isValidExposedModulePath pInfo path = do
14486 let dir = mkCompletionDirectory pInfo
0 commit comments