restoreByArr.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. const vscode = require('vscode');
  2. const path = require('path');
  3. async function getFilePath() {
  4. return `/home/user/work/itk-extensions/server.js`
  5. }
  6. function setScroll(item, editor) {
  7. function setSelections() {
  8. const selections = item.selections.map(selection => {
  9. const start = new vscode.Position(selection.startLine - 1, selection.startChar - 1);
  10. const end = new vscode.Position(selection.endLine - 1, selection.endChar - 1);
  11. return new vscode.Selection(start, end);
  12. });
  13. editor.selections = selections;
  14. }
  15. function setCoursor() {
  16. const cursorPosition = new vscode.Position(item.cursor.line - 1, item.cursor.character - 1);
  17. editor.selection = new vscode.Selection(cursorPosition, cursorPosition);
  18. }
  19. if (item.selections && item.selections.length > 0) {
  20. setTimeout(() => {
  21. setSelections()
  22. }, 500);
  23. // setSelections()
  24. // setTimeout(() =>{
  25. // setSelections()
  26. // })
  27. }
  28. // Restore cursor position
  29. // if (item.cursor) {
  30. // setCoursor()
  31. // setTimeout(() => {
  32. // setCoursor()
  33. // }, 100);
  34. // }
  35. // Restore scroll position
  36. function setScr() {
  37. try {
  38. let lineTo = (+item.scroll.firstVisibleLine + +item.scroll.lastVisibleLine) / 2;
  39. const range = new vscode.Range(lineTo, 0, lineTo, 0);
  40. editor.revealRange(range, vscode.TextEditorRevealType.AtTop);
  41. } catch (e) {
  42. }
  43. }
  44. if (item.scroll) {
  45. setScr()
  46. setTimeout(async () => {
  47. setScr()
  48. }, 300)
  49. }
  50. }
  51. function openTab(filePath, item, groupTabs, groupId) {
  52. function onAction(editor) {
  53. let lastTab = groupTabs[groupId] || 1;
  54. groupTabs[groupId] = lastTab + 1;
  55. setScroll(item, editor); // Если уже открыт, просто применяем действие
  56. }
  57. // Проверяем, открыт ли уже файл в нужной группе
  58. const uri = vscode.Uri.file(filePath);
  59. const targetEditor = vscode.window.visibleTextEditors.find(editor => {
  60. let isOk = editor.document.uri.fsPath === filePath.path &&
  61. editor.viewColumn === groupId;
  62. return isOk;
  63. }
  64. );
  65. if (targetEditor) {
  66. return onAction(targetEditor)
  67. }
  68. return vscode.workspace.openTextDocument(filePath)
  69. .then(doc => vscode.window.showTextDocument(doc, { viewColumn: groupId, preview: false }))
  70. .then(async editor => {
  71. onAction(editor)
  72. })
  73. }
  74. async function gitRun(filePath) {
  75. // const uri = vscode.Uri.file(filePath);
  76. const gitExtension = vscode.extensions.getExtension('vscode.git');
  77. // if (!gitExtension) {
  78. // vscode.window.showErrorMessage("🚨 Git extension is not available.");
  79. // return;
  80. // }
  81. // Активируем Git API
  82. // const gitApi = await gitExtension.activate();
  83. // if (!gitApi || !gitApi.repositories || gitApi.repositories.length === 0) {
  84. // vscode.window.showErrorMessage("🚨 No Git repositories found.");
  85. // return;
  86. // }
  87. // // Находим репозиторий, к которому относится файл
  88. // const repo = gitApi.repositories.find(r => uri.fsPath.startsWith(r.rootUri.fsPath));
  89. // if (!repo) {
  90. // vscode.window.showErrorMessage("🚨 File is not in a Git repository.");
  91. // return;
  92. // }
  93. // // Получаем Git URI для версии HEAD
  94. // const gitUri = repo.toGitUri(uri, 'HEAD');
  95. // return gitUri;
  96. // Открываем файл в VS Code
  97. // vscode.workspace.openTextDocument(gitUri)
  98. // .then(doc => vscode.window.showTextDocument(doc))
  99. }
  100. // setInterval(() => {
  101. // delete require.cache[require.resolve('./files/openTabs')];
  102. // let groups = require('./files/openTabs')
  103. // restoreByArr(groups.tabs)
  104. // }, 500)
  105. async function restoreByArr(groups) {
  106. await vscode.extensions.getExtension('vscode.git')
  107. const cGroups = vscode.window.tabGroups.all;
  108. let groupTabs = {};
  109. const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0];
  110. if (!workspaceFolder) {
  111. vscode.window.showErrorMessage('No workspace folder is open. Please open a folder or workspace.');
  112. return;
  113. }
  114. function openGit(item, groupId) {
  115. const filePath = vscode.Uri.joinPath(workspaceFolder.uri, item.relPath);
  116. let gitOrig = decodeURIComponent(item.original);
  117. const fileUri = vscode.Uri.parse(`${filePath.path}`);
  118. let gUri = vscode.Uri.parse(gitOrig);
  119. vscode.commands.executeCommand('vscode.diff', fileUri, gUri, `${item.relPath} (Working Tree)`, {
  120. viewColumn: groupId, // Открываем в указанной группе вкладок
  121. preview: true
  122. }).then(async e2 => {
  123. await new Promise(resolve => setTimeout(resolve, 500));
  124. const editor = vscode.window.visibleTextEditors.find(editor =>
  125. editor.document.uri.toString() === fileUri.toString()
  126. );
  127. if (editor) {
  128. setScroll(item, editor)
  129. } else {
  130. // vscode.window.showErrorMessage("🚨 Failed to find the opened diff editor.");
  131. }
  132. })
  133. }
  134. let gind = {}
  135. groups.forEach(async (group, groupIndex) => {
  136. const groupId = groupIndex + 1; // Group ID corresponds to column
  137. let cTabs = (cGroups[groupIndex] || {}).tabs || [];
  138. gind[groupIndex] = {};
  139. let _filePath;
  140. group.filter(item => item.relPath).forEach(async (item, index) => {
  141. const filePath = vscode.Uri.joinPath(workspaceFolder.uri, item.relPath);
  142. _filePath = filePath;
  143. let vsTab;
  144. if (item.inputType == 'git') {
  145. let matchIndex = -1;
  146. let absPath = filePath?.fsPath;
  147. _filePath = absPath;
  148. cTabs.forEach((tab, ind) => {
  149. let isOk = tab?.input?.original?.path?.toString() == absPath;
  150. if (isOk) {
  151. matchIndex = ind
  152. gind[groupIndex][ind] = true;
  153. }
  154. })
  155. console.log('******', cTabs, gind[groupIndex])
  156. if (matchIndex == -1) {
  157. console.log('math index is -1!!!')
  158. openGit(item, groupId)
  159. }
  160. } else {
  161. let matchIndex = -1;
  162. let absPath = filePath.path
  163. cTabs.forEach((tab, ind) => {
  164. if (tab?.input?.uri?.fsPath == absPath) {
  165. matchIndex = ind
  166. gind[groupIndex][ind] = true
  167. }
  168. })
  169. if (matchIndex == -1) {
  170. vsTab = openTab(filePath, item, groupTabs, groupId)
  171. }
  172. global.tabsInfo[absPath] = {
  173. selections: item.selections, cursor: item.cursor, scroll: item.scroll
  174. }
  175. }
  176. });
  177. let item = group.find(it => it.isActive);
  178. if (item) {
  179. if (!/git/gi.test(item?.inputType.toString())) {
  180. openTab(vscode.Uri.joinPath(workspaceFolder.uri, item.relPath), item, groupTabs, groupId)
  181. } else {
  182. openGit(item, groupId)
  183. }
  184. }
  185. const tabsToClose = cTabs.filter((tab, ind) => {
  186. // console.log("CTAB -----", goodInd, goodInd[ind], ind, tab.input?.original?.path)
  187. return !gind[groupIndex][ind]
  188. });
  189. console.log(tabsToClose, ".................. GMI ]]] ", { groupIndex }, gind[groupIndex], cTabs.map(it => it.input), _filePath)
  190. closeTabs(tabsToClose, gind)
  191. });
  192. cGroups.forEach((group, ind) => {
  193. if (ind >= groups.length) {
  194. closeTabs(group.tabs)
  195. }
  196. })
  197. }
  198. function pub(tabs) {
  199. return tabs.map(it => it.label)
  200. }
  201. function closeTabs(tabsToClose, goodInd) {
  202. try {
  203. if (tabsToClose.length > 0) {
  204. console.log('tabsToClose!!!!!')
  205. console.log('tabsToClose!!!!!')
  206. console.log('tabsToClose!!!!!')
  207. console.log('tabsToClose!!!!!', goodInd)
  208. console.log('tabsToClose!!!!!', tabsToClose.map(it => it.label))
  209. vscode.window.tabGroups.close(tabsToClose).then();
  210. }
  211. } catch (e) {
  212. }
  213. }
  214. module.exports = { restoreByArr };