module Development.IDE.Plugin.Plugins.ImportUtils
( ImportStyle(..),
quickFixImportKind',
quickFixImportKind,
renderImportStyle,
unImportStyle,
importStyles,
QualifiedImportStyle(..),
qualifiedImportStyle
) where
import Data.List.NonEmpty (NonEmpty ((:|)))
import qualified Data.Text as T
import Development.IDE.GHC.Compat
import Development.IDE.Plugin.CodeAction.ExactPrint (wildCardSymbol)
import Development.IDE.Types.Exports
import Language.LSP.Types (CodeActionKind (..))
data ImportStyle
= ImportTopLevel T.Text
| ImportViaParent T.Text T.Text
| ImportAllConstructors T.Text
deriving Int -> ImportStyle -> ShowS
[ImportStyle] -> ShowS
ImportStyle -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ImportStyle] -> ShowS
$cshowList :: [ImportStyle] -> ShowS
show :: ImportStyle -> String
$cshow :: ImportStyle -> String
showsPrec :: Int -> ImportStyle -> ShowS
$cshowsPrec :: Int -> ImportStyle -> ShowS
Show
importStyles :: IdentInfo -> NonEmpty ImportStyle
importStyles :: IdentInfo -> NonEmpty ImportStyle
importStyles i :: IdentInfo
i@(IdentInfo {Maybe OccName
parent :: IdentInfo -> Maybe OccName
parent :: Maybe OccName
parent})
| Just Text
p <- Maybe Text
pr
= Text -> Text -> ImportStyle
ImportViaParent Text
rend Text
p
forall a. a -> [a] -> NonEmpty a
:| [Text -> ImportStyle
ImportTopLevel Text
rend | Bool -> Bool
not (IdentInfo -> Bool
isDatacon IdentInfo
i)]
forall a. Semigroup a => a -> a -> a
<> [Text -> ImportStyle
ImportAllConstructors Text
p]
| Bool
otherwise
= Text -> ImportStyle
ImportTopLevel Text
rend forall a. a -> [a] -> NonEmpty a
:| []
where rend :: Text
rend = IdentInfo -> Text
rendered IdentInfo
i
pr :: Maybe Text
pr = OccName -> Text
occNameText forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe OccName
parent
renderImportStyle :: ImportStyle -> T.Text
renderImportStyle :: ImportStyle -> Text
renderImportStyle (ImportTopLevel Text
x) = Text
x
renderImportStyle (ImportViaParent Text
x p :: Text
p@(Text -> Maybe (Char, Text)
T.uncons -> Just (Char
'(', Text
_))) = Text
"type " forall a. Semigroup a => a -> a -> a
<> Text
p forall a. Semigroup a => a -> a -> a
<> Text
"(" forall a. Semigroup a => a -> a -> a
<> Text
x forall a. Semigroup a => a -> a -> a
<> Text
")"
renderImportStyle (ImportViaParent Text
x Text
p) = Text
p forall a. Semigroup a => a -> a -> a
<> Text
"(" forall a. Semigroup a => a -> a -> a
<> Text
x forall a. Semigroup a => a -> a -> a
<> Text
")"
renderImportStyle (ImportAllConstructors Text
p) = Text
p forall a. Semigroup a => a -> a -> a
<> Text
"(..)"
unImportStyle :: ImportStyle -> (Maybe String, String)
unImportStyle :: ImportStyle -> (Maybe String, String)
unImportStyle (ImportTopLevel Text
x) = (forall a. Maybe a
Nothing, Text -> String
T.unpack Text
x)
unImportStyle (ImportViaParent Text
x Text
y) = (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
y, Text -> String
T.unpack Text
x)
unImportStyle (ImportAllConstructors Text
x) = (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
x, String
wildCardSymbol)
quickFixImportKind' :: T.Text -> ImportStyle -> CodeActionKind
quickFixImportKind' :: Text -> ImportStyle -> CodeActionKind
quickFixImportKind' Text
x (ImportTopLevel Text
_) = Text -> CodeActionKind
CodeActionUnknown forall a b. (a -> b) -> a -> b
$ Text
"quickfix.import." forall a. Semigroup a => a -> a -> a
<> Text
x forall a. Semigroup a => a -> a -> a
<> Text
".list.topLevel"
quickFixImportKind' Text
x (ImportViaParent Text
_ Text
_) = Text -> CodeActionKind
CodeActionUnknown forall a b. (a -> b) -> a -> b
$ Text
"quickfix.import." forall a. Semigroup a => a -> a -> a
<> Text
x forall a. Semigroup a => a -> a -> a
<> Text
".list.withParent"
quickFixImportKind' Text
x (ImportAllConstructors Text
_) = Text -> CodeActionKind
CodeActionUnknown forall a b. (a -> b) -> a -> b
$ Text
"quickfix.import." forall a. Semigroup a => a -> a -> a
<> Text
x forall a. Semigroup a => a -> a -> a
<> Text
".list.allConstructors"
quickFixImportKind :: T.Text -> CodeActionKind
quickFixImportKind :: Text -> CodeActionKind
quickFixImportKind Text
x = Text -> CodeActionKind
CodeActionUnknown forall a b. (a -> b) -> a -> b
$ Text
"quickfix.import." forall a. Semigroup a => a -> a -> a
<> Text
x
data QualifiedImportStyle = QualifiedImportPostfix | QualifiedImportPrefix
deriving Int -> QualifiedImportStyle -> ShowS
[QualifiedImportStyle] -> ShowS
QualifiedImportStyle -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [QualifiedImportStyle] -> ShowS
$cshowList :: [QualifiedImportStyle] -> ShowS
show :: QualifiedImportStyle -> String
$cshow :: QualifiedImportStyle -> String
showsPrec :: Int -> QualifiedImportStyle -> ShowS
$cshowsPrec :: Int -> QualifiedImportStyle -> ShowS
Show
qualifiedImportStyle :: DynFlags -> QualifiedImportStyle
qualifiedImportStyle :: DynFlags -> QualifiedImportStyle
qualifiedImportStyle DynFlags
df | Bool
hasImportQualifedPostEnabled Bool -> Bool -> Bool
&& Bool
hasPrePositiveQualifiedWarning = QualifiedImportStyle
QualifiedImportPostfix
| Bool
otherwise = QualifiedImportStyle
QualifiedImportPrefix
where hasImportQualifedPostEnabled :: Bool
hasImportQualifedPostEnabled = Extension -> DynFlags -> Bool
xopt Extension
ImportQualifiedPost DynFlags
df
hasPrePositiveQualifiedWarning :: Bool
hasPrePositiveQualifiedWarning = WarningFlag -> DynFlags -> Bool
wopt WarningFlag
Opt_WarnPrepositiveQualifiedModule DynFlags
df