Skip to content

Commit

Permalink
fix: format Windows and old Mac line breaks to enable copy-paste
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSaba committed Dec 13, 2024
1 parent 6beb376 commit 1d5ed92
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/tests/editorOperationUtils/lockedCode.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable quotes */
import { describe, expect, test, beforeEach, afterEach, jest } from '@jest/globals'
import * as monaco from 'monaco-editor'
import { DisposableStore } from 'vscode/monaco'
Expand Down Expand Up @@ -356,4 +357,50 @@ function findLargest(numbers: number[]): number {
text: ''
})
})

test('Handle all systems line break character', () => {
const model = createDefaultTestModel()
disposableStore.add(model)
const editor = monaco.editor.create(document.createElement('div'), {
model
})
disposableStore.add(editor)
disposableStore.add(lockCodeRanges(editor, {
getLockedRanges () {
return createDefaultTestLockedCodeRanges(model)
}
}))

const operationRange = createTestRange(model, 12, 22)
const operation = createTestOperation(operationRange,
"/* Ignore and do not change the code below */\n" +
"// toto\r\n" +
"/* Ignore and do not change the code above */\n" +
"\n" +
"// new comment\r\n" +
"// on two lines\r" +
"\n" +
"/* Ignore and do not change the code below */\r\n" +
"// toto\r" +
"/* Ignore and do not change the code above */\n" +
"\n" +
"// other comment\n"
)

const onDidChangeContent = jest.fn()
disposableStore.add(model.onDidChangeContent(onDidChangeContent))

editor.executeEdits(null, [operation])

expect(onDidChangeContent).toHaveBeenCalledTimes(1)
expect(onDidChangeContent.mock.calls[0]![0]).toMatchObject({
changes: [{
range: { startLineNumber: 21, startColumn: 1, endLineNumber: 22, endColumn: 24 },
text: '\n// other comment\n'
}, {
range: { startLineNumber: 15, startColumn: 1, endLineNumber: 17, endColumn: 1 },
text: '\n// new comment\n// on two lines'
}]
})
})
})
3 changes: 2 additions & 1 deletion src/tools/utils/editorOperationUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as monaco from 'monaco-editor'
import { ValidAnnotatedEditOperation } from 'vscode/vscode/vs/editor/common/model'
import { excludeRanges } from './rangeUtils'
import { normalizeStringLineBreaks } from './stringUtils'

export class LockedCodeError extends Error {}

Expand Down Expand Up @@ -50,7 +51,7 @@ function tryIgnoreLockedCodeTextForOperation (
const splitText: string[] = []
const uneditableRangesText = uneditableRangesInOperationRange.map(range => model.getValueInRange(range))
let currentRange: number = 0
let remainingText: string = operationText
let remainingText: string = normalizeStringLineBreaks(operationText)
while (remainingText.length > 0 && currentRange < uneditableRangesText.length) {
const rangeText = uneditableRangesText[currentRange]
if (rangeText != null && rangeText !== '') {
Expand Down
3 changes: 3 additions & 0 deletions src/tools/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function normalizeStringLineBreaks (str: string, lineBreakCharacter: string = '\n'): string {
return str.replaceAll(/\r?\n|\r/g, lineBreakCharacter)
}

0 comments on commit 1d5ed92

Please sign in to comment.