Skip to content

Commit 5db38c5

Browse files
committed
Rely on gitignore to exclude listed files in ghcide (#4665)
1 parent c5a1093 commit 5db38c5

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

ghcide/ghcide.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ library
8989
, optparse-applicative
9090
, os-string
9191
, parallel
92+
, process
9293
, prettyprinter >=1.7
9394
, prettyprinter-ansi-terminal
9495
, random

ghcide/src/Development/IDE/Main.hs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,17 @@ import qualified Language.LSP.Server as LSP
114114
import Numeric.Natural (Natural)
115115
import Options.Applicative hiding (action)
116116
import qualified System.Directory.Extra as IO
117-
import System.Exit (ExitCode (ExitFailure),
117+
import System.Exit (ExitCode (ExitFailure, ExitSuccess),
118118
exitWith)
119119
import System.FilePath (takeExtension,
120-
takeFileName)
120+
takeFileName, (</>))
121121
import System.IO (BufferMode (LineBuffering, NoBuffering),
122122
Handle, hFlush,
123123
hPutStrLn,
124124
hSetBuffering,
125125
hSetEncoding, stderr,
126126
stdin, stdout, utf8)
127+
import System.Process (readProcessWithExitCode)
127128
import System.Random (newStdGen)
128129
import System.Time.Extra (Seconds, offsetTime,
129130
showDuration)
@@ -446,15 +447,29 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
446447
c ide
447448

448449
expandFiles :: [FilePath] -> IO [FilePath]
449-
expandFiles = concatMapM $ \x -> do
450+
expandFiles paths = do
451+
let haskellFind x =
452+
let recurse "." = True
453+
recurse y | "." `isPrefixOf` takeFileName y = False -- skip .git etc
454+
recurse y = takeFileName y `notElem` ["dist", "dist-newstyle"] -- cabal directories
455+
in filter (\y -> takeExtension y `elem` [".hs", ".lhs"]) <$> IO.listFilesInside (return . recurse) x
456+
(testGitExitCode, _, _) <- readProcessWithExitCode "git" ["status"] ""
457+
let findFiles =
458+
case testGitExitCode of
459+
ExitSuccess -> \path -> do
460+
let lookups = [path, path </> "*.hs", path </> "*.lhs"]
461+
(trackedExitCode, trackedStdout, _) <- readProcessWithExitCode "git" ("ls-files":lookups) ""
462+
(untrackedExitCode, untrackedStdout, _) <- readProcessWithExitCode "git" ("ls-files":"-o":lookups) ""
463+
if trackedExitCode == ExitSuccess && untrackedExitCode == ExitSuccess
464+
then pure $ lines trackedStdout <> lines untrackedStdout
465+
else haskellFind path
466+
_ -> haskellFind
467+
flip concatMapM paths $ \x -> do
450468
b <- IO.doesFileExist x
451469
if b
452470
then return [x]
453471
else do
454-
let recurse "." = True
455-
recurse y | "." `isPrefixOf` takeFileName y = False -- skip .git etc
456-
recurse y = takeFileName y `notElem` ["dist", "dist-newstyle"] -- cabal directories
457-
files <- filter (\y -> takeExtension y `elem` [".hs", ".lhs"]) <$> IO.listFilesInside (return . recurse) x
472+
files <- findFiles x
458473
when (null files) $
459474
fail $ "Couldn't find any .hs/.lhs files inside directory: " ++ x
460475
return files

0 commit comments

Comments
 (0)