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

Commit 2265c33

Browse files
committed
Document and rename formatting type options.
1 parent 1a58045 commit 2265c33

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,23 @@ type HoverProvider = Uri -> Position -> IdeM (IdeResult [Hover])
207207

208208
type SymbolProvider = Uri -> IdeDeferM (IdeResult [DocumentSymbol])
209209

210-
-- | Format the document either as a whole or only a given Range of it.
211-
data FormattingType = FormatDocument
210+
-- | Format the given Text as a whole or only a @Range@ of it.
211+
-- Range must be relative to the text to format.
212+
-- To format the whole document, read the Text from the file and use 'FormatText'
213+
-- as the FormattingType.
214+
data FormattingType = FormatText
212215
| FormatRange Range
213216

214217
-- | Formats the given Text associated with the given Uri.
215218
-- Should, but might not, honor the provided formatting options (e.g. Floskell does not).
216-
-- A formatting type can be given to either format the whole document or only a Range.
217-
--
218-
-- Text to format, may or may not, originate from the associated Uri.
219+
-- A formatting type can be given to either format the whole text or only a Range.
220+
--
221+
-- E.g. to format the whole document:
222+
-- @
223+
-- let uri = ...
224+
-- text =
225+
--
226+
-- Text to format, may or may not, originate from the associated Uri.
219227
-- E.g. it is ok, to modify the text and then reformat it through this API.
220228
--
221229
-- The Uri is mainly used to discover formatting configurations in the file's path.

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ provider
4343
provider text uri formatType opts = pluginGetFile "brittanyCmd: " uri $ \fp -> do
4444
confFile <- liftIO $ getConfFile fp
4545
let (range, selectedContents) = case formatType of
46-
FormatDocument -> (fullRange text, text)
47-
FormatRange r -> (normalize r, extractRange r text)
46+
FormatText -> (fullRange text, text)
47+
FormatRange r -> (normalize r, extractRange r text)
4848

4949
res <- formatText confFile opts selectedContents
5050
case res of
@@ -70,16 +70,23 @@ formatText confFile opts text =
7070
liftIO $ runBrittany tabSize confFile text
7171
where tabSize = opts ^. J.tabSize
7272

73-
-- | Extend to the line below to replace newline character, as above.
73+
-- | Extend to the line below and above to replace newline character.
7474
normalize :: Range -> Range
7575
normalize (Range (Position sl _) (Position el _)) =
7676
Range (Position sl 0) (Position (el + 1) 0)
7777

78-
-- | Recursively search in every directory of the given filepath for brittany.yaml
78+
-- | Recursively search in every directory of the given filepath for brittany.yaml.
7979
-- If no such file has been found, return Nothing.
8080
getConfFile :: FilePath -> IO (Maybe FilePath)
8181
getConfFile = findLocalConfigPath . takeDirectory
8282

83+
-- | Run Brittany on the given text with the given tab size and
84+
-- a configuration path. If no configuration path is given, a
85+
-- default configuration is chosen. The configuration may overwrite
86+
-- tab size parameter.
87+
--
88+
-- Returns either a list of Brittany Errors or the reformatted text.
89+
-- May not throw an exception.
8390
runBrittany :: Int -- ^ tab size
8491
-> Maybe FilePath -- ^ local config file
8592
-> Text -- ^ text to format

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ provider contents uri typ _opts =
3535
pluginGetFile "Floskell: " uri $ \file -> do
3636
config <- liftIO $ findConfigOrDefault file
3737
let (range, selectedContents) = case typ of
38-
FormatDocument -> (fullRange contents, contents)
39-
FormatRange r -> (r, extractRange r contents)
38+
FormatText -> (fullRange contents, contents)
39+
FormatRange r -> (r, extractRange r contents)
4040
result = reformat config (Just file) (BS.fromStrict (T.encodeUtf8 selectedContents))
4141
case result of
4242
Left err -> return $ IdeResultFail (IdeError PluginError (T.pack $ "floskellCmd: " ++ err) Null)

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import qualified Haskell.Ide.Engine.Plugin.Hoogle
2929
import System.Directory
3030
import System.IO
3131

32+
3233
hsimportDescriptor :: PluginId -> PluginDescriptor
3334
hsimportDescriptor plId = PluginDescriptor
3435
{ pluginId = plId
@@ -58,6 +59,9 @@ importCmd = CmdSync $ \(ImportParams uri importList modName) ->
5859

5960
-- | Import the given module for the given file.
6061
-- May take an explicit function name to perform an import-list import.
62+
-- Multiple import-list imports will result in merged imports,
63+
-- e.g. two consecutive imports for the same module will result in a single
64+
-- import line.
6165
importModule
6266
:: Uri -> Maybe T.Text -> T.Text -> IdeGhcM (IdeResult J.WorkspaceEdit)
6367
importModule uri importList modName =
@@ -75,33 +79,39 @@ importModule uri importList modName =
7579
, symbolName = T.unpack $ fromMaybe "" importList
7680
, outputSrcFile = output
7781
}
82+
-- execute hsimport on the given file and write into a temporary file.
7883
maybeErr <- liftIO $ hsimportWithArgs defaultConfig args
7984
case maybeErr of
8085
Just err -> do
8186
liftIO $ removeFile output
8287
let msg = T.pack $ show err
8388
return $ IdeResultFail (IdeError PluginError msg Null)
8489
Nothing -> do
90+
-- Since no error happened, calculate the differences of
91+
-- the original file and after the import has been done.
8592
newText <- liftIO $ T.readFile output
8693
liftIO $ removeFile output
8794
J.WorkspaceEdit mChanges mDocChanges <- liftToGhc
8895
$ makeDiffResult input newText fileMap
8996

97+
-- If the client wants its import formatted,
98+
-- it can be configured in the config.
9099
if shouldFormat
91100
then do
92101
config <- getConfig
93102
plugins <- getPlugins
94103
let mprovider = Hie.getFormattingPlugin config plugins
95104
case mprovider of
105+
-- Client may have no formatter selected
106+
-- but still the option to format on import.
96107
Nothing ->
97108
return $ IdeResultOk (J.WorkspaceEdit mChanges mDocChanges)
98109

99110
Just (_, provider) -> do
100111
let formatEdit :: J.TextEdit -> IdeGhcM J.TextEdit
101112
formatEdit origEdit@(J.TextEdit r t) = do
102-
let strippedText = T.dropWhileEnd (=='\n') t
103113
-- TODO: are these default FormattingOptions ok?
104-
res <- liftToGhc $ provider strippedText uri FormatDocument (FormattingOptions 2 True)
114+
res <- liftToGhc $ provider t uri FormatText (FormattingOptions 2 True)
105115
let formatEdits = case res of
106116
IdeResultOk xs -> xs
107117
_ -> [origEdit]

src/Haskell/Ide/Engine/Transport/LspStdio.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ reactor inp diagIn = do
736736
doc = params ^. J.textDocument . J.uri
737737
withDocumentContents (req ^. J.id) doc $ \text ->
738738
let callback = reactorSend . RspDocumentFormatting . Core.makeResponseMessage req . J.List
739-
hreq = IReq tn (req ^. J.id) callback $ lift $ provider text doc FormatDocument (params ^. J.options)
739+
hreq = IReq tn (req ^. J.id) callback $ lift $ provider text doc FormatText (params ^. J.options)
740740
in makeRequest hreq
741741

742742
-- -------------------------------
@@ -809,10 +809,10 @@ reactor inp diagIn = do
809809
-- ---------------------------------------------------------------------
810810

811811
-- | Execute a function in the current request with an Uri.
812-
-- Reads the content of the file specified by the Uri and invokes
812+
-- Reads the content of the file specified by the Uri and invokes
813813
-- the function on it.
814814
--
815-
-- If the Uri can not be mapped to a real file, the function will
815+
-- If the Uri can not be mapped to a real file, the function will
816816
-- not be executed and an error message will be sent to the client.
817817
-- Error message is associated with the request id and, thus, identifiable.
818818
withDocumentContents :: J.LspId -> J.Uri -> (T.Text -> R ()) -> R ()
@@ -830,9 +830,9 @@ withDocumentContents reqId uri f = do
830830

831831
-- | Get the currently configured formatter provider.
832832
-- The currently configured formatter provider is defined in @Config@ by PluginId.
833-
--
833+
--
834834
-- It is possible that formatter configured by the user is not present.
835-
-- In this case, a nop (No-Operation) formatter is returned and a message will
835+
-- In this case, a nop (No-Operation) formatter is returned and a message will
836836
-- be sent to the user.
837837
getFormattingProvider :: R FormattingProvider
838838
getFormattingProvider = do
@@ -847,7 +847,7 @@ getFormattingProvider = do
847847
unless (providerName == "none") $ do
848848
let msg = providerName <> " is not a recognised plugin for formatting. Check your config"
849849
reactorSend $ NotShowMessage $ fmServerShowMessageNotification J.MtWarning msg
850-
reactorSend $ NotLogMessage $ fmServerLogMessageNotification J.MtWarning msg
850+
reactorSend $ NotLogMessage $ fmServerLogMessageNotification J.MtWarning msg
851851
return (\_ _ _ _ -> return (IdeResultOk [])) -- nop formatter
852852
Just (_, provider) -> return provider
853853

0 commit comments

Comments
 (0)