Skip to content

Commit

Permalink
Add formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
VeryMilkyJoe committed Aug 3, 2021
1 parent 35c2b60 commit 60b2f1b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
8 changes: 4 additions & 4 deletions ghcide/src/Development/IDE/LSP/Notifications.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ import Development.IDE.Core.RuleTypes (GetClientSettings (..))
import Development.IDE.Types.Shake (toKey)
import Ide.Plugin.Config (CheckParents (CheckOnClose))
import Ide.Types
import System.FilePath (takeExtension)
import System.FilePath (takeExtension)

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

descriptor :: PluginId -> PluginDescriptor IdeState
descriptor plId = (defaultPluginDescriptor plId) { pluginNotificationHandlers = mconcat
Expand Down
15 changes: 15 additions & 0 deletions hls-plugin-api/src/Ide/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ data PluginDescriptor ideState =
, pluginNotificationHandlers :: PluginNotificationHandlers ideState
, pluginModifyDynflags :: DynFlagsModifications
, pluginCli :: Maybe (ParserInfo (IdeCommand ideState))
, pluginFileType :: [T.Text]
}

-- | An existential wrapper of 'Properties'
Expand Down Expand Up @@ -356,6 +357,20 @@ defaultPluginDescriptor plId =
mempty
mempty
Nothing
["hs", "lhs"]

defaultCabalPluginDescriptor :: PluginId -> PluginDescriptor ideState
defaultCabalPluginDescriptor plId =
PluginDescriptor
plId
mempty
mempty
mempty
defaultConfigDescriptor
mempty
mempty
Nothing
["cabal"]

newtype CommandId = CommandId T.Text
deriving (Show, Read, Eq, Ord)
Expand Down
40 changes: 20 additions & 20 deletions plugins/hls-cabal-fmt-plugin/src/Ide/Plugin/CabalFmt.hs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
module Ide.Plugin.CabalFmt where

import Control.Exception (bracket_)
import CabalFmt
import CabalFmt.Error
import CabalFmt.Monad
import CabalFmt.Options
import Control.Exception (bracket_)
import Control.Monad.IO.Class
import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)
import Data.Maybe (mapMaybe, maybeToList)
import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)
import Data.Maybe (mapMaybe, maybeToList)
import Data.Semigroup
import Data.Text (Text)
import qualified Data.Text as T
import Development.IDE hiding (pluginHandlers)
import Development.IDE.GHC.Compat (ModSummary (ms_hspp_opts), topDir)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding
import Development.IDE hiding (pluginHandlers)
import Development.IDE.GHC.Compat (ModSummary (ms_hspp_opts), topDir)
import Ide.PluginUtils
import Ide.Types
import Language.LSP.Types as J
import qualified Language.LSP.Types.Lens as J
import System.Environment (setEnv, unsetEnv)
import Language.LSP.Types as J
import qualified Language.LSP.Types.Lens as J
import System.Environment (setEnv, unsetEnv)
import System.FilePath
import CabalFmt.Monad
import CabalFmt
import Data.Text.Encoding
import CabalFmt.Options
import CabalFmt.Error

descriptor :: PluginId -> PluginDescriptor IdeState
descriptor plId = (defaultPluginDescriptor plId)
descriptor plId = (defaultCabalPluginDescriptor plId)
{ pluginHandlers = mkFormattingHandlers provider
}

Expand All @@ -32,19 +32,19 @@ descriptor plId = (defaultPluginDescriptor plId)
provider :: FormattingHandler IdeState
provider ide typ contents nfp opts = liftIO $ do
cabalFmtOutput <- runCabalFmtIO (Just $ takeDirectory fp) cabalFmtOpts (cabalFmt fp (encodeUtf8 contents))
case cabalFmtOutput of
case cabalFmtOutput of
Left err -> pure $ Left (cabalErrToRespErr err)
Right out -> do
Right out -> do
let fmtDiff = makeDiffTextEdit contents (T.pack out)
pure $ Right fmtDiff
where
fp = fromNormalizedFilePath nfp
cabalFmtOpts = defaultOptions {
optIndent = _tabSize opts,
optIndent = _tabSize opts,
optTabular = not $ _insertSpaces opts
}
cabalErrToRespErr :: Error -> ResponseError
cabalErrToRespErr (SomeError err) = ResponseError UnknownErrorCode (T.pack err) Nothing
cabalErrToRespErr (CabalParseError _ _ _ _ _) = ResponseError ParseError (T.pack "Failed to parse cabal file.") Nothing
cabalErrToRespErr (PanicCannotParseInput _) = ResponseError ParseError (T.pack "Failed to parse cabal file.") Nothing
cabalErrToRespErr (WarningError err) = ResponseError ParseError (T.pack err) Nothing
cabalErrToRespErr (WarningError err) = ResponseError ParseError (T.pack err) Nothing

0 comments on commit 60b2f1b

Please sign in to comment.