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 selection #114

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