Skip to content

Commit 99fed59

Browse files
authored
Merge pull request #361 from michaelpj/mpj/markup-content
Fix the Semigroup instance for MarkupContent
2 parents 58055c2 + e421dd1 commit 99fed59

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lsp-types/src/Language/LSP/Types/MarkupContent.hs

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Language.LSP.Types.MarkupContent where
1212
import Data.Aeson
1313
import Data.Aeson.TH
1414
import Data.Text (Text)
15+
import qualified Data.Text as T
1516
import Language.LSP.Types.Utils
1617

1718
-- | Describes the content type that a client supports in various
@@ -81,10 +82,18 @@ sectionSeparator = "* * *\n"
8182

8283
-- ---------------------------------------------------------------------
8384

85+
-- | Given some plaintext, convert it into some equivalent markdown text.
86+
-- This is not *quite* the identity function.
87+
plainTextToMarkdown :: Text -> Text
88+
-- Line breaks in markdown paragraphs are ignored unless the line ends with two spaces.
89+
-- In order to respect the line breaks in the original plaintext, we stick two spaces on the end of every line.
90+
plainTextToMarkdown = T.unlines . fmap (<> " ") . T.lines
91+
8492
instance Semigroup MarkupContent where
8593
MarkupContent MkPlainText s1 <> MarkupContent MkPlainText s2 = MarkupContent MkPlainText (s1 `mappend` s2)
86-
MarkupContent MkMarkdown s1 <> MarkupContent _ s2 = MarkupContent MkMarkdown (s1 `mappend` s2)
87-
MarkupContent _ s1 <> MarkupContent MkMarkdown s2 = MarkupContent MkMarkdown (s1 `mappend` s2)
94+
MarkupContent MkMarkdown s1 <> MarkupContent MkMarkdown s2 = MarkupContent MkMarkdown (s1 `mappend` s2)
95+
MarkupContent MkPlainText s1 <> MarkupContent MkMarkdown s2 = MarkupContent MkMarkdown (plainTextToMarkdown s1 `mappend` s2)
96+
MarkupContent MkMarkdown s1 <> MarkupContent MkPlainText s2 = MarkupContent MkMarkdown (s1 `mappend` plainTextToMarkdown s2)
8897

8998
instance Monoid MarkupContent where
9099
mempty = MarkupContent MkPlainText ""

lsp/test/TypesSpec.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ diagnosticsSpec = do
2121
J.unmarkedUpContent "string1\n" <> J.unmarkedUpContent "string2\n"
2222
`shouldBe` J.unmarkedUpContent "string1\nstring2\n"
2323
it "appends a marked up and a plain string" $ do
24-
J.markedUpContent "haskell" "foo :: Int" <> J.unmarkedUpContent "string2\n"
25-
`shouldBe` J.MarkupContent J.MkMarkdown "\n```haskell\nfoo :: Int\n```\nstring2\n"
24+
J.markedUpContent "haskell" "foo :: Int" <> J.unmarkedUpContent "string2\nstring3\n"
25+
`shouldBe` J.MarkupContent J.MkMarkdown "\n```haskell\nfoo :: Int\n```\nstring2 \nstring3 \n"
2626
it "appends a plain string and a marked up string" $ do
2727
J.unmarkedUpContent "string2\n" <> J.markedUpContent "haskell" "foo :: Int"
28-
`shouldBe` J.MarkupContent J.MkMarkdown "string2\n\n```haskell\nfoo :: Int\n```\n"
28+
`shouldBe` J.MarkupContent J.MkMarkdown "string2 \n\n```haskell\nfoo :: Int\n```\n"
2929

3030
-- ---------------------------------------------------------------------

0 commit comments

Comments
 (0)