-
Notifications
You must be signed in to change notification settings - Fork 45
Dedent multiline text to preserve content indentation #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4cdd539
Dedent multiline text to preserve localizer whitespace.
Pike aa4d42b
More tests
Pike ca76e6f
Use let and for-of
stasm 22a48ae
Don't trim all leading whitespace
stasm bfb4f0d
Comment the code
stasm 4573574
Add a test about leading indent in block text
stasm bd15bd6
Count no indent before a placeable as 0
stasm 5df76e9
Remove FTL.Indent
stasm 8bac02c
Clarify the comment about dedent()
stasm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ export function list_into(Type) { | |
case FTL.Pattern: | ||
return elements => | ||
always(new FTL.Pattern( | ||
elements | ||
dedent(elements) | ||
.reduce(join_adjacent(FTL.TextElement), []) | ||
.map(trim_text_at_extremes) | ||
.filter(remove_empty_text))); | ||
|
@@ -111,33 +111,38 @@ export function into(Type) { | |
} | ||
} | ||
|
||
// Create a reducer suitable for joining adjacent nodes of the same type, if | ||
// type is one of types specified. | ||
function join_adjacent(...types) { | ||
return function(acc, cur) { | ||
let prev = acc[acc.length - 1]; | ||
for (let Type of types) { | ||
if (prev instanceof Type && cur instanceof Type) { | ||
join_of_type(Type, prev, cur); | ||
// Replace prev with a new node of the same type whose value is | ||
// the sum of prev and cur, and discard cur. | ||
acc[acc.length - 1] = join_of_type(Type, prev, cur); | ||
return acc; | ||
} | ||
} | ||
return acc.concat(cur); | ||
}; | ||
} | ||
|
||
// Join values of two or more nodes of the same type. Return a new node. | ||
function join_of_type(Type, ...elements) { | ||
// TODO Join annotations and spans. | ||
switch (Type) { | ||
case FTL.TextElement: | ||
return elements.reduce((a, b) => | ||
(a.value += b.value, a)); | ||
new Type(a.value + b.value)); | ||
case FTL.Comment: | ||
case FTL.GroupComment: | ||
case FTL.ResourceComment: | ||
return elements.reduce((a, b) => | ||
(a.content += `\n${b.content}`, a)); | ||
new Type(a.content + "\n" + b.content)); | ||
case FTL.Junk: | ||
return elements.reduce((a, b) => | ||
(a.content += b.content, a)); | ||
new Type(a.content + b.content)); | ||
} | ||
} | ||
|
||
|
@@ -154,18 +159,36 @@ function attach_comments(acc, cur) { | |
} | ||
} | ||
|
||
const LEADING_BLANK = /^[ \n\r]*/; | ||
const TRAILING_BLANK = /[ \n\r]*$/; | ||
// Remove the largest common indentation from a list of PatternElements. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extend this comment to make it expressive that all literal strings are indents, thanks to grammar.mjs separating those out? |
||
function dedent(elements) { | ||
// Calculate the maximum common indent. | ||
let indents = elements.filter(element => typeof element === "string"); | ||
let common = Math.min(...indents.map(indent => indent.length)); | ||
|
||
function trim_indents(element) { | ||
if (typeof element === "string") { | ||
// Trim the indent and convert it to a proper TextElement. | ||
// It will be joined with its adjacents later on. | ||
return new FTL.TextElement(element.slice(common)); | ||
} | ||
return element; | ||
} | ||
|
||
return elements.map(trim_indents); | ||
} | ||
|
||
const LEADING_BLANK_BLOCK = /^\n*/; | ||
const TRAILING_BLANK_INLINE = / *$/; | ||
|
||
function trim_text_at_extremes(element, index, array) { | ||
if (element instanceof FTL.TextElement) { | ||
if (index === 0) { | ||
element.value = element.value.replace( | ||
LEADING_BLANK, ""); | ||
LEADING_BLANK_BLOCK, ""); | ||
} | ||
if (index === array.length - 1) { | ||
element.value = element.value.replace( | ||
TRAILING_BLANK, ""); | ||
TRAILING_BLANK_INLINE, ""); | ||
} | ||
} | ||
return element; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...
discard cur and prev.
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it's needed given that the comment starts with
Replace...
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could go without the
, and discard ...
altogether, too.