-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathFormatter.hs
98 lines (82 loc) · 3.97 KB
/
Formatter.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
module Ide.Plugin.Formatter
(
formatting
, rangeFormatting
, noneProvider
, responseError
, extractRange
, fullRange
)
where
import qualified Data.Map as Map
import qualified Data.Text as T
import Development.IDE
import Ide.Types
import Ide.Plugin.Config
import qualified Language.Haskell.LSP.Core as LSP
import Language.Haskell.LSP.Types
import Text.Regex.TDFA.Text()
-- ---------------------------------------------------------------------
formatting :: Map.Map PluginId (FormattingProvider IO)
-> LSP.LspFuncs Config -> IdeState -> DocumentFormattingParams
-> IO (Either ResponseError (List TextEdit))
formatting providers lf ideState
(DocumentFormattingParams (TextDocumentIdentifier uri) params _mprogress)
= doFormatting lf providers ideState FormatText uri params
-- ---------------------------------------------------------------------
rangeFormatting :: Map.Map PluginId (FormattingProvider IO)
-> LSP.LspFuncs Config -> IdeState -> DocumentRangeFormattingParams
-> IO (Either ResponseError (List TextEdit))
rangeFormatting providers lf ideState
(DocumentRangeFormattingParams (TextDocumentIdentifier uri) range params _mprogress)
= doFormatting lf providers ideState (FormatRange range) uri params
-- ---------------------------------------------------------------------
doFormatting :: LSP.LspFuncs Config -> Map.Map PluginId (FormattingProvider IO)
-> IdeState -> FormattingType -> Uri -> FormattingOptions
-> IO (Either ResponseError (List TextEdit))
doFormatting lf providers ideState ft uri params = do
mc <- LSP.config lf
let mf = maybe "none" formattingProvider mc
case Map.lookup (PluginId mf) providers of
Just provider ->
case uriToFilePath uri of
Just (toNormalizedFilePath -> fp) -> do
(_, mb_contents) <- runAction "Formatter" ideState $ getFileContents fp
case mb_contents of
Just contents -> do
logDebug (ideLogger ideState) $ T.pack $
"Formatter.doFormatting: contents=" ++ show contents -- AZ
provider lf ideState ft contents fp params
Nothing -> return $ Left $ responseError $ T.pack $ "Formatter plugin: could not get file contents for " ++ show uri
Nothing -> return $ Left $ responseError $ T.pack $ "Formatter plugin: uriToFilePath failed for: " ++ show uri
Nothing -> return $ Left $ responseError $ T.pack $ "Formatter plugin: no formatter found for:[" ++ T.unpack mf ++ "]"
-- ---------------------------------------------------------------------
noneProvider :: FormattingProvider IO
noneProvider _ _ _ _ _ _ = return $ Right (List [])
-- ---------------------------------------------------------------------
responseError :: T.Text -> ResponseError
responseError txt = ResponseError InvalidParams txt Nothing
-- ---------------------------------------------------------------------
extractRange :: Range -> T.Text -> T.Text
extractRange (Range (Position sl _) (Position el _)) s = newS
where focusLines = take (el-sl+1) $ drop sl $ T.lines s
newS = T.unlines focusLines
-- | Gets the range that covers the entire text
fullRange :: T.Text -> Range
fullRange s = Range startPos endPos
where startPos = Position 0 0
endPos = Position lastLine 0
{-
In order to replace everything including newline characters,
the end range should extend below the last line. From the specification:
"If you want to specify a range that contains a line including
the line ending character(s) then use an end position denoting
the start of the next line"
-}
lastLine = length $ T.lines s
-- ---------------------------------------------------------------------