Skip to content

Commit a8035c7

Browse files
author
Santiago Weight
committed
refact: extract ImportUtils
1 parent 5dbbd85 commit a8035c7

File tree

2 files changed

+85
-69
lines changed

2 files changed

+85
-69
lines changed

plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import qualified Language.LSP.Server as LSP
7272
import Language.LSP.Types (ApplyWorkspaceEditParams (..),
7373
CodeAction (..),
7474
CodeActionContext (CodeActionContext, _diagnostics),
75-
CodeActionKind (CodeActionQuickFix, CodeActionUnknown),
75+
CodeActionKind (CodeActionQuickFix),
7676
CodeActionParams (CodeActionParams),
7777
Command,
7878
Diagnostic (..),
@@ -102,6 +102,7 @@ import GHC (AddEpAnn (Ad
102102
EpaLocation (..),
103103
LEpaComment,
104104
LocatedA)
105+
import Development.IDE.Plugin.Plugins.ImportUtils
105106
#else
106107
import Language.Haskell.GHC.ExactPrint.Types (Annotation (annsDP),
107108
DeltaPos,
@@ -2054,71 +2055,3 @@ matchRegExMultipleImports message = do
20542055
imps <- regExImports imports
20552056
return (binding, imps)
20562057

2057-
-- | Possible import styles for an 'IdentInfo'.
2058-
--
2059-
-- The first 'Text' parameter corresponds to the 'rendered' field of the
2060-
-- 'IdentInfo'.
2061-
data ImportStyle
2062-
= ImportTopLevel T.Text
2063-
-- ^ Import a top-level export from a module, e.g., a function, a type, a
2064-
-- class.
2065-
--
2066-
-- > import M (?)
2067-
--
2068-
-- Some exports that have a parent, like a type-class method or an
2069-
-- associated type/data family, can still be imported as a top-level
2070-
-- import.
2071-
--
2072-
-- Note that this is not the case for constructors, they must always be
2073-
-- imported as part of their parent data type.
2074-
2075-
| ImportViaParent T.Text T.Text
2076-
-- ^ Import an export (first parameter) through its parent (second
2077-
-- parameter).
2078-
--
2079-
-- import M (P(?))
2080-
--
2081-
-- @P@ and @?@ can be a data type and a constructor, a class and a method,
2082-
-- a class and an associated type/data family, etc.
2083-
2084-
| ImportAllConstructors T.Text
2085-
-- ^ Import all constructors for a specific data type.
2086-
--
2087-
-- import M (P(..))
2088-
--
2089-
-- @P@ can be a data type or a class.
2090-
deriving Show
2091-
2092-
importStyles :: IdentInfo -> NonEmpty ImportStyle
2093-
importStyles IdentInfo {parent, rendered, isDatacon}
2094-
| Just p <- parent
2095-
-- Constructors always have to be imported via their parent data type, but
2096-
-- methods and associated type/data families can also be imported as
2097-
-- top-level exports.
2098-
= ImportViaParent rendered p
2099-
:| [ImportTopLevel rendered | not isDatacon]
2100-
<> [ImportAllConstructors p]
2101-
| otherwise
2102-
= ImportTopLevel rendered :| []
2103-
2104-
-- | Used for adding new imports
2105-
renderImportStyle :: ImportStyle -> T.Text
2106-
renderImportStyle (ImportTopLevel x) = x
2107-
renderImportStyle (ImportViaParent x p@(T.uncons -> Just ('(', _))) = "type " <> p <> "(" <> x <> ")"
2108-
renderImportStyle (ImportViaParent x p) = p <> "(" <> x <> ")"
2109-
renderImportStyle (ImportAllConstructors p) = p <> "(..)"
2110-
2111-
-- | Used for extending import lists
2112-
unImportStyle :: ImportStyle -> (Maybe String, String)
2113-
unImportStyle (ImportTopLevel x) = (Nothing, T.unpack x)
2114-
unImportStyle (ImportViaParent x y) = (Just $ T.unpack y, T.unpack x)
2115-
unImportStyle (ImportAllConstructors x) = (Just $ T.unpack x, wildCardSymbol)
2116-
2117-
2118-
quickFixImportKind' :: T.Text -> ImportStyle -> CodeActionKind
2119-
quickFixImportKind' x (ImportTopLevel _) = CodeActionUnknown $ "quickfix.import." <> x <> ".list.topLevel"
2120-
quickFixImportKind' x (ImportViaParent _ _) = CodeActionUnknown $ "quickfix.import." <> x <> ".list.withParent"
2121-
quickFixImportKind' x (ImportAllConstructors _) = CodeActionUnknown $ "quickfix.import." <> x <> ".list.allConstructors"
2122-
2123-
quickFixImportKind :: T.Text -> CodeActionKind
2124-
quickFixImportKind x = CodeActionUnknown $ "quickfix.import." <> x
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
module Development.IDE.Plugin.Plugins.ImportUtils
2+
( ImportStyle(..),
3+
quickFixImportKind',
4+
quickFixImportKind,
5+
renderImportStyle,
6+
unImportStyle,
7+
importStyles
8+
) where
9+
10+
import Data.List.NonEmpty (NonEmpty ((:|)))
11+
import qualified Data.Text as T
12+
import Development.IDE.Plugin.CodeAction.ExactPrint (wildCardSymbol)
13+
import Development.IDE.Types.Exports (IdentInfo (..))
14+
import Language.LSP.Types (CodeActionKind (..))
15+
16+
-- | Possible import styles for an 'IdentInfo'.
17+
--
18+
-- The first 'Text' parameter corresponds to the 'rendered' field of the
19+
-- 'IdentInfo'.
20+
data ImportStyle
21+
= ImportTopLevel T.Text
22+
-- ^ Import a top-level export from a module, e.g., a function, a type, a
23+
-- class.
24+
--
25+
-- > import M (?)
26+
--
27+
-- Some exports that have a parent, like a type-class method or an
28+
-- associated type/data family, can still be imported as a top-level
29+
-- import.
30+
--
31+
-- Note that this is not the case for constructors, they must always be
32+
-- imported as part of their parent data type.
33+
34+
| ImportViaParent T.Text T.Text
35+
-- ^ Import an export (first parameter) through its parent (second
36+
-- parameter).
37+
--
38+
-- import M (P(?))
39+
--
40+
-- @P@ and @?@ can be a data type and a constructor, a class and a method,
41+
-- a class and an associated type/data family, etc.
42+
43+
| ImportAllConstructors T.Text
44+
-- ^ Import all constructors for a specific data type.
45+
--
46+
-- import M (P(..))
47+
--
48+
-- @P@ can be a data type or a class.
49+
deriving Show
50+
51+
importStyles :: IdentInfo -> NonEmpty ImportStyle
52+
importStyles IdentInfo {parent, rendered, isDatacon}
53+
| Just p <- parent
54+
-- Constructors always have to be imported via their parent data type, but
55+
-- methods and associated type/data families can also be imported as
56+
-- top-level exports.
57+
= ImportViaParent rendered p
58+
:| [ImportTopLevel rendered | not isDatacon]
59+
<> [ImportAllConstructors p]
60+
| otherwise
61+
= ImportTopLevel rendered :| []
62+
63+
-- | Used for adding new imports
64+
renderImportStyle :: ImportStyle -> T.Text
65+
renderImportStyle (ImportTopLevel x) = x
66+
renderImportStyle (ImportViaParent x p@(T.uncons -> Just ('(', _))) = "type " <> p <> "(" <> x <> ")"
67+
renderImportStyle (ImportViaParent x p) = p <> "(" <> x <> ")"
68+
renderImportStyle (ImportAllConstructors p) = p <> "(..)"
69+
70+
-- | Used for extending import lists
71+
unImportStyle :: ImportStyle -> (Maybe String, String)
72+
unImportStyle (ImportTopLevel x) = (Nothing, T.unpack x)
73+
unImportStyle (ImportViaParent x y) = (Just $ T.unpack y, T.unpack x)
74+
unImportStyle (ImportAllConstructors x) = (Just $ T.unpack x, wildCardSymbol)
75+
76+
77+
quickFixImportKind' :: T.Text -> ImportStyle -> CodeActionKind
78+
quickFixImportKind' x (ImportTopLevel _) = CodeActionUnknown $ "quickfix.import." <> x <> ".list.topLevel"
79+
quickFixImportKind' x (ImportViaParent _ _) = CodeActionUnknown $ "quickfix.import." <> x <> ".list.withParent"
80+
quickFixImportKind' x (ImportAllConstructors _) = CodeActionUnknown $ "quickfix.import." <> x <> ".list.allConstructors"
81+
82+
quickFixImportKind :: T.Text -> CodeActionKind
83+
quickFixImportKind x = CodeActionUnknown $ "quickfix.import." <> x

0 commit comments

Comments
 (0)