|
| 1 | +{-# LANGUAGE RecordWildCards #-} |
| 2 | +{-# LANGUAGE CPP #-} |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE TypeApplications #-} |
| 6 | +{-# LANGUAGE ViewPatterns #-} |
| 7 | + |
| 8 | +module Ide.Plugin.Formatter |
| 9 | + ( |
| 10 | + formatterPlugin |
| 11 | + , FormattingType(..) |
| 12 | + , FormattingProvider |
| 13 | + , responseError |
| 14 | + ) |
| 15 | +where |
| 16 | + |
| 17 | +import qualified Data.Text as T |
| 18 | +import Development.IDE.Core.FileStore |
| 19 | +import Development.IDE.Core.Rules |
| 20 | +import Development.IDE.LSP.Server |
| 21 | +import Development.IDE.Plugin |
| 22 | +import Development.IDE.Types.Diagnostics as D |
| 23 | +import Development.IDE.Types.Location |
| 24 | +import Development.Shake hiding ( Diagnostic ) |
| 25 | +import qualified Language.Haskell.LSP.Core as LSP |
| 26 | +import Language.Haskell.LSP.Messages |
| 27 | +import Language.Haskell.LSP.Types |
| 28 | +import Text.Regex.TDFA.Text() |
| 29 | + |
| 30 | +-- --------------------------------------------------------------------- |
| 31 | + |
| 32 | +formatterPlugin :: FormattingProvider IO -> Plugin |
| 33 | +formatterPlugin provider = Plugin rules (handlers provider) |
| 34 | + |
| 35 | +-- --------------------------------------------------------------------- |
| 36 | +-- New style plugin |
| 37 | + |
| 38 | +rules :: Rules () |
| 39 | +rules = mempty |
| 40 | + |
| 41 | +handlers :: FormattingProvider IO -> PartialHandlers |
| 42 | +handlers provider = PartialHandlers $ \WithMessage{..} x -> return x |
| 43 | + { LSP.documentFormattingHandler |
| 44 | + = withResponse RspDocumentFormatting (formatting provider) |
| 45 | + , LSP.documentRangeFormattingHandler |
| 46 | + = withResponse RspDocumentRangeFormatting (rangeFormatting provider) |
| 47 | + } |
| 48 | + |
| 49 | +-- --------------------------------------------------------------------- |
| 50 | + |
| 51 | +formatting :: FormattingProvider IO |
| 52 | + -> LSP.LspFuncs () -> IdeState -> DocumentFormattingParams |
| 53 | + -> IO (Either ResponseError (List TextEdit)) |
| 54 | +formatting provider _lf ideState |
| 55 | + (DocumentFormattingParams (TextDocumentIdentifier uri) params _mprogress) |
| 56 | + = doFormatting provider ideState FormatText uri params |
| 57 | + |
| 58 | +-- --------------------------------------------------------------------- |
| 59 | + |
| 60 | +rangeFormatting :: FormattingProvider IO |
| 61 | + -> LSP.LspFuncs () -> IdeState -> DocumentRangeFormattingParams |
| 62 | + -> IO (Either ResponseError (List TextEdit)) |
| 63 | +rangeFormatting provider _lf ideState |
| 64 | + (DocumentRangeFormattingParams (TextDocumentIdentifier uri) range params _mprogress) |
| 65 | + = doFormatting provider ideState (FormatRange range) uri params |
| 66 | + |
| 67 | +-- --------------------------------------------------------------------- |
| 68 | + |
| 69 | +doFormatting :: FormattingProvider IO |
| 70 | + -> IdeState -> FormattingType -> Uri -> FormattingOptions |
| 71 | + -> IO (Either ResponseError (List TextEdit)) |
| 72 | +doFormatting provider ideState ft uri params |
| 73 | + = case uriToFilePath uri of |
| 74 | + Just (toNormalizedFilePath -> fp) -> do |
| 75 | + (_, mb_contents) <- runAction ideState $ getFileContents fp |
| 76 | + case mb_contents of |
| 77 | + Just contents -> provider ideState ft contents fp params |
| 78 | + Nothing -> return $ Left $ responseError $ T.pack $ "Formatter plugin: could not get file contents for " ++ show uri |
| 79 | + Nothing -> return $ Left $ responseError $ T.pack $ "Formatter plugin: uriToFilePath failed for: " ++ show uri |
| 80 | + |
| 81 | +-- --------------------------------------------------------------------- |
| 82 | + |
| 83 | +-- | Format the given Text as a whole or only a @Range@ of it. |
| 84 | +-- Range must be relative to the text to format. |
| 85 | +-- To format the whole document, read the Text from the file and use 'FormatText' |
| 86 | +-- as the FormattingType. |
| 87 | +data FormattingType = FormatText |
| 88 | + | FormatRange Range |
| 89 | + |
| 90 | + |
| 91 | +-- | To format a whole document, the 'FormatText' @FormattingType@ can be used. |
| 92 | +-- It is required to pass in the whole Document Text for that to happen, an empty text |
| 93 | +-- and file uri, does not suffice. |
| 94 | +type FormattingProvider m |
| 95 | + = IdeState |
| 96 | + -> FormattingType -- ^ How much to format |
| 97 | + -> T.Text -- ^ Text to format |
| 98 | + -> NormalizedFilePath -- ^ location of the file being formatted |
| 99 | + -> FormattingOptions -- ^ Options for the formatter |
| 100 | + -> m (Either ResponseError (List TextEdit)) -- ^ Result of the formatting |
| 101 | + |
| 102 | +-- --------------------------------------------------------------------- |
| 103 | + |
| 104 | +responseError :: T.Text -> ResponseError |
| 105 | +responseError txt = ResponseError InvalidParams txt Nothing |
| 106 | + |
| 107 | +-- --------------------------------------------------------------------- |
0 commit comments