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 hide code #102

Merged
merged 2 commits into from
Feb 21, 2024
Merged
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
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
Loading