{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Ide.Plugin.Floskell
( descriptor
, provider
) where
import Control.Monad.Except (throwError)
import Control.Monad.IO.Class
import Data.List (find)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Development.IDE hiding (pluginHandlers)
import Floskell
import Ide.Plugin.Error
import Ide.PluginUtils
import Ide.Types
import Language.LSP.Protocol.Types
descriptor :: PluginId -> PluginDescriptor IdeState
descriptor :: PluginId -> PluginDescriptor IdeState
descriptor PluginId
plId = (PluginId -> Text -> PluginDescriptor IdeState
forall ideState. PluginId -> Text -> PluginDescriptor ideState
defaultPluginDescriptor PluginId
plId Text
desc)
{ pluginHandlers = mkFormattingHandlers provider
}
where
desc :: Text
desc = Text
"Provides formatting of Haskell files via floskell. Built with floskell-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> VERSION_floskell
provider :: FormattingHandler IdeState
provider :: FormattingHandler IdeState
provider IdeState
_ideState Maybe ProgressToken
_token FormattingType
typ Text
contents NormalizedFilePath
fp FormattingOptions
_ = do
let file :: [Char]
file = NormalizedFilePath -> [Char]
fromNormalizedFilePath NormalizedFilePath
fp
AppConfig
config <- IO AppConfig -> ExceptT PluginError (HandlerM Config) AppConfig
forall a. IO a -> ExceptT PluginError (HandlerM Config) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO AppConfig -> ExceptT PluginError (HandlerM Config) AppConfig)
-> IO AppConfig -> ExceptT PluginError (HandlerM Config) AppConfig
forall a b. (a -> b) -> a -> b
$ [Char] -> IO AppConfig
findConfigOrDefault [Char]
file
let (Range
range, Text
selectedContents) = case FormattingType
typ of
FormattingType
FormatText -> (Text -> Range
fullRange Text
contents, Text
contents)
FormatRange Range
r -> (Range -> Range
normalize Range
r, Range -> Text -> Text
extractTextInRange (Range -> Range
extendToFullLines Range
r) Text
contents)
result :: Either [Char] Text
result = AppConfig -> Maybe [Char] -> Text -> Either [Char] Text
reformat AppConfig
config ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
file) (Text -> Either [Char] Text) -> Text -> Either [Char] Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
TL.fromStrict Text
selectedContents
case Either [Char] Text
result of
Left [Char]
err -> PluginError
-> ExceptT PluginError (HandlerM Config) ([TextEdit] |? Null)
forall a. PluginError -> ExceptT PluginError (HandlerM Config) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PluginError
-> ExceptT PluginError (HandlerM Config) ([TextEdit] |? Null))
-> PluginError
-> ExceptT PluginError (HandlerM Config) ([TextEdit] |? Null)
forall a b. (a -> b) -> a -> b
$ Text -> PluginError
PluginInternalError (Text -> PluginError) -> Text -> PluginError
forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$ [Char]
"floskellCmd: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
err
Right Text
new -> ([TextEdit] |? Null)
-> ExceptT PluginError (HandlerM Config) ([TextEdit] |? Null)
forall a. a -> ExceptT PluginError (HandlerM Config) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([TextEdit] |? Null)
-> ExceptT PluginError (HandlerM Config) ([TextEdit] |? Null))
-> ([TextEdit] |? Null)
-> ExceptT PluginError (HandlerM Config) ([TextEdit] |? Null)
forall a b. (a -> b) -> a -> b
$ [TextEdit] -> [TextEdit] |? Null
forall a b. a -> a |? b
InL [Range -> Text -> TextEdit
TextEdit Range
range (Text -> TextEdit) -> Text -> TextEdit
forall a b. (a -> b) -> a -> b
$ Text -> Text
TL.toStrict Text
new]
findConfigOrDefault :: FilePath -> IO AppConfig
findConfigOrDefault :: [Char] -> IO AppConfig
findConfigOrDefault [Char]
file = do
Maybe [Char]
mbConf <- [Char] -> IO (Maybe [Char])
findAppConfigIn [Char]
file
case Maybe [Char]
mbConf of
Just [Char]
confFile -> [Char] -> IO AppConfig
readAppConfig [Char]
confFile
Maybe [Char]
Nothing ->
AppConfig -> IO AppConfig
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AppConfig -> IO AppConfig) -> AppConfig -> IO AppConfig
forall a b. (a -> b) -> a -> b
$ case (Style -> Bool) -> [Style] -> Maybe Style
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\Style
s -> Style -> Text
styleName Style
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"gibiansky") [Style]
styles of
Just Style
gibiansky -> AppConfig
defaultAppConfig { appStyle = gibiansky }
Maybe Style
Nothing -> AppConfig
defaultAppConfig