@@ -20,7 +20,7 @@ module Experiments
2020, exampleToOptions
2121) where
2222import Control.Applicative.Combinators (skipManyTill )
23- import Control.Exception.Safe
23+ import Control.Exception.Safe ( IOException , handleAny , try )
2424import Control.Monad.Extra
2525import Control.Monad.IO.Class
2626import Data.Aeson (Value (Null ))
@@ -41,6 +41,7 @@ import System.FilePath ((</>), (<.>))
4141import System.Process
4242import System.Time.Extra
4343import Text.ParserCombinators.ReadP (readP_to_S )
44+ import Development.Shake (cmd_ , CmdOption (Cwd , FileStdout ))
4445
4546charEdit :: Position -> TextDocumentContentChangeEvent
4647charEdit p =
@@ -423,19 +424,24 @@ setup :: HasConfig => IO SetupResult
423424setup = do
424425-- when alreadyExists $ removeDirectoryRecursive examplesPath
425426 benchDir <- case example ? config of
426- UsePackage {.. } -> return examplePath
427+ UsePackage {.. } -> do
428+ let hieYamlPath = examplePath </> " hie.yaml"
429+ alreadyExists <- doesFileExist hieYamlPath
430+ unless alreadyExists $
431+ cmd_ (Cwd examplePath) (FileStdout hieYamlPath) (" gen-hie" :: String )
432+ return examplePath
427433 GetPackage {.. } -> do
428434 let path = examplesPath </> package
429435 package = exampleName <> " -" <> showVersion exampleVersion
436+ hieYamlPath = path </> " hie.yaml"
430437 alreadySetup <- doesDirectoryExist path
431438 unless alreadySetup $
432439 case buildTool ? config of
433440 Cabal -> do
434441 let cabalVerbosity = " -v" ++ show (fromEnum (verbose ? config))
435442 callCommandLogging $ " cabal get " <> cabalVerbosity <> " " <> package <> " -d " <> examplesPath
436- writeFile
437- (path </> " hie.yaml" )
438- (" cradle: {cabal: {component: " <> exampleName <> " }}" )
443+ let hieYamlPath = path </> " hie.yaml"
444+ cmd_ (Cwd path) (FileStdout hieYamlPath) (" gen-hie" :: String )
439445 -- Need this in case there is a parent cabal.project somewhere
440446 writeFile
441447 (path </> " cabal.project" )
@@ -464,9 +470,7 @@ setup = do
464470 ]
465471 )
466472
467- writeFile
468- (path </> " hie.yaml" )
469- (" cradle: {stack: {component: " <> show (exampleName <> " :lib" ) <> " }}" )
473+ cmd_ (Cwd path) (FileStdout hieYamlPath) (" gen-hie" :: String ) [" --stack" :: String ]
470474 return path
471475
472476 whenJust (shakeProfiling ? config) $ createDirectoryIfMissing True
@@ -498,22 +502,21 @@ setupDocumentContents config =
498502
499503 -- Find an identifier defined in another file in this project
500504 symbols <- getDocumentSymbols doc
501- case symbols of
502- Left [DocumentSymbol {_children = Just (List symbols)}] -> do
503- let endOfImports = case symbols of
504- DocumentSymbol {_kind = SkModule , _name = " imports" , _range } : _ ->
505- Position (succ $ _line $ _end _range) 4
506- DocumentSymbol {_range} : _ -> _start _range
507- [] -> error " Module has no symbols"
508- contents <- documentContents doc
509-
510- identifierP <- searchSymbol doc contents endOfImports
511-
512- return $ DocumentPositions {.. }
513- other ->
514- error $ " symbols: " <> show other
515-
516-
505+ let endOfImports = case symbols of
506+ Left symbols | Just x <- findEndOfImports symbols -> x
507+ _ -> error $ " symbols: " <> show symbols
508+ contents <- documentContents doc
509+ identifierP <- searchSymbol doc contents endOfImports
510+ return $ DocumentPositions {.. }
511+
512+ findEndOfImports :: [DocumentSymbol ] -> Maybe Position
513+ findEndOfImports (DocumentSymbol {_kind = SkModule , _name = " imports" , _range} : _) =
514+ Just $ Position (succ $ _line $ _end _range) 4
515+ findEndOfImports [DocumentSymbol {_kind = SkFile , _children = Just (List cc)}] =
516+ findEndOfImports cc
517+ findEndOfImports (DocumentSymbol {_range} : _) =
518+ Just $ _start _range
519+ findEndOfImports _ = Nothing
517520
518521--------------------------------------------------------------------------------------------
519522
0 commit comments