Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 3e21f2c

Browse files
committed
First pass at Floskell and formatterProvider
1 parent 7dd6fd7 commit 3e21f2c

38 files changed

+165
-40
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@
3232
url = https://github.com/alanz/ghc-mod.git
3333

3434

35+
[submodule "submodules/floskell"]
36+
path = submodules/floskell
37+
url = https://github.com/bubba/floskell

app/MainHie.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import Haskell.Ide.Engine.Plugin.HsImport
3636
import Haskell.Ide.Engine.Plugin.Liquid
3737
import Haskell.Ide.Engine.Plugin.Package
3838
import Haskell.Ide.Engine.Plugin.Pragmas
39+
import Haskell.Ide.Engine.Plugin.Floskell
3940

4041
-- ---------------------------------------------------------------------
4142

@@ -59,6 +60,7 @@ plugins includeExamples = pluginDescToIdePlugins allPlugins
5960
, liquidDescriptor "liquid"
6061
, packageDescriptor "package"
6162
, pragmasDescriptor "pragmas"
63+
, floskellDescriptor "floskell"
6264
]
6365
examplePlugins =
6466
[example2Descriptor "eg2"

cabal.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ packages:
77
./submodules/cabal-helper/
88
./submodules/ghc-mod/
99
./submodules/ghc-mod/core/
10+
./submodules/floskell

haskell-ide-engine.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ library
2929
Haskell.Ide.Engine.Plugin.Brittany
3030
Haskell.Ide.Engine.Plugin.Build
3131
Haskell.Ide.Engine.Plugin.Example2
32+
Haskell.Ide.Engine.Plugin.Floskell
3233
Haskell.Ide.Engine.Plugin.Fuzzy
3334
Haskell.Ide.Engine.Plugin.GhcMod
3435
Haskell.Ide.Engine.Plugin.HaRe
@@ -60,6 +61,7 @@ library
6061
, data-default
6162
, directory
6263
, filepath
64+
, floskell
6365
, fold-debounce
6466
, ghc >= 8.0.1
6567
, ghc-exactprint
@@ -96,6 +98,7 @@ library
9698
if flag(pedantic)
9799
ghc-options: -Werror
98100
default-language: Haskell2010
101+
mixins: floskell (Floskell as FloskellFloskell)
99102

100103
executable hie
101104
hs-source-dirs: app

hie-plugin-api/Haskell/Ide/Engine/Config.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ data Config =
2626
, liquidOn :: Bool
2727
, completionSnippetsOn :: Bool
2828
, formatOnImportOn :: Bool
29+
, formattingProvider :: T.Text
2930
} deriving (Show,Eq)
3031

3132
instance Default Config where
@@ -36,6 +37,7 @@ instance Default Config where
3637
, liquidOn = False
3738
, completionSnippetsOn = True
3839
, formatOnImportOn = True
40+
, formattingProvider = "brittany"
3941
}
4042

4143
-- TODO: Add API for plugins to expose their own LSP config options
@@ -49,6 +51,7 @@ instance FromJSON Config where
4951
<*> o .:? "liquidOn" .!= liquidOn def
5052
<*> o .:? "completionSnippetsOn" .!= completionSnippetsOn def
5153
<*> o .:? "formatOnImportOn" .!= formatOnImportOn def
54+
<*> o .:? "formattingProvider" .!= formattingProvider def
5255

5356
-- 2017-10-09 23:22:00.710515298 [ThreadId 11] - ---> {"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"languageServerHaskell":{"maxNumberOfProblems":100,"hlintOn":true}}}}
5457
-- 2017-10-09 23:22:00.710667381 [ThreadId 15] - reactor:got didChangeConfiguration notification:
@@ -60,12 +63,13 @@ instance FromJSON Config where
6063
-- ,("maxNumberOfProblems",Number 100.0)]))])}}
6164

6265
instance ToJSON Config where
63-
toJSON (Config h m d l c f) = object [ "languageServerHaskell" .= r ]
66+
toJSON (Config h m d l c f fp) = object [ "languageServerHaskell" .= r ]
6467
where
6568
r = object [ "hlintOn" .= h
6669
, "maxNumberOfProblems" .= m
6770
, "diagnosticsDebounceDuration" .= d
6871
, "liquidOn" .= l
6972
, "completionSnippetsOn" .= c
7073
, "formatOnImportOn" .= f
74+
, "formattingProvider" .= fp
7175
]

hie-plugin-api/Haskell/Ide/Engine/PluginUtils.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module Haskell.Ide.Engine.PluginUtils
1717
, srcSpan2Loc
1818
, unpackRealSrcSpan
1919
, reverseMapFile
20+
, extractRange
21+
, fullRange
2022
, fileInfo
2123
, realSrcSpan2Range
2224
, canonicalizeUri
@@ -221,6 +223,27 @@ diffText' supports (f,fText) f2Text withDeletions =
221223

222224
-- ---------------------------------------------------------------------
223225

226+
extractRange :: Range -> T.Text -> T.Text
227+
extractRange (Range (Position sl _) (Position el _)) s = newS
228+
where focusLines = take (el-sl+1) $ drop sl $ T.lines s
229+
newS = T.unlines focusLines
230+
231+
-- | Gets the range that covers the entire text
232+
fullRange :: T.Text -> Range
233+
fullRange s = Range startPos endPos
234+
where startPos = Position 0 0
235+
endPos = Position lastLine 0
236+
{-
237+
In order to replace everything including newline characters,
238+
the end range should extend below the last line. From the specification:
239+
"If you want to specify a range that contains a line including
240+
the line ending character(s) then use an end position denoting
241+
the start of the next line"
242+
-}
243+
lastLine = length $ T.lines s
244+
245+
-- ---------------------------------------------------------------------
246+
224247
-- | Returns the directory and file name
225248
fileInfo :: T.Text -> (FilePath,FilePath)
226249
fileInfo tfileName =

hie-plugin-api/Haskell/Ide/Engine/PluginsIdeMonads.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module Haskell.Ide.Engine.PluginsIdeMonads
3939
, DiagnosticTrigger(..)
4040
, HoverProvider
4141
, SymbolProvider
42+
, FormattingType(..)
43+
, FormattingProvider
4244
, IdePlugins(..)
4345
, getDiagnosticProvidersConfig
4446
-- * IDE monads
@@ -66,11 +68,13 @@ module Haskell.Ide.Engine.PluginsIdeMonads
6668
, Location(..)
6769
, TextDocumentIdentifier(..)
6870
, TextDocumentPositionParams(..)
71+
, TextEdit(..)
6972
, WorkspaceEdit(..)
7073
, Diagnostic(..)
7174
, DiagnosticSeverity(..)
7275
, PublishDiagnosticsParams(..)
7376
, List(..)
77+
, FormattingOptions(..)
7478
)
7579
where
7680

@@ -114,13 +118,15 @@ import Language.Haskell.LSP.Types ( Command(..)
114118
, DiagnosticSeverity(..)
115119
, DocumentSymbol(..)
116120
, List(..)
121+
, FormattingOptions(..)
117122
, Hover(..)
118123
, Location(..)
119124
, Position(..)
120125
, PublishDiagnosticsParams(..)
121126
, Range(..)
122127
, TextDocumentIdentifier(..)
123128
, TextDocumentPositionParams(..)
129+
, TextEdit(..)
124130
, Uri(..)
125131
, VersionedTextDocumentIdentifier(..)
126132
, WorkspaceEdit(..)
@@ -202,6 +208,10 @@ type HoverProvider = Uri -> Position -> IdeM (IdeResult [Hover])
202208

203209
type SymbolProvider = Uri -> IdeDeferM (IdeResult [DocumentSymbol])
204210

211+
data FormattingType = FormatDocument
212+
| FormatRange Range
213+
type FormattingProvider = Uri -> FormattingType -> FormattingOptions -> IdeGhcM (IdeResult [TextEdit])
214+
205215
data PluginDescriptor =
206216
PluginDescriptor { pluginId :: PluginId
207217
, pluginName :: T.Text
@@ -211,6 +221,7 @@ data PluginDescriptor =
211221
, pluginDiagnosticProvider :: Maybe DiagnosticProvider
212222
, pluginHoverProvider :: Maybe HoverProvider
213223
, pluginSymbolProvider :: Maybe SymbolProvider
224+
, pluginFormattingProvider :: Maybe FormattingProvider
214225
} deriving (Generic)
215226

216227
instance Show PluginCommand where

src/Haskell/Ide/Engine/LSP/Reactor.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ import qualified Language.Haskell.LSP.Types as J
2929
-- ---------------------------------------------------------------------
3030

3131
data REnv = REnv
32-
{ scheduler :: Scheduler.Scheduler R
33-
, lspFuncs :: Core.LspFuncs Config
32+
{ scheduler :: Scheduler.Scheduler R
33+
, lspFuncs :: Core.LspFuncs Config
3434
-- | The process ID of HIE. See 'HasPidCache'
35-
, reactorPidCache :: Int
36-
, diagnosticSources :: Map.Map DiagnosticTrigger [(PluginId,DiagnosticProviderFunc)]
37-
, hoverProviders :: [HoverProvider]
38-
, symbolProviders :: [SymbolProvider]
35+
, reactorPidCache :: Int
36+
, diagnosticSources :: Map.Map DiagnosticTrigger [(PluginId,DiagnosticProviderFunc)]
37+
, hoverProviders :: [HoverProvider]
38+
, symbolProviders :: [SymbolProvider]
39+
, formattingProviders :: Map.Map PluginId FormattingProvider
3940
-- TODO: Add code action providers here
4041
}
4142

@@ -56,11 +57,12 @@ runReactor
5657
-> Map.Map DiagnosticTrigger [(PluginId, DiagnosticProviderFunc)]
5758
-> [HoverProvider]
5859
-> [SymbolProvider]
60+
-> Map.Map PluginId FormattingProvider
5961
-> R a
6062
-> IO a
61-
runReactor lf sc dps hps sps f = do
63+
runReactor lf sc dps hps sps fps f = do
6264
pid <- getProcessID
63-
runReaderT f (REnv sc lf pid dps hps sps)
65+
runReaderT f (REnv sc lf pid dps hps sps fps)
6466

6567
-- ---------------------------------------------------------------------
6668

src/Haskell/Ide/Engine/Plugin/ApplyRefact.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ applyRefactDescriptor plId = PluginDescriptor
4747
, pluginDiagnosticProvider = Nothing
4848
, pluginHoverProvider = Nothing
4949
, pluginSymbolProvider = Nothing
50+
, pluginFormattingProvider = Nothing
5051
}
5152

5253
-- ---------------------------------------------------------------------

src/Haskell/Ide/Engine/Plugin/Base.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ baseDescriptor plId = PluginDescriptor
4444
, pluginDiagnosticProvider = Nothing
4545
, pluginHoverProvider = Nothing
4646
, pluginSymbolProvider = Nothing
47+
, pluginFormattingProvider = Nothing
4748
}
4849

4950
-- ---------------------------------------------------------------------

0 commit comments

Comments
 (0)