|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE PackageImports #-} |
| 3 | +{-# LANGUAGE RecordWildCards #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE TypeApplications #-} |
| 6 | + |
| 7 | +module Ide.Plugin.Fourmolu |
| 8 | + ( |
| 9 | + descriptor |
| 10 | + , provider |
| 11 | + ) |
| 12 | +where |
| 13 | + |
| 14 | +import Control.Exception |
| 15 | +import qualified Data.Text as T |
| 16 | +import Development.IDE.Core.Rules |
| 17 | +import Development.IDE.Core.RuleTypes (GhcSession (GhcSession)) |
| 18 | +import Development.IDE.Core.Shake (use) |
| 19 | +import Development.IDE.GHC.Util (hscEnv) |
| 20 | +import Development.IDE.Types.Diagnostics as D |
| 21 | +import Development.IDE.Types.Location |
| 22 | +import qualified DynFlags as D |
| 23 | +import qualified EnumSet as S |
| 24 | +import GHC |
| 25 | +import GHC.LanguageExtensions.Type |
| 26 | +import GhcPlugins (HscEnv (hsc_dflags)) |
| 27 | +import Ide.Plugin.Formatter |
| 28 | +import Ide.PluginUtils |
| 29 | +import Ide.Types |
| 30 | +import Language.Haskell.LSP.Core (LspFuncs (withIndefiniteProgress), |
| 31 | + ProgressCancellable (Cancellable)) |
| 32 | +import Language.Haskell.LSP.Types |
| 33 | +import "fourmolu" Ormolu |
| 34 | +import System.FilePath (takeFileName) |
| 35 | +import Text.Regex.TDFA.Text () |
| 36 | + |
| 37 | +-- --------------------------------------------------------------------- |
| 38 | + |
| 39 | +descriptor :: PluginId -> PluginDescriptor |
| 40 | +descriptor plId = (defaultPluginDescriptor plId) |
| 41 | + { pluginFormattingProvider = Just provider |
| 42 | + } |
| 43 | + |
| 44 | +-- --------------------------------------------------------------------- |
| 45 | + |
| 46 | +provider :: FormattingProvider IO |
| 47 | +provider lf ideState typ contents fp _ = withIndefiniteProgress lf title Cancellable $ do |
| 48 | + let |
| 49 | + fromDyn :: DynFlags -> IO [DynOption] |
| 50 | + fromDyn df = |
| 51 | + let |
| 52 | + pp = |
| 53 | + let p = D.sPgm_F $ D.settings df |
| 54 | + in if null p then [] else ["-pgmF=" <> p] |
| 55 | + pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df |
| 56 | + ex = map showExtension $ S.toList $ D.extensionFlags df |
| 57 | + in |
| 58 | + return $ map DynOption $ pp <> pm <> ex |
| 59 | + |
| 60 | + ghc <- runAction "Fourmolu" ideState $ use GhcSession fp |
| 61 | + let df = hsc_dflags . hscEnv <$> ghc |
| 62 | + fileOpts <- case df of |
| 63 | + Nothing -> return [] |
| 64 | + Just df -> fromDyn df |
| 65 | + |
| 66 | + let |
| 67 | + fullRegion = RegionIndices Nothing Nothing |
| 68 | + rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1) |
| 69 | + mkConf o region = do |
| 70 | + printerOpts <- loadConfigFile True (Just fp') defaultPrinterOpts |
| 71 | + return $ defaultConfig |
| 72 | + { cfgDynOptions = o |
| 73 | + , cfgRegion = region |
| 74 | + , cfgDebug = True |
| 75 | + , cfgPrinterOpts = printerOpts |
| 76 | + } |
| 77 | + fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text) |
| 78 | + fmt cont conf = |
| 79 | + try @OrmoluException (ormolu conf fp' $ T.unpack cont) |
| 80 | + fp' = fromNormalizedFilePath fp |
| 81 | + |
| 82 | + case typ of |
| 83 | + FormatText -> ret <$> (fmt contents =<< mkConf fileOpts fullRegion) |
| 84 | + FormatRange (Range (Position sl _) (Position el _)) -> |
| 85 | + ret <$> (fmt contents =<< mkConf fileOpts (rangeRegion sl el)) |
| 86 | + where |
| 87 | + title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp) |
| 88 | + ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit) |
| 89 | + ret (Left err) = Left |
| 90 | + (responseError (T.pack $ "fourmoluCmd: " ++ show err) ) |
| 91 | + ret (Right new) = Right (makeDiffTextEdit contents new) |
| 92 | + |
| 93 | +showExtension :: Extension -> String |
| 94 | +showExtension Cpp = "-XCPP" |
| 95 | +showExtension other = "-X" ++ show other |
0 commit comments