Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/markdown-language-features/src/preview/scrolling.ts
3292 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import * as vscode from 'vscode';
6
7
/**
8
* Change the top-most visible line of `editor` to be at `line`
9
*/
10
export function scrollEditorToLine(
11
line: number,
12
editor: vscode.TextEditor
13
) {
14
const revealRange = toRevealRange(line, editor);
15
editor.revealRange(revealRange, vscode.TextEditorRevealType.AtTop);
16
}
17
18
function toRevealRange(line: number, editor: vscode.TextEditor): vscode.Range {
19
line = Math.max(0, line);
20
const sourceLine = Math.floor(line);
21
if (sourceLine >= editor.document.lineCount) {
22
return new vscode.Range(editor.document.lineCount - 1, 0, editor.document.lineCount - 1, 0);
23
}
24
25
const fraction = line - sourceLine;
26
const text = editor.document.lineAt(sourceLine).text;
27
const start = Math.floor(fraction * text.length);
28
return new vscode.Range(sourceLine, start, sourceLine + 1, 0);
29
}
30
31
export class StartingScrollFragment {
32
public readonly type = 'fragment';
33
34
constructor(
35
public readonly fragment: string,
36
) { }
37
}
38
39
export class StartingScrollLine {
40
public readonly type = 'line';
41
42
constructor(
43
public readonly line: number,
44
) { }
45
}
46
47
export type StartingScrollLocation = StartingScrollLine | StartingScrollFragment;
48
49