Skip to content

Commit

Permalink
Merge pull request #102 from CodinGame/fix-hide-code
Browse files Browse the repository at this point in the history
Fix hide code
  • Loading branch information
CGNonofr authored Feb 21, 2024
2 parents d1e65f3 + 5059bd6 commit d5493e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
12 changes: 9 additions & 3 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,23 @@ ${files.map((_, index) => ` whenReady${index}()`).join(',\n')}
breakpoints,
taskDefinitions,
viewsWelcome,
terminal,
viewsContainers,
...remainingContribute
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} = manifest.contributes as any
} = manifest.contributes ?? {}

const {
activationEvents,
devDependencies,
dependencies,
scripts,
browser,
main,
l10n,
extensionDependencies, // for pure-d that requires hbenl.vscode-test-explorer
...remainingManifest
} = manifest
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} = manifest as any

return {
...remainingManifest,
Expand Down
39 changes: 11 additions & 28 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,20 @@ export function lockCodeWithoutDecoration (
return disposableStore
}

let hideCodeWithoutDecorationCounter = 0
export function hideCodeWithoutDecoration (editor: monaco.editor.ICodeEditor, decorationFilter: (decoration: monaco.editor.IModelDecoration) => boolean): monaco.IDisposable {
let otherHiddenAreas: monaco.IRange[] = editor._getViewModel()?.getHiddenAreas() ?? []
function getHiddenAreas () {
const hideId = `hideCodeWithoutDecoration:${hideCodeWithoutDecorationCounter++}`

function updateHiddenAreas (): void {
const model = editor.getModel()
if (model == null) {
return []
return
}

const decorations = model.getAllDecorations()
.filter(decorationFilter)
if (decorations.length === 0) {
return otherHiddenAreas
return
}

const ranges = decorations
Expand All @@ -218,40 +220,22 @@ export function hideCodeWithoutDecoration (editor: monaco.editor.ICodeEditor, de
}
}, [])

let hiddenAreas: monaco.IRange[] = [...otherHiddenAreas]
const hiddenAreas: monaco.IRange[] = []
let position = new monaco.Position(1, 1)
for (const range of ranges) {
const startPosition = model.modifyPosition(range.getStartPosition(), -1)
const endPosition = model.modifyPosition(range.getEndPosition(), 1)
hiddenAreas = [
...hiddenAreas,
monaco.Range.fromPositions(position, startPosition)
]
hiddenAreas.push(monaco.Range.fromPositions(position, startPosition))
position = endPosition
}
hiddenAreas = [
...hiddenAreas,
monaco.Range.fromPositions(position, model.getFullModelRange().getEndPosition())
]

return hiddenAreas
}

const originalSetHiddenAreas = editor.setHiddenAreas.bind(editor)
function updateHiddenAreas () {
originalSetHiddenAreas(getHiddenAreas())
}
hiddenAreas.push(monaco.Range.fromPositions(position, model.getFullModelRange().getEndPosition()))

// Hack to make it work with the folding service calling setHiddenAreas with its own areas
editor.setHiddenAreas = (ranges) => {
otherHiddenAreas = ranges
updateHiddenAreas()
editor.setHiddenAreas(hiddenAreas, hideId)
}

const disposableStore = new DisposableStore()

disposableStore.add(editor.onDidChangeModel(() => {
otherHiddenAreas = editor._getViewModel()?.getHiddenAreas() ?? []
updateHiddenAreas()
}))
disposableStore.add(editor.onDidChangeModelDecorations(() => {
Expand All @@ -261,8 +245,7 @@ export function hideCodeWithoutDecoration (editor: monaco.editor.ICodeEditor, de

disposableStore.add({
dispose () {
editor.setHiddenAreas = originalSetHiddenAreas
editor.setHiddenAreas(otherHiddenAreas)
editor.setHiddenAreas([], hideId)
}
})

Expand Down
2 changes: 1 addition & 1 deletion src/types/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare module 'monaco-editor' {
interface ICodeEditor {
// This method is internal and is supposed to be used by the folding feature
// We still use it to hide parts of the code in the `hideCodeWithoutDecoration` function
setHiddenAreas(ranges: IRange[]): void
setHiddenAreas(ranges: IRange[], source?: unknown): void

_getViewModel(): {
getHiddenAreas(): IRange[]
Expand Down

0 comments on commit d5493e4

Please sign in to comment.