Skip to content

Commit

Permalink
fix: make tools return Disposables
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Feb 5, 2024
1 parent ef097eb commit 8f4e492
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export function lockCodeWithoutDecoration (
decorations: string[],
allowChangeFromSources: string[] = [],
errorMessage?: string
): () => void {
): monaco.IDisposable {
const disposableStore = new DisposableStore()
function displayLockedCodeError (position: monaco.Position) {
if (errorMessage == null) {
return
Expand Down Expand Up @@ -124,38 +125,42 @@ export function lockCodeWithoutDecoration (
model.applyEdits = originalApplyEdit as typeof model.applyEdits
}
}
const editorChangeModelDisposable = editor.onDidChangeModel(lockModel)
disposableStore.add(editor.onDidChangeModel(lockModel))
lockModel()

// Handle selection of the last line of an editable range
const selectionDisposable = editor.onDidChangeCursorSelection(e => {
if (canEditRange(e.selection)) {
return
}
const model = editor.getModel()
if (model == null) {
return
}
const shiftedRange = monaco.Range.fromPositions(
model.getPositionAt(model.getOffsetAt(e.selection.getStartPosition()) - 1),
model.getPositionAt(model.getOffsetAt(e.selection.getEndPosition()) - 1)
)
if (canEditRange(shiftedRange)) {
editor.setSelection(shiftedRange)
disposableStore.add(
editor.onDidChangeCursorSelection((e) => {
if (canEditRange(e.selection)) {
return
}
const model = editor.getModel()
if (model == null) {
return
}
const shiftedRange = monaco.Range.fromPositions(
model.getPositionAt(model.getOffsetAt(e.selection.getStartPosition()) - 1),
model.getPositionAt(model.getOffsetAt(e.selection.getEndPosition()) - 1)
)
if (canEditRange(shiftedRange)) {
editor.setSelection(shiftedRange)
}
})
)

disposableStore.add({
dispose () {
restoreModelApplyEdit()
editor.executeEdits = originalExecuteEdit
editor.executeCommands = originalExecuteCommands
editor.trigger = originalTrigger
}
})

return () => {
selectionDisposable.dispose()
restoreModelApplyEdit()
editorChangeModelDisposable.dispose()
editor.executeEdits = originalExecuteEdit
editor.executeCommands = originalExecuteCommands
editor.trigger = originalTrigger
}
return disposableStore
}

export function hideCodeWithoutDecoration (editor: monaco.editor.IStandaloneCodeEditor, decorations: string[]): () => void {
export function hideCodeWithoutDecoration (editor: monaco.editor.IStandaloneCodeEditor, decorations: string[]): monaco.IDisposable {
let otherHiddenAreas: monaco.IRange[] = editor._getViewModel()?.getHiddenAreas() ?? []
function getHiddenAreas () {
const model = editor.getModel()!
Expand Down Expand Up @@ -212,12 +217,17 @@ export function hideCodeWithoutDecoration (editor: monaco.editor.IStandaloneCode
updateHiddenAreas()
}

const disposableStore = new DisposableStore()
updateHiddenAreas()

return () => {
editor.setHiddenAreas = originalSetHiddenAreas
editor.setHiddenAreas(otherHiddenAreas)
}
disposableStore.add({
dispose () {
editor.setHiddenAreas = originalSetHiddenAreas
editor.setHiddenAreas(otherHiddenAreas)
}
})

return disposableStore
}

/**
Expand Down

0 comments on commit 8f4e492

Please sign in to comment.