Skip to content

Commit f62d2c8

Browse files
author
Aiday Marlen Kyzy
authored
Merge pull request microsoft#159198 from microsoft/aiday/issue157165
When no document symbol provider use the folding model for the sticky scroll
2 parents 2ba0464 + cb8f0bb commit f62d2c8

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export class StickyScrollController extends Disposable implements IEditorContrib
3333
this._stickyLineCandidateProvider = new StickyLineCandidateProvider(this._editor, _languageFeaturesService);
3434
this._widgetState = new StickyScrollWidgetState([], 0);
3535

36+
this._register(this._stickyScrollWidget);
37+
this._register(this._stickyLineCandidateProvider);
3638
this._register(this._editor.onDidChangeConfiguration(e => {
3739
if (e.hasChanged(EditorOption.stickyScroll)) {
3840
this.readConfiguration();

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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { RunOnceScheduler } from 'vs/base/common/async';
1313
import { Range } from 'vs/editor/common/core/range';
1414
import { Emitter } from 'vs/base/common/event';
1515
import { binarySearch } from 'vs/base/common/arrays';
16+
import { FoldingController } from 'vs/editor/contrib/folding/browser/folding';
17+
import { FoldingModel } from 'vs/editor/contrib/folding/browser/foldingModel';
1618

1719
export class StickyRange {
1820
constructor(
@@ -92,7 +94,24 @@ export class StickyLineCandidateProvider extends Disposable {
9294
if (token.isCancellationRequested) {
9395
return;
9496
}
95-
this._outlineModel = StickyOutlineElement.fromOutlineModel(outlineModel, -1);
97+
if (outlineModel.children.size !== 0) {
98+
this._outlineModel = StickyOutlineElement.fromOutlineModel(outlineModel, -1);
99+
} else {
100+
const foldingController = FoldingController.get(this._editor);
101+
const foldingModel = await foldingController?.getFoldingModel();
102+
if (token.isCancellationRequested) {
103+
return;
104+
}
105+
if (foldingModel && foldingModel.regions.length !== 0) {
106+
this._outlineModel = StickyOutlineElement.fromFoldingModel(foldingModel);
107+
} else {
108+
this._outlineModel = new StickyOutlineElement(
109+
new StickyRange(-1, -1),
110+
[],
111+
undefined
112+
);
113+
}
114+
}
96115
this._modelVersionId = modelVersionId;
97116
}
98117
}
@@ -191,9 +210,44 @@ class StickyOutlineElement {
191210
}
192211
return new StickyOutlineElement(
193212
range,
194-
children
213+
children,
214+
undefined
195215
);
196216
}
217+
218+
public static fromFoldingModel(foldingModel: FoldingModel): StickyOutlineElement {
219+
const regions = foldingModel.regions;
220+
const length = regions.length;
221+
let range: StickyRange | undefined;
222+
const stackOfParents: StickyRange[] = [];
223+
224+
const stickyOutlineElement = new StickyOutlineElement(
225+
undefined,
226+
[],
227+
undefined
228+
);
229+
let parentStickyOutlineElement = stickyOutlineElement;
230+
231+
for (let i = 0; i < length; i++) {
232+
range = new StickyRange(regions.getStartLineNumber(i), regions.getEndLineNumber(i));
233+
while (stackOfParents.length !== 0 && (range.startLineNumber < stackOfParents[stackOfParents.length - 1].startLineNumber || range.endLineNumber > stackOfParents[stackOfParents.length - 1].endLineNumber)) {
234+
stackOfParents.pop();
235+
if (parentStickyOutlineElement.parent !== undefined) {
236+
parentStickyOutlineElement = parentStickyOutlineElement.parent;
237+
}
238+
}
239+
const child = new StickyOutlineElement(
240+
range,
241+
[],
242+
parentStickyOutlineElement
243+
);
244+
parentStickyOutlineElement.children.push(child);
245+
parentStickyOutlineElement = child;
246+
stackOfParents.push(range);
247+
}
248+
return stickyOutlineElement;
249+
}
250+
197251
constructor(
198252
/**
199253
* Range of line numbers spanned by the current scope
@@ -202,7 +256,11 @@ class StickyOutlineElement {
202256
/**
203257
* Must be sorted by start line number
204258
*/
205-
public readonly children: readonly StickyOutlineElement[],
259+
public readonly children: StickyOutlineElement[],
260+
/**
261+
* Parent sticky outline element
262+
*/
263+
public readonly parent: StickyOutlineElement | undefined
206264
) {
207265
}
208266
}

src/vs/editor/test/browser/testCodeEditor.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function isTextModel(arg: ITextModel | string | string[] | ITextBufferFactory):
133133

134134
function _withTestCodeEditor(arg: ITextModel | string | string[] | ITextBufferFactory, options: TestCodeEditorInstantiationOptions, callback: (editor: ITestCodeEditor, viewModel: ViewModel, instantiationService: TestInstantiationService) => void): void;
135135
function _withTestCodeEditor(arg: ITextModel | string | string[] | ITextBufferFactory, options: TestCodeEditorInstantiationOptions, callback: (editor: ITestCodeEditor, viewModel: ViewModel, instantiationService: TestInstantiationService) => Promise<void>): Promise<void>;
136-
function _withTestCodeEditor(arg: ITextModel | string | string[] | ITextBufferFactory, options: TestCodeEditorInstantiationOptions, callback: (editor: ITestCodeEditor, viewModel: ViewModel, instantiationService: TestInstantiationService) => Promise<void> | void): Promise<void> | void {
136+
async function _withTestCodeEditor(arg: ITextModel | string | string[] | ITextBufferFactory, options: TestCodeEditorInstantiationOptions, callback: (editor: ITestCodeEditor, viewModel: ViewModel, instantiationService: TestInstantiationService) => Promise<void> | void): Promise<Promise<void> | void> {
137137
const disposables = new DisposableStore();
138138
const instantiationService = createCodeEditorServices(disposables, options.serviceCollection);
139139
delete options.serviceCollection;
@@ -149,12 +149,12 @@ function _withTestCodeEditor(arg: ITextModel | string | string[] | ITextBufferFa
149149
const editor = disposables.add(instantiateTestCodeEditor(instantiationService, model, options));
150150
const viewModel = editor.getViewModel()!;
151151
viewModel.setHasFocus(true);
152-
const result = callback(<ITestCodeEditor>editor, editor.getViewModel()!, instantiationService);
153-
if (result) {
154-
return result.then(() => disposables.dispose());
152+
try {
153+
const result = callback(<ITestCodeEditor>editor, editor.getViewModel()!, instantiationService);
154+
await result;
155+
} finally {
156+
disposables.dispose();
155157
}
156-
157-
disposables.dispose();
158158
}
159159

160160
export function createCodeEditorServices(disposables: DisposableStore, services: ServiceCollection = new ServiceCollection()): TestInstantiationService {

src/vs/workbench/test/browser/workbenchTestServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export class TestTextResourceEditor extends TextResourceEditor {
194194
export class TestTextFileEditor extends TextFileEditor {
195195

196196
protected override createEditorControl(parent: HTMLElement, configuration: any): void {
197-
this.editorControl = this.instantiationService.createInstance(TestCodeEditor, parent, configuration, {});
197+
this.editorControl = this.instantiationService.createInstance(TestCodeEditor, parent, configuration, { contributions: [] });
198198
}
199199

200200
setSelection(selection: Selection | undefined, reason: EditorPaneSelectionChangeReason): void {

0 commit comments

Comments
 (0)