@@ -114,9 +114,10 @@ import qualified Language.LSP.Server as LSP
114114import Numeric.Natural (Natural )
115115import Options.Applicative hiding (action )
116116import 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 )
121122import System.IO (BufferMode (LineBuffering , NoBuffering ),
122123 Handle , hFlush ,
@@ -125,6 +126,7 @@ import System.IO (BufferMode (LineBuffe
125126 hSetEncoding , stderr ,
126127 stdin , stdout , utf8 )
127128import System.Random (newStdGen )
129+ import System.Process (readProcessWithExitCode )
128130import System.Time.Extra (Seconds , offsetTime ,
129131 showDuration )
130132
@@ -446,15 +448,29 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
446448 c ide
447449
448450expandFiles :: [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