123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- const vscode = require('vscode');
- const path = require('path');
- async function getOpenedTabs() {
- const tabGroups = vscode.window.tabGroups.all;
- if (tabGroups.length === 0) {
- vscode.window.showInformationMessage('No open tabs found.');
- return;
- }
- let tabInfo = [];
- const workspaceFolder = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
- for (const group of tabGroups) {
- let curTabsInGroup = [];
- for (const tab of group.tabs) {
- let filePath = "";
- let relativePath = "";
- let inputType = "unknown";
- let cursorPosition = null;
- let selectionInfo = null;
- let scrollInfo = null;
- let scrollInfo2 = null;
- let original = ''
- if (tab.input.modified) {
- filePath = tab.input.original.path;
- relativePath = workspaceFolder ? path.relative(workspaceFolder, filePath) : filePath;
- original = tab.input.original._formatted;
- inputType = 'git'
-
- } else if (tab.input instanceof vscode.TabInputText) {
- filePath = tab.input.uri.fsPath;
- relativePath = workspaceFolder ? path.relative(workspaceFolder, filePath) : filePath;
- inputType = "text";
- } else if (tab.input instanceof vscode.TabInputNotebook) {
- inputType = "notebook";
- } else if (tab.input instanceof vscode.TabInputCustom) {
- inputType = "custom";
- } else if (tab.input instanceof vscode.TabInputTerminal) {
- inputType = "terminal";
- } else if (tab.input instanceof vscode.TabInputWebview) {
- // Check if the tab is related to Git
- if (tab.label.includes("Source Control") || tab.label.includes("SCM") || tab.label.includes("Git")) {
- inputType = "git";
- } else {
- inputType = "webview";
- }
- }
- let activeEditor = vscode.window.visibleTextEditors.find(ed => ed.document.uri.toString() === filePath);
- activeEditor = activeEditor || vscode.window.activeTextEditor;
- if (activeEditor && activeEditor.document.uri.fsPath === filePath) {
- const cursor = activeEditor.selection.active;
- cursorPosition = { line: cursor.line + 1, character: cursor.character + 1 };
- const selections = activeEditor.selections.map(selection => {
- return {
- startLine: selection.start.line + 1,
- startChar: selection.start.character + 1,
- endLine: selection.end.line + 1,
- endChar: selection.end.character + 1,
- }});
- const visibleRanges = activeEditor.visibleRanges;
- scrollInfo = {
- firstVisibleLine: visibleRanges[0]?.start.line + 1 || null,
- lastVisibleLine: visibleRanges[0]?.end.line + 1 || null,
- };
-
-
- selectionInfo = selections;
- global.tabsInfo[filePath] = {selectionInfo, scrollInfo, cursorPosition}
- } else {
- let it = global.tabsInfo[filePath] || {}
- selectionInfo = it?.selectionInfo;
- scrollInfo = it?.scrollInfo;
- cursorPosition = it?.cursorPosition;
- }
- curTabsInGroup.push({
- group: tab.group.viewColumn,
- label: tab.label,
- isActive: tab.isActive,
- isPinned: tab.isPinned,
- absPath: filePath,
- relPath: relativePath,
- original: original,
- inputType: inputType,
- cursor: cursorPosition,
- selections: selectionInfo,
- scroll: scrollInfo,
- });
- }
- tabInfo.push(curTabsInGroup);
- }
- vscode.window.showInformationMessage(`Opened Tabs:\n${JSON.stringify(tabInfo, null, 4)}`);
- return { tabs: tabInfo };
- }
- module.exports = {
- getOpenedTabs
- };
|