Skip to content

Commit a92845a

Browse files
committed
fix mkPrintUnqualifiedDefault
1 parent a1e6af1 commit a92845a

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

ghcide/src/Development/IDE/GHC/Compat/Outputable.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Development.IDE.GHC.Compat.Outputable (
3131

3232

3333
#if MIN_VERSION_ghc(9,2,0)
34+
import GHC.Driver.Env
3435
import GHC.Driver.Ppr
3536
import GHC.Driver.Session
3637
import GHC.Parser.Errors
@@ -153,14 +154,14 @@ type PsWarning = ErrMsg
153154
type PsError = ErrMsg
154155
#endif
155156

156-
mkPrintUnqualifiedDefault :: GlobalRdrEnv -> PrintUnqualified
157-
mkPrintUnqualifiedDefault =
157+
mkPrintUnqualifiedDefault :: HscEnv -> GlobalRdrEnv -> PrintUnqualified
158+
mkPrintUnqualifiedDefault env =
158159
#if MIN_VERSION_ghc(9,2,0)
159160
-- GHC 9.2.1 version
160161
-- mkPrintUnqualified :: UnitEnv -> GlobalRdrEnv -> PrintUnqualified
161-
mkPrintUnqualified unsafeGlobalDynFlags
162+
mkPrintUnqualified (hsc_unit_env env)
162163
#else
163-
HscTypes.mkPrintUnqualified unsafeGlobalDynFlags
164+
HscTypes.mkPrintUnqualified (hsc_dflags env)
164165
#endif
165166

166167
mkWarnMsg :: DynFlags -> SrcSpan -> PrintUnqualified -> SDoc -> MsgEnvelope DecoratedSDoc

ghcide/src/Development/IDE/Plugin/CodeAction/Args.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ runGhcideCodeAction :: LSP.MonadLsp Config m => IdeState -> MessageParams TextDo
5454
runGhcideCodeAction state (CodeActionParams _ _ (TextDocumentIdentifier uri) _range CodeActionContext {_diagnostics = List diags}) codeAction = do
5555
let mbFile = toNormalizedFilePath' <$> uriToFilePath uri
5656
runRule key = runAction ("GhcideCodeActions." <> show key) state $ runMaybeT $ MaybeT (pure mbFile) >>= MaybeT . use key
57+
caaGhcSession <- onceIO $ runRule GhcSession
5758
caaExportsMap <-
5859
onceIO $
59-
runRule GhcSession >>= \case
60+
caaGhcSession >>= \case
6061
Just env -> do
6162
pkgExports <- envPackageExports env
6263
localExports <- readTVarIO (exportsMap $ shakeExtras state)
@@ -134,6 +135,7 @@ instance (ToTextEdit a, ToTextEdit b) => ToTextEdit (Either a b) where
134135

135136
data CodeActionArgs = CodeActionArgs
136137
{ caaExportsMap :: IO ExportsMap,
138+
caaGhcSession :: IO (Maybe HscEnvEq),
137139
caaIdeOptions :: IO IdeOptions,
138140
caaParsedModule :: IO (Maybe ParsedModule),
139141
caaContents :: IO (Maybe T.Text),
@@ -267,3 +269,9 @@ instance ToCodeAction r => ToCodeAction (Maybe GlobalBindingTypeSigsResult -> r)
267269

268270
instance ToCodeAction r => ToCodeAction (GlobalBindingTypeSigsResult -> r) where
269271
toCodeAction = toCodeAction2 caaGblSigs
272+
273+
instance ToCodeAction r => ToCodeAction (Maybe HscEnvEq -> r) where
274+
toCodeAction = toCodeAction1 caaGhcSession
275+
276+
instance ToCodeAction r => ToCodeAction (Maybe HscEnv -> r) where
277+
toCodeAction = toCodeAction1 ((fmap.fmap.fmap) hscEnv caaGhcSession)

ghcide/src/Development/IDE/Plugin/TypeLenses.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import qualified Data.Text as T
2626
import Development.IDE (GhcSession (..),
2727
HscEnvEq (hscEnv),
2828
RuleResult, Rules, define,
29-
srcSpanToRange)
29+
srcSpanToRange,
30+
tmrModSummary)
3031
import Development.IDE.Core.Compile (TcModuleResult (..))
3132
import Development.IDE.Core.RuleTypes (GetBindings (GetBindings),
3233
TypeCheck (TypeCheck))
@@ -97,6 +98,7 @@ codeLensProvider ideState pId CodeLensParams{_textDocument = TextDocumentIdentif
9798
mode <- usePropertyLsp #mode pId properties
9899
fmap (Right . List) $ case uriToFilePath' uri of
99100
Just (toNormalizedFilePath' -> filePath) -> liftIO $ do
101+
env <- fmap hscEnv <$> runAction "codeLens.GhcSession" ideState (use GhcSession filePath)
100102
tmr <- runAction "codeLens.TypeCheck" ideState (use TypeCheck filePath)
101103
bindings <- runAction "codeLens.GetBindings" ideState (use GetBindings filePath)
102104
gblSigs <- runAction "codeLens.GetGlobalBindingTypeSigs" ideState (use GetGlobalBindingTypeSigs filePath)
@@ -123,9 +125,9 @@ codeLensProvider ideState pId CodeLensParams{_textDocument = TextDocumentIdentif
123125
case mode of
124126
Always ->
125127
pure (catMaybes $ generateLensForGlobal <$> gblSigs')
126-
<> generateLensFromDiags (suggestLocalSignature False tmr bindings) -- we still need diagnostics for local bindings
128+
<> generateLensFromDiags (suggestLocalSignature False env tmr bindings) -- we still need diagnostics for local bindings
127129
Exported -> pure $ catMaybes $ generateLensForGlobal <$> filter gbExported gblSigs'
128-
Diagnostics -> generateLensFromDiags $ suggestSignature False gblSigs tmr bindings
130+
Diagnostics -> generateLensFromDiags $ suggestSignature False env gblSigs tmr bindings
129131
Nothing -> pure []
130132

131133
generateLens :: PluginId -> Range -> T.Text -> WorkspaceEdit -> CodeLens
@@ -140,9 +142,9 @@ commandHandler _ideState wedit = do
140142

141143
--------------------------------------------------------------------------------
142144

143-
suggestSignature :: Bool -> Maybe GlobalBindingTypeSigsResult -> Maybe TcModuleResult -> Maybe Bindings -> Diagnostic -> [(T.Text, [TextEdit])]
144-
suggestSignature isQuickFix mGblSigs mTmr mBindings diag =
145-
suggestGlobalSignature isQuickFix mGblSigs diag <> suggestLocalSignature isQuickFix mTmr mBindings diag
145+
suggestSignature :: Bool -> Maybe HscEnv -> Maybe GlobalBindingTypeSigsResult -> Maybe TcModuleResult -> Maybe Bindings -> Diagnostic -> [(T.Text, [TextEdit])]
146+
suggestSignature isQuickFix env mGblSigs mTmr mBindings diag =
147+
suggestGlobalSignature isQuickFix mGblSigs diag <> suggestLocalSignature isQuickFix env mTmr mBindings diag
146148

147149
suggestGlobalSignature :: Bool -> Maybe GlobalBindingTypeSigsResult -> Diagnostic -> [(T.Text, [TextEdit])]
148150
suggestGlobalSignature isQuickFix mGblSigs Diagnostic{_message, _range}
@@ -156,19 +158,20 @@ suggestGlobalSignature isQuickFix mGblSigs Diagnostic{_message, _range}
156158
[(title, [action])]
157159
| otherwise = []
158160

159-
suggestLocalSignature :: Bool -> Maybe TcModuleResult -> Maybe Bindings -> Diagnostic -> [(T.Text, [TextEdit])]
160-
suggestLocalSignature isQuickFix mTmr mBindings Diagnostic{_message, _range = _range@Range{..}}
161+
suggestLocalSignature :: Bool -> Maybe HscEnv -> Maybe TcModuleResult -> Maybe Bindings -> Diagnostic -> [(T.Text, [TextEdit])]
162+
suggestLocalSignature isQuickFix mEnv mTmr mBindings Diagnostic{_message, _range = _range@Range{..}}
161163
| Just (_ :: T.Text, _ :: T.Text, _ :: T.Text, [identifier]) <-
162164
(T.unwords . T.words $ _message)
163165
=~~ ("Polymorphic local binding with no type signature: (.*) ::" :: T.Text)
164166
, Just bindings <- mBindings
167+
, Just env <- mEnv
165168
, localScope <- getFuzzyScope bindings _start _end
166169
, -- we can't use srcspan to lookup scoped bindings, because the error message reported by GHC includes the entire binding, instead of simply the name
167170
Just (name, ty) <- find (\(x, _) -> printName x == T.unpack identifier) localScope >>= \(name, mTy) -> (name,) <$> mTy
168171
, Just TcModuleResult{tmrTypechecked = TcGblEnv{tcg_rdr_env, tcg_sigs}} <- mTmr
169172
, -- not a top-level thing, to avoid duplication
170173
not $ name `elemNameSet` tcg_sigs
171-
, tyMsg <- printSDocQualifiedUnsafe (mkPrintUnqualifiedDefault tcg_rdr_env) $ pprSigmaType ty
174+
, tyMsg <- printSDocQualifiedUnsafe (mkPrintUnqualifiedDefault env tcg_rdr_env) $ pprSigmaType ty
172175
, signature <- T.pack $ printName name <> " :: " <> tyMsg
173176
, startCharacter <- _character _start
174177
, startOfLine <- Position (_line _start) startCharacter

0 commit comments

Comments
 (0)