-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix main-is completion not being relative to source dirs
- Loading branch information
1 parent
af6387c
commit 379d385
Showing
8 changed files
with
212 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completer/Paths.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
module Ide.Plugin.Cabal.Completion.Completer.Paths where | ||
|
||
import qualified Data.List as List | ||
import qualified Data.Text as T | ||
import Distribution.PackageDescription (Benchmark (..), | ||
BuildInfo (..), | ||
CondTree (condTreeData), | ||
Executable (..), | ||
GenericPackageDescription (..), | ||
Library (..), | ||
UnqualComponentName, | ||
mkUnqualComponentName, | ||
testBuildInfo) | ||
import Distribution.Utils.Path (getSymbolicPath) | ||
import Ide.Plugin.Cabal.Completion.Types | ||
import qualified System.FilePath as FP | ||
import qualified System.FilePath.Posix as Posix | ||
|
||
|
||
-- | Information used to query and build path completions. | ||
-- | ||
-- Note that pathSegment combined with queryDirectory results in | ||
-- the original prefix. | ||
-- | ||
-- Example: | ||
-- When given the written prefix, @dir1\/dir2\/fi@, the | ||
-- resulting PathCompletionInfo would be: | ||
-- | ||
-- @ | ||
-- pathSegment = "fi" | ||
-- queryDirectory = "dir1\/dir2\/fi" | ||
-- ... | ||
-- @ | ||
data PathCompletionInfo = PathCompletionInfo | ||
{ -- | partly written segment of the next part of the path | ||
pathSegment :: T.Text, | ||
-- | written part of path, platform dependent | ||
queryDirectory :: FilePath, | ||
-- | directory relative to which relative paths are interpreted, platform dependent | ||
workingDirectory :: FilePath, | ||
-- | Did the completion happen in the context of a string notation, | ||
-- if yes, contains the state of the string notation | ||
isStringNotationPath :: Maybe Apostrophe | ||
} | ||
deriving (Eq, Show) | ||
|
||
pathCompletionInfoFromCabalPrefixInfo :: FilePath -> CabalPrefixInfo -> PathCompletionInfo | ||
pathCompletionInfoFromCabalPrefixInfo fp prefInfo = | ||
PathCompletionInfo | ||
{ pathSegment = T.pack pathSegment', | ||
queryDirectory = queryDirectory', | ||
workingDirectory = completionWorkingDir prefInfo FP.</> fp, | ||
isStringNotationPath = isStringNotation prefInfo | ||
} | ||
where | ||
prefix = T.unpack $ completionPrefix prefInfo | ||
(queryDirectory', pathSegment') = Posix.splitFileName prefix | ||
|
||
-- | Extracts the source directories of the library stanza. | ||
sourceDirsExtractionLibrary :: Maybe StanzaName -> GenericPackageDescription -> [FilePath] | ||
sourceDirsExtractionLibrary Nothing gpd = | ||
-- we use condLibrary to get the information contained in the library stanza | ||
-- since the library in PackageDescription is not populated by us | ||
case libM of | ||
Just lib -> do | ||
map getSymbolicPath $ hsSourceDirs $ libBuildInfo $ condTreeData lib | ||
Nothing -> [] | ||
where | ||
libM = condLibrary gpd | ||
sourceDirsExtractionLibrary name gpd = extractRelativeDirsFromStanza name gpd condSubLibraries libBuildInfo | ||
|
||
-- | Extracts the source directories of the executable stanza with the given name. | ||
sourceDirsExtractionExecutable :: Maybe StanzaName -> GenericPackageDescription -> [FilePath] | ||
sourceDirsExtractionExecutable name gpd = extractRelativeDirsFromStanza name gpd condExecutables buildInfo | ||
|
||
-- | Extracts the source directories of the test suite stanza with the given name. | ||
sourceDirsExtractionTestSuite :: Maybe StanzaName -> GenericPackageDescription -> [FilePath] | ||
sourceDirsExtractionTestSuite name gpd = extractRelativeDirsFromStanza name gpd condTestSuites testBuildInfo | ||
|
||
-- | Extracts the source directories of benchmark stanza with the given name. | ||
sourceDirsExtractionBenchmark :: Maybe StanzaName -> GenericPackageDescription -> [FilePath] | ||
sourceDirsExtractionBenchmark name gpd = extractRelativeDirsFromStanza name gpd condBenchmarks benchmarkBuildInfo | ||
|
||
-- | Takes a possible stanza name, a GenericPackageDescription, | ||
-- a function to access the stanza information we are interested in | ||
-- and a function to access the build info from the specific stanza. | ||
-- | ||
-- Returns a list of relative source directory paths specified for the extracted stanza. | ||
extractRelativeDirsFromStanza :: | ||
Maybe StanzaName -> | ||
GenericPackageDescription -> | ||
(GenericPackageDescription -> [(UnqualComponentName, CondTree b c a)]) -> | ||
(a -> BuildInfo) -> | ||
[FilePath] | ||
extractRelativeDirsFromStanza Nothing _ _ _ = [] | ||
extractRelativeDirsFromStanza (Just name) gpd getStanza getBuildInfo | ||
| Just stanza <- stanzaM = map getSymbolicPath $ hsSourceDirs $ getBuildInfo stanza | ||
| otherwise = [] | ||
where | ||
stanzaM = fmap (condTreeData . snd) res | ||
allStanzasM = getStanza gpd | ||
res = | ||
List.find | ||
( \(n, _) -> | ||
n == mkUnqualComponentName (T.unpack name) | ||
) | ||
allStanzasM |
Oops, something went wrong.