|
| 1 | +{-# LANGUAGE PackageImports #-} |
| 2 | +{-# LANGUAGE RecordWildCards #-} |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE TypeApplications #-} |
| 6 | +{-# LANGUAGE ViewPatterns #-} |
| 7 | + |
| 8 | +module Ide.Plugin.Fourmolu |
| 9 | + ( |
| 10 | + descriptor |
| 11 | + , provider |
| 12 | + ) |
| 13 | +where |
| 14 | + |
| 15 | +import Control.Exception |
| 16 | +import qualified Data.Text as T |
| 17 | +import Development.IDE.Core.Rules |
| 18 | +import Development.IDE.Types.Diagnostics as D |
| 19 | +import Development.IDE.Types.Location |
| 20 | +import qualified DynFlags as D |
| 21 | +import qualified EnumSet as S |
| 22 | +import GHC |
| 23 | +import Ide.Types |
| 24 | +import Ide.PluginUtils |
| 25 | +import Ide.Plugin.Formatter |
| 26 | +import Language.Haskell.LSP.Types |
| 27 | +import "fourmolu" Ormolu |
| 28 | +import Text.Regex.TDFA.Text() |
| 29 | + |
| 30 | +-- --------------------------------------------------------------------- |
| 31 | + |
| 32 | +descriptor :: PluginId -> PluginDescriptor |
| 33 | +descriptor plId = (defaultPluginDescriptor plId) |
| 34 | + { pluginFormattingProvider = Just provider |
| 35 | + } |
| 36 | + |
| 37 | +-- --------------------------------------------------------------------- |
| 38 | + |
| 39 | +provider :: FormattingProvider IO |
| 40 | +provider _lf ideState typ contents fp _ = do |
| 41 | + let |
| 42 | + fromDyn :: ParsedModule -> IO [DynOption] |
| 43 | + fromDyn pmod = |
| 44 | + let |
| 45 | + df = ms_hspp_opts $ pm_mod_summary pmod |
| 46 | + pp = |
| 47 | + let p = D.sPgm_F $ D.settings df |
| 48 | + in if null p then [] else ["-pgmF=" <> p] |
| 49 | + pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df |
| 50 | + ex = map (("-X" <>) . show) $ S.toList $ D.extensionFlags df |
| 51 | + in |
| 52 | + return $ map DynOption $ pp <> pm <> ex |
| 53 | + |
| 54 | + m_parsed <- runAction "Fourmolu" ideState $ getParsedModule fp |
| 55 | + fileOpts <- case m_parsed of |
| 56 | + Nothing -> return [] |
| 57 | + Just pm -> fromDyn pm |
| 58 | + |
| 59 | + let |
| 60 | + fullRegion = RegionIndices Nothing Nothing |
| 61 | + rangeRegion s e = RegionIndices (Just s) (Just e) |
| 62 | + mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region } |
| 63 | + fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text) |
| 64 | + fmt cont conf = |
| 65 | + try @OrmoluException (ormolu conf (fromNormalizedFilePath fp) $ T.unpack cont) |
| 66 | + |
| 67 | + case typ of |
| 68 | + FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion) |
| 69 | + FormatRange r -> |
| 70 | + let |
| 71 | + Range (Position sl _) (Position el _) = normalize r |
| 72 | + in |
| 73 | + ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el)) |
| 74 | + where |
| 75 | + ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit) |
| 76 | + ret (Left err) = Left |
| 77 | + (responseError (T.pack $ "fourmoluCmd: " ++ show err) ) |
| 78 | + ret (Right new) = Right (makeDiffTextEdit contents new) |
0 commit comments