summaryrefslogtreecommitdiff
path: root/ghc
diff options
context:
space:
mode:
authorSimon Jakobi <simon.jakobi@gmail.com>2018-06-04 17:51:03 -0400
committerBen Gamari <ben@smart-cactus.org>2018-06-04 17:56:57 -0400
commit85309a3cda367425cca727dfa45e5e6c63b47391 (patch)
tree0a4aff565a1e34843cbb178707971f86786d939f /ghc
parentaa77c602e910cb9a4e17022464c0341fd731f3e0 (diff)
downloadhaskell-85309a3cda367425cca727dfa45e5e6c63b47391.tar.gz
Serialize docstrings to ifaces, display them with new GHCi :doc command
If `-haddock` is set, we now extract docstrings from the renamed ast and serialize them in the .hi-files. This includes some of the changes from D4749 with the notable exceptions of the docstring lexing and renaming. A currently limited and experimental GHCi :doc command can be used to display docstrings for declarations. The formatting of pretty-printed docstrings is changed slightly, causing some changes in testsuite/tests/haddock. Test Plan: ./validate Reviewers: alexbiehl, hvr, gershomb, harpocrates, bgamari Reviewed By: alexbiehl Subscribers: rwbarton, thomie, carter Differential Revision: https://phabricator.haskell.org/D4758
Diffstat (limited to 'ghc')
-rw-r--r--ghc/GHCi/UI.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/ghc/GHCi/UI.hs b/ghc/GHCi/UI.hs
index 67f2cbb147..7c427a03a8 100644
--- a/ghc/GHCi/UI.hs
+++ b/ghc/GHCi/UI.hs
@@ -48,6 +48,7 @@ import GhcMonad ( modifySession )
import qualified GHC
import GHC ( LoadHowMuch(..), Target(..), TargetId(..), InteractiveImport(..),
TyThing(..), Phase, BreakIndex, Resume, SingleStep, Ghc,
+ GetDocsFailure(..),
getModuleGraph, handleSourceError )
import HsImpExp
import HsSyn
@@ -99,6 +100,7 @@ import Data.List ( find, group, intercalate, intersperse, isPrefixOf, nub,
partition, sort, sortBy )
import qualified Data.Set as S
import Data.Maybe
+import Data.Map (Map)
import qualified Data.Map as M
import Data.Time.LocalTime ( getZonedTime )
import Data.Time.Format ( formatTime, defaultTimeLocale )
@@ -179,6 +181,7 @@ ghciCommands = map mkCmd [
("def", keepGoing (defineMacro False), completeExpression),
("def!", keepGoing (defineMacro True), completeExpression),
("delete", keepGoing deleteCmd, noCompletion),
+ ("doc", keepGoing' docCmd, completeIdentifier),
("edit", keepGoing' editFile, completeFilename),
("etags", keepGoing createETagsFileCmd, completeFilename),
("force", keepGoing forceCmd, completeExpression),
@@ -288,6 +291,7 @@ defFullHelpText =
" (!: use regex instead of line number)\n" ++
" :def <cmd> <expr> define command :<cmd> (later defined command has\n" ++
" precedence, ::<cmd> is always a builtin command)\n" ++
+ " :doc <name> display docs for the given name (experimental)\n" ++
" :edit <file> edit file\n" ++
" :edit edit last module\n" ++
" :etags [<file>] create tags file <file> for Emacs (default: \"TAGS\")\n" ++
@@ -1604,6 +1608,38 @@ checkModule m = do
return True
afterLoad (successIf ok) False
+-----------------------------------------------------------------------------
+-- :doc
+
+docCmd :: String -> InputT GHCi ()
+docCmd "" =
+ throwGhcException (CmdLineError "syntax: ':doc <thing-you-want-docs-for>'")
+docCmd s = do
+ -- TODO: Maybe also get module headers for module names
+ names <- GHC.parseName s
+ e_docss <- mapM GHC.getDocs names
+ sdocs <- mapM (either handleGetDocsFailure (pure . pprDocs)) e_docss
+ let sdocs' = vcat (intersperse (text "") sdocs)
+ unqual <- GHC.getPrintUnqual
+ dflags <- getDynFlags
+ (liftIO . putStrLn . showSDocForUser dflags unqual) sdocs'
+
+-- TODO: also print arg docs.
+pprDocs :: (Maybe HsDocString, Map Int HsDocString) -> SDoc
+pprDocs (mb_decl_docs, _arg_docs) =
+ maybe
+ (text "<has no documentation>")
+ (text . unpackHDS)
+ mb_decl_docs
+
+handleGetDocsFailure :: GHC.GhcMonad m => GetDocsFailure -> m SDoc
+handleGetDocsFailure no_docs = do
+ dflags <- getDynFlags
+ let msg = showPpr dflags no_docs
+ throwGhcException $ case no_docs of
+ NameHasNoModule {} -> Sorry msg
+ NoDocsInIface {} -> InstallationError msg
+ InteractiveName -> ProgramError msg
-----------------------------------------------------------------------------
-- :load, :add, :reload