mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-07-23 12:45:04 +00:00
fix: scroll source correctly when no text editor is active (#395)
This commit is contained in:
parent
1d1f4bf6e5
commit
1e424eca8a
3 changed files with 29 additions and 13 deletions
|
@ -33,7 +33,7 @@ import {
|
|||
previewPreload,
|
||||
previewProcessOutline,
|
||||
} from "./preview";
|
||||
import { DisposeList } from "./util";
|
||||
import { DisposeList, getSensibleTextEditorColumn } from "./util";
|
||||
import { client, setClient } from "./lsp";
|
||||
|
||||
let previewIsEnabled = false;
|
||||
|
@ -146,19 +146,24 @@ async function startClient(context: ExtensionContext): Promise<void> {
|
|||
end: [number, number] | null;
|
||||
}
|
||||
client.onNotification("tinymist/preview/scrollSource", async (jump: JumpInfo) => {
|
||||
const activeEditor = window.activeTextEditor;
|
||||
if (!activeEditor) {
|
||||
return;
|
||||
}
|
||||
console.log(
|
||||
"recv editorScrollTo request",
|
||||
jump,
|
||||
"active",
|
||||
window.activeTextEditor !== undefined,
|
||||
"documents",
|
||||
vscode.workspace.textDocuments.map((doc) => doc.uri.fsPath)
|
||||
);
|
||||
|
||||
console.log("recv editorScrollTo request", jump);
|
||||
if (jump.start === null || jump.end === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// open this file and show in editor
|
||||
const doc = await vscode.workspace.openTextDocument(jump.filepath);
|
||||
const editor = await vscode.window.showTextDocument(doc, activeEditor.viewColumn);
|
||||
const doc =
|
||||
vscode.workspace.textDocuments.find((doc) => doc.uri.fsPath === jump.filepath) ||
|
||||
(await vscode.workspace.openTextDocument(jump.filepath));
|
||||
const editor = await vscode.window.showTextDocument(doc, getSensibleTextEditorColumn());
|
||||
const startPosition = new vscode.Position(jump.start[0], jump.start[1]);
|
||||
const endPosition = new vscode.Position(jump.end[0], jump.end[1]);
|
||||
const range = new vscode.Range(startPosition, endPosition);
|
||||
|
|
|
@ -293,9 +293,9 @@ async function launchPreviewLsp(task: LaunchInBrowserTask | LaunchInWebViewTask)
|
|||
const editor = e.textEditor;
|
||||
const kind = e.kind;
|
||||
|
||||
console.log(
|
||||
`selection changed, kind: ${kind && vscode.TextEditorSelectionChangeKind[kind]}`
|
||||
);
|
||||
// console.log(
|
||||
// `selection changed, kind: ${kind && vscode.TextEditorSelectionChangeKind[kind]}`
|
||||
// );
|
||||
const shouldScrollPanel =
|
||||
// scroll by mouse
|
||||
kind === vscode.TextEditorSelectionChangeKind.Mouse ||
|
||||
|
@ -303,7 +303,7 @@ async function launchPreviewLsp(task: LaunchInBrowserTask | LaunchInWebViewTask)
|
|||
(scrollSyncMode === ScrollSyncModeEnum.onSelectionChange &&
|
||||
kind === vscode.TextEditorSelectionChangeKind.Keyboard);
|
||||
if (shouldScrollPanel) {
|
||||
console.log(`selection changed, sending src2doc jump request`);
|
||||
// console.log(`selection changed, sending src2doc jump request`);
|
||||
reportPosition(editor, "panelScrollTo");
|
||||
}
|
||||
|
||||
|
@ -571,7 +571,7 @@ class OutlineProvider implements vscode.TreeDataProvider<OutlineItem> {
|
|||
|
||||
outline: { items: OutlineItemData[] } | undefined = undefined;
|
||||
postOutlineItem(outline: any) {
|
||||
console.log("postOutlineItemProvider", outline);
|
||||
// console.log("postOutlineItemProvider", outline);
|
||||
this.outline = outline;
|
||||
this.refresh();
|
||||
}
|
||||
|
|
|
@ -13,6 +13,17 @@ export function getTargetViewColumn(viewColumn: ViewColumn | undefined): ViewCol
|
|||
return ViewColumn.Beside;
|
||||
}
|
||||
|
||||
export function getSensibleTextEditorColumn(): ViewColumn {
|
||||
let editor = vscode.window.activeTextEditor;
|
||||
if (!editor) {
|
||||
// first visible editor
|
||||
if (vscode.window.visibleTextEditors.length > 0) {
|
||||
editor = vscode.window.visibleTextEditors[0];
|
||||
}
|
||||
}
|
||||
return editor?.viewColumn !== undefined ? editor.viewColumn : ViewColumn.Beside;
|
||||
}
|
||||
|
||||
export async function loadHTMLFile(context: vscode.ExtensionContext, relativePath: string) {
|
||||
const filePath = path.resolve(context.extensionPath, relativePath);
|
||||
const fileContents = await readFile(filePath, "utf8");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue