Skip to content

Commit

Permalink
fix: fix ranges computation
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSaba committed Dec 12, 2024
1 parent cf76aee commit 8c76d0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function lockCodeUsingDecoration (
return true
}
return withDecoration
? ranges.every((uneditableRange) => !monaco.Range.areIntersecting(uneditableRange, range))
? ranges.every((uneditableRange) => !monaco.Range.areIntersectingOrTouching(uneditableRange, range))
: ranges.some((editableRange) => editableRange.containsRange(range))
}

Expand Down Expand Up @@ -254,7 +254,7 @@ export function hideCodeWithoutDecoration (editor: monaco.editor.ICodeEditor, de
const {
firstRanges: hiddenAreas,
secondRanges: visibleRanges
} = minusRanges(editor, model.getFullModelRange(), ranges)
} = minusRanges(model, model.getFullModelRange(), ranges)

editor.setHiddenAreas(hiddenAreas, hideId)

Expand Down
28 changes: 10 additions & 18 deletions src/tools/utils/rangeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import * as monaco from 'monaco-editor'

export function getRangesFromDecorations (
editor: monaco.editor.ICodeEditor,
model: monaco.editor.ITextModel,
decorationFilter: (decoration: monaco.editor.IModelDecoration) => boolean
): monaco.Range[] {
const model = editor.getModel()
if (model == null) {
return []
}

return model
.getAllDecorations()
.filter(decorationFilter)
.map((decoration) => decoration.range)
}

export function minusRanges (
editor: monaco.editor.ICodeEditor,
model: monaco.editor.ITextModel,
uniqueRange: monaco.Range,
ranges: monaco.Range[]
): {
firstRanges: monaco.Range[]
secondRanges: monaco.Range[]
} {
const model = editor.getModel()!
const firstRanges: monaco.Range[] = []
const secondRanges: monaco.Range[] = []
let lastEndPosition = uniqueRange.getStartPosition()
const uniqueRangeEndPosition = uniqueRange.getEndPosition()
const intersectingRanges = ranges.filter(range => monaco.Range.areIntersecting(range, uniqueRange))
const intersectingRanges = ranges.filter(range => monaco.Range.areIntersectingOrTouching(range, uniqueRange))

for (const range of intersectingRanges) {
const rangeStart = range.getStartPosition()
Expand All @@ -52,24 +46,22 @@ export function minusRanges (
}

if (lastEndPosition.isBeforeOrEqual(uniqueRangeEndPosition)) {
firstRanges.push(monaco.Range.fromPositions(model.modifyPosition(lastEndPosition, 1), uniqueRangeEndPosition))
const firstRangeStart = lastEndPosition.equals(uniqueRange.getStartPosition())
? lastEndPosition
: model.modifyPosition(lastEndPosition, 1)
firstRanges.push(monaco.Range.fromPositions(firstRangeStart, uniqueRangeEndPosition))
}

return { firstRanges, secondRanges }
}

export function getLockedRanges (
editor: monaco.editor.ICodeEditor,
model: monaco.editor.ITextModel,
decorationFilter: (decoration: monaco.editor.IModelDecoration) => boolean,
withDecoration: boolean
): monaco.Range[] {
const model = editor.getModel()
if (model == null) {
return []
}

const fullModelRange = model.getFullModelRange()
const ranges = getRangesFromDecorations(editor, decorationFilter)
const { firstRanges, secondRanges } = minusRanges(editor, fullModelRange, ranges)
const ranges = getRangesFromDecorations(model, decorationFilter)
const { firstRanges, secondRanges } = minusRanges(model, fullModelRange, ranges)
return withDecoration ? secondRanges : firstRanges
}

0 comments on commit 8c76d0f

Please sign in to comment.