Skip to content

Commit

Permalink
refactor: rename minusRange function and return values
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSaba committed Dec 12, 2024
1 parent 59ba25d commit 896f2d2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as monaco from 'monaco-editor'
import { DisposableStore } from 'vscode/monaco'
import { IIdentifiedSingleEditOperation, ValidAnnotatedEditOperation } from 'vscode/vscode/vs/editor/common/model'
import { getRangesFromDecorations, minusRanges } from './tools/utils/rangeUtils'
import { getRangesFromDecorations, excludeRanges } from './tools/utils/rangeUtils'
import { LockedCodeError, tryIgnoreLockedCode } from './tools/utils/editorOperationUtils'

/**
Expand Down Expand Up @@ -266,9 +266,9 @@ export function hideCodeWithoutDecoration (editor: monaco.editor.ICodeEditor, de
}, [])

const {
firstRanges: hiddenAreas,
secondRanges: visibleRanges
} = minusRanges(model, model.getFullModelRange(), ranges)
filteredRanges: hiddenAreas,
removedRanges: visibleRanges
} = excludeRanges(model, model.getFullModelRange(), ranges)

editor.setHiddenAreas(hiddenAreas, hideId)

Expand Down
8 changes: 4 additions & 4 deletions src/tools/utils/editorOperationUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as monaco from 'monaco-editor'
import { ValidAnnotatedEditOperation } from 'vscode/vscode/vs/editor/common/model'
import { getLockedRanges, minusRanges } from './rangeUtils'
import { getLockedRanges, excludeRanges } from './rangeUtils'

export class LockedCodeError extends Error {
constructor (message: string) {
Expand Down Expand Up @@ -98,9 +98,9 @@ export function tryIgnoreLockedCodeForOperations (
let newOperations: ValidAnnotatedEditOperation[] = []
for (const operation of operations) {
const {
firstRanges: operationEditableRanges,
secondRanges: operationUneditableRanges
} = minusRanges(model, operation.range, uneditableRanges)
filteredRanges: operationEditableRanges,
removedRanges: operationUneditableRanges
} = excludeRanges(model, operation.range, uneditableRanges)
const splitText = tryIgnoreLockedCodeTextForOperation(model, operationUneditableRanges, operation.text)
newOperations = [
...newOperations,
Expand Down
20 changes: 10 additions & 10 deletions src/tools/utils/rangeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export function getRangesFromDecorations (
.map((decoration) => decoration.range)
}

export function minusRanges (
export function excludeRanges (
model: monaco.editor.ITextModel,
uniqueRange: monaco.Range,
ranges: monaco.Range[]
): {
firstRanges: monaco.Range[]
secondRanges: monaco.Range[]
filteredRanges: monaco.Range[]
removedRanges: monaco.Range[]
} {
const firstRanges: monaco.Range[] = []
const secondRanges: monaco.Range[] = []
const filteredRanges: monaco.Range[] = []
const removedRanges: monaco.Range[] = []
let lastEndPosition = uniqueRange.getStartPosition()
const uniqueRangeEndPosition = uniqueRange.getEndPosition()
const intersectingRanges = ranges.filter(range => monaco.Range.areIntersectingOrTouching(range, uniqueRange))
Expand All @@ -32,15 +32,15 @@ export function minusRanges (
const firstRangeStart = lastEndPosition.equals(uniqueRange.getStartPosition())
? lastEndPosition
: model.modifyPosition(lastEndPosition, 1)
firstRanges.push(monaco.Range.fromPositions(
filteredRanges.push(monaco.Range.fromPositions(
firstRangeStart,
(model.modifyPosition(rangeStart, -1))
))
}

const secondRangeStart = lastEndPosition.isBefore(rangeStart) ? rangeStart : lastEndPosition
const secondRangeEnd = uniqueRangeEndPosition.isBefore(rangeEnd) ? uniqueRangeEndPosition : rangeEnd
secondRanges.push(monaco.Range.fromPositions(secondRangeStart, secondRangeEnd))
removedRanges.push(monaco.Range.fromPositions(secondRangeStart, secondRangeEnd))

lastEndPosition = rangeEnd
}
Expand All @@ -49,10 +49,10 @@ export function minusRanges (
const firstRangeStart = lastEndPosition.equals(uniqueRange.getStartPosition())
? lastEndPosition
: model.modifyPosition(lastEndPosition, 1)
firstRanges.push(monaco.Range.fromPositions(firstRangeStart, uniqueRangeEndPosition))
filteredRanges.push(monaco.Range.fromPositions(firstRangeStart, uniqueRangeEndPosition))
}

return { firstRanges, secondRanges }
return { filteredRanges, removedRanges }
}

export function getLockedRanges (
Expand All @@ -62,6 +62,6 @@ export function getLockedRanges (
): monaco.Range[] {
const fullModelRange = model.getFullModelRange()
const ranges = getRangesFromDecorations(model, decorationFilter)
const { firstRanges, secondRanges } = minusRanges(model, fullModelRange, ranges)
const { filteredRanges: firstRanges, removedRanges: secondRanges } = excludeRanges(model, fullModelRange, ranges)
return withDecoration ? secondRanges : firstRanges
}

0 comments on commit 896f2d2

Please sign in to comment.