|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE TypeFamilies #-} |
| 4 | + |
| 5 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 6 | +{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} |
| 7 | + |
| 8 | +module Ide.Plugin.InlayHints(descriptor) where |
| 9 | + |
| 10 | +import Control.DeepSeq (NFData (rnf), rwhnf) |
| 11 | +import Control.Monad.IO.Class (MonadIO (liftIO)) |
| 12 | +import Data.Either (isRight) |
| 13 | +import Data.Hashable (Hashable) |
| 14 | +import qualified Data.Map.Strict as M |
| 15 | +import Data.Maybe (fromMaybe) |
| 16 | +import Data.String (IsString (fromString)) |
| 17 | +import Data.Text (Text) |
| 18 | +import Development.IDE (GhcSessionDeps (GhcSessionDeps), |
| 19 | + HieAstResult (HAR, refMap), |
| 20 | + IdeState, |
| 21 | + Position (Position), |
| 22 | + Pretty (pretty), |
| 23 | + RuleResult, Rules, |
| 24 | + TcModuleResult (tmrTypechecked), |
| 25 | + TypeCheck (TypeCheck), |
| 26 | + cmapWithPrio, define, |
| 27 | + hscEnv, printOutputable, |
| 28 | + use_) |
| 29 | +import Development.IDE.Core.PluginUtils (runActionE, |
| 30 | + useWithStaleE) |
| 31 | +import Development.IDE.Core.PositionMapping (idDelta) |
| 32 | +import Development.IDE.Core.RuleTypes (GetHieAst (GetHieAst)) |
| 33 | +import Development.IDE.Core.Shake (addPersistentRule) |
| 34 | +import qualified Development.IDE.Core.Shake as Shake |
| 35 | +import Development.IDE.GHC.Compat (Fixity (Fixity), Name, |
| 36 | + TcGblEnv, defaultFixity, |
| 37 | + initTcWithGbl, |
| 38 | + lookupFixityRn, |
| 39 | + mkRealSrcLoc, |
| 40 | + realSrcLocSpan, |
| 41 | + realSrcSpanEnd, |
| 42 | + realSrcSpanStart, |
| 43 | + srcLocCol, srcLocLine) |
| 44 | +import Development.IDE.GHC.Compat.Core (HscEnv) |
| 45 | +import qualified Development.IDE.GHC.Compat.Util as Util |
| 46 | +import GHC.Generics (Generic) |
| 47 | +import Ide.Logger (Recorder, WithPriority) |
| 48 | +import Ide.Plugin.Error (getNormalizedFilePathE) |
| 49 | +import Ide.Types (PluginDescriptor (pluginHandlers, pluginRules), |
| 50 | + PluginId, |
| 51 | + PluginMethodHandler, |
| 52 | + defaultPluginDescriptor, |
| 53 | + mkPluginHandler) |
| 54 | +import Language.LSP.Protocol.Message (Method (Method_TextDocumentInlayHint), |
| 55 | + SMethod (SMethod_TextDocumentInlayHint)) |
| 56 | +import Language.LSP.Protocol.Types (InlayHint (InlayHint), |
| 57 | + InlayHintParams (InlayHintParams), |
| 58 | + TextDocumentIdentifier (TextDocumentIdentifier), |
| 59 | + maybeToNull, |
| 60 | + type (|?) (InL)) |
| 61 | + |
| 62 | +descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState |
| 63 | +descriptor recorder pluginId = (defaultPluginDescriptor pluginId "Provides Info in Inlay Hints") |
| 64 | + { |
| 65 | + pluginRules = fixityRule recorder, |
| 66 | + pluginHandlers = mkPluginHandler SMethod_TextDocumentInlayHint inlayHint |
| 67 | + } |
| 68 | + |
| 69 | +inlayHint :: PluginMethodHandler IdeState Method_TextDocumentInlayHint |
| 70 | +inlayHint state _pid (InlayHintParams _ (TextDocumentIdentifier uri) _range) = do |
| 71 | + nfp <- getNormalizedFilePathE uri |
| 72 | + runActionE "InlayHints" state $ do |
| 73 | + (FixityMap fixmap, _) <- useWithStaleE GetFixity nfp |
| 74 | + pure $ maybeToNull $ toAbsInlayHints fixmap |
| 75 | + where |
| 76 | + toAbsInlayHints :: M.Map Position Fixity -> Maybe [InlayHint] |
| 77 | + toAbsInlayHints fixmap = |
| 78 | + Just (M.elems $ M.mapWithKey (\(Position x y) (Fixity _ pre direction) -> |
| 79 | + InlayHint |
| 80 | + (Position (x - 1) (y - 1)) |
| 81 | + (InL (printOutputable direction <> printOutputable pre)) |
| 82 | + Nothing Nothing Nothing Nothing Nothing Nothing |
| 83 | + ) fixmap) |
| 84 | + |
| 85 | +newtype Log = LogShake Shake.Log |
| 86 | + |
| 87 | +instance Pretty Log where |
| 88 | + pretty = \case |
| 89 | + LogShake log -> pretty log |
| 90 | + |
| 91 | +newtype FixityMap = FixityMap (M.Map Position Fixity) |
| 92 | +instance Show FixityMap where |
| 93 | + show _ = "FixityMap" |
| 94 | + |
| 95 | +instance NFData FixityMap where |
| 96 | + rnf (FixityMap xs) = rnf xs |
| 97 | + |
| 98 | +instance NFData Fixity where |
| 99 | + rnf = rwhnf |
| 100 | + |
| 101 | +data GetFixity = GetFixity deriving (Show, Eq, Generic) |
| 102 | + |
| 103 | +instance Hashable GetFixity |
| 104 | +instance NFData GetFixity |
| 105 | + |
| 106 | +type instance RuleResult GetFixity = FixityMap |
| 107 | + |
| 108 | +fixityRule :: Recorder (WithPriority Log) -> Rules () |
| 109 | +fixityRule recorder = do |
| 110 | + define (cmapWithPrio LogShake recorder) $ \GetFixity nfp -> do |
| 111 | + HAR{refMap} <- use_ GetHieAst nfp |
| 112 | + -- deps necessary so that we can consult already loaded in ifaces instead of loading in duplicates |
| 113 | + env <- hscEnv <$> use_ GhcSessionDeps nfp |
| 114 | + tcGblEnv <- tmrTypechecked <$> use_ TypeCheck nfp |
| 115 | + fs <- lookupFixities env tcGblEnv $ |
| 116 | + M.mapKeys (\(Right x) -> x) |
| 117 | + $ M.filterWithKey (\k _ -> isRight k) |
| 118 | + $ M.map |
| 119 | + (fmap ( |
| 120 | + (\loc -> Position (fromIntegral $ srcLocLine loc) (fromIntegral $ srcLocCol loc)) |
| 121 | + . realSrcSpanEnd |
| 122 | + . fst)) |
| 123 | + refMap |
| 124 | + pure ([], Just (FixityMap fs)) |
| 125 | + |
| 126 | + -- Ensure that this plugin doesn't block on startup |
| 127 | + addPersistentRule GetFixity $ const $ pure $ Just (FixityMap M.empty, idDelta, Nothing) |
| 128 | + |
| 129 | +-- | Convert a HieAST to FixityTree with fixity info gathered |
| 130 | +lookupFixities :: MonadIO m => HscEnv -> TcGblEnv -> M.Map Name [Position] -> m (M.Map Position Fixity) |
| 131 | +lookupFixities hscEnv tcGblEnv names |
| 132 | + = liftIO |
| 133 | + $ fmap (fromMaybe M.empty . snd) |
| 134 | + $ initTcWithGbl hscEnv tcGblEnv (realSrcLocSpan $ mkRealSrcLoc "<dummy>" 0 0) |
| 135 | + $ M.traverseMaybeWithKey (\_ v -> v) |
| 136 | + $ M.fromList |
| 137 | + $ concat |
| 138 | + $ M.elems |
| 139 | + $ M.mapWithKey lookupFixity names |
| 140 | + where |
| 141 | + lookupFixity name positions = |
| 142 | + fmap (,fixity) positions |
| 143 | + where |
| 144 | + fixity = do |
| 145 | + f <- Util.handleGhcException |
| 146 | + (const $ pure Nothing) |
| 147 | + (Just <$> lookupFixityRn name) |
| 148 | + if f == Just defaultFixity |
| 149 | + then pure Nothing |
| 150 | + else pure f |
0 commit comments