Fix multiline basic string patching tests and fix line handling#131
Draft
DecimalTurn wants to merge 7 commits intolatestfrom
Draft
Fix multiline basic string patching tests and fix line handling#131DecimalTurn wants to merge 7 commits intolatestfrom
DecimalTurn wants to merge 7 commits intolatestfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends multiline string format preservation during patch() so that TOML basic multiline strings using line-continuation backslashes (\<NL><ws>) can be edited while keeping their original structural formatting.
Changes:
- Add
rebuildLineContinuation()ingenerateString()to reconstruct multiline basic strings that use line-continuation backslashes. - Add/expand test coverage for editing multiline string values inside multiline inline tables when line continuations are present.
- Add a one-off audit script to print actual
patch()outputs for these cases.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/generate.ts | Adds line-continuation detection and a reconstruction helper for multiline strings during format preservation. |
| src/tests/patch.test.ts | Adds regression tests ensuring line-continuation backslashes + closing indent are preserved after edits. |
| scripts/audit-multiline-inline-outputs.cjs | Adds an audit utility to print patch() outputs for multiline inline-table test cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| decodedValue: string, | ||
| delimiter: string, | ||
| newlineChar: string | ||
| ): string { |
| } | ||
|
|
||
| let stripped = line; | ||
| const hasBackslash = stripped.endsWith('\\'); |
Comment on lines
+231
to
+255
| const splitWords = (s: string): string[] => s.match(/\S+/g) || []; | ||
|
|
||
| const newWords = splitWords(decodedValue); | ||
|
|
||
| // Map content segments to word ranges: segmentWordRanges[i] = [startWordIdx, endWordIdx) | ||
| const segmentWordRanges: [number, number][] = []; | ||
| let wordIdx = 0; | ||
| for (const seg of contentSegments) { | ||
| const segWords = splitWords(seg.content); | ||
| const start = wordIdx; | ||
| wordIdx += segWords.length; | ||
| segmentWordRanges.push([start, wordIdx]); | ||
| } | ||
|
|
||
| // Redistribute new words across content segments using the same ranges. | ||
| const newContents: string[] = []; | ||
| for (let i = 0; i < contentSegments.length; i++) { | ||
| const [origStart, origEnd] = segmentWordRanges[i]; | ||
| const origCount = origEnd - origStart; | ||
| const segNewWords = | ||
| i === contentSegments.length - 1 | ||
| ? newWords.slice(origStart) | ||
| : newWords.slice(origStart, origStart + origCount); | ||
| newContents.push(segNewWords.join(' ')); | ||
| } |
Comment on lines
+233
to
+257
| const newWords = splitWords(decodedValue); | ||
|
|
||
| // Map content segments to word ranges: segmentWordRanges[i] = [startWordIdx, endWordIdx) | ||
| const segmentWordRanges: [number, number][] = []; | ||
| let wordIdx = 0; | ||
| for (const seg of contentSegments) { | ||
| const segWords = splitWords(seg.content); | ||
| const start = wordIdx; | ||
| wordIdx += segWords.length; | ||
| segmentWordRanges.push([start, wordIdx]); | ||
| } | ||
|
|
||
| // Redistribute new words across content segments using the same ranges. | ||
| const newContents: string[] = []; | ||
| for (let i = 0; i < contentSegments.length; i++) { | ||
| const [origStart, origEnd] = segmentWordRanges[i]; | ||
| const origCount = origEnd - origStart; | ||
| const segNewWords = | ||
| i === contentSegments.length - 1 | ||
| ? newWords.slice(origStart) | ||
| : newWords.slice(origStart, origStart + origCount); | ||
| newContents.push(segNewWords.join(' ')); | ||
| } | ||
|
|
||
| // Trim trailing empty content entries (when new value has fewer words) |
Comment on lines
+210
to
+211
| if (line.trim() === '') { | ||
| return { indent: '', content: '', trailingWs: '', hasBackslash: false, isBlank: true }; |
Comment on lines
+334
to
+347
| // Detect line-continuation backslashes anywhere in the multiline string body. | ||
| // A line ending with an odd number of backslashes is a continuation line. | ||
| // Line-continuation is only meaningful in basic (""") strings, not literal ('''). | ||
| const innerContent = existingRaw.slice(delimiter.length, existingRaw.length - delimiter.length); | ||
| const hasLineContinuation = !isLiteral && !escaped.includes(newlineChar) && | ||
| innerContent.split(newlineChar).some(line => { | ||
| const m = line.match(/(\\+)$/); | ||
| return m !== null && m[1].length % 2 === 1; | ||
| }); | ||
|
|
||
| // Generate the replacement raw string, preserving the structural format of the existing raw. | ||
| if (hasLineContinuation) { | ||
| raw = rebuildLineContinuation(existingRaw, escaped, value, delimiter, newlineChar); | ||
| } else if (hasLeadingNewline) { |
6f0ed0c to
adcfa8f
Compare
adcfa8f to
a4354e7
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.