From d296266602b2d9084f0628227606234cef8ac71c Mon Sep 17 00:00:00 2001 From: Paul Chiusano Date: Mon, 4 Nov 2019 21:15:13 -0500 Subject: [PATCH] simplify paragraphyText, it's now much better behaved --- .../src/Unison/CommandLine/DisplayValues.hs | 2 +- parser-typechecker/src/Unison/Util/Pretty.hs | 23 +++++++------------ unison-src/transcripts/docs.md | 2 +- unison-src/transcripts/docs.output.md | 10 +++++--- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/parser-typechecker/src/Unison/CommandLine/DisplayValues.hs b/parser-typechecker/src/Unison/CommandLine/DisplayValues.hs index 16ff4a05e..8fdba4547 100644 --- a/parser-typechecker/src/Unison/CommandLine/DisplayValues.hs +++ b/parser-typechecker/src/Unison/CommandLine/DisplayValues.hs @@ -49,7 +49,7 @@ displayDoc :: (Var v, Monad m) displayDoc ppe terms typeOf evaluated types t = go t where go (DD.DocJoin docs) = foldMap id <$> traverse go docs - go (DD.DocBlob txt) = pure $ P.text txt + go (DD.DocBlob txt) = pure $ P.paragraphyText txt go (DD.DocLink (DD.LinkTerm (Term.TermLink' r))) = pure $ P.underline (termName ppe r) go (DD.DocLink (DD.LinkType (Term.TypeLink' r))) = pure $ P.underline (typeName ppe r) go (DD.DocSource (DD.LinkTerm (Term.TermLink' r))) = prettyTerm terms r diff --git a/parser-typechecker/src/Unison/Util/Pretty.hs b/parser-typechecker/src/Unison/Util/Pretty.hs index 9667de049..171571ac4 100644 --- a/parser-typechecker/src/Unison/Util/Pretty.hs +++ b/parser-typechecker/src/Unison/Util/Pretty.hs @@ -143,22 +143,15 @@ wrapImpl (p:ps) = wrap_ . Seq.fromList $ wrapString :: (LL.ListLike s Char, IsString s) => String -> Pretty s wrapString s = wrap (lit $ fromString s) --- 0. Preserve all leading and trailing spaces --- 1. Convert multiple adjacent blank lines to a single blank line. --- 2. Combine adjacent non-blank lines into one line. --- 3. Wrap each remaining line. +-- 0. Preserve all leading and trailing whitespace +-- 1. Preserve all newlines +-- 2. Wrap all text in between newlines paragraphyText :: (LL.ListLike s Char, IsString s) => Text -> Pretty s -paragraphyText t = case span isSpace (Text.unpack $ Text.dropWhileEnd isSpace t) of - (sp,t) -> string sp <> lines (go mempty t) - where - trailingSp = Text.takeWhileEnd isSpace t - go acc [] = [wrap acc <> text trailingSp] - go acc (span isSpace -> (sp, rest)) = - if length (filter (=='\n') sp) < 2 then munch acc - else wrap acc : mempty : munch mempty - where - munch acc = case break isSpace rest of - (nonsp, rest) -> go (acc <> string nonsp) rest +paragraphyText t = text start <> inner <> text end where + inner = sep "\n" . fmap (wrap . text) . Text.splitOn "\n" $ t' + (start, t0) = Text.span isSpace t + t' = Text.dropWhileEnd isSpace t0 + end = Text.takeWhileEnd isSpace t0 wrap :: (LL.ListLike s Char, IsString s) => Pretty s -> Pretty s wrap p = wrapImpl (toLeaves [p]) where diff --git a/unison-src/transcripts/docs.md b/unison-src/transcripts/docs.md index 3ff35f595..c922ff327 100644 --- a/unison-src/transcripts/docs.md +++ b/unison-src/transcripts/docs.md @@ -50,7 +50,7 @@ use .builtin docs.List.take = [: -`@List.take n xs` returns the first `n` elements of `xs` +`@List.take n xs` returns the first `n` elements of `xs`. (No need to add line breaks manually. The display command will do wrapping of text for you.) ## Examples: diff --git a/unison-src/transcripts/docs.output.md b/unison-src/transcripts/docs.output.md index abacb581b..fe3a1abf3 100644 --- a/unison-src/transcripts/docs.output.md +++ b/unison-src/transcripts/docs.output.md @@ -92,7 +92,7 @@ use .builtin docs.List.take = [: -`@List.take n xs` returns the first `n` elements of `xs` +`@List.take n xs` returns the first `n` elements of `xs`. (No need to add line breaks manually. The display command will do wrapping of text for you.) ## Examples: @@ -147,7 +147,9 @@ Now that documentation is linked to the definition. We can view it if we like: .> display 1 - `builtin.List.take n xs` returns the first `n` elements of `xs` + `builtin.List.take n xs` returns the first `n` elements of + `xs`. (No need to add line breaks manually. The display + command will do wrapping of text for you.) ## Examples: @@ -166,7 +168,9 @@ Or there's also a convenient function, `docs`, which shows the `Doc` values that ```ucm .> docs builtin.List.take - `builtin.List.take n xs` returns the first `n` elements of `xs` + `builtin.List.take n xs` returns the first `n` elements of + `xs`. (No need to add line breaks manually. The display + command will do wrapping of text for you.) ## Examples: