Skip to content

Commit

Permalink
refactor: rename functions on locked code and their variables
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSaba committed Dec 12, 2024
1 parent bbf980c commit 4211c36
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
18 changes: 9 additions & 9 deletions src/tests/editorOperationUtils/lockedCode.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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;'))
Expand All @@ -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'))
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
38 changes: 19 additions & 19 deletions src/tools/utils/editorOperationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,56 +41,56 @@ 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)
}
splitText.push(textToKeep)
}

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[]
Expand All @@ -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)
Expand All @@ -121,5 +121,5 @@ export function tryIgnoreLockedCode (
return editorOperations
}

return splitOperationsForLockedCode(model, editorOperations, uneditableRanges)
return tryIgnoreLockedCodeForOperations(model, editorOperations, uneditableRanges)
}

0 comments on commit 4211c36

Please sign in to comment.