getOpenedTabs.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const vscode = require('vscode');
  2. const path = require('path');
  3. async function getOpenedTabs() {
  4. const tabGroups = vscode.window.tabGroups.all;
  5. if (tabGroups.length === 0) {
  6. vscode.window.showInformationMessage('No open tabs found.');
  7. return;
  8. }
  9. let tabInfo = [];
  10. const workspaceFolder = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
  11. for (const group of tabGroups) {
  12. let curTabsInGroup = [];
  13. for (const tab of group.tabs) {
  14. let filePath = "";
  15. let relativePath = "";
  16. let inputType = "unknown";
  17. let cursorPosition = null;
  18. let selectionInfo = null;
  19. let scrollInfo = null;
  20. let scrollInfo2 = null;
  21. let original = ''
  22. if (tab.input.modified) {
  23. filePath = tab.input.original.path;
  24. relativePath = workspaceFolder ? path.relative(workspaceFolder, filePath) : filePath;
  25. original = tab.input.original._formatted;
  26. inputType = 'git'
  27. } else if (tab.input instanceof vscode.TabInputText) {
  28. filePath = tab.input.uri.fsPath;
  29. relativePath = workspaceFolder ? path.relative(workspaceFolder, filePath) : filePath;
  30. inputType = "text";
  31. } else if (tab.input instanceof vscode.TabInputNotebook) {
  32. inputType = "notebook";
  33. } else if (tab.input instanceof vscode.TabInputCustom) {
  34. inputType = "custom";
  35. } else if (tab.input instanceof vscode.TabInputTerminal) {
  36. inputType = "terminal";
  37. } else if (tab.input instanceof vscode.TabInputWebview) {
  38. // Check if the tab is related to Git
  39. if (tab.label.includes("Source Control") || tab.label.includes("SCM") || tab.label.includes("Git")) {
  40. inputType = "git";
  41. } else {
  42. inputType = "webview";
  43. }
  44. }
  45. let activeEditor = vscode.window.visibleTextEditors.find(ed => ed.document.uri.toString() === filePath);
  46. activeEditor = activeEditor || vscode.window.activeTextEditor;
  47. if (activeEditor && activeEditor.document.uri.fsPath === filePath) {
  48. const cursor = activeEditor.selection.active;
  49. cursorPosition = { line: cursor.line + 1, character: cursor.character + 1 };
  50. const selections = activeEditor.selections.map(selection => {
  51. return {
  52. startLine: selection.start.line + 1,
  53. startChar: selection.start.character + 1,
  54. endLine: selection.end.line + 1,
  55. endChar: selection.end.character + 1,
  56. }});
  57. const visibleRanges = activeEditor.visibleRanges;
  58. scrollInfo = {
  59. firstVisibleLine: visibleRanges[0]?.start.line + 1 || null,
  60. lastVisibleLine: visibleRanges[0]?.end.line + 1 || null,
  61. };
  62. selectionInfo = selections;
  63. global.tabsInfo[filePath] = {selectionInfo, scrollInfo, cursorPosition}
  64. } else {
  65. let it = global.tabsInfo[filePath] || {}
  66. selectionInfo = it?.selectionInfo;
  67. scrollInfo = it?.scrollInfo;
  68. cursorPosition = it?.cursorPosition;
  69. }
  70. curTabsInGroup.push({
  71. group: tab.group.viewColumn,
  72. label: tab.label,
  73. isActive: tab.isActive,
  74. isPinned: tab.isPinned,
  75. absPath: filePath,
  76. relPath: relativePath,
  77. original: original,
  78. inputType: inputType,
  79. cursor: cursorPosition,
  80. selections: selectionInfo,
  81. scroll: scrollInfo,
  82. });
  83. }
  84. tabInfo.push(curTabsInGroup);
  85. }
  86. vscode.window.showInformationMessage(`Opened Tabs:\n${JSON.stringify(tabInfo, null, 4)}`);
  87. return { tabs: tabInfo };
  88. }
  89. module.exports = {
  90. getOpenedTabs
  91. };