From 8ba06e8dfe5d71f4f6ef9ed623755e89f8fd53ea Mon Sep 17 00:00:00 2001 From: Kai Saba Date: Thu, 12 Dec 2024 10:57:51 +0100 Subject: [PATCH] fix: fix operation split and operation text split --- src/tools.ts | 7 ++-- src/tools/utils/editorOperationUtils.ts | 56 ++++++++++--------------- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/tools.ts b/src/tools.ts index 6a7337d..cb585d8 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -90,10 +90,11 @@ function lockCodeUsingDecoration ( } function canEditRange (range: monaco.IRange) { - if (editor.getModel() == null) { + const model = editor.getModel() + if (model == null) { return false } - const ranges = getRangesFromDecorations(editor, decorationFilter) + const ranges = getRangesFromDecorations(model, decorationFilter) if (ranges.length === 0) { return true } @@ -140,7 +141,7 @@ function lockCodeUsingDecoration ( return editorOperations } - editorOperations = computeNewOperationsForLockedCode(editor, decorationFilter, editorOperations, withDecoration) + editorOperations = computeNewOperationsForLockedCode(model, decorationFilter, editorOperations, withDecoration) if (transactionMode) { const firstForbiddenOperation = editorOperations.find(operation => !canEditRange(operation.range)) if (firstForbiddenOperation != null) { diff --git a/src/tools/utils/editorOperationUtils.ts b/src/tools/utils/editorOperationUtils.ts index 84d3060..3ae768a 100644 --- a/src/tools/utils/editorOperationUtils.ts +++ b/src/tools/utils/editorOperationUtils.ts @@ -26,11 +26,7 @@ function createNewOperationsFromRanges ( editableRanges: monaco.Range[], splitText: (string | null)[] ): ValidAnnotatedEditOperation[] { - if (editableRanges.length <= 0) { - return [] - } - - if (splitText.length === 1) { + if (editableRanges.length <= 0 || splitText.length <= 0) { return [oldOperation] } @@ -38,17 +34,12 @@ function createNewOperationsFromRanges ( } function splitOperationText ( - editor: monaco.editor.ICodeEditor, + model: monaco.editor.ITextModel, uneditableRanges: monaco.Range[], text: string | null ): (string | null)[] { if (text == null || text === '') { - return [text] - } - - const model = editor.getModel() - if (model == null) { - return [] + return uneditableRanges.length > 0 ? [] : [text] } const splitText: string[] = [] @@ -59,22 +50,24 @@ function splitOperationText ( const rangeText = uneditableRangesText[currentRange] if (rangeText != null && rangeText !== '') { const rangeTextIndex = textToSplit.indexOf(rangeText) - if (rangeTextIndex !== -1) { - const currentUneditableRange = uneditableRanges[currentRange]! + + if (rangeTextIndex === -1) { + return [] + } + + const currentUneditableRange = uneditableRanges[currentRange]! + if (rangeTextIndex !== 0) { let textToKeep = textToSplit.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) - } - } else { - splitText.push(textToSplit) - textToSplit = '' + 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) } } currentRange++ @@ -86,8 +79,8 @@ function splitOperationText ( return splitText } -function splitOperationsForLockedCode ( - editor: monaco.editor.ICodeEditor, +export function splitOperationsForLockedCode ( + model: monaco.editor.ITextModel, operations: ValidAnnotatedEditOperation[], uneditableRanges: monaco.Range[] ): ValidAnnotatedEditOperation[] { @@ -96,8 +89,8 @@ function splitOperationsForLockedCode ( const { firstRanges: operationEditableRanges, secondRanges: operationUneditableRanges - } = minusRanges(editor, operation.range, uneditableRanges) - const splitText = splitOperationText(editor, operationUneditableRanges, operation.text) + } = minusRanges(model, operation.range, uneditableRanges) + const splitText = splitOperationText(model, operationUneditableRanges, operation.text) newOperations = [ ...newOperations, ...createNewOperationsFromRanges(operation, operationEditableRanges, splitText) @@ -107,20 +100,15 @@ function splitOperationsForLockedCode ( } export function computeNewOperationsForLockedCode ( - editor: monaco.editor.ICodeEditor, + model: monaco.editor.ITextModel, decorationFilter: (decoration: monaco.editor.IModelDecoration) => boolean, editorOperations: ValidAnnotatedEditOperation[], withDecoration: boolean ): ValidAnnotatedEditOperation[] { - const model = editor.getModel() - if (model == null) { - return [] - } - - const uneditableRanges = getLockedRanges(editor, decorationFilter, withDecoration) + const uneditableRanges = getLockedRanges(model, decorationFilter, withDecoration) if (uneditableRanges.length <= 0) { return editorOperations } - return splitOperationsForLockedCode(editor, editorOperations, uneditableRanges) + return splitOperationsForLockedCode(model, editorOperations, uneditableRanges) }