This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathStylish.hs
43 lines (37 loc) · 1.76 KB
/
Stylish.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{-# LANGUAGE OverloadedStrings #-}
module Haskell.Ide.Engine.Plugin.Stylish where
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (Value (Null))
import Data.List (intercalate)
import qualified Data.Text as T
import Haskell.Ide.Engine.MonadTypes
import Haskell.Ide.Engine.PluginUtils (fullRange, pluginGetFile)
import Language.Haskell.Stylish (ConfigPath (..), format)
stylishDescriptor :: PluginId -> PluginDescriptor
stylishDescriptor plId = PluginDescriptor
{ pluginId = plId
, pluginName = "Stylish"
, pluginDesc = "Stylish is a tool to format source code."
, pluginCommands = []
, pluginCodeActionProvider = Nothing
, pluginDiagnosticProvider = Nothing
, pluginHoverProvider = Nothing
, pluginSymbolProvider = Nothing
, pluginFormattingProvider = Just provider
}
provider :: FormattingProvider
provider contents uri typ _ =
case typ of
FormatRange _ ->
return $ IdeResultFail (IdeError PluginError (T.pack "Selection formatting for Stylish is not currently supported.") Null)
FormatText -> pluginGetFile "stylish:" uri $ \file -> do
res <- liftIO $ runStylish Nothing file contents
case res of
Left err -> return $ IdeResultFail
(IdeError PluginError
(T.pack $ "stylish: " ++ err)
Null
)
Right new -> return $ IdeResultOk [TextEdit (fullRange contents) (T.pack $ ((intercalate "\n" new) <> "\n"))]
runStylish :: Maybe ConfigPath -> FilePath -> T.Text -> IO (Either String [String])
runStylish config file contents = format config (Just file) (T.unpack contents)