Skip to content

Commit

Permalink
Pushes import suggestions for the Prelude to the back
Browse files Browse the repository at this point in the history
This way if you just apply import suggestions one by one the Prelude
always remains the last implicit import
  • Loading branch information
kritzcreek committed Feb 28, 2020
1 parent dc9d444 commit 63dfa5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Main where

import Prelude

import Control.Monad.Reader (class MonadAsk, ReaderT, ask, runReaderT)
import Data.Argonaut (Json)
import Control.Monad.Reader (class MonadAsk, ReaderT, ask, runReaderT)
import Data.Array as Array
import Data.Either (Either(..), either, isRight)
import Data.Maybe (Maybe(..))
Expand Down
23 changes: 21 additions & 2 deletions src/Pscid/Psa.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Data.Argonaut (Json, decodeJson)
import Data.Array as Array
import Data.Either (Either, hush)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Maybe as Maybe
import Data.Set as Set
import Data.String as String
import Data.Traversable (traverse)
Expand All @@ -19,7 +20,7 @@ import Effect.Console as Console
import Effect.Exception (catchException)
import Node.Encoding as Encoding
import Node.FS.Sync as File
import Psa (Output, PsaError, PsaOptions, PsaResult, StatVerbosity(..), output, parsePsaError)
import Psa (Output, PsaError, PsaOptions, PsaResult, StatVerbosity(..), Suggestion, output, parsePsaError)
import Psa.Printer (renderAnsi, renderRow)
import Psa.Printer.Default (renderError, renderWarning)
import Psa.Util (iter_)
Expand Down Expand Up @@ -86,6 +87,24 @@ loadLines filename pos = do
let source = Array.slice (pos.startLine - 1) (pos.endLine) contents
pure (Just source)

-- | We do this to push imports suggestions for the Prelude to the
-- | back of the suggestions, so all other modules can be explicitly
-- | imported first through the suggestions
reorderWarnings Array PsaError Array PsaError
reorderWarnings errors =
let
isPreludeImport Suggestion Boolean
isPreludeImport { replacement } =
-- We're checking for two patterns to also catch the case of
-- custom preludes that still have Prelude in their name
(String.contains (String.Pattern "import")
&& String.contains (String.Pattern "Prelude")) replacement
{ yes, no } =
Array.partition (Maybe.maybe false isPreludeImport ∘ _.suggestion) errors
in no <> yes

-- | Removes warnings that have their error codes ignored and moves
-- | import suggestions for the Prelude to the back
filterWarnings Array String Array PsaError Array PsaError
filterWarnings ignored errors =
Array.filter (\e → e.errorCode `Array.notElem` ignored) errors
reorderWarnings (Array.filter (\e → e.errorCode `Array.notElem` ignored) errors)

0 comments on commit 63dfa5b

Please sign in to comment.