@@ -27,7 +27,7 @@ import Data.Maybe as Maybe
2727import Data.Foldable
2828import qualified Data.Map as Map
2929import qualified Data.Text as T
30−import Data.Text.Prettyprint.Doc.Syntax
30+import Data.Text.Prettyprint.Doc
3131import qualified Data.SortedList as SL
3232import qualified Text.PrettyPrint.Annotated.HughesPJClass as Pretty
3333import qualified Language.Haskell.LSP.Types as LSP
@@ -38,6 +38,9 @@ import Language.Haskell.LSP.Types as LSP (
3838 , DiagnosticRelatedInformation(..)
3939 )
4040import Language.Haskell.LSP.Diagnostics
41+import Data.Text.Prettyprint.Doc.Render.Text
42+import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Terminal
43+import Data.Text.Prettyprint.Doc.Render.Terminal (Color(..), color)
4144
4245import Development.IDE.Types.Location
4346
@@ -77,7 +80,7 @@ diagnostic rng sev src msg
7780--
7881type FileDiagnostic = (NormalizedFilePath, Diagnostic)
7982
80−prettyRange :: Range -> Doc SyntaxClass
83+prettyRange :: Range -> Doc Terminal.AnsiStyle
8184prettyRange Range{..} = f _start <> "-" <> f _end
8285 where f Position{..} = pretty (_line+1) <> colon <> pretty _character
8386
@@ -91,10 +94,10 @@ showDiagnosticsColored :: [FileDiagnostic] -> T.Text
9194showDiagnosticsColored = srenderColored . prettyDiagnostics
9295
9396
94−prettyDiagnostics :: [FileDiagnostic] -> Doc SyntaxClass
97+prettyDiagnostics :: [FileDiagnostic] -> Doc Terminal.AnsiStyle
9598prettyDiagnostics = vcat . map prettyDiagnostic
9699
97−prettyDiagnostic :: FileDiagnostic -> Doc SyntaxClass
100+prettyDiagnostic :: FileDiagnostic -> Doc Terminal.AnsiStyle
98101prettyDiagnostic (fp, LSP.Diagnostic{..}) =
99102 vcat
100103 [ slabel_ "File: " $ pretty (fromNormalizedFilePath fp)
@@ -103,10 +106,10 @@ prettyDiagnostic (fp, LSP.Diagnostic{..}) =
103106 , slabel_ "Severity:" $ pretty $ show sev
104107 , slabel_ "Message: "
105108 $ case sev of
106− LSP.DsError -> annotate ErrorSC
107− LSP.DsWarning -> annotate WarningSC
108− LSP.DsInfo -> annotate InfoSC
109− LSP.DsHint -> annotate HintSC
109+ LSP.DsError -> annotate $ color Red
110+ LSP.DsWarning -> annotate $ color Yellow
111+ LSP.DsInfo -> annotate $ color Blue
112+ LSP.DsHint -> annotate $ color Magenta
110113 $ stringParagraphs _message
111114 , slabel_ "Code:" $ pretty _code
112115 ]
@@ -154,3 +157,31 @@ filterDiagnostics ::
154157 DiagnosticStore
155158filterDiagnostics keep =
156159 Map.filterWithKey (\uri _ -> maybe True (keep . toNormalizedFilePath) $ uriToFilePath' $ fromNormalizedUri uri)
160+
161+
162+
163+-- | Label a document.
164+slabel_ :: String -> Doc a -> Doc a
165+slabel_ t d = nest 2 $ sep [pretty t, d]
166+
167+-- | The layout options used for the SDK assistant.
168+cliLayout ::
169+ Int
170+ -- ^ Rendering width of the pretty printer.
171+ -> LayoutOptions
172+cliLayout renderWidth = LayoutOptions
173+ { layoutPageWidth = AvailablePerLine renderWidth 0.9
174+ }
175+
176+-- | Render without any syntax annotations
177+srenderPlain :: Doc ann -> T.Text
178+srenderPlain = renderStrict . layoutSmart (cliLayout defaultTermWidth)
179+
180+-- | Render a 'Document' as an ANSII colored string.
181+srenderColored :: Doc Terminal.AnsiStyle -> T.Text
182+srenderColored =
183+ Terminal.renderStrict .
184+ layoutSmart defaultLayoutOptions { layoutPageWidth = AvailablePerLine 100 1.0 }
185+
186+defaultTermWidth :: Int
187+defaultTermWidth = 80