[commit: haddock] master: Create simple mechanism for associating tokens with AST names. (d06a2b5)

git at git.haskell.org git at git.haskell.org
Wed Jul 8 08:40:01 UTC 2015


Repository : ssh://git@git.haskell.org/haddock

On branch  : master
Link       : http://git.haskell.org/haddock.git/commitdiff/d06a2b502e7909dff517a7c825e772b493bade24

>---------------------------------------------------------------

commit d06a2b502e7909dff517a7c825e772b493bade24
Author: Łukasz Hanuszczak <lukasz.hanuszczak at gmail.com>
Date:   Sat Jun 6 20:04:02 2015 +0200

    Create simple mechanism for associating tokens with AST names.


>---------------------------------------------------------------

d06a2b502e7909dff517a7c825e772b493bade24
 .../src/Haddock/Backends/Hyperlinker/Ast.hs        | 30 +++++++++++++++++-----
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs b/haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs
index 62c0d43..031ddd5 100644
--- a/haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs
+++ b/haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs
@@ -2,11 +2,13 @@
 
 module Haddock.Backends.Hyperlinker.Ast where
 
+import Haddock.Backends.Hyperlinker.Parser
+
 import qualified GHC
-import Data.Data
-import Control.Applicative
 
-import Haddock.Backends.Hyperlinker.Parser
+import Control.Applicative
+import Data.Data
+import Data.Maybe
 
 data RichToken = RichToken
     { rtkToken :: Token
@@ -17,20 +19,34 @@ enrich :: GHC.RenamedSource -> [Token] -> [RichToken]
 enrich src =
     map $ \token -> RichToken
         { rtkToken = token
-        , rtkName = lookupName src $ tkSpan token
+        , rtkName = lookupBySpan (tkSpan token) nameMap
         }
+  where
+    nameMap = variables src
+
+type NameMap = [(GHC.SrcSpan, GHC.Name)]
 
-lookupName :: GHC.RenamedSource -> Span -> Maybe GHC.Name
-lookupName = undefined
+lookupBySpan :: Span -> NameMap -> Maybe GHC.Name
+lookupBySpan tspan = listToMaybe . map snd . filter (matches tspan . fst)
 
 everything :: (r -> r -> r) -> (forall a. Data a => a -> r)
            -> (forall a. Data a => a -> r)
 everything k f x = foldl k (f x) (gmapQ (everything k f) x)
 
-variables :: GHC.RenamedSource -> [(GHC.SrcSpan, GHC.Name)]
+variables :: GHC.RenamedSource -> NameMap
 variables =
     everything (<|>) var
   where
     var term = case cast term of
         (Just (GHC.L sspan (GHC.HsVar sid))) -> pure (sspan, sid)
         _ -> empty
+
+matches :: Span -> GHC.SrcSpan -> Bool
+matches tspan (GHC.RealSrcSpan aspan)
+    | rs && cs && re && ce = True
+  where
+    rs = (posRow . spStart) tspan == GHC.srcSpanStartLine aspan
+    cs = (posCol . spStart) tspan == GHC.srcSpanStartCol aspan
+    re = (posRow . spEnd) tspan == GHC.srcSpanEndLine aspan
+    ce = (posCol . spEnd) tspan == GHC.srcSpanStartLine aspan
+matches _ _ = False



More information about the ghc-commits mailing list