Skip to content
Open
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
45 changes: 41 additions & 4 deletions src/vs/editor/contrib/folding/browser/folding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,16 @@ export class FoldingController extends Disposable implements IEditorContribution
let scrollState: StableEditorScrollState | undefined;

if (this._foldingImportsByDefault && !this._currentModelHasFoldedImports) {
const hasChanges = foldingRanges.setCollapsedAllOfType(FoldingRangeKind.Imports.value, true);
if (hasChanges) {
scrollState = StableEditorScrollState.capture(this.editor);
this._currentModelHasFoldedImports = hasChanges;
// Check if current selection intersects with any import ranges
const selections = this.editor.getSelections();
const shouldSkipImportFolding = this._shouldSkipImportFolding(foldingRanges, selections);

if (!shouldSkipImportFolding) {
const hasChanges = foldingRanges.setCollapsedAllOfType(FoldingRangeKind.Imports.value, true);
if (hasChanges) {
scrollState = StableEditorScrollState.capture(this.editor);
this._currentModelHasFoldedImports = hasChanges;
}
}
}

Expand Down Expand Up @@ -361,6 +367,37 @@ export class FoldingController extends Disposable implements IEditorContribution
this.editor.setHiddenAreas(hiddenRanges, this);
}

/**
* Determines if import folding should be skipped because the current selection
* intersects with import ranges, indicating the user is navigating to an error
* or other content within the imports section.
*/
private _shouldSkipImportFolding(foldingRanges: FoldingRegions, selections: Selection[] | null): boolean {
if (!selections || selections.length === 0) {
return false;
}

// Check if any selection intersects with import ranges
for (const selection of selections) {
const selectionStartLine = selection.startLineNumber;
const selectionEndLine = selection.endLineNumber;

for (let i = 0; i < foldingRanges.length; i++) {
const rangeType = foldingRanges.getType(i);
if (rangeType === FoldingRangeKind.Imports.value) {
const rangeStartLine = foldingRanges.getStartLineNumber(i);
const rangeEndLine = foldingRanges.getEndLineNumber(i);

if (selectionStartLine <= rangeEndLine && selectionEndLine >= rangeStartLine) {
return true;
}
}
}
}

return false;
}

private onCursorPositionChanged() {
if (this.hiddenRangeModel && this.hiddenRangeModel.hasRanges()) {
this.cursorChangedScheduler!.schedule();
Expand Down