@@ -114,16 +114,17 @@ 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 )
119119import System.FilePath (takeExtension ,
120- takeFileName )
120+ takeFileName , (</>) )
121121import System.IO (BufferMode (LineBuffering , NoBuffering ),
122122 Handle , hFlush ,
123123 hPutStrLn ,
124124 hSetBuffering ,
125125 hSetEncoding , stderr ,
126126 stdin , stdout , utf8 )
127+ import System.Process (readProcessWithExitCode )
127128import System.Random (newStdGen )
128129import System.Time.Extra (Seconds , offsetTime ,
129130 showDuration )
@@ -446,15 +447,29 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re
446447 c ide
447448
448449expandFiles :: [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