Skip to content

Commit 60b2f1b

Browse files
committed
Add formatting
1 parent 35c2b60 commit 60b2f1b

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

ghcide/src/Development/IDE/LSP/Notifications.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ import Development.IDE.Core.RuleTypes (GetClientSettings (..))
3838
import Development.IDE.Types.Shake (toKey)
3939
import Ide.Plugin.Config (CheckParents (CheckOnClose))
4040
import Ide.Types
41-
import System.FilePath (takeExtension)
41+
import System.FilePath (takeExtension)
4242

4343
whenUriHaskellFile :: Uri -> (NormalizedFilePath -> IO ()) -> IO ()
4444
whenUriHaskellFile uri act = whenJust maybeHaskellFile $ act . toNormalizedFilePath'
45-
where
46-
maybeHaskellFile = do
45+
where
46+
maybeHaskellFile = do
4747
fp <- LSP.uriToFilePath uri
48-
if takeExtension fp `elem` [".hs", ".lhs"] then Just fp else Nothing
48+
if takeExtension fp `elem` [".hs", ".lhs"] then Just fp else Nothing
4949

5050
descriptor :: PluginId -> PluginDescriptor IdeState
5151
descriptor plId = (defaultPluginDescriptor plId) { pluginNotificationHandlers = mconcat

hls-plugin-api/src/Ide/Types.hs

+15
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ data PluginDescriptor ideState =
112112
, pluginNotificationHandlers :: PluginNotificationHandlers ideState
113113
, pluginModifyDynflags :: DynFlagsModifications
114114
, pluginCli :: Maybe (ParserInfo (IdeCommand ideState))
115+
, pluginFileType :: [T.Text]
115116
}
116117

117118
-- | An existential wrapper of 'Properties'
@@ -356,6 +357,20 @@ defaultPluginDescriptor plId =
356357
mempty
357358
mempty
358359
Nothing
360+
["hs", "lhs"]
361+
362+
defaultCabalPluginDescriptor :: PluginId -> PluginDescriptor ideState
363+
defaultCabalPluginDescriptor plId =
364+
PluginDescriptor
365+
plId
366+
mempty
367+
mempty
368+
mempty
369+
defaultConfigDescriptor
370+
mempty
371+
mempty
372+
Nothing
373+
["cabal"]
359374

360375
newtype CommandId = CommandId T.Text
361376
deriving (Show, Read, Eq, Ord)
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
module Ide.Plugin.CabalFmt where
22

3-
import Control.Exception (bracket_)
3+
import CabalFmt
4+
import CabalFmt.Error
5+
import CabalFmt.Monad
6+
import CabalFmt.Options
7+
import Control.Exception (bracket_)
48
import Control.Monad.IO.Class
5-
import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)
6-
import Data.Maybe (mapMaybe, maybeToList)
9+
import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)
10+
import Data.Maybe (mapMaybe, maybeToList)
711
import Data.Semigroup
8-
import Data.Text (Text)
9-
import qualified Data.Text as T
10-
import Development.IDE hiding (pluginHandlers)
11-
import Development.IDE.GHC.Compat (ModSummary (ms_hspp_opts), topDir)
12+
import Data.Text (Text)
13+
import qualified Data.Text as T
14+
import Data.Text.Encoding
15+
import Development.IDE hiding (pluginHandlers)
16+
import Development.IDE.GHC.Compat (ModSummary (ms_hspp_opts), topDir)
1217
import Ide.PluginUtils
1318
import Ide.Types
14-
import Language.LSP.Types as J
15-
import qualified Language.LSP.Types.Lens as J
16-
import System.Environment (setEnv, unsetEnv)
19+
import Language.LSP.Types as J
20+
import qualified Language.LSP.Types.Lens as J
21+
import System.Environment (setEnv, unsetEnv)
1722
import System.FilePath
18-
import CabalFmt.Monad
19-
import CabalFmt
20-
import Data.Text.Encoding
21-
import CabalFmt.Options
22-
import CabalFmt.Error
2323

2424
descriptor :: PluginId -> PluginDescriptor IdeState
25-
descriptor plId = (defaultPluginDescriptor plId)
25+
descriptor plId = (defaultCabalPluginDescriptor plId)
2626
{ pluginHandlers = mkFormattingHandlers provider
2727
}
2828

@@ -32,19 +32,19 @@ descriptor plId = (defaultPluginDescriptor plId)
3232
provider :: FormattingHandler IdeState
3333
provider ide typ contents nfp opts = liftIO $ do
3434
cabalFmtOutput <- runCabalFmtIO (Just $ takeDirectory fp) cabalFmtOpts (cabalFmt fp (encodeUtf8 contents))
35-
case cabalFmtOutput of
35+
case cabalFmtOutput of
3636
Left err -> pure $ Left (cabalErrToRespErr err)
37-
Right out -> do
37+
Right out -> do
3838
let fmtDiff = makeDiffTextEdit contents (T.pack out)
3939
pure $ Right fmtDiff
4040
where
4141
fp = fromNormalizedFilePath nfp
4242
cabalFmtOpts = defaultOptions {
43-
optIndent = _tabSize opts,
43+
optIndent = _tabSize opts,
4444
optTabular = not $ _insertSpaces opts
4545
}
4646
cabalErrToRespErr :: Error -> ResponseError
4747
cabalErrToRespErr (SomeError err) = ResponseError UnknownErrorCode (T.pack err) Nothing
4848
cabalErrToRespErr (CabalParseError _ _ _ _ _) = ResponseError ParseError (T.pack "Failed to parse cabal file.") Nothing
4949
cabalErrToRespErr (PanicCannotParseInput _) = ResponseError ParseError (T.pack "Failed to parse cabal file.") Nothing
50-
cabalErrToRespErr (WarningError err) = ResponseError ParseError (T.pack err) Nothing
50+
cabalErrToRespErr (WarningError err) = ResponseError ParseError (T.pack err) Nothing

0 commit comments

Comments
 (0)