Skip to content

Commit

Permalink
Make rule of 2-spaces to preserve a line break universal
Browse files Browse the repository at this point in the history
It's now in the wrapping code and taken out of each parser.

This does though mean that things won't be treated as separate
paragraphs from the point-of-view of what to wrap with an empty
selection.
  • Loading branch information
stkb committed Feb 6, 2022
1 parent 6153cd4 commit 5511097
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/Parsers_Markdown.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let private mdMarker marker = regex (@"^ {0,3}" + marker)

/// The default paragraph type (fallback for if no other parser matches)
let private defaultPara (_ctx: Context) : FirstLineParser =
let lineBreakEnd = regex @"(\\|\s{2}|<br/?>)$"
let lineBreakEnd = regex @"(\\|<br/?>)$"
let rec parseLine (line: Line) : FirstLineRes =
// We assume a >3 space indent on the first line would have already been
// picked up by another parser, so it's ok to just trim all indent
Expand Down
4 changes: 1 addition & 3 deletions core/Parsing.DocComments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ let ddoc =
let godoc settings =
let indentedLines =
ignoreParser (Nonempty.span (fun line -> line.[0] = ' ' || line.[0] = '\t'))
let textLines =
splitIntoChunks (afterRegex (Regex(" $"))) >> map (Wrap << Wrappable.fromLines ("", ""))

let textLines = Nonempty.singleton << Wrap << Wrappable.fromLines ("", "")
textLines
|> takeUntil (tryMany [blankLines; indentedLines])
|> repeatToEnd
Expand Down
1 change: 0 additions & 1 deletion core/Parsing.Documents.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ open Parsing_SourceCode
let oldPlainText settings =
let paragraphs =
splitIntoChunks (onIndent settings.tabWidth)
>> Nonempty.concatMap (splitIntoChunks (afterRegex (Regex(" $"))))
>> map (indentSeparatedParagraphBlock textBlock)

takeUntil blankLines paragraphs |> repeatToEnd
Expand Down
3 changes: 1 addition & 2 deletions core/Parsing.Latex.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ let private markdown = Parsing.Markdown.markdown
let private newlineRegex: Regex =
Regex
( @"(\\(\\\*?|hline|newline|break|linebreak)(\[.*?\])?(\{.*?\})?\s*$)"
+ @"| $|"
+ @"([^\\]%)"
+ @"|([^\\]%)"
)

/// Commands that, when starting a line, should always preserve the line break
Expand Down
2 changes: 1 addition & 1 deletion core/Parsing.Sgml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let sgml

let breakBefore line = justBlockEndTag line || beginsWithBlockStartTag line
let breakAfter line =
Line.contains (regex @"([""\s]>\s*| )$") line || endsWithBlockTag line
Line.contains (regex @"([""\s]>\s*)$") line || endsWithBlockTag line

let paragraphBlocks =
splitIntoChunks (splitBefore breakBefore)
Expand Down
16 changes: 11 additions & 5 deletions core/Wrapping.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let canBreakBetweenChars c1 c2 =

/// When concatenating lines before breaking up again, whether to add a space
/// between chars
let addSpaceBetweenChars c1 c2 = if isCJ c1 || isCJ c2 then false else true
let addSpaceBetweenChars c1 c2 = if c1 = 10us || isCJ c1 || isCJ c2 then false else true


/// Concatenates lines into a single string, adding the right amount of
Expand All @@ -47,10 +47,9 @@ let concatLines doubleSentenceSpacing lines =
let addLine (acc: string) (line: string) =
// Shouldn't be getting empty strings in this function but just in case...
if line = String.Empty || acc = String.Empty then acc else
let acc = acc.TrimEnd()
let acc = if acc.EndsWith(" ") then acc + "\n" else acc.TrimEnd()
let accEnd = uint16 acc.[acc.Length-1]
let space =
if doubleSentenceSpacing && Array.contains accEnd stops then " " else " "
let space = if doubleSentenceSpacing && Array.contains accEnd stops then " " else " "
if addSpaceBetweenChars accEnd (uint16 line.[0]) then acc + space + line else acc + line
List.reduce addLine (Nonempty.toList lines)

Expand All @@ -76,7 +75,9 @@ let breakUpString addLine tabWidth maxWidth (str: string) =
let outputLine prefixes pStart pEnd =
let prefix, nextPrefixes = popOrPeek prefixes
let content =
if pEnd > pStart then str.Substring(pStart, pEnd - pStart).Trim()
if pEnd > pStart then
if str[pEnd-1] = '\n' then str.Substring(pStart, pEnd - pStart - 1)
else str.Substring(pStart, pEnd - pStart).TrimEnd()
else str.Substring(pStart)
addLine (prefix + content.Replace('\000', ' '))
nextPrefixes
Expand All @@ -87,6 +88,11 @@ let breakUpString addLine tabWidth maxWidth (str: string) =
if pStr >= str.Length then outputLine prefixes lineStart 0 |> ignore
else
let charCode = uint16 str.[pStr]
// If we come across a LF char then start a new line
if charCode = 10us then
let nextPrefixes = outputLine prefixes lineStart (pStr+1)
loop nextPrefixes (pStr+1) (prefixWidth nextPrefixes) (pStr+1)
else
let newWidth = curWidth + Line.charWidth tabWidth curWidth charCode
// If current char is whitespace we don't need to wrap yet. Wait until we come across
// a non-whitespace char
Expand Down
8 changes: 7 additions & 1 deletion docs/specs/features/spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ wrapping column.
## At the end of a line

Two spaces at the end of a line preserves the line break after it. This comes
from Markdown should work for any content.
from Markdown and should work for any content.

> language: "plaintext"
Expand Down Expand Up @@ -64,3 +64,9 @@ from Markdown should work for any content.
Baz foo ¦ Baz foo bar ¦
bar baz. ¦ baz. ¦
#> ¦ #> ¦

> language: "rst"
Foo bar.·· ¦ -> Foo bar.·· ¦
Baz foo ¦ Baz foo bar ¦
bar baz. ¦ baz. ¦

0 comments on commit 5511097

Please sign in to comment.