|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE RecordWildCards #-} |
| 3 | + |
| 4 | +module Ide.Plugin.Export (descriptor) where |
| 5 | + |
| 6 | +import Control.Concurrent.STM (atomically) |
| 7 | +import Control.Lens |
| 8 | +import Control.Monad.IO.Class (liftIO) |
| 9 | +import Data.Text (Text) |
| 10 | +import qualified Data.Text as T |
| 11 | +import Development.IDE |
| 12 | +import Development.IDE.Core.PluginUtils (runActionE, useE) |
| 13 | +import Development.IDE.Core.Shake (getDiagnostics) |
| 14 | +import Development.IDE.GHC.Compat |
| 15 | +import Development.IDE.GHC.Compat.Error (_TcRnMessage, |
| 16 | + msgEnvelopeErrorL) |
| 17 | +import GHC.Tc.Errors.Types (TcRnMessage (TcRnUnusedName), |
| 18 | + UnusedNameProv (UnusedNameTopDecl)) |
| 19 | +import Ide.Plugin.Error (getNormalizedFilePathE) |
| 20 | +import Ide.Plugin.Export.Cursor |
| 21 | +import Ide.Plugin.Export.ExactPrint |
| 22 | +import Ide.Plugin.Export.Exports |
| 23 | +import Ide.Plugin.Export.Utils |
| 24 | +import Ide.Types |
| 25 | +import qualified Ide.Types as Ide |
| 26 | +import qualified Language.LSP.Protocol.Lens as L |
| 27 | +import Language.LSP.Protocol.Message (Method (..), SMethod (..)) |
| 28 | +import Language.LSP.Protocol.Types |
| 29 | + |
| 30 | +descriptor :: PluginId -> PluginDescriptor IdeState |
| 31 | +descriptor plId = |
| 32 | + let exportHandlers = mkPluginHandler SMethod_TextDocumentCodeAction quickCodeActionHandlers |
| 33 | + in (defaultPluginDescriptor plId "Code actions for module export lists") |
| 34 | + { Ide.pluginHandlers = exportHandlers |
| 35 | + } |
| 36 | + |
| 37 | +quickCodeActionHandlers :: PluginMethodHandler IdeState Method_TextDocumentCodeAction |
| 38 | +quickCodeActionHandlers state _plId (CodeActionParams _ _ doc range _) = do |
| 39 | + let uri = doc ^. L.uri |
| 40 | + nfp <- getNormalizedFilePathE uri |
| 41 | + pm <- runActionE "Export.GetParsedModuleWithComments" state (useE GetParsedModuleWithComments nfp) |
| 42 | + let ps = pm_parsed_source pm |
| 43 | + case (isExplicit ps, locateUnderCursor (range ^. L.start) ps) of |
| 44 | + (True, Just under) -> do |
| 45 | + -- The names GHC flags as defined-but-unused. Attach the action to the |
| 46 | + -- unused diagnostics as well. |
| 47 | + unusedDiags <- liftIO $ unusedTopBindDiagnostics state nfp |
| 48 | + pure . InL . map InR $ |
| 49 | + [ ca |
| 50 | + | Just (verb, title, edits) <- |
| 51 | + [ addAction under ps |
| 52 | + ] |
| 53 | + , let fixes = [ d | d <- unusedDiags, locateUnderCursor (d ^. L.range . L.start) ps == Just under ] |
| 54 | + ca = mkAction (verb <> " `" <> title <> "`") |
| 55 | + & L.edit ?~ singleFileEdit uri edits |
| 56 | + & L.diagnostics .~ (if null fixes then Nothing else Just fixes) |
| 57 | + ] |
| 58 | + _ -> pure (InL []) |
| 59 | + |
| 60 | +-- | The LSP diagnostics for names GHC reports as unused top-level definitions. |
| 61 | +unusedTopBindDiagnostics :: IdeState -> NormalizedFilePath -> IO [Diagnostic] |
| 62 | +unusedTopBindDiagnostics state nfp = do |
| 63 | + diags <- atomically $ getDiagnostics state |
| 64 | + pure [ fdLspDiagnostic d | d <- diags, fdFilePath d == nfp, isUnusedTopBind d ] |
| 65 | + where |
| 66 | + isUnusedTopBind d = |
| 67 | + case d ^? fdStructuredMessageL . _SomeStructuredMessage . msgEnvelopeErrorL . _TcRnMessage of |
| 68 | + Just (TcRnUnusedName _ UnusedNameTopDecl) -> True |
| 69 | + _ -> False |
| 70 | + |
| 71 | +addAction :: UnderCursor -> ParsedSource -> Maybe (Text, Text, [TextEdit]) |
| 72 | +addAction under ps = case under of |
| 73 | + Decl flavor n |
| 74 | + | n `isExported` ps -> Nothing |
| 75 | + | otherwise -> ("Export", T.pack (printRdrName n),) <$> addExport ps (mkExportIE flavor n) |
| 76 | + Constructor t c |
| 77 | + | c `isExported` ps -> Nothing |
| 78 | + | otherwise -> |
| 79 | + ("Export", T.pack (printRdrName t) <> "(" <> T.pack (printRdrName c) <> ")",) |
| 80 | + <$> addConstructorExport t c ps |
| 81 | + Header -> Nothing |
0 commit comments