Refactoring the stickyScrollController setState method (#213917)

* adding changes

* fixing compilation errors, using undefined as the fallback type not null
This commit is contained in:
Aiday Marlen Kyzy 2024-06-03 15:00:45 +02:00 committed by GitHub
parent 8dd604a8d8
commit cda51f7b70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 26 deletions

View file

@ -51,7 +51,7 @@ export class StickyScrollController extends Disposable implements IEditorContrib
private readonly _sessionStore: DisposableStore = new DisposableStore();
private _widgetState: StickyScrollWidgetState;
private _foldingModel: FoldingModel | null = null;
private _foldingModel: FoldingModel | undefined;
private _maxStickyLines: number = Number.MAX_SAFE_INTEGER;
private _stickyRangeProjectedOnEditor: IRange | undefined;
@ -67,7 +67,7 @@ export class StickyScrollController extends Disposable implements IEditorContrib
private _positionRevealed = false;
private _onMouseDown = false;
private _endLineNumbers: number[] = [];
private _showEndForLine: number | null = null;
private _showEndForLine: number | undefined;
constructor(
private readonly _editor: ICodeEditor,
@ -84,7 +84,7 @@ export class StickyScrollController extends Disposable implements IEditorContrib
this._register(this._stickyScrollWidget);
this._register(this._stickyLineCandidateProvider);
this._widgetState = new StickyScrollWidgetState([], [], 0);
this._widgetState = StickyScrollWidgetState.Empty;
this._onDidResize();
this._readConfiguration();
const stickyScrollDomNode = this._stickyScrollWidget.getDomNode();
@ -291,14 +291,14 @@ export class StickyScrollController extends Disposable implements IEditorContrib
this._renderStickyScroll();
return;
}
if (this._showEndForLine !== null) {
this._showEndForLine = null;
if (this._showEndForLine !== undefined) {
this._showEndForLine = undefined;
this._renderStickyScroll();
}
}));
this._register(dom.addDisposableListener(stickyScrollWidgetDomNode, dom.EventType.MOUSE_LEAVE, (e) => {
if (this._showEndForLine !== null) {
this._showEndForLine = null;
if (this._showEndForLine !== undefined) {
this._showEndForLine = undefined;
this._renderStickyScroll();
}
}));
@ -415,14 +415,14 @@ export class StickyScrollController extends Disposable implements IEditorContrib
this._editor.addOverlayWidget(this._stickyScrollWidget);
this._sessionStore.add(this._editor.onDidScrollChange((e) => {
if (e.scrollTopChanged) {
this._showEndForLine = null;
this._showEndForLine = undefined;
this._renderStickyScroll();
}
}));
this._sessionStore.add(this._editor.onDidLayoutChange(() => this._onDidResize()));
this._sessionStore.add(this._editor.onDidChangeModelTokens((e) => this._onTokensChange(e)));
this._sessionStore.add(this._stickyLineCandidateProvider.onDidChangeStickyScroll(() => {
this._showEndForLine = null;
this._showEndForLine = undefined;
this._renderStickyScroll();
}));
this._enabled = true;
@ -431,7 +431,7 @@ export class StickyScrollController extends Disposable implements IEditorContrib
const lineNumberOption = this._editor.getOption(EditorOption.lineNumbers);
if (lineNumberOption.renderType === RenderLineNumbersType.Relative) {
this._sessionStore.add(this._editor.onDidChangeCursorPosition(() => {
this._showEndForLine = null;
this._showEndForLine = undefined;
this._renderStickyScroll(0);
}));
}
@ -479,32 +479,28 @@ export class StickyScrollController extends Disposable implements IEditorContrib
this._maxStickyLines = Math.round(theoreticalLines * .25);
}
private async _renderStickyScroll(rebuildFromLine?: number) {
private async _renderStickyScroll(rebuildFromLine?: number): Promise<void> {
const model = this._editor.getModel();
if (!model || model.isTooLargeForTokenization()) {
this._foldingModel = null;
this._stickyScrollWidget.setState(undefined, null);
this._resetState();
return;
}
const stickyLineVersion = this._stickyLineCandidateProvider.getVersionId();
if (stickyLineVersion === undefined || stickyLineVersion === model.getVersionId()) {
this._foldingModel = await FoldingController.get(this._editor)?.getFoldingModel() ?? null;
this._widgetState = this.findScrollWidgetState();
this._stickyScrollVisibleContextKey.set(!(this._widgetState.startLineNumbers.length === 0));
const stickyWidgetVersion = this._stickyLineCandidateProvider.getVersionId();
const shouldUpdateState = stickyWidgetVersion === undefined || stickyWidgetVersion === model.getVersionId();
if (shouldUpdateState) {
if (!this._focused) {
this._stickyScrollWidget.setState(this._widgetState, this._foldingModel, rebuildFromLine);
await this._updateState(rebuildFromLine);
} else {
// Suppose that previously the sticky scroll widget had height 0, then if there are visible lines, set the last line as focused
if (this._focusedStickyElementIndex === -1) {
this._stickyScrollWidget.setState(this._widgetState, this._foldingModel, rebuildFromLine);
await this._updateState(rebuildFromLine);
this._focusedStickyElementIndex = this._stickyScrollWidget.lineNumberCount - 1;
if (this._focusedStickyElementIndex !== -1) {
this._stickyScrollWidget.focusLineWithIndex(this._focusedStickyElementIndex);
}
} else {
const focusedStickyElementLineNumber = this._stickyScrollWidget.lineNumbers[this._focusedStickyElementIndex];
this._stickyScrollWidget.setState(this._widgetState, this._foldingModel, rebuildFromLine);
await this._updateState(rebuildFromLine);
// Suppose that after setting the state, there are no sticky lines, set the focused index to -1
if (this._stickyScrollWidget.lineNumberCount === 0) {
this._focusedStickyElementIndex = -1;
@ -522,6 +518,20 @@ export class StickyScrollController extends Disposable implements IEditorContrib
}
}
}
private async _updateState(rebuildFromLine?: number): Promise<void> {
this._foldingModel = await FoldingController.get(this._editor)?.getFoldingModel() ?? undefined;
this._widgetState = this.findScrollWidgetState();
const stickyWidgetHasLines = this._widgetState.startLineNumbers.length > 0;
this._stickyScrollVisibleContextKey.set(stickyWidgetHasLines);
this._stickyScrollWidget.setState(this._widgetState, this._foldingModel, rebuildFromLine);
}
private async _resetState(): Promise<void> {
this._foldingModel = undefined;
this._widgetState = StickyScrollWidgetState.Empty;
this._stickyScrollVisibleContextKey.set(false);
this._stickyScrollWidget.setState(undefined, undefined);
}
findScrollWidgetState(): StickyScrollWidgetState {
const lineHeight: number = this._editor.getOption(EditorOption.lineHeight);

View file

@ -35,6 +35,10 @@ export class StickyScrollWidgetState {
&& equals(this.startLineNumbers, other.startLineNumbers)
&& equals(this.endLineNumbers, other.endLineNumbers);
}
static get Empty() {
return new StickyScrollWidgetState([], [], 0);
}
}
const _ttPolicy = createTrustedTypesPolicy('stickyScrollViewLayer', { createHTML: value => value });
@ -126,7 +130,7 @@ export class StickyScrollWidget extends Disposable implements IOverlayWidget {
return this._lineNumbers;
}
setState(_state: StickyScrollWidgetState | undefined, foldingModel: FoldingModel | null, _rebuildFromLine?: number): void {
setState(_state: StickyScrollWidgetState | undefined, foldingModel: FoldingModel | undefined, _rebuildFromLine?: number): void {
if (_rebuildFromLine === undefined &&
((!this._previousState && !_state) || (this._previousState && this._previousState.equals(_state)))
) {
@ -205,7 +209,7 @@ export class StickyScrollWidget extends Disposable implements IOverlayWidget {
}
}
private async _renderRootNode(state: StickyScrollWidgetState | undefined, foldingModel: FoldingModel | null, rebuildFromLine: number): Promise<void> {
private async _renderRootNode(state: StickyScrollWidgetState | undefined, foldingModel: FoldingModel | undefined, rebuildFromLine: number): Promise<void> {
this._clearStickyLinesFromLine(rebuildFromLine);
if (!state) {
return;
@ -258,7 +262,7 @@ export class StickyScrollWidget extends Disposable implements IOverlayWidget {
}));
}
private _renderChildNode(index: number, line: number, foldingModel: FoldingModel | null, layoutInfo: EditorLayoutInfo): RenderedStickyLine | undefined {
private _renderChildNode(index: number, line: number, foldingModel: FoldingModel | undefined, layoutInfo: EditorLayoutInfo): RenderedStickyLine | undefined {
const viewModel = this._editor._getViewModel();
if (!viewModel) {
return;
@ -358,7 +362,7 @@ export class StickyScrollWidget extends Disposable implements IOverlayWidget {
return stickyLine;
}
private _renderFoldingIconForLine(foldingModel: FoldingModel | null, line: number): StickyFoldingIcon | undefined {
private _renderFoldingIconForLine(foldingModel: FoldingModel | undefined, line: number): StickyFoldingIcon | undefined {
const showFoldingControls: 'mouseover' | 'always' | 'never' = this._editor.getOption(EditorOption.showFoldingControls);
if (!foldingModel || showFoldingControls === 'never') {
return;