diff --git a/src/tests/editorOperationUtils/lockedCode.test.ts b/src/tests/editorOperationUtils/lockedCode.test.ts index 762b021..917afb8 100644 --- a/src/tests/editorOperationUtils/lockedCode.test.ts +++ b/src/tests/editorOperationUtils/lockedCode.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from '@jest/globals' import { canTestOperationsEditRanges, createDefaultTestLockedCodeRanges, createDefaultTestModel, createTestOperation, createTestRange } from '../utils' -import { splitOperationsForLockedCode } from '../../tools/utils/editorOperationUtils' +import { tryIgnoreLockedCodeForOperations } from '../../tools/utils/editorOperationUtils' describe('Locked code', () => { test('Edit editable range', async () => { @@ -9,7 +9,7 @@ describe('Locked code', () => { const operationRange = createTestRange(model, 8, 9) const operation = createTestOperation(operationRange, ' return 42;') - const splitOperations = splitOperationsForLockedCode(model, [operation], uneditableRanges) + const splitOperations = tryIgnoreLockedCodeForOperations(model, [operation], uneditableRanges) expect(splitOperations.length).toEqual(1) expect(splitOperations[0]).toEqual(createTestOperation(operationRange, ' return 42;')) @@ -22,7 +22,7 @@ describe('Locked code', () => { const operationRange = createTestRange(model, 4, 4) const operation = createTestOperation(operationRange, '// tata') - const splitOperations = splitOperationsForLockedCode(model, [operation], uneditableRanges) + const splitOperations = tryIgnoreLockedCodeForOperations(model, [operation], uneditableRanges) expect(splitOperations.length).toEqual(1) expect(splitOperations[0]).toEqual(createTestOperation(operationRange, '// tata')) @@ -56,7 +56,7 @@ function findLargest(numbers: number[]): number { /* Ignore and do not change the code above */ // last comment`) - const splitOperations = splitOperationsForLockedCode(model, [operation], uneditableRanges) + const splitOperations = tryIgnoreLockedCodeForOperations(model, [operation], uneditableRanges) expect(splitOperations.length).toEqual(4) expect(splitOperations[0]).toEqual( @@ -92,7 +92,7 @@ function findLargest(numbers: number[]): number { // function return 42; }`) - const splitOperations = splitOperationsForLockedCode(model, [operation], uneditableRanges) + const splitOperations = tryIgnoreLockedCodeForOperations(model, [operation], uneditableRanges) expect(splitOperations.length).toEqual(1) expect(splitOperations[0]).toEqual(createTestOperation(fullModelRange, `function findLargest(numbers: number[]): number { @@ -112,7 +112,7 @@ function findLargest(numbers: number[]): number { /* Ignore and do not change the code below */ // toto`) - const splitOperations = splitOperationsForLockedCode(model, [operation], uneditableRanges) + const splitOperations = tryIgnoreLockedCodeForOperations(model, [operation], uneditableRanges) expect(splitOperations.length).toEqual(1) expect(splitOperations[0]).toEqual( @@ -138,7 +138,7 @@ function findLargest(numbers: number[]): number { /* Ignore and do not change the code above */ // other comment`) - const splitOperations = splitOperationsForLockedCode(model, [operation], uneditableRanges) + const splitOperations = tryIgnoreLockedCodeForOperations(model, [operation], uneditableRanges) expect(splitOperations.length).toEqual(2) expect(splitOperations[0]).toEqual( @@ -158,7 +158,7 @@ function findLargest(numbers: number[]): number { const firstOperation = createTestOperation(firstRange, ' // first comment\n return 42;', { major: 0, minor: 0 }) const secondRange = createTestRange(model, 16, 16) const secondOperation = createTestOperation(secondRange, '// second operation comment', { major: 1, minor: 0 }) - const splitOperations = splitOperationsForLockedCode(model, [firstOperation, secondOperation], uneditableRanges) + const splitOperations = tryIgnoreLockedCodeForOperations(model, [firstOperation, secondOperation], uneditableRanges) expect(splitOperations.length).toEqual(2) expect(splitOperations[0]).toEqual( @@ -178,7 +178,7 @@ function findLargest(numbers: number[]): number { const firstOperation = createTestOperation(firstRange, ' // function comment\n return 42;', { major: 0, minor: 0 }) const secondRange = createTestRange(model, 13, 13) const secondOperation = createTestOperation(secondRange, '// uneditable comment', { major: 1, minor: 0 }) - const splitOperations = splitOperationsForLockedCode(model, [firstOperation, secondOperation], uneditableRanges) + const splitOperations = tryIgnoreLockedCodeForOperations(model, [firstOperation, secondOperation], uneditableRanges) expect(splitOperations.length).toEqual(2) expect(splitOperations[0]).toEqual( diff --git a/src/tools/utils/editorOperationUtils.ts b/src/tools/utils/editorOperationUtils.ts index e843df8..3e40dc9 100644 --- a/src/tools/utils/editorOperationUtils.ts +++ b/src/tools/utils/editorOperationUtils.ts @@ -41,34 +41,34 @@ function createNewOperationsFromRanges ( return splitText.map((text, index) => createNewOperation(oldOperation, editableRanges[index]!, text, index)) } -function splitOperationText ( +function tryIgnoreLockedCodeTextForOperation ( model: monaco.editor.ITextModel, - uneditableRanges: monaco.Range[], - text: string | null + uneditableRangesInOperationRange: monaco.Range[], + operationText: string | null ): (string | null)[] { - if (text == null || text === '') { - if (uneditableRanges.length > 0) { + if (operationText == null || operationText === '') { + if (uneditableRangesInOperationRange.length > 0) { throw new LockedCodeError('Cannot delete locked code sections') } return [operationText] } const splitText: string[] = [] - const uneditableRangesText = uneditableRanges.map(range => model.getValueInRange(range)) + const uneditableRangesText = uneditableRangesInOperationRange.map(range => model.getValueInRange(range)) let currentRange: number = 0 - let textToSplit: string = text - while (textToSplit !== '' && currentRange < uneditableRangesText.length) { + let remainingText: string = operationText + while (remainingText.length > 0 && currentRange < uneditableRangesText.length) { const rangeText = uneditableRangesText[currentRange] if (rangeText != null && rangeText !== '') { - const rangeTextIndex = textToSplit.indexOf(rangeText) + const rangeTextIndex = remainingText.indexOf(rangeText) if (rangeTextIndex === -1) { throw new LockedCodeError('Cannot edit locked code sections') } - const currentUneditableRange = uneditableRanges[currentRange]! + const currentUneditableRange = uneditableRangesInOperationRange[currentRange]! if (rangeTextIndex !== 0) { - let textToKeep = textToSplit.slice(0, rangeTextIndex) + let textToKeep = remainingText.slice(0, rangeTextIndex) if (textToKeep.endsWith('\n') && currentUneditableRange.startColumn === 1) { textToKeep = textToKeep.slice(0, textToKeep.length - 1) } @@ -76,21 +76,21 @@ function splitOperationText ( } const uneditableRangeMaxEndColumn = model.getLineMaxColumn(currentUneditableRange.endLineNumber) - textToSplit = textToSplit.slice(rangeTextIndex + rangeText.length) - if (textToSplit.startsWith('\n') && currentUneditableRange.endColumn === uneditableRangeMaxEndColumn) { - textToSplit = textToSplit.slice(1, textToSplit.length) + remainingText = remainingText.slice(rangeTextIndex + rangeText.length) + if (remainingText.startsWith('\n') && currentUneditableRange.endColumn === uneditableRangeMaxEndColumn) { + remainingText = remainingText.slice(1, remainingText.length) } } currentRange++ } - if (textToSplit !== '') { - splitText.push(textToSplit.endsWith('\n') ? textToSplit.slice(0, textToSplit.length - 1) : textToSplit) + if (remainingText !== '') { + splitText.push(remainingText.endsWith('\n') ? remainingText.slice(0, remainingText.length - 1) : remainingText) } return splitText } -export function splitOperationsForLockedCode ( +export function tryIgnoreLockedCodeForOperations ( model: monaco.editor.ITextModel, operations: ValidAnnotatedEditOperation[], uneditableRanges: monaco.Range[] @@ -101,7 +101,7 @@ export function splitOperationsForLockedCode ( firstRanges: operationEditableRanges, secondRanges: operationUneditableRanges } = minusRanges(model, operation.range, uneditableRanges) - const splitText = splitOperationText(model, operationUneditableRanges, operation.text) + const splitText = tryIgnoreLockedCodeTextForOperation(model, operationUneditableRanges, operation.text) newOperations = [ ...newOperations, ...createNewOperationsFromRanges(operation, operationEditableRanges, splitText) @@ -121,5 +121,5 @@ export function tryIgnoreLockedCode ( return editorOperations } - return splitOperationsForLockedCode(model, editorOperations, uneditableRanges) + return tryIgnoreLockedCodeForOperations(model, editorOperations, uneditableRanges) }