module Development.IDE.Plugin.Plugins.Diagnostic (
  matchVariableNotInScope,
  matchRegexUnifySpaces,
  unifySpaces,
  matchFoundHole,
  matchFoundHoleIncludeUnderscore,
  )
  where

import           Data.Bifunctor  (Bifunctor (..))
import qualified Data.Text       as T
import           Text.Regex.TDFA ((=~~))

unifySpaces :: T.Text -> T.Text
unifySpaces :: Text -> Text
unifySpaces    = [Text] -> Text
T.unwords forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
T.words

-- | Returns Just (the submatches) for the first capture, or Nothing.
matchRegex :: T.Text -> T.Text -> Maybe [T.Text]
matchRegex :: Text -> Text -> Maybe [Text]
matchRegex Text
message Text
regex = case Text
message forall source source1 target (m :: * -> *).
(RegexMaker Regex CompOption ExecOption source,
 RegexContext Regex source1 target, MonadFail m) =>
source1 -> source -> m target
=~~ Text
regex of
    Just (Text
_ :: T.Text, Text
_ :: T.Text, Text
_ :: T.Text, [Text]
bindings) -> forall a. a -> Maybe a
Just [Text]
bindings
    Maybe (Text, Text, Text, [Text])
Nothing                                                -> forall a. Maybe a
Nothing

-- | 'matchRegex' combined with 'unifySpaces'
matchRegexUnifySpaces :: T.Text -> T.Text -> Maybe [T.Text]
matchRegexUnifySpaces :: Text -> Text -> Maybe [Text]
matchRegexUnifySpaces Text
message = Text -> Text -> Maybe [Text]
matchRegex (Text -> Text
unifySpaces Text
message)

matchFoundHole :: T.Text -> Maybe (T.Text, T.Text)
matchFoundHole :: Text -> Maybe (Text, Text)
matchFoundHole Text
message
  | Just [Text
name, Text
typ] <- Text -> Text -> Maybe [Text]
matchRegexUnifySpaces Text
message Text
"Found hole: _([^ ]+) :: ([^*•]+) Or perhaps" =
      forall a. a -> Maybe a
Just (Text
name, Text
typ)
  | Bool
otherwise = forall a. Maybe a
Nothing

matchFoundHoleIncludeUnderscore :: T.Text -> Maybe (T.Text, T.Text)
matchFoundHoleIncludeUnderscore :: Text -> Maybe (Text, Text)
matchFoundHoleIncludeUnderscore Text
message = forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text
"_" forall a. Semigroup a => a -> a -> a
<>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe (Text, Text)
matchFoundHole Text
message

matchVariableNotInScope :: T.Text -> Maybe (T.Text, Maybe T.Text)
matchVariableNotInScope :: Text -> Maybe (Text, Maybe Text)
matchVariableNotInScope Text
message
  --     * Variable not in scope:
  --         suggestAcion :: Maybe T.Text -> Range -> Range
  --     * Variable not in scope:
  --         suggestAcion
  | Just (Text
name, Text
typ) <- Text -> Maybe (Text, Text)
matchVariableNotInScopeTyped Text
message = forall a. a -> Maybe a
Just (Text
name, forall a. a -> Maybe a
Just Text
typ)
  | Just Text
name <- Text -> Maybe Text
matchVariableNotInScopeUntyped Text
message = forall a. a -> Maybe a
Just (Text
name, forall a. Maybe a
Nothing)
  | Bool
otherwise = forall a. Maybe a
Nothing
  where
    matchVariableNotInScopeTyped :: Text -> Maybe (Text, Text)
matchVariableNotInScopeTyped Text
message
      | Just [Text
name, Text
typ] <- Text -> Text -> Maybe [Text]
matchRegexUnifySpaces Text
message Text
"Variable not in scope: ([^ ]+) :: ([^*•]+)" =
          forall a. a -> Maybe a
Just (Text
name, Text
typ)
      | Bool
otherwise = forall a. Maybe a
Nothing
    matchVariableNotInScopeUntyped :: Text -> Maybe Text
matchVariableNotInScopeUntyped Text
message
      | Just [Text
name] <- Text -> Text -> Maybe [Text]
matchRegexUnifySpaces Text
message Text
"Variable not in scope: ([^ ]+)" =
          forall a. a -> Maybe a
Just Text
name
      | Bool
otherwise = forall a. Maybe a
Nothing