Skip to content

Commit 013cd7b

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

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-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: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ 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)
119-
import System.FilePath (takeExtension,
119+
import System.FilePath ((</>),
120+
takeExtension,
120121
takeFileName)
121122
import System.IO (BufferMode (LineBuffering, NoBuffering),
122123
Handle, hFlush,
@@ -125,6 +126,7 @@ import System.IO (BufferMode (LineBuffe
125126
hSetEncoding, stderr,
126127
stdin, stdout, utf8)
127128
import System.Random (newStdGen)
129+
import System.Process (readProcessWithExitCode)
128130
import System.Time.Extra (Seconds, offsetTime,
129131
showDuration)
130132

@@ -446,15 +448,29 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
446448
c ide
447449

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

0 commit comments

Comments
 (0)