Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
67 changes: 66 additions & 1 deletion src/tests/editorOperationUtils/lockedCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
createTestOperation,
createTestRange
} from '../utils'
import { lockCodeRanges } from '../../tools'
import { lockCodeRanges, lockCodeWithDecoration } from '../../tools'
import { initialize } from '../../services'

beforeAll(async () => {
Expand Down Expand Up @@ -427,6 +427,71 @@ function findLargest(numbers: number[]): number {
})
})

test('Throwing onError still rejects the edit without failing executeEdits', () => {
const model = createDefaultTestModel()
disposableStore.add(model)
const editor = monaco.editor.create(document.createElement('div'), {
model
})
disposableStore.add(editor)
disposableStore.add(
lockCodeRanges(editor, {
getLockedRanges() {
return createDefaultTestLockedCodeRanges(model)
},
onError() {
throw new Error('InstantiationService has been disposed')
}
})
)

const operationRange = createTestRange(model, 4, 4)
const operation = createTestOperation(operationRange, '// tata')

const onDidChangeContent = jest.fn()
disposableStore.add(model.onDidChangeContent(onDidChangeContent))

expect(() => editor.executeEdits(null, [operation])).not.toThrow()
expect(onDidChangeContent).not.toHaveBeenCalled()
})

test('Missing message contribution still rejects the locked edit without failing executeEdits', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are we talking about exactly? 🤔

const model = createDefaultTestModel()
disposableStore.add(model)
const editor = monaco.editor.create(document.createElement('div'), {
model
})
disposableStore.add(editor)
const lockedRange = createDefaultTestLockedCodeRanges(model)[0]!
const decorations = editor.createDecorationsCollection([
{
range: lockedRange,
options: { isWholeLine: true }
}
])
disposableStore.add(
lockCodeWithDecoration(editor, {
errorMessage: 'This section is read-only and cannot be edited',
decorationFilter: (decoration) => decorations.has(decoration)
})
)
const originalGetContribution = editor.getContribution.bind(editor)
jest.spyOn(editor, 'getContribution').mockImplementation((id: string) => {
if (id === 'editor.contrib.messageController') {
throw new Error('InstantiationService has been disposed')
}
return originalGetContribution(id)
})

const operation = createTestOperation(lockedRange, '// tata')

const onDidChangeContent = jest.fn()
disposableStore.add(model.onDidChangeContent(onDidChangeContent))

expect(() => editor.executeEdits(null, [operation])).not.toThrow()
expect(onDidChangeContent).not.toHaveBeenCalled()
})

test('Handle all systems line break character', () => {
const model = createDefaultTestModel()
disposableStore.add(model)
Expand Down
31 changes: 26 additions & 5 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,30 @@ type ErrorHandler = (

function generateErrorMessageErrorHandler(errorMessage: string): ErrorHandler {
return (editor, operation) => {
const messageContribution = editor.getContribution('editor.contrib.messageController')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(messageContribution as any).showMessage(errorMessage, operation.range.getStartPosition())
try {
const messageContribution = editor.getContribution('editor.contrib.messageController') as {
showMessage(message: string, position: monaco.IPosition): void
} | null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can even import the proper type from @codingame/monaco-vscode-api/vscode/vs/editor/contrib/message/browser/messageController I guess

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or event do MessageController.get(editor)

messageContribution?.showMessage(errorMessage, operation.range.getStartPosition())
} catch {
// Contributions can be missing or already disposed (e.g. InstantiationService has been
// disposed). Never throw from onError: that aborts pushEditOperations and blocks typing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can it be disposed?

}
}
}

function notifyLockedCodeError(
onError: ErrorHandler | undefined,
editor: monaco.editor.ICodeEditor,
operation: ValidAnnotatedEditOperation
) {
if (onError == null) {
return
}
try {
onError(editor, operation)
} catch (e) {
console.error(e)
}
}

Expand Down Expand Up @@ -174,15 +195,15 @@ export function lockCodeRanges(
(operation) => !canEditRange(operation.range)
)
if (firstForbiddenOperation != null) {
onError?.(editor, firstForbiddenOperation)
notifyLockedCodeError(onError, editor, firstForbiddenOperation)
return []
} else {
return editorOperations
}
} else {
return editorOperations.filter((operation) => {
if (!canEditRange(operation.range)) {
onError?.(editor, operation)
notifyLockedCodeError(onError, editor, operation)
return false
}
return true
Expand Down
Loading