Skip to content

Commit

Permalink
refactor: extract error handling to facilitate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Dec 12, 2024
1 parent 6308574 commit cb5a8a4
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ export function extractRangesFromTokens (editor: monaco.editor.ICodeEditor, star
return []
}

type ErrorHandler = (editor: monaco.editor.ICodeEditor, firstForbiddenOperation: ValidAnnotatedEditOperation) => void

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())
}
}

export interface LockCodeOptions {
/**
* Error message displayed in a tooltip when an edit failed
*/
errorMessage?: string
onError?: ErrorHandler
/**
* Allows edit coming from a specific source
*/
allowChangeFromSources: string[]
allowChangeFromSources?: string[]
/**
* Locked ranges
*/
Expand All @@ -67,22 +77,14 @@ export interface LockCodeOptions {
export function lockCodeRanges (
editor: monaco.editor.ICodeEditor,
{
errorMessage,
onError,
allowChangeFromSources = [],
getLockedRanges = () => [],
transactionMode = true,
allowUndoRedo = true
}: LockCodeOptions
): monaco.IDisposable {
const disposableStore = new DisposableStore()
function displayLockedCodeError (position: monaco.Position) {
if (errorMessage == null) {
return
}
const messageContribution = editor.getContribution('editor.contrib.messageController')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(messageContribution as any).showMessage(errorMessage, position)
}

function canEditRange (range: monaco.IRange) {
const model = editor.getModel()
Expand Down Expand Up @@ -149,17 +151,15 @@ export function lockCodeRanges (
if (transactionMode) {
const firstForbiddenOperation = editorOperations.find(operation => !canEditRange(operation.range))
if (firstForbiddenOperation != null) {
displayLockedCodeError(
new monaco.Position(firstForbiddenOperation.range.startLineNumber, firstForbiddenOperation.range.startColumn))
onError?.(editor, firstForbiddenOperation)
return []
} else {
return editorOperations
}
} else {
return editorOperations.filter(operation => {
if (!canEditRange(operation.range)) {
displayLockedCodeError(
new monaco.Position(operation.range.startLineNumber, operation.range.startColumn))
onError?.(editor, operation)
return false
}
return true
Expand Down Expand Up @@ -212,7 +212,7 @@ export interface LockCodeFromDecorationOptions {
/**
* Allows edit coming from a specific source
*/
allowChangeFromSources: string[]
allowChangeFromSources?: string[]
/**
* Only take some decorations into account
*/
Expand All @@ -232,7 +232,7 @@ export function lockCodeWithDecoration (
lockOptions: LockCodeFromDecorationOptions
): monaco.IDisposable {
return lockCodeRanges(editor, {
errorMessage: lockOptions.errorMessage,
onError: lockOptions.errorMessage != null ? generateErrorMessageErrorHandler(lockOptions.errorMessage) : undefined,
allowChangeFromSources: lockOptions.allowChangeFromSources,
getLockedRanges () {
const model = editor.getModel()
Expand All @@ -251,7 +251,7 @@ export function lockCodeWithoutDecoration (
lockOptions: LockCodeFromDecorationOptions
): monaco.IDisposable {
return lockCodeRanges(editor, {
errorMessage: lockOptions.errorMessage,
onError: lockOptions.errorMessage != null ? generateErrorMessageErrorHandler(lockOptions.errorMessage) : undefined,
allowChangeFromSources: lockOptions.allowChangeFromSources,
getLockedRanges () {
const model = editor.getModel()
Expand Down

0 comments on commit cb5a8a4

Please sign in to comment.