Skip to content

Commit

Permalink
Merge pull request #126 from CodinGame/ksa/fix-handle-all-systems-lin…
Browse files Browse the repository at this point in the history
…e-breaks

Fix Windows and old Mac line breaks to enable copy-paste
  • Loading branch information
CGNonofr authored Dec 13, 2024
2 parents 6beb376 + 5da9691 commit 2f38f51
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/tests/editorOperationUtils/lockedCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,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'
}]
})
})
})
2 changes: 1 addition & 1 deletion src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export function lockCodeRanges (
// Handle selection of the last line of an editable range
disposableStore.add(
editor.onDidChangeCursorSelection((e) => {
if (canEditRange(e.selection)) {
if (canEditRange(e.selection) || e.selection.isEmpty()) {
return
}
const model = editor.getModel()
Expand Down
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, model.getEOL())
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): string {
return str.replace(/\r\n|\r|\n/g, lineBreakCharacter)
}

0 comments on commit 2f38f51

Please sign in to comment.