Skip to content

Commit 6eeebdd

Browse files
committed
Support for revealing at definition
This scrolls the target closer to the top, but leaves sufficient room for e.g. class/function comments, while showing more of the source below the target (i.e.. more of the class/function body).
1 parent 7720c91 commit 6eeebdd

13 files changed

Lines changed: 156 additions & 9 deletions

File tree

src/vs/editor/browser/controller/coreCommands.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ export namespace RevealLine_ {
270270
export const RawAtArgument = {
271271
Top: 'top',
272272
Center: 'center',
273-
Bottom: 'bottom'
273+
Bottom: 'bottom',
274+
Definition: 'definition',
274275
};
275276
}
276277

@@ -1477,6 +1478,9 @@ export namespace CoreNavigationCommands {
14771478
case RevealLine_.RawAtArgument.Bottom:
14781479
revealAt = VerticalRevealType.Bottom;
14791480
break;
1481+
case RevealLine_.RawAtArgument.Definition:
1482+
revealAt = VerticalRevealType.Definition;
1483+
break;
14801484
default:
14811485
break;
14821486
}

src/vs/editor/browser/viewParts/lines/viewLines.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,14 +616,23 @@ export class ViewLines extends ViewPart implements IVisibleLinesHost<ViewLine>,
616616

617617
let newScrollTop: number;
618618

619-
if (verticalType === viewEvents.VerticalRevealType.Center || verticalType === viewEvents.VerticalRevealType.CenterIfOutsideViewport) {
619+
if (verticalType === viewEvents.VerticalRevealType.Center || verticalType === viewEvents.VerticalRevealType.CenterIfOutsideViewport
620+
|| verticalType === viewEvents.VerticalRevealType.Definition
621+
) {
620622
if (verticalType === viewEvents.VerticalRevealType.CenterIfOutsideViewport && viewportStartY <= boxStartY && boxEndY <= viewportEndY) {
621623
// Box is already in the viewport... do nothing
622624
newScrollTop = viewportStartY;
623625
} else {
624626
// Box is outside the viewport... center it
625627
const boxMiddleY = (boxStartY + boxEndY) / 2;
626-
newScrollTop = Math.max(0, boxMiddleY - viewportHeight / 2);
628+
let delta = viewportHeight * 0.5;
629+
if (verticalType === viewEvents.VerticalRevealType.Definition) {
630+
// Definition scrolls to 20% from the top of the viewport, but ensures a minimum amount of space from the top
631+
// and never scrolls beyond the center.
632+
const minSpace = 100;
633+
delta = Math.min(delta, Math.max(minSpace, viewportHeight * 0.2));
634+
}
635+
newScrollTop = Math.max(0, boxMiddleY - delta);
627636
}
628637
} else {
629638
newScrollTop = this._computeMinimumScrolling(viewportStartY, viewportEndY, boxStartY, boxEndY, verticalType === viewEvents.VerticalRevealType.Top, verticalType === viewEvents.VerticalRevealType.Bottom);

src/vs/editor/browser/widget/codeEditorWidget.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
547547
this._revealLine(lineNumber, VerticalRevealType.CenterIfOutsideViewport, scrollType);
548548
}
549549

550+
public revealLineAtDefinition(lineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
551+
this._revealLine(lineNumber, VerticalRevealType.Definition, scrollType);
552+
}
553+
550554
private _revealLine(lineNumber: number, revealType: VerticalRevealType, scrollType: editorCommon.ScrollType): void {
551555
if (typeof lineNumber !== 'number') {
552556
throw new Error('Invalid arguments');
@@ -587,6 +591,15 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
587591
);
588592
}
589593

594+
public revealPositionAtDefinition(position: IPosition, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
595+
this._revealPosition(
596+
position,
597+
VerticalRevealType.Definition,
598+
true,
599+
scrollType
600+
);
601+
}
602+
590603
private _revealPosition(position: IPosition, verticalType: VerticalRevealType, revealHorizontal: boolean, scrollType: editorCommon.ScrollType): void {
591604
if (!Position.isIPosition(position)) {
592605
throw new Error('Invalid arguments');
@@ -675,6 +688,15 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
675688
);
676689
}
677690

691+
public revealLinesAtDefinition(startLineNumber: number, endLineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
692+
this._revealLines(
693+
startLineNumber,
694+
endLineNumber,
695+
VerticalRevealType.Definition,
696+
scrollType
697+
);
698+
}
699+
678700
private _revealLines(startLineNumber: number, endLineNumber: number, verticalType: VerticalRevealType, scrollType: editorCommon.ScrollType): void {
679701
if (typeof startLineNumber !== 'number' || typeof endLineNumber !== 'number') {
680702
throw new Error('Invalid arguments');
@@ -715,6 +737,15 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
715737
);
716738
}
717739

740+
public revealRangeAtDefinition(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
741+
this._revealRange(
742+
range,
743+
VerticalRevealType.Definition,
744+
true,
745+
scrollType
746+
);
747+
}
748+
718749
public revealRangeAtTop(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
719750
this._revealRange(
720751
range,
@@ -724,6 +755,15 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
724755
);
725756
}
726757

758+
public revealRangeNearTop(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
759+
this._revealRange(
760+
range,
761+
VerticalRevealType.Definition,
762+
true,
763+
scrollType
764+
);
765+
}
766+
727767
private _revealRange(range: IRange, verticalType: VerticalRevealType, revealHorizontal: boolean, scrollType: editorCommon.ScrollType): void {
728768
if (!Range.isIRange(range)) {
729769
throw new Error('Invalid arguments');

src/vs/editor/browser/widget/diffEditorWidget.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,10 @@ export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffE
695695
this.modifiedEditor.revealLineInCenterIfOutsideViewport(lineNumber, scrollType);
696696
}
697697

698+
public revealLineAtDefinition(lineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
699+
this.modifiedEditor.revealLineAtDefinition(lineNumber, scrollType);
700+
}
701+
698702
public revealPosition(position: IPosition, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
699703
this.modifiedEditor.revealPosition(position, scrollType);
700704
}
@@ -707,6 +711,10 @@ export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffE
707711
this.modifiedEditor.revealPositionInCenterIfOutsideViewport(position, scrollType);
708712
}
709713

714+
public revealPositionAtDefinition(position: IPosition, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
715+
this.modifiedEditor.revealPositionAtDefinition(position, scrollType);
716+
}
717+
710718
public getSelection(): Selection | null {
711719
return this.modifiedEditor.getSelection();
712720
}
@@ -739,6 +747,10 @@ export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffE
739747
this.modifiedEditor.revealLinesInCenterIfOutsideViewport(startLineNumber, endLineNumber, scrollType);
740748
}
741749

750+
public revealLinesAtDefinition(startLineNumber: number, endLineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
751+
this.modifiedEditor.revealLinesAtDefinition(startLineNumber, endLineNumber, scrollType);
752+
}
753+
742754
public revealRange(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth, revealVerticalInCenter: boolean = false, revealHorizontal: boolean = true): void {
743755
this.modifiedEditor.revealRange(range, scrollType, revealVerticalInCenter, revealHorizontal);
744756
}
@@ -751,10 +763,18 @@ export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffE
751763
this.modifiedEditor.revealRangeInCenterIfOutsideViewport(range, scrollType);
752764
}
753765

766+
public revealRangeAtDefinition(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
767+
this.modifiedEditor.revealRangeAtDefinition(range, scrollType);
768+
}
769+
754770
public revealRangeAtTop(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
755771
this.modifiedEditor.revealRangeAtTop(range, scrollType);
756772
}
757773

774+
public revealRangeNearTop(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
775+
this.modifiedEditor.revealRangeNearTop(range, scrollType);
776+
}
777+
758778
public getSupportedActions(): editorCommon.IEditorAction[] {
759779
return this.modifiedEditor.getSupportedActions();
760780
}

src/vs/editor/common/editorCommon.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ export interface IEditor {
339339
*/
340340
revealLineInCenterIfOutsideViewport(lineNumber: number, scrollType?: ScrollType): void;
341341

342+
/**
343+
* Scroll vertically as necessary and reveal a line close to the top of the viewport,
344+
* optimized for viewing a code definition.
345+
*/
346+
revealLineAtDefinition(lineNumber: number, scrollType?: ScrollType): void;
347+
342348
/**
343349
* Scroll vertically or horizontally as necessary and reveal a position.
344350
*/
@@ -354,6 +360,12 @@ export interface IEditor {
354360
*/
355361
revealPositionInCenterIfOutsideViewport(position: IPosition, scrollType?: ScrollType): void;
356362

363+
/**
364+
* Scroll vertically or horizontally as necessary and reveal a position close to the top of the viewport,
365+
* optimized for viewing a code definition.
366+
*/
367+
revealPositionAtDefinition(position: IPosition, scrollType?: ScrollType): void;
368+
357369
/**
358370
* Returns the primary selection of the editor.
359371
*/
@@ -406,6 +418,12 @@ export interface IEditor {
406418
*/
407419
revealLinesInCenterIfOutsideViewport(lineNumber: number, endLineNumber: number, scrollType?: ScrollType): void;
408420

421+
/**
422+
* Scroll vertically as necessary and reveal lines close to the top of the viewport,
423+
* optimized for viewing a code definition.
424+
*/
425+
revealLinesAtDefinition(lineNumber: number, endLineNumber: number, scrollType?: ScrollType): void;
426+
409427
/**
410428
* Scroll vertically or horizontally as necessary and reveal a range.
411429
*/
@@ -426,6 +444,12 @@ export interface IEditor {
426444
*/
427445
revealRangeInCenterIfOutsideViewport(range: IRange, scrollType?: ScrollType): void;
428446

447+
/**
448+
* Scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport,
449+
* optimized for viewing a code definition.
450+
*/
451+
revealRangeAtDefinition(range: IRange, scrollType?: ScrollType): void;
452+
429453
/**
430454
* Directly trigger a handler or an editor action.
431455
* @param source The source of the call.

src/vs/editor/common/view/viewEvents.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ export const enum VerticalRevealType {
159159
Center = 1,
160160
CenterIfOutsideViewport = 2,
161161
Top = 3,
162-
Bottom = 4
162+
Bottom = 4,
163+
Definition = 5,
163164
}
164165

165166
export class ViewRevealRangeRequestEvent {

src/vs/monaco.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,11 @@ declare namespace monaco.editor {
21082108
* Scroll vertically as necessary and reveal a line centered vertically only if it lies outside the viewport.
21092109
*/
21102110
revealLineInCenterIfOutsideViewport(lineNumber: number, scrollType?: ScrollType): void;
2111+
/**
2112+
* Scroll vertically as necessary and reveal a line close to the top of the viewport,
2113+
* optimized for viewing a code definition.
2114+
*/
2115+
revealLineAtDefinition(lineNumber: number, scrollType?: ScrollType): void;
21112116
/**
21122117
* Scroll vertically or horizontally as necessary and reveal a position.
21132118
*/
@@ -2120,6 +2125,11 @@ declare namespace monaco.editor {
21202125
* Scroll vertically or horizontally as necessary and reveal a position centered vertically only if it lies outside the viewport.
21212126
*/
21222127
revealPositionInCenterIfOutsideViewport(position: IPosition, scrollType?: ScrollType): void;
2128+
/**
2129+
* Scroll vertically or horizontally as necessary and reveal a position close to the top of the viewport,
2130+
* optimized for viewing a code definition.
2131+
*/
2132+
revealPositionAtDefinition(position: IPosition, scrollType?: ScrollType): void;
21232133
/**
21242134
* Returns the primary selection of the editor.
21252135
*/
@@ -2165,6 +2175,11 @@ declare namespace monaco.editor {
21652175
* Scroll vertically as necessary and reveal lines centered vertically only if it lies outside the viewport.
21662176
*/
21672177
revealLinesInCenterIfOutsideViewport(lineNumber: number, endLineNumber: number, scrollType?: ScrollType): void;
2178+
/**
2179+
* Scroll vertically as necessary and reveal lines close to the top of the viewport,
2180+
* optimized for viewing a code definition.
2181+
*/
2182+
revealLinesAtDefinition(lineNumber: number, endLineNumber: number, scrollType?: ScrollType): void;
21682183
/**
21692184
* Scroll vertically or horizontally as necessary and reveal a range.
21702185
*/
@@ -2181,6 +2196,11 @@ declare namespace monaco.editor {
21812196
* Scroll vertically or horizontally as necessary and reveal a range centered vertically only if it lies outside the viewport.
21822197
*/
21832198
revealRangeInCenterIfOutsideViewport(range: IRange, scrollType?: ScrollType): void;
2199+
/**
2200+
* Scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport,
2201+
* optimized for viewing a code definition.
2202+
*/
2203+
revealRangeAtDefinition(range: IRange, scrollType?: ScrollType): void;
21842204
/**
21852205
* Directly trigger a handler or an editor action.
21862206
* @param source The source of the call.

src/vs/platform/editor/common/editor.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ export interface ITextEditorOptions extends IEditorOptions {
202202

203203
/**
204204
* Option to scroll vertically or horizontally as necessary and reveal a range centered vertically only if it lies outside the viewport.
205+
* This can't be used in combination with revealAtDefinition.
205206
*/
206207
revealInCenterIfOutsideViewport?: boolean;
208+
209+
/**
210+
* Option to scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport,
211+
* optimized for viewing a code definition.
212+
*/
213+
revealAtDefinition?: boolean;
207214
}

src/vs/vscode.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,12 @@ declare module 'vscode' {
710710
/**
711711
* The range will always be revealed at the top of the viewport.
712712
*/
713-
AtTop = 3
713+
AtTop = 3,
714+
/**
715+
* The range will always be revealed close to the top of the viewport,
716+
* optimized for viewing a code definition.
717+
*/
718+
AtDefinition = 4,
714719
}
715720

716721
/**

src/vs/workbench/api/browser/mainThreadEditor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ export class MainThreadTextEditor {
415415
case TextEditorRevealType.AtTop:
416416
this._codeEditor.revealRangeAtTop(range, editorCommon.ScrollType.Smooth);
417417
break;
418+
case TextEditorRevealType.AtDefinition:
419+
this._codeEditor.revealRangeAtDefinition(range, editorCommon.ScrollType.Smooth);
420+
break;
418421
default:
419422
console.warn(`Unknown revealType: ${revealType}`);
420423
break;

0 commit comments

Comments
 (0)