|
2 | 2 | {-# LANGUAGE OverloadedStrings #-}
|
3 | 3 | {-# LANGUAGE ScopedTypeVariables #-}
|
4 | 4 |
|
5 |
| -module Ide.Plugin.Cabal.FilepathCompletions where |
6 |
| - |
7 |
| -import Control.Exception (evaluate, try) |
8 |
| -import Control.Monad (filterM) |
9 |
| -import Control.Monad.Extra (concatForM, forM) |
10 |
| -import Data.List (stripPrefix) |
11 |
| -import Data.Maybe (fromMaybe) |
12 |
| -import qualified Data.Text as T |
| 5 | + |
| 6 | +module Ide.Plugin.Cabal.Completion.Completer.FilePath where |
| 7 | + |
| 8 | +import Data.Maybe (fromMaybe) |
| 9 | +import qualified Data.Text as T |
| 10 | +import Ide.Plugin.Cabal.Completion.Completer.Types |
| 11 | + |
| 12 | +import Control.Exception (evaluate, try) |
| 13 | +import Control.Monad (filterM) |
| 14 | +import Control.Monad.Extra (forM) |
13 | 15 | import Development.IDE.Types.Logger
|
14 |
| -import Ide.Plugin.Cabal.Types |
15 |
| -import System.Directory (doesDirectoryExist, |
16 |
| - doesFileExist, listDirectory) |
17 |
| -import qualified System.FilePath as FP |
18 |
| -import System.FilePath (dropExtension) |
19 |
| -import qualified System.FilePath.Posix as Posix |
20 |
| -import qualified Text.Fuzzy.Parallel as Fuzzy |
| 16 | +import Ide.Plugin.Cabal.Completion.Types |
| 17 | +import System.Directory (doesDirectoryExist, |
| 18 | + doesFileExist, |
| 19 | + listDirectory) |
| 20 | +import qualified System.FilePath as FP |
| 21 | +import qualified System.FilePath.Posix as Posix |
| 22 | +import qualified Text.Fuzzy.Parallel as Fuzzy |
| 23 | +import Ide.Plugin.Cabal.Completion.Completer.Simple |
| 24 | + |
| 25 | + |
| 26 | +{- | Completer to be used when a file path can be |
| 27 | + completed for a field, takes the file path of the directory to start from. |
| 28 | + Completes file paths as well as directories. |
| 29 | +-} |
| 30 | +filePathCompleter :: Completer |
| 31 | +filePathCompleter recorder cData = do |
| 32 | + let prefInfo = cabalPrefixInfo cData |
| 33 | + suffix = fromMaybe "" $ completionSuffix prefInfo |
| 34 | + complInfo = pathCompletionInfoFromCabalPrefixInfo prefInfo |
| 35 | + toMatch = fromMaybe (partialFileName complInfo) $ T.stripPrefix "./" $ partialFileName complInfo |
| 36 | + filePathCompletions <- listFileCompletions recorder complInfo |
| 37 | + let scored = Fuzzy.simpleFilter 1000 10 toMatch (map T.pack filePathCompletions) |
| 38 | + forM |
| 39 | + scored |
| 40 | + ( \compl' -> do |
| 41 | + let compl = Fuzzy.original compl' |
| 42 | + fullFilePath <- mkFilePathCompletion suffix compl complInfo |
| 43 | + pure $ mkCompletionItem (completionRange prefInfo) fullFilePath fullFilePath |
| 44 | + ) |
| 45 | + |
| 46 | +{- | Completer to be used when a directory can be completed for the field, |
| 47 | + takes the file path of the directory to start from. |
| 48 | + Only completes directories. |
| 49 | +-} |
| 50 | +directoryCompleter :: Completer |
| 51 | +directoryCompleter recorder cData = do |
| 52 | + let prefInfo = cabalPrefixInfo cData |
| 53 | + complInfo = pathCompletionInfoFromCabalPrefixInfo prefInfo |
| 54 | + directoryCompletions <- listDirectoryCompletions recorder complInfo |
| 55 | + let scored = |
| 56 | + Fuzzy.simpleFilter |
| 57 | + 1000 |
| 58 | + 10 |
| 59 | + (partialFileName complInfo) |
| 60 | + (map T.pack directoryCompletions) |
| 61 | + forM |
| 62 | + scored |
| 63 | + ( \compl' -> do |
| 64 | + let compl = Fuzzy.original compl' |
| 65 | + let fullDirPath = mkPathCompletion complInfo compl |
| 66 | + pure $ mkCompletionItem (completionRange prefInfo) fullDirPath fullDirPath |
| 67 | + ) |
21 | 68 |
|
22 | 69 | {- Note [Using correct file path separators]
|
23 | 70 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
@@ -97,55 +144,6 @@ pathCompletionInfoFromCabalPrefixInfo ctx =
|
97 | 144 | dirNamePrefix = T.pack $ Posix.takeFileName prefix
|
98 | 145 | dir = completionWorkingDir ctx
|
99 | 146 |
|
100 |
| -{- | Extracts the source dirs from the library stanza in the cabal file using the GPD |
101 |
| - and returns a list of path completions relative to any source dir which fit the passed prefix info. |
102 |
| --} |
103 |
| -filePathsForExposedModules :: [FilePath] -> Recorder (WithPriority Log) -> CabalPrefixInfo -> IO [T.Text] |
104 |
| -filePathsForExposedModules srcDirs recorder prefInfo = do |
105 |
| - concatForM |
106 |
| - srcDirs |
107 |
| - (\dir -> do |
108 |
| - let pInfo = |
109 |
| - PathCompletionInfo |
110 |
| - { partialFileName = T.pack $ Posix.takeFileName prefix |
111 |
| - , partialFileDir = Posix.addTrailingPathSeparator $ Posix.takeDirectory prefix |
112 |
| - , workingDir = completionWorkingDir prefInfo FP.</> dir |
113 |
| - } |
114 |
| - completions <- listFileCompletions recorder pInfo |
115 |
| - validExposedCompletions <- filterM (isValidExposedModulePath pInfo) completions |
116 |
| - let filePathCompletions = map (fpToExposedModulePath dir) validExposedCompletions |
117 |
| - toMatch = fromMaybe (partialFileName pInfo) $ T.stripPrefix "./" $ partialFileName pInfo |
118 |
| - scored = Fuzzy.simpleFilter 1000 10 toMatch (map T.pack filePathCompletions) |
119 |
| - forM |
120 |
| - scored |
121 |
| - ( \compl' -> do |
122 |
| - let compl = Fuzzy.original compl' |
123 |
| - fullFilePath <- mkExposedModulePathCompletion compl pInfo |
124 |
| - pure fullFilePath |
125 |
| - ) |
126 |
| - ) |
127 |
| - where |
128 |
| - prefix = |
129 |
| - exposedModulePathToFp |
130 |
| - $ completionPrefix prefInfo |
131 |
| - isValidExposedModulePath :: PathCompletionInfo -> FilePath -> IO Bool |
132 |
| - isValidExposedModulePath pInfo path = do |
133 |
| - let dir = mkCompletionDirectory pInfo |
134 |
| - fileExists <- doesFileExist (dir FP.</> path) |
135 |
| - pure $ not fileExists || FP.isExtensionOf ".hs" path |
136 |
| - |
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 |
| --} |
140 |
| -fpToExposedModulePath :: FilePath -> FilePath -> FilePath |
141 |
| -fpToExposedModulePath srcDir cabalDir = T.unpack $ T.intercalate "." $ fmap T.pack $ FP.splitDirectories fp |
142 |
| - where |
143 |
| - fp = fromMaybe cabalDir $ stripPrefix srcDir cabalDir |
144 |
| - |
145 |
| -{- | Takes a path in the exposed module field and translates it to a filepath. |
146 |
| --} |
147 |
| -exposedModulePathToFp :: T.Text -> FilePath |
148 |
| -exposedModulePathToFp fp = T.unpack $ T.replace "." (T.singleton FP.pathSeparator) fp |
149 | 147 |
|
150 | 148 | {- | Returns the directory, the currently handled cabal file is in.
|
151 | 149 |
|
@@ -191,16 +189,4 @@ mkFilePathCompletion suffix completion complInfo = do
|
191 | 189 | let completedPath = if isFilePath then combinedPath ++ T.unpack suffix else combinedPath
|
192 | 190 | pure $ T.pack completedPath
|
193 | 191 |
|
194 |
| -{- Takes a completed path and a pathCompletionInfo and generates the whole completed |
195 |
| - filepath including the already written prefix using the cabal syntax for exposed modules. |
196 | 192 |
|
197 |
| - i.e. Dir.Dir2.HaskellFile |
198 |
| - or Dir.Dir2. |
199 |
| --} |
200 |
| -mkExposedModulePathCompletion :: T.Text -> PathCompletionInfo -> IO T.Text |
201 |
| -mkExposedModulePathCompletion completion complInfo = do |
202 |
| - let combinedPath = T.unpack $ mkPathCompletion complInfo completion |
203 |
| - isFilePath <- doesFileExist (workingDir complInfo FP.</> combinedPath) |
204 |
| - let completedPath = T.pack $ if isFilePath then dropExtension combinedPath else combinedPath ++ "." |
205 |
| - let exposedPath = fromMaybe completedPath $ T.stripPrefix "./" completedPath |
206 |
| - pure $ T.pack $ fpToExposedModulePath "" $ T.unpack exposedPath |
0 commit comments