Skip to content

Commit

Permalink
Merge pull request #114 from CodinGame/fix-hide-code-selection
Browse files Browse the repository at this point in the history
Fix hide code selection
  • Loading branch information
CGNonofr authored May 7, 2024
2 parents d10363c + 8074914 commit 20c4474
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 0 additions & 4 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ export function isInitialized (): boolean {
}

export async function initialize (constructionOptions: IWorkbenchConstructionOptions = {}, container?: HTMLElement): Promise<void> {
if (typeof process !== 'undefined') {
console.warn('`process` detected. It may have negative impacts on VSCode behavior')
}

if (constructionOptions.workspaceProvider == null) {
constructionOptions = {
...constructionOptions,
Expand Down
17 changes: 17 additions & 0 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ export function hideCodeWithoutDecoration (editor: monaco.editor.ICodeEditor, de
hiddenAreas.push(monaco.Range.fromPositions(position, model.getFullModelRange().getEndPosition()))

editor.setHiddenAreas(hiddenAreas, hideId)

// Make sure only visible code is selected
const selections = editor.getSelections()
if (selections != null) {
const visibleRanges = editor._getViewModel()!.getModelVisibleRanges()
let newSelections = selections.flatMap(selection =>
visibleRanges.map(visibleRange => selection.intersectRanges(visibleRange))
.filter((range): range is monaco.Range => range != null)
.map(range => monaco.Selection.fromRange(range, selection.getDirection()))
)
if (newSelections.length === 0 && visibleRanges.length > 0) {
newSelections = [monaco.Selection.fromPositions(visibleRanges[0]!.getStartPosition())]
}
if (newSelections.length > 0) {
editor.setSelections(newSelections)
}
}
}

const disposableStore = new DisposableStore()
Expand Down
5 changes: 3 additions & 2 deletions src/types/monaco.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IRange } from 'monaco-editor'
import { IRange, Range } from 'monaco-editor'

declare module 'monaco-editor' {
namespace editor {
Expand All @@ -8,7 +8,8 @@ declare module 'monaco-editor' {
setHiddenAreas(ranges: IRange[], source?: unknown): void

_getViewModel(): {
getHiddenAreas(): IRange[]
getHiddenAreas(): Range[]
getModelVisibleRanges(): Range[]
} | undefined
}
}
Expand Down

0 comments on commit 20c4474

Please sign in to comment.