1
+ {-# LANGUAGE DataKinds #-}
1
2
{-# LANGUAGE LambdaCase #-}
3
+ {-# LANGUAGE OverloadedLabels #-}
2
4
{-# LANGUAGE OverloadedStrings #-}
3
5
4
6
module Ide.Plugin.CabalFmt where
@@ -9,6 +11,7 @@ import Control.Monad.IO.Class
9
11
import qualified Data.Text as T
10
12
import Development.IDE hiding (pluginHandlers )
11
13
import Ide.Plugin.Error (PluginError (PluginInternalError , PluginInvalidParams ))
14
+ import Ide.Plugin.Properties
12
15
import Ide.PluginUtils
13
16
import Ide.Types
14
17
import qualified Language.LSP.Protocol.Lens as L
@@ -24,7 +27,7 @@ data Log
24
27
= LogProcessInvocationFailure Int
25
28
| LogReadCreateProcessInfo T. Text [String ]
26
29
| LogInvalidInvocationInfo
27
- | LogCabalFmtNotFound
30
+ | LogFormatterBinNotFound FilePath
28
31
deriving (Show )
29
32
30
33
instance Pretty Log where
@@ -35,29 +38,39 @@ instance Pretty Log where
35
38
[" Invocation of cabal-fmt with arguments" <+> pretty args]
36
39
++ [" failed with standard error:" <+> pretty stdErrorOut | not (T. null stdErrorOut)]
37
40
LogInvalidInvocationInfo -> " Invocation of cabal-fmt with range was called but is not supported."
38
- LogCabalFmtNotFound -> " Couldn't find executable 'cabal-fmt'"
41
+ LogFormatterBinNotFound fp -> " Couldn't find formatter executable 'cabal-fmt' at: " <+> pretty fp
39
42
40
43
descriptor :: Recorder (WithPriority Log ) -> PluginId -> PluginDescriptor IdeState
41
44
descriptor recorder plId =
42
45
(defaultCabalPluginDescriptor plId " Provides formatting of cabal files with cabal-fmt" )
43
- { pluginHandlers = mkFormattingHandlers (provider recorder)
46
+ { pluginHandlers = mkFormattingHandlers (provider recorder plId)
47
+ , pluginConfigDescriptor = defaultConfigDescriptor{configCustomConfig = mkCustomConfig properties}
44
48
}
45
49
50
+ properties :: Properties '[ 'PropertyKey " path" 'TString]
51
+ properties =
52
+ emptyProperties
53
+ & defineStringProperty
54
+ # path
55
+ " Set path to 'cabal-fmt' executable"
56
+ " cabal-fmt"
57
+
46
58
-- | Formatter provider of cabal fmt.
47
59
-- Formats the given source in either a given Range or the whole Document.
48
60
-- If the provider fails an error is returned that can be displayed to the user.
49
- provider :: Recorder (WithPriority Log ) -> FormattingHandler IdeState
50
- provider recorder _ _ (FormatRange _) _ _ _ = do
61
+ provider :: Recorder (WithPriority Log ) -> PluginId -> FormattingHandler IdeState
62
+ provider recorder _ _ _ (FormatRange _) _ _ _ = do
51
63
logWith recorder Info LogInvalidInvocationInfo
52
64
throwError $ PluginInvalidParams " You cannot format a text-range using cabal-fmt."
53
- provider recorder _ide _ FormatText contents nfp opts = do
65
+ provider recorder plId ideState _ FormatText contents nfp opts = do
54
66
let cabalFmtArgs = [ " --indent" , show tabularSize]
55
- x <- liftIO $ findExecutable " cabal-fmt"
67
+ cabalFmtExePath <- fmap T. unpack $ liftIO $ runAction " cabal-gild" ideState $ usePropertyAction # path plId properties
68
+ x <- liftIO $ findExecutable cabalFmtExePath
56
69
case x of
57
70
Just _ -> do
58
71
(exitCode, out, err) <-
59
72
liftIO $ Process. readCreateProcessWithExitCode
60
- ( proc " cabal-fmt " cabalFmtArgs
73
+ ( proc cabalFmtExePath cabalFmtArgs
61
74
)
62
75
{ cwd = Just $ takeDirectory fp
63
76
}
@@ -71,8 +84,8 @@ provider recorder _ide _ FormatText contents nfp opts = do
71
84
let fmtDiff = makeDiffTextEdit contents out
72
85
pure $ InL fmtDiff
73
86
Nothing -> do
74
- log Error LogCabalFmtNotFound
75
- throwError (PluginInternalError " No installation of cabal-fmt could be found. Please install it into your global environment. " )
87
+ log Error $ LogFormatterBinNotFound cabalFmtExePath
88
+ throwError (PluginInternalError " No installation of cabal-gild could be found. Please install it globally, or provide the full path to the executable " )
76
89
where
77
90
fp = fromNormalizedFilePath nfp
78
91
tabularSize = opts ^. L. tabSize
0 commit comments