Path: blob/main/extensions/markdown-language-features/src/preview/scrolling.ts
3292 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import * as vscode from 'vscode';56/**7* Change the top-most visible line of `editor` to be at `line`8*/9export function scrollEditorToLine(10line: number,11editor: vscode.TextEditor12) {13const revealRange = toRevealRange(line, editor);14editor.revealRange(revealRange, vscode.TextEditorRevealType.AtTop);15}1617function toRevealRange(line: number, editor: vscode.TextEditor): vscode.Range {18line = Math.max(0, line);19const sourceLine = Math.floor(line);20if (sourceLine >= editor.document.lineCount) {21return new vscode.Range(editor.document.lineCount - 1, 0, editor.document.lineCount - 1, 0);22}2324const fraction = line - sourceLine;25const text = editor.document.lineAt(sourceLine).text;26const start = Math.floor(fraction * text.length);27return new vscode.Range(sourceLine, start, sourceLine + 1, 0);28}2930export class StartingScrollFragment {31public readonly type = 'fragment';3233constructor(34public readonly fragment: string,35) { }36}3738export class StartingScrollLine {39public readonly type = 'line';4041constructor(42public readonly line: number,43) { }44}4546export type StartingScrollLocation = StartingScrollLine | StartingScrollFragment;474849