From e54ba06d7bea923a2f50f5ecd156e2609e754b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Tue, 7 May 2024 17:20:43 +0200 Subject: [PATCH 1/2] fix: ensure selection is always in visible code --- src/tools.ts | 17 +++++++++++++++++ src/types/monaco.d.ts | 5 +++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/tools.ts b/src/tools.ts index 72d9609..1797a5a 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -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 newSelection = 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 (newSelection.length === 0 && visibleRanges.length > 0) { + newSelection = [monaco.Selection.fromPositions(visibleRanges[0]!.getStartPosition())] + } + if (newSelection.length > 0) { + editor.setSelections(newSelection) + } + } } const disposableStore = new DisposableStore() diff --git a/src/types/monaco.d.ts b/src/types/monaco.d.ts index e7b54f4..99dd286 100644 --- a/src/types/monaco.d.ts +++ b/src/types/monaco.d.ts @@ -1,4 +1,4 @@ -import { IRange } from 'monaco-editor' +import { IRange, Range } from 'monaco-editor' declare module 'monaco-editor' { namespace editor { @@ -8,7 +8,8 @@ declare module 'monaco-editor' { setHiddenAreas(ranges: IRange[], source?: unknown): void _getViewModel(): { - getHiddenAreas(): IRange[] + getHiddenAreas(): Range[] + getModelVisibleRanges(): Range[] } | undefined } } From 8074914108aeb22cdaca9bd844d3c403862a4a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Tue, 7 May 2024 17:20:52 +0200 Subject: [PATCH 2/2] cleanup: remove outdated check --- src/services.ts | 4 ---- src/tools.ts | 10 +++++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/services.ts b/src/services.ts index c9ccba0..9303355 100644 --- a/src/services.ts +++ b/src/services.ts @@ -126,10 +126,6 @@ export function isInitialized (): boolean { } export async function initialize (constructionOptions: IWorkbenchConstructionOptions = {}, container?: HTMLElement): Promise { - if (typeof process !== 'undefined') { - console.warn('`process` detected. It may have negative impacts on VSCode behavior') - } - if (constructionOptions.workspaceProvider == null) { constructionOptions = { ...constructionOptions, diff --git a/src/tools.ts b/src/tools.ts index 1797a5a..ade32fc 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -236,16 +236,16 @@ export function hideCodeWithoutDecoration (editor: monaco.editor.ICodeEditor, de const selections = editor.getSelections() if (selections != null) { const visibleRanges = editor._getViewModel()!.getModelVisibleRanges() - let newSelection = selections.flatMap(selection => + 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 (newSelection.length === 0 && visibleRanges.length > 0) { - newSelection = [monaco.Selection.fromPositions(visibleRanges[0]!.getStartPosition())] + if (newSelections.length === 0 && visibleRanges.length > 0) { + newSelections = [monaco.Selection.fromPositions(visibleRanges[0]!.getStartPosition())] } - if (newSelection.length > 0) { - editor.setSelections(newSelection) + if (newSelections.length > 0) { + editor.setSelections(newSelections) } } }