Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 82d20f0

Browse files
committed
Add config for formatting on import
1 parent fc89420 commit 82d20f0

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

hie-plugin-api/Haskell/Ide/Engine/Config.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ data Config =
2525
, maxNumberOfProblems :: Int
2626
, liquidOn :: Bool
2727
, completionSnippetsOn :: Bool
28+
, formatOnImportOn :: Bool
2829
} deriving (Show,Eq)
2930

3031
instance Default Config where
3132
def = Config
32-
{ hlintOn = True
33-
, maxNumberOfProblems = 100
34-
, liquidOn = False
35-
, completionSnippetsOn = True
33+
{ hlintOn = True
34+
, maxNumberOfProblems = 100
35+
, liquidOn = False
36+
, completionSnippetsOn = True
37+
, formatOnImportOn = True
3638
}
3739

3840
-- TODO: Add API for plugins to expose their own LSP config options
@@ -44,6 +46,7 @@ instance FromJSON Config where
4446
<*> o .:? "maxNumberOfProblems" .!= maxNumberOfProblems def
4547
<*> o .:? "liquidOn" .!= liquidOn def
4648
<*> o .:? "completionSnippetsOn" .!= completionSnippetsOn def
49+
<*> o .:? "formatOnImportOn" .!= formatOnImportOn def
4750

4851
-- 2017-10-09 23:22:00.710515298 [ThreadId 11] - ---> {"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"languageServerHaskell":{"maxNumberOfProblems":100,"hlintOn":true}}}}
4952
-- 2017-10-09 23:22:00.710667381 [ThreadId 15] - reactor:got didChangeConfiguration notification:
@@ -55,12 +58,13 @@ instance FromJSON Config where
5558
-- ,("maxNumberOfProblems",Number 100.0)]))])}}
5659

5760
instance ToJSON Config where
58-
toJSON (Config h m l c) = object [ "languageServerHaskell" .= r ]
61+
toJSON (Config h m l c f) = object [ "languageServerHaskell" .= r ]
5962
where
6063
r = object [ "hlintOn" .= h
6164
, "maxNumberOfProblems" .= m
6265
, "liquidOn" .= l
6366
, "completionSnippetsOn" .= c
67+
, "completionSnippetsOn" .= f
6468
]
6569

6670
-- ---------------------------------------------------------------------

src/Haskell/Ide/Engine/Plugin/HsImport.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import qualified Data.Text.IO as T
1919
import qualified GHC.Generics as Generics
2020
import qualified GhcMod.Utils as GM
2121
import HsImport
22+
import Haskell.Ide.Engine.Config
2223
import Haskell.Ide.Engine.MonadTypes
2324
import qualified Language.Haskell.LSP.Types as J
2425
import qualified Language.Haskell.LSP.Types.Lens as J
@@ -54,6 +55,9 @@ importCmd = CmdSync $ \(ImportParams uri modName) -> importModule uri modName
5455
importModule :: Uri -> T.Text -> IdeGhcM (IdeResult J.WorkspaceEdit)
5556
importModule uri modName =
5657
pluginGetFile "hsimport cmd: " uri $ \origInput -> do
58+
59+
shouldFormat <- formatOnImportOn <$> liftToGhc getConfig
60+
5761
fileMap <- GM.mkRevRedirMapFunc
5862
GM.withMappedFile origInput $ \input -> do
5963

@@ -78,12 +82,17 @@ importModule uri modName =
7882

7983
confFile <- liftIO $ Brittany.getConfFile origInput
8084
-- Format the import with Brittany
81-
newChanges <- forM mChanges $ mapM $ mapM (formatTextEdit confFile)
82-
newDocChanges <- forM mDocChanges $ mapM $ \(J.TextDocumentEdit vDocId tes) -> do
83-
ftes <- forM tes (formatTextEdit confFile)
84-
return (J.TextDocumentEdit vDocId ftes)
8585

86-
return $ IdeResultOk (J.WorkspaceEdit newChanges newDocChanges)
86+
if shouldFormat
87+
then do
88+
newChanges <- forM mChanges $ mapM $ mapM (formatTextEdit confFile)
89+
newDocChanges <- forM mDocChanges $ mapM $ \(J.TextDocumentEdit vDocId tes) -> do
90+
ftes <- forM tes (formatTextEdit confFile)
91+
return (J.TextDocumentEdit vDocId ftes)
92+
93+
return $ IdeResultOk (J.WorkspaceEdit newChanges newDocChanges)
94+
else
95+
return $ IdeResultOk (J.WorkspaceEdit mChanges mDocChanges)
8796

8897
where formatTextEdit confFile (J.TextEdit r t) = do
8998
-- TODO: This tab size of 2 spaces should probably be taken from a config

0 commit comments

Comments
 (0)