Skip to content

Commit 1a3639e

Browse files
author
aiday-mar
committed
Changes from review
1 parent ca1a8f6 commit 1a3639e

File tree

1 file changed

+64
-50
lines changed

1 file changed

+64
-50
lines changed

src/vs/editor/contrib/stickyScroll/browser/stickyScrollProvider.ts

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class StickyLineCandidateProvider extends Disposable {
4343
private _outlineModel: StickyOutlineElement | undefined;
4444
private readonly _sessionStore: DisposableStore = new DisposableStore();
4545
private _modelVersionId: number = 0;
46-
private _providerString: string = '';
46+
private _providerID: string = '';
4747

4848
constructor(
4949
editor: ICodeEditor,
@@ -68,13 +68,13 @@ export class StickyLineCandidateProvider extends Disposable {
6868
return;
6969
} else {
7070
this._sessionStore.add(this._editor.onDidChangeModel(() => {
71-
this._providerString = '';
71+
this._providerID = '';
7272
this.update();
7373
}));
7474
this._sessionStore.add(this._editor.onDidChangeHiddenAreas(() => this.update()));
7575
this._sessionStore.add(this._editor.onDidChangeModelContent(() => this._updateSoon.schedule()));
7676
this._sessionStore.add(this._languageFeaturesService.documentSymbolProvider.onDidChange(() => {
77-
this._providerString = '';
77+
this._providerID = '';
7878
this.update();
7979
}));
8080
this.update();
@@ -92,45 +92,17 @@ export class StickyLineCandidateProvider extends Disposable {
9292
this.onStickyScrollChangeEmitter.fire();
9393
}
9494

95-
private findSumOfRangesOfGroup(outline: OutlineGroup | OutlineElement): number {
96-
let res = 0;
97-
for (const child of outline.children.values()) {
98-
res += this.findSumOfRangesOfGroup(child);
99-
}
100-
if (outline instanceof OutlineElement) {
101-
return res + outline.symbol.range.endLineNumber - outline.symbol.selectionRange.startLineNumber;
102-
} else {
103-
return res;
104-
}
105-
}
106-
10795
private async updateOutlineModel(token: CancellationToken) {
10896
if (this._editor.hasModel()) {
10997
const model = this._editor.getModel();
11098
const modelVersionId = model.getVersionId();
111-
let outlineModel = await OutlineModel.create(this._languageFeaturesService.documentSymbolProvider, model, token) as OutlineModel;
99+
const outlineModel = await OutlineModel.create(this._languageFeaturesService.documentSymbolProvider, model, token) as OutlineModel;
112100
if (token.isCancellationRequested) {
113101
return;
114102
}
115-
// When several possible outline providers
116-
if (outlineModel.children.size !== 0 && Iterable.first(outlineModel.children) instanceof OutlineGroup) {
117-
if (outlineModel.children.has(this._providerString)) {
118-
outlineModel = outlineModel.children.get(this._providerString) as unknown as OutlineModel;
119-
} else {
120-
let providerString = '';
121-
let maxTotalSumOfRanges = 0;
122-
for (const [key, outlineGroup] of outlineModel.children.entries()) {
123-
const totalSumRanges = this.findSumOfRangesOfGroup(outlineGroup);
124-
if (totalSumRanges > maxTotalSumOfRanges) {
125-
maxTotalSumOfRanges = totalSumRanges;
126-
providerString = key;
127-
}
128-
}
129-
this._providerString = providerString;
130-
outlineModel = outlineModel.children.get(this._providerString) as unknown as OutlineModel;
131-
}
132-
}
133-
this._outlineModel = StickyOutlineElement.fromOutlineModel(outlineModel, -1);
103+
const outlineData = StickyOutlineElement.fromOutlineModel(outlineModel, this._providerID);
104+
this._outlineModel = outlineData.stickyOutlineElement;
105+
this._providerID = outlineData.providerID;
134106
this._modelVersionId = modelVersionId;
135107
}
136108
}
@@ -196,18 +168,16 @@ export class StickyLineCandidateProvider extends Disposable {
196168
}
197169

198170
class StickyOutlineElement {
199-
public static fromOutlineModel(outlineModel: OutlineModel | OutlineElement | OutlineGroup, previousStartLine: number): StickyOutlineElement {
200171

172+
public static fromOutlineElement(outlineElement: OutlineElement, previousStartLine: number): StickyOutlineElement {
201173
const children: StickyOutlineElement[] = [];
202-
for (const child of outlineModel.children.values()) {
203-
if (child instanceof OutlineGroup || child instanceof OutlineModel) {
204-
children.push(StickyOutlineElement.fromOutlineModel(child, previousStartLine));
205-
} else if (child instanceof OutlineElement && child.symbol.selectionRange.startLineNumber !== child.symbol.range.endLineNumber) {
174+
for (const child of outlineElement.children.values()) {
175+
if (child.symbol.selectionRange.startLineNumber !== child.symbol.range.endLineNumber) {
206176
if (child.symbol.selectionRange.startLineNumber !== previousStartLine) {
207-
children.push(StickyOutlineElement.fromOutlineModel(child, child.symbol.selectionRange.startLineNumber));
177+
children.push(StickyOutlineElement.fromOutlineElement(child, child.symbol.selectionRange.startLineNumber));
208178
} else {
209179
for (const subchild of child.children.values()) {
210-
children.push(StickyOutlineElement.fromOutlineModel(subchild, child.symbol.selectionRange.startLineNumber));
180+
children.push(StickyOutlineElement.fromOutlineElement(subchild, child.symbol.selectionRange.startLineNumber));
211181
}
212182
}
213183
}
@@ -221,17 +191,61 @@ class StickyOutlineElement {
221191
return child2.range.endLineNumber - child1.range.endLineNumber;
222192
}
223193
});
224-
let range: StickyRange | undefined;
225-
if (outlineModel instanceof OutlineElement) {
226-
range = new StickyRange(outlineModel.symbol.selectionRange.startLineNumber, outlineModel.symbol.range.endLineNumber);
194+
const range = new StickyRange(outlineElement.symbol.selectionRange.startLineNumber, outlineElement.symbol.range.endLineNumber);
195+
return new StickyOutlineElement(range, children);
196+
}
197+
198+
public static fromOutlineModel(outlineModel: OutlineModel, providerID: string): { stickyOutlineElement: StickyOutlineElement; providerID: string } {
199+
200+
let ID: string = providerID;
201+
let outlineElements: Map<string, OutlineElement>;
202+
// When several possible outline providers
203+
if (outlineModel.children.size !== 0 && Iterable.first(outlineModel.children.values()) instanceof OutlineGroup) {
204+
const filteredProviders = Array.from(outlineModel.children.values()).filter(outlineGroupOfModel => outlineGroupOfModel.id === providerID);
205+
if (filteredProviders && filteredProviders.length !== 0) {
206+
outlineElements = filteredProviders[0].children;
207+
} else {
208+
let tempID = '';
209+
let maxTotalSumOfRanges = 0;
210+
let optimalOutlineGroup = undefined;
211+
for (const [_key, outlineGroup] of outlineModel.children.entries()) {
212+
const totalSumRanges = StickyOutlineElement.findSumOfRangesOfGroup(outlineGroup);
213+
if (totalSumRanges > maxTotalSumOfRanges) {
214+
optimalOutlineGroup = outlineGroup;
215+
maxTotalSumOfRanges = totalSumRanges;
216+
tempID = outlineGroup.id;
217+
}
218+
}
219+
ID = tempID;
220+
outlineElements = optimalOutlineGroup?.children as Map<string, OutlineElement>;
221+
}
222+
} else {
223+
outlineElements = outlineModel.children as Map<string, OutlineElement>;
224+
}
225+
const stickyChildren: StickyOutlineElement[] = [];
226+
for (const outlineElement of outlineElements.values()) {
227+
stickyChildren.push(StickyOutlineElement.fromOutlineElement(outlineElement, outlineElement.symbol.selectionRange.startLineNumber));
228+
}
229+
const stickyOutlineElement = new StickyOutlineElement(undefined, stickyChildren);
230+
231+
return {
232+
stickyOutlineElement: stickyOutlineElement,
233+
providerID: ID
234+
};
235+
}
236+
237+
private static findSumOfRangesOfGroup(outline: OutlineGroup | OutlineElement): number {
238+
let res = 0;
239+
for (const child of outline.children.values()) {
240+
res += this.findSumOfRangesOfGroup(child);
241+
}
242+
if (outline instanceof OutlineElement) {
243+
return res + outline.symbol.range.endLineNumber - outline.symbol.selectionRange.startLineNumber;
227244
} else {
228-
range = undefined;
245+
return res;
229246
}
230-
return new StickyOutlineElement(
231-
range,
232-
children
233-
);
234247
}
248+
235249
constructor(
236250
/**
237251
* Range of line numbers spanned by the current scope

0 commit comments

Comments
 (0)