Skip to content

Commit

Permalink
[purs ide] Reads files in TextMode for adding imports (#2850)
Browse files Browse the repository at this point in the history
The functions provided in System.IO.UTF8 use ByteString's readFile, which uses
https://hackage.haskell.org/package/base-4.9.1.0/docs/System-IO.html#v:openBinaryFile
under the hood, which in turn causes trouble when we treat source files as text
on Windows.
  • Loading branch information
kritzcreek authored and paf31 committed Apr 17, 2017
1 parent 82f1144 commit d5732af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions purescript.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ extra-source-files:
examples/warning/2411.purs
examples/warning/2542.purs
examples/warning/CustomWarning.purs
examples/warning/CustomWarning2.purs
examples/warning/CustomWarning3.purs
examples/warning/DuplicateExportRef.purs
examples/warning/DuplicateImport.purs
examples/warning/DuplicateImportRef.purs
Expand Down
2 changes: 1 addition & 1 deletion src/Language/PureScript/Ide/Imports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ parseImportsFromFile file = do
parseImportsFromFile' :: (MonadIO m, MonadError IdeError m) =>
FilePath -> m (P.ModuleName, [Text], [Import], [Text])
parseImportsFromFile' fp = do
file <- ideReadFile fp
file <- ideReadTextFile fp
case sliceImportSection (T.lines file) of
Right res -> pure res
Left err -> throwError (GeneralError err)
Expand Down
23 changes: 20 additions & 3 deletions src/Language/PureScript/Ide/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Language.PureScript.Ide.Util
, identT
, opNameT
, ideReadFile
, ideReadTextFile
, module Language.PureScript.Ide.Logging
) where

Expand All @@ -37,6 +38,7 @@ import Protolude hiding (decodeUtf8,
import Control.Lens hiding ((&), op)
import Data.Aeson
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Data.Text.Lazy as TL
import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
import qualified Language.PureScript as P
Expand Down Expand Up @@ -129,10 +131,25 @@ identT = iso P.runIdent P.Ident
opNameT :: Iso' (P.OpName a) Text
opNameT = iso P.runOpName P.OpName

ideReadFile :: (MonadIO m, MonadError IdeError m) => FilePath -> m Text
ideReadFile fp = do
contents :: Either IOException Text <- liftIO (try (readUTF8FileT fp))
ideReadFile'
:: (MonadIO m, MonadError IdeError m)
=> (FilePath -> IO Text)
-> FilePath
-> m Text
ideReadFile' fileReader fp = do
contents :: Either IOException Text <- liftIO (try (fileReader fp))
either
(\_ -> throwError (GeneralError ("Couldn't find file at: " <> T.pack fp)))
pure
contents

ideReadFile :: (MonadIO m, MonadError IdeError m) => FilePath -> m Text
ideReadFile = ideReadFile' readUTF8FileT

-- | This function is to be used over @ideReadFile@ when the result is not just
-- passed on to the PureScript parser, but also needs to be treated as lines of
-- text. Because @ideReadFile@ reads the file as ByteString in @BinaryMode@
-- rather than @TextMode@ line endings get mangled on Windows otherwise. This is
-- irrelevant for parsing, because the Lexer strips these additional @\r@'s.
ideReadTextFile :: (MonadIO m, MonadError IdeError m) => FilePath -> m Text
ideReadTextFile = ideReadFile' TIO.readFile

0 comments on commit d5732af

Please sign in to comment.