Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix haddock documentation links (#2542) #2550

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion ghcide/src/Development/IDE/Spans/Documentation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Development.IDE.Spans.Documentation (
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Extra (findM)
import Data.Char
import Data.Either
import Data.Foldable
import Data.List.Extra
Expand Down Expand Up @@ -87,7 +88,7 @@ getDocumentationsTryGhc env mod names = do
src <- toFileUriText $ lookupSrcHtmlForModule env mod
return (doc, src)
Nothing -> pure (Nothing, Nothing)
let docUri = (<> "#" <> selector <> showNameWithoutUniques name) <$> docFu
let docUri = (<> "#" <> selector <> makeDocAnchor (showNameWithoutUniques name)) <$> docFu
srcUri = (<> "#" <> showNameWithoutUniques name) <$> srcFu
selector
| isValName name = "v:"
Expand All @@ -96,6 +97,27 @@ getDocumentationsTryGhc env mod names = do

toFileUriText = (fmap . fmap) (getUri . filePathToUri)

-- | Takes an arbitrary string and makes it a valid anchor ID. This is ported
-- from the function @Haddock.Utils.makeAnchorId@ in the @haddock-api@ package,
-- which seems to be the function in haddock that does the original encoding
-- that we reproduce here.
--
-- Note that this isn't just necessary for operators, it also affects function
-- names like @foldl'@ with apostrophes in them.
makeDocAnchor :: T.Text -> T.Text
makeDocAnchor = T.concatMap docEscapeChar
where
docEscapeChar :: Char -> T.Text
docEscapeChar c
| isLegal c = T.singleton c
| otherwise = T.pack $ '-' : show (ord c) ++ "-"

isLegal :: Char -> Bool
isLegal ':' = True
isLegal '_' = True
isLegal '.' = True
isLegal c = isAscii c && isAlphaNum c

getDocumentation
:: HasSrcSpan name
=> [ParsedModule] -- ^ All of the possible modules it could be defined in.
Expand Down
5 changes: 4 additions & 1 deletion ghcide/test/data/hover/GotoHover.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE OverloadedStrings, TemplateHaskell #-}
{- HLINT ignore -}
module GotoHover ( module GotoHover) where
import Data.Text (Text, pack)
import Data.Text (Text, pack, foldl')
import Foo (Bar, foo)


Expand Down Expand Up @@ -61,3 +61,6 @@ aa2 = $(id [| True |])

hole :: Int
hole = _

externalFuncWithPrime :: Char
externalFuncWithPrime = foldl' max 'a' "word"
4 changes: 4 additions & 0 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3987,6 +3987,8 @@ findDefinitionAndHoverTests = let
innL48 = Position 52 5 ; innSig = [ExpectHoverText ["inner", "Char"], mkR 49 2 49 7]
holeL60 = Position 62 7 ; hleInfo = [ExpectHoverText ["_ ::"]]
cccL17 = Position 17 16 ; docLink = [ExpectHoverText ["[Documentation](file:///"]]
opL25 = Position 24 18 ; docOpEnc = [ExpectHoverText ["[Documentation](file:///", "#v:-60--62-)"]]
funcL66 = Position 65 26 ; docFuncEnc = [ExpectHoverText ["[Documentation](file:///", "#v:foldl-39-)"]]
imported = Position 56 13 ; importedSig = getDocUri "Foo.hs" >>= \foo -> return [ExpectHoverText ["foo", "Foo", "Haddock"], mkL foo 5 0 5 3]
reexported = Position 55 14 ; reexportedSig = getDocUri "Bar.hs" >>= \bar -> return [ExpectHoverText ["Bar", "Bar", "Haddock"], mkL bar 3 0 3 14]
thLocL57 = Position 59 10 ; thLoc = [ExpectHoverText ["Identity"]]
Expand Down Expand Up @@ -4041,6 +4043,8 @@ findDefinitionAndHoverTests = let
, test broken broken innL48 innSig "inner signature #767"
, test no yes holeL60 hleInfo "hole without internal name #831"
, test no skip cccL17 docLink "Haddock html links"
, test yes skip opL25 docOpEnc "Haddock html doc link operator #2542"
, test yes skip funcL66 docFuncEnc "Haddock html doc link function #2542"
, testM yes yes imported importedSig "Imported symbol"
, testM yes yes reexported reexportedSig "Imported symbol (reexported)"
, if ghcVersion == GHC90 && isWindows then
Expand Down