Skip to content

Ormolu flags #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions haskell-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ library
, filepath
, floskell == 0.10.*
, ghc
, ghc-boot-th
, ghcide >= 0.1
, gitrev
, hashable
Expand Down
48 changes: 30 additions & 18 deletions src/Ide/Plugin/Ormolu.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TypeApplications #-}

module Ide.Plugin.Ormolu
(
Expand All @@ -12,19 +11,27 @@ module Ide.Plugin.Ormolu
where

import Control.Exception
import qualified Data.Text as T
import qualified Data.Text as T
import Development.IDE.Core.Rules
import Development.IDE.Core.RuleTypes (GhcSession (GhcSession))
import Development.IDE.Core.Shake (use)
import Development.IDE.GHC.Util (hscEnv)
import Development.IDE.Types.Diagnostics as D
import Development.IDE.Types.Location
import qualified DynFlags as D
import qualified EnumSet as S
import qualified DynFlags as D
import qualified EnumSet as S
import GHC
import Ide.Types
import Ide.PluginUtils
import GHC.LanguageExtensions.Type
import GhcPlugins (HscEnv (hsc_dflags))
import Ide.Plugin.Formatter
import Ide.PluginUtils
import Ide.Types
import Language.Haskell.LSP.Core (LspFuncs (withIndefiniteProgress),
ProgressCancellable (Cancellable))
import Language.Haskell.LSP.Types
import Ormolu
import Text.Regex.TDFA.Text()
import System.FilePath (takeFileName)
import Text.Regex.TDFA.Text ()

-- ---------------------------------------------------------------------

Expand All @@ -36,24 +43,24 @@ descriptor plId = (defaultPluginDescriptor plId)
-- ---------------------------------------------------------------------

provider :: FormattingProvider IO
provider _lf ideState typ contents fp _ = do
provider lf ideState typ contents fp _ = withIndefiniteProgress lf title Cancellable $ do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should have these for all formatting providers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In many other plugins too, specially in the Eval plugin

let
fromDyn :: ParsedModule -> IO [DynOption]
fromDyn pmod =
fromDyn :: DynFlags -> IO [DynOption]
fromDyn df =
let
df = ms_hspp_opts $ pm_mod_summary pmod
pp =
let p = D.sPgm_F $ D.settings df
in if null p then [] else ["-pgmF=" <> p]
pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df
ex = map (("-X" <>) . show) $ S.toList $ D.extensionFlags df
ex = map showExtension $ S.toList $ D.extensionFlags df
in
return $ map DynOption $ pp <> pm <> ex

m_parsed <- runAction "Ormolu" ideState $ getParsedModule fp
fileOpts <- case m_parsed of
ghc <- runAction "Ormolu" ideState $ use GhcSession fp
let df = hsc_dflags . hscEnv <$> ghc
fileOpts <- case df of
Nothing -> return []
Just pm -> fromDyn pm
Just df -> fromDyn df

let
fullRegion = RegionIndices Nothing Nothing
Expand All @@ -71,7 +78,12 @@ provider _lf ideState typ contents fp _ = do
in
ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el))
where
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)
ret (Left err) = Left
(responseError (T.pack $ "ormoluCmd: " ++ show err) )
ret (Right new) = Right (makeDiffTextEdit contents new)

showExtension :: Extension -> String
showExtension Cpp = "-XCPP"
showExtension other = "-X" ++ show other