@@ -20,9 +20,13 @@ import Data.Char
2020import  Data.List 
2121import  Data.Maybe 
2222import  qualified  Data.Text  as  T 
23+ import  GHC 
2324import  Ormolu 
2425import  Haskell.Ide.Engine.PluginUtils 
26+ import  Haskell.Ide.Engine.Support.HieExtras 
2527import  HIE.Bios.Types 
28+ import  qualified  DynFlags  as  D 
29+ import  qualified  EnumSet  as  S 
2630#endif 
2731
2832ormoluDescriptor  ::  PluginId  ->  PluginDescriptor 
@@ -44,62 +48,58 @@ provider :: FormattingProvider
4448{-# LANGUAGE  BlockArguments #-}
4549provider contents uri typ _ =  pluginGetFile contents uri $  \ fp ->  do 
4650 opts <-  lookupComponentOptions fp
47-  let  opts'  = 
51+  let  cradleOpts  = 
4852 map  DynOption 
4953 $  filter  exop
5054 $  join
5155 $  maybeToList
5256 $  componentOptions
5357 <$>  opts
54-  conf =  Config  opts' False False True False 
55-  fmt  ::  T. Text->  IdeM  (Either OrmoluException  T. Text
56-  fmt cont =  liftIO $  try @ OrmoluException  (ormolu conf fp $  T. unpack cont)
58+ 
59+  fromDyn tcm _ ()  = 
60+  let 
61+  df =  getDynFlags tcm
62+  pp = 
63+  let  p =  D. sPgm_F $  D. settings df
64+  in  if  null  p then  []  else  [" -pgmF=" <>  p]
65+  pm =  map  ((" -fplugin=" <> ) .  moduleNameString) $  D. pluginModNames df
66+  ex =  map  ((" -X" <> ) .  show ) $  S. toList $  D. extensionFlags df
67+  in 
68+  return  $  map  DynOption  $  pp <>  pm <>  ex
69+  fileOpts <-  ifCachedModuleAndData fp cradleOpts fromDyn
70+  let 
71+  conf o =  Config  o False False True False 
72+  fmt  ::  T. Text->  [DynOption ] ->  IdeM  (Either OrmoluException  T. Text
73+  fmt cont o = 
74+  liftIO $  try @ OrmoluException  (ormolu (conf o) fp $  T. unpack cont)
5775
5876 case  typ of 
59-  FormatText  ->  ret (fullRange contents) <$>  fmt contents
77+  FormatText  ->  ret (fullRange contents) <$>  fmt contents cradleOpts 
6078 FormatRange  r -> 
6179 let 
6280 txt =  T. lines  $  extractRange r contents
6381 lineRange (Range  (Position  sl _) (Position  el _)) = 
6482 Range  (Position  sl 0 ) $  Position  el $  T. length  $  last  txt
65-  --  Pragmas will not be picked up in a non standard location,
66-  --  or when range starts on a Pragma
67-  extPragmas =  takeWhile  (" {-#" `T.isPrefixOf` )
68-  pragmas = 
69-  let  cp =  extPragmas $  T. lines  contents
70-  rp =  not  $  null  $  extPragmas txt
71-  in  if  null  cp ||  rp
72-  then  [] 
73-  --  head txt is safe when extractRange txt is safe
74-  else  cp <>  if  T. all  isSpace $  head  txt then  []  else  [" " 
7583 fixLine t =  if  T. all  isSpace $  last  txt then  t else  T. init  t
7684 unStrip ws new = 
77-  fixLine
78-  $  T. unlines 
79-  $  map  (ws `T.append` )
80-  $  drop  (length  pragmas)
81-  $  T. lines  new
85+  fixLine $  T. unlines  $  map  (ws `T.append` ) $  T. lines  new
8286 mStrip =  case  txt of 
8387 (l :  _) -> 
8488 let  ws =  fst  $  T. span  isSpace l
8589 in  (,) ws .  T. unlines  <$>  traverse  (T. stripPrefix ws) txt
8690 _ ->  Nothing 
87-  in 
88-  maybe 
89-  (return  $  IdeResultFail 
90-  (IdeError 
91-  PluginError 
92-  (T. pack
93-  " You must format a whole block of code. Ormolu does not support arbitrary ranges." 
94-  )
95-  Null 
91+  err =  return  $  IdeResultFail 
92+  (IdeError 
93+  PluginError 
94+  (T. pack
95+  " You must format a whole block of code. Ormolu does not support arbitrary ranges." 
9696 )
97+  Null 
9798 )
98-  (\ (ws, striped) -> 
99-  ret (lineRange r)
100-  <$>  (fmap  (unStrip ws) <$>  fmt (T. unlines  pragmas <>  striped))
101-  )
102-  mStrip
99+  fmt' (ws, striped) = 
100+  ret (lineRange r) <$>  (fmap  (unStrip ws) <$>  fmt striped fileOpts)
101+  in 
102+  maybe  err fmt' mStrip
103103 where 
104104 ret _ (Left =  IdeResultFail 
105105 (IdeError  PluginError  (T. pack $  " ormoluCmd: " ++  show  err) Null )
0 commit comments