Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Enhancements to Haddock -> Markdown conversion #344

Merged
merged 8 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests for Haddock -> Markdown conversion
  • Loading branch information
serras committed Jan 24, 2020
commit 1725f74742c1c4cf6732e937acc5b6f17d3b4437
2 changes: 1 addition & 1 deletion ghcide.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ library
Development.IDE.LSP.LanguageServer
Development.IDE.LSP.Protocol
Development.IDE.LSP.Server
Development.IDE.Spans.Common
Development.IDE.Types.Diagnostics
Development.IDE.Types.Location
Development.IDE.Types.Logger
Expand All @@ -135,7 +136,6 @@ library
Development.IDE.LSP.Outline
Development.IDE.Spans.AtPoint
Development.IDE.Spans.Calculate
Development.IDE.Spans.Common
Development.IDE.Spans.Documentation
Development.IDE.Spans.Type
ghc-options: -Wall -Wno-name-shadowing
Expand Down
9 changes: 6 additions & 3 deletions src/Development/IDE/Spans/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Development.IDE.Spans.Common (
, SpanDoc(..)
, emptySpanDoc
, spanDocToMarkdown
, spanDocToMarkdownForTest
) where

import Data.Data
Expand Down Expand Up @@ -81,7 +82,10 @@ spanDocToMarkdown (SpanDocString _)
#endif
spanDocToMarkdown (SpanDocText txt) = txt

#if MIN_GHC_API_VERSION(8,6,0)
spanDocToMarkdownForTest :: String -> String
spanDocToMarkdownForTest
= haddockToMarkdown . H.toRegular . H._doc . H.parseParas Nothing

-- Simple (and a bit hacky) conversion from Haddock markup to Markdown
haddockToMarkdown
:: H.DocH String String -> String
Expand Down Expand Up @@ -162,5 +166,4 @@ splitForList :: String -> String
splitForList s
= case lines s of
[] -> ""
(first:rest) -> unlines $ first : (map ((" " ++) . dropWhile isSpace) rest)
#endif
(first:rest) -> unlines $ first : (map ((" " ++) . dropWhile isSpace) rest)
15 changes: 15 additions & 0 deletions test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Data.Foldable
import Data.List
import Development.IDE.GHC.Util
import qualified Data.Text as T
import Development.IDE.Spans.Common
import Development.IDE.Test
import Development.IDE.Test.Runfiles
import Development.IDE.Types.Location
Expand Down Expand Up @@ -53,6 +54,7 @@ main = defaultMain $ testGroup "HIE"
, preprocessorTests
, thTests
, unitTests
, haddockTests
]

initializeResponseTests :: TestTree
Expand Down Expand Up @@ -1546,6 +1548,19 @@ data Expect

mkR :: Int -> Int -> Int -> Int -> Expect
mkR startLine startColumn endLine endColumn = ExpectRange $ mkRange startLine startColumn endLine endColumn

haddockTests :: TestTree
haddockTests
= testGroup "haddock"
[ testCase "Num" $ checkHaddock
"However, '(+)' and '(*)' are\ncustomarily expected to define a ring and have the following properties:\n\n[__Associativity of (+)__]: @(x + y) + z@ = @x + (y + z)@\n[__Commutativity of (+)__]: @x + y@ = @y + x@\n[__@fromInteger 0@ is the additive identity__]: @x + fromInteger 0@ = @x@"
"\n\nHowever, '(+)' and '(*)' are\ncustomarily expected to define a ring and have the following properties: \n+ ****Associativity of (+)****: `(x + y) + z` = `x + (y + z)`\n+ ****Commutativity of (+)****: `x + y` = `y + x`\n+ ****`fromInteger 0` is the additive identity****: `x + fromInteger 0` = `x`\n"
, testCase "unsafePerformIO" $ checkHaddock
"may require\ndifferent precautions:\n\n * Use @{\\-\\# NOINLINE foo \\#-\\}@ as a pragma on any function @foo@\n that calls 'unsafePerformIO'. If the call is inlined,\n the I\\/O may be performed more than once.\n\n * Use the compiler flag @-fno-cse@ to prevent common sub-expression\n elimination being performed on the module."
"\n\nmay require\ndifferent precautions: \n+ Use `{-# NOINLINE foo #-}` as a pragma on any function `foo` \n that calls `unsafePerformIO` . If the call is inlined,\n the I/O may be performed more than once.\n\n+ Use the compiler flag `-fno-cse` to prevent common sub-expression\n elimination being performed on the module.\n\n"]
where
checkHaddock s txt = spanDocToMarkdownForTest s @?= txt

----------------------------------------------------------------------
-- Utils

Expand Down