Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix whole editor paste with locked code #125

Merged
merged 31 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f2e327c
fix: select first editable range when pasting for locked code with de…
KaiSaba Dec 5, 2024
f538ee0
fix: recompute editor operations for locking code using decorations
KaiSaba Dec 6, 2024
04bfcfb
cleanup: remove override of editor paste
KaiSaba Dec 6, 2024
c40f3c1
fix: increment uneditable range index correctly when computing new op…
KaiSaba Dec 6, 2024
7ee4792
refactor: use position in minUsRange instead of lines and columns
KaiSaba Dec 6, 2024
b598071
refactor: split compute of new operations in multiple functions
KaiSaba Dec 6, 2024
8faaeb6
refactor: move range and operations functions in separate files
KaiSaba Dec 9, 2024
c291e16
refactor: split editable ranges and operation text separately
KaiSaba Dec 9, 2024
8412173
fix: allow paste when selected locked code doesn't change
KaiSaba Dec 9, 2024
e47df09
fix: use string indexOf instead of split to split text at the first o…
KaiSaba Dec 9, 2024
f8cea32
refactor: refactor minusRanges function to return all ranges
KaiSaba Dec 11, 2024
f8c1ed4
fix: use minusRanges to get hidden areas and visible ranges in hideCo…
KaiSaba Dec 11, 2024
0c5ae90
fix: fix split operation text function when uneditable range text not…
KaiSaba Dec 11, 2024
cf76aee
fix: fix locked code ranges and text split
KaiSaba Dec 11, 2024
8c76d0f
fix: fix ranges computation
KaiSaba Dec 12, 2024
8ba06e8
fix: fix operation split and operation text split
KaiSaba Dec 12, 2024
acd5928
test: add jest config and tests on locked code
KaiSaba Dec 12, 2024
8ec2433
test: simplify createTestRange function
KaiSaba Dec 12, 2024
0759eb0
refactor: rename computeNewOperationsForLockedCode to tryIgnoreLocked…
KaiSaba Dec 12, 2024
bbf980c
fix: throw custom error when operation is invalid with locked code
KaiSaba Dec 12, 2024
4211c36
refactor: rename functions on locked code and their variables
KaiSaba Dec 12, 2024
59ba25d
cleanup: remove unecessary configs in tsconfig
KaiSaba Dec 12, 2024
896f2d2
refactor: rename minusRange function and return values
KaiSaba Dec 12, 2024
7c5a675
cleanup: remove unneeded console.info
KaiSaba Dec 12, 2024
b749674
fix: removed line bug when pasting code
KaiSaba Dec 12, 2024
b450e74
fix: remove pollution logs in tests
Dec 12, 2024
6308574
refactor: change code so it's easier to test
Dec 12, 2024
cb5a8a4
refactor: extract error handling to facilitate tests
Dec 12, 2024
36c4ef1
lib: install @jest/globals
Dec 12, 2024
28be49e
test: update test by mocking code lock and edit operations
KaiSaba Dec 12, 2024
5cfb9f1
cleanup: remove unecessary constructor for LockedCodeError
KaiSaba Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ function getRangesFromDecorations (
.map((decoration) => decoration.range)
}

function getEditableRange (fullModelRange: monaco.Range, ranges: monaco.Range[], withDecoration: boolean): monaco.Range | undefined {
if (ranges.length <= 0) {
return undefined
}

if (!withDecoration) {
return ranges[ranges.length - 1]
}

const firstUneditableRange = ranges[0]
return firstUneditableRange != null
? new monaco.Range(fullModelRange.startLineNumber, fullModelRange.startColumn, firstUneditableRange.startLineNumber, firstUneditableRange.startColumn)
: undefined
}
CGNonofr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Exctract ranges between startToken and endToken
*/
Expand Down Expand Up @@ -129,27 +144,26 @@ function lockCodeUsingDecoration (
const originalTrigger = editor.trigger
editor.trigger = function (source, handlerId, payload) {
// Try to transform whole file pasting into a paste in the editable area only
const model = editor.getModel()!
const fullModelRange = model.getFullModelRange()
const ranges = getRangesFromDecorations(editor, decorationFilter)
const lastEditableRange =
ranges.length > 0 ? ranges[ranges.length - 1] : undefined
if (isPasteAction(handlerId, payload) && lastEditableRange != null) {
const editableRange = getEditableRange(fullModelRange, ranges, withDecoration)
if (isPasteAction(handlerId, payload) && editableRange != null) {
const selections = editor.getSelections()
const model = editor.getModel()!
if (selections != null && selections.length === 1) {
const selection = selections[0]!
const fullModelRange = model.getFullModelRange()
const wholeFileSelected = fullModelRange.equalsRange(selection)
if (wholeFileSelected) {
const currentEditorValue = editor.getValue()
const before = model.getOffsetAt(lastEditableRange.getStartPosition())
const before = model.getOffsetAt(editableRange.getStartPosition())
const after =
currentEditorValue.length - model.getOffsetAt(lastEditableRange.getEndPosition())
currentEditorValue.length - model.getOffsetAt(editableRange.getEndPosition())
if (
currentEditorValue.slice(0, before) === payload.text.slice(0, before) &&
currentEditorValue.slice(currentEditorValue.length - after) ===
payload.text.slice(payload.text.length - after)
) {
editor.setSelection(lastEditableRange)
editor.setSelection(editableRange)
const newPayload: PastePayload = {
...payload,
text: payload.text.slice(before, payload.text.length - after)
Expand Down
Loading