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

When no document symbol provider use the folding model for the sticky scroll #159198

Merged
merged 14 commits into from
Aug 30, 2022
64 changes: 61 additions & 3 deletions src/vs/editor/contrib/stickyScroll/browser/stickyScrollProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { RunOnceScheduler } from 'vs/base/common/async';
import { Range } from 'vs/editor/common/core/range';
import { Emitter } from 'vs/base/common/event';
import { binarySearch } from 'vs/base/common/arrays';
import { FoldingController } from 'vs/editor/contrib/folding/browser/folding';
import { FoldingModel } from 'vs/editor/contrib/folding/browser/foldingModel';

export class StickyRange {
constructor(
Expand Down Expand Up @@ -92,7 +94,24 @@ export class StickyLineCandidateProvider extends Disposable {
if (token.isCancellationRequested) {
return;
}
this._outlineModel = StickyOutlineElement.fromOutlineModel(outlineModel);
if (outlineModel.children.size !== 0) {
this._outlineModel = StickyOutlineElement.fromOutlineModel(outlineModel);
} else {
const foldingController = FoldingController.get(this._editor);
const foldingModel = await foldingController?.getFoldingModel();
aiday-mar marked this conversation as resolved.
Show resolved Hide resolved
if (token.isCancellationRequested) {
return;
}
if (foldingModel && foldingModel.regions.length !== 0) {
this._outlineModel = StickyOutlineElement.fromFoldingModel(foldingModel);
} else {
this._outlineModel = new StickyOutlineElement(
new StickyRange(-1, -1),
[],
undefined
);
}
}
this._modelVersionId = modelVersionId;
}
}
Expand Down Expand Up @@ -183,9 +202,44 @@ class StickyOutlineElement {
}
return new StickyOutlineElement(
range,
children
children,
undefined
);
}

public static fromFoldingModel(foldingModel: FoldingModel): StickyOutlineElement {
const regions = foldingModel.regions;
const length = regions.length;
let range: StickyRange | undefined;
const stackOfParents: StickyRange[] = [];

const stickyOutlineElement = new StickyOutlineElement(
undefined,
[],
undefined
);
let parentStickyOutlineElement = stickyOutlineElement;

for (let i = 0; i < length; i++) {
range = new StickyRange(regions.getStartLineNumber(i), regions.getEndLineNumber(i));
while (stackOfParents.length !== 0 && (range.startLineNumber < stackOfParents[stackOfParents.length - 1].startLineNumber || range.endLineNumber > stackOfParents[stackOfParents.length - 1].endLineNumber)) {
stackOfParents.pop();
if (parentStickyOutlineElement.parent !== undefined) {
parentStickyOutlineElement = parentStickyOutlineElement.parent;
}
}
const child = new StickyOutlineElement(
range,
[],
parentStickyOutlineElement
);
parentStickyOutlineElement.children.push(child);
parentStickyOutlineElement = child;
stackOfParents.push(range);
}
return stickyOutlineElement;
}

constructor(
/**
* Range of line numbers spanned by the current scope
Expand All @@ -194,7 +248,11 @@ class StickyOutlineElement {
/**
* Must be sorted by start line number
*/
public readonly children: readonly StickyOutlineElement[],
public readonly children: StickyOutlineElement[],
/**
* Parent sticky outline element
*/
public readonly parent: StickyOutlineElement | undefined
) {
}
}