123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- const vscode = require('vscode');
- const path = require('path');
- async function getFilePath() {
- return `/home/user/work/itk-extensions/server.js`
- }
- function setScroll(item, editor) {
- function setSelections() {
- const selections = item.selections.map(selection => {
- const start = new vscode.Position(selection.startLine - 1, selection.startChar - 1);
- const end = new vscode.Position(selection.endLine - 1, selection.endChar - 1);
- return new vscode.Selection(start, end);
- });
- editor.selections = selections;
- }
- function setCoursor() {
- const cursorPosition = new vscode.Position(item.cursor.line - 1, item.cursor.character - 1);
- editor.selection = new vscode.Selection(cursorPosition, cursorPosition);
- }
- if (item.selections && item.selections.length > 0) {
- setTimeout(() => {
- setSelections()
- }, 500);
- // setSelections()
- // setTimeout(() =>{
- // setSelections()
- // })
- }
- // Restore cursor position
- // if (item.cursor) {
- // setCoursor()
- // setTimeout(() => {
- // setCoursor()
- // }, 100);
- // }
- // Restore scroll position
- function setScr() {
- try {
- let lineTo = (+item.scroll.firstVisibleLine + +item.scroll.lastVisibleLine) / 2;
- const range = new vscode.Range(lineTo, 0, lineTo, 0);
- editor.revealRange(range, vscode.TextEditorRevealType.AtTop);
- } catch (e) {
- }
- }
- if (item.scroll) {
- setScr()
- setTimeout(async () => {
- setScr()
- }, 300)
- }
- }
- function openTab(filePath, item, groupTabs, groupId) {
- function onAction(editor) {
- let lastTab = groupTabs[groupId] || 1;
- groupTabs[groupId] = lastTab + 1;
- setScroll(item, editor); // Если уже открыт, просто применяем действие
- }
- // Проверяем, открыт ли уже файл в нужной группе
- const uri = vscode.Uri.file(filePath);
- const targetEditor = vscode.window.visibleTextEditors.find(editor => {
- let isOk = editor.document.uri.fsPath === filePath.path &&
- editor.viewColumn === groupId;
- return isOk;
- }
- );
- if (targetEditor) {
- return onAction(targetEditor)
- }
- return vscode.workspace.openTextDocument(filePath)
- .then(doc => vscode.window.showTextDocument(doc, { viewColumn: groupId, preview: false }))
- .then(async editor => {
- onAction(editor)
- })
- }
- async function gitRun(filePath) {
- // const uri = vscode.Uri.file(filePath);
- const gitExtension = vscode.extensions.getExtension('vscode.git');
- // if (!gitExtension) {
- // vscode.window.showErrorMessage("🚨 Git extension is not available.");
- // return;
- // }
- // Активируем Git API
- // const gitApi = await gitExtension.activate();
- // if (!gitApi || !gitApi.repositories || gitApi.repositories.length === 0) {
- // vscode.window.showErrorMessage("🚨 No Git repositories found.");
- // return;
- // }
- // // Находим репозиторий, к которому относится файл
- // const repo = gitApi.repositories.find(r => uri.fsPath.startsWith(r.rootUri.fsPath));
- // if (!repo) {
- // vscode.window.showErrorMessage("🚨 File is not in a Git repository.");
- // return;
- // }
- // // Получаем Git URI для версии HEAD
- // const gitUri = repo.toGitUri(uri, 'HEAD');
- // return gitUri;
- // Открываем файл в VS Code
- // vscode.workspace.openTextDocument(gitUri)
- // .then(doc => vscode.window.showTextDocument(doc))
- }
- // setInterval(() => {
- // delete require.cache[require.resolve('./files/openTabs')];
- // let groups = require('./files/openTabs')
- // restoreByArr(groups.tabs)
- // }, 500)
- async function restoreByArr(groups) {
- await vscode.extensions.getExtension('vscode.git')
- const cGroups = vscode.window.tabGroups.all;
- let groupTabs = {};
- const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0];
- if (!workspaceFolder) {
- vscode.window.showErrorMessage('No workspace folder is open. Please open a folder or workspace.');
- return;
- }
- function openGit(item, groupId) {
- const filePath = vscode.Uri.joinPath(workspaceFolder.uri, item.relPath);
- let gitOrig = decodeURIComponent(item.original);
- const fileUri = vscode.Uri.parse(`${filePath.path}`);
- let gUri = vscode.Uri.parse(gitOrig);
- vscode.commands.executeCommand('vscode.diff', fileUri, gUri, `${item.relPath} (Working Tree)`, {
- viewColumn: groupId, // Открываем в указанной группе вкладок
- preview: true
- }).then(async e2 => {
- await new Promise(resolve => setTimeout(resolve, 500));
- const editor = vscode.window.visibleTextEditors.find(editor =>
- editor.document.uri.toString() === fileUri.toString()
- );
- if (editor) {
- setScroll(item, editor)
- } else {
- // vscode.window.showErrorMessage("🚨 Failed to find the opened diff editor.");
- }
- })
- }
- let gind = {}
- groups.forEach(async (group, groupIndex) => {
- const groupId = groupIndex + 1; // Group ID corresponds to column
- let cTabs = (cGroups[groupIndex] || {}).tabs || [];
- gind[groupIndex] = {};
- let _filePath;
- group.filter(item => item.relPath).forEach(async (item, index) => {
- const filePath = vscode.Uri.joinPath(workspaceFolder.uri, item.relPath);
- _filePath = filePath;
- let vsTab;
- if (item.inputType == 'git') {
- let matchIndex = -1;
- let absPath = filePath?.fsPath;
- _filePath = absPath;
- cTabs.forEach((tab, ind) => {
- let isOk = tab?.input?.original?.path?.toString() == absPath;
- if (isOk) {
- matchIndex = ind
- gind[groupIndex][ind] = true;
- }
- })
- console.log('******', cTabs, gind[groupIndex])
- if (matchIndex == -1) {
- console.log('math index is -1!!!')
- openGit(item, groupId)
- }
- } else {
- let matchIndex = -1;
- let absPath = filePath.path
- cTabs.forEach((tab, ind) => {
- if (tab?.input?.uri?.fsPath == absPath) {
- matchIndex = ind
- gind[groupIndex][ind] = true
- }
- })
- if (matchIndex == -1) {
- vsTab = openTab(filePath, item, groupTabs, groupId)
- }
- global.tabsInfo[absPath] = {
- selections: item.selections, cursor: item.cursor, scroll: item.scroll
- }
- }
- });
- let item = group.find(it => it.isActive);
- if (item) {
- if (!/git/gi.test(item?.inputType.toString())) {
- openTab(vscode.Uri.joinPath(workspaceFolder.uri, item.relPath), item, groupTabs, groupId)
- } else {
- openGit(item, groupId)
- }
- }
- const tabsToClose = cTabs.filter((tab, ind) => {
- // console.log("CTAB -----", goodInd, goodInd[ind], ind, tab.input?.original?.path)
- return !gind[groupIndex][ind]
- });
- console.log(tabsToClose, ".................. GMI ]]] ", { groupIndex }, gind[groupIndex], cTabs.map(it => it.input), _filePath)
- closeTabs(tabsToClose, gind)
- });
- cGroups.forEach((group, ind) => {
- if (ind >= groups.length) {
- closeTabs(group.tabs)
- }
- })
- }
- function pub(tabs) {
- return tabs.map(it => it.label)
- }
- function closeTabs(tabsToClose, goodInd) {
- try {
- if (tabsToClose.length > 0) {
- console.log('tabsToClose!!!!!')
- console.log('tabsToClose!!!!!')
- console.log('tabsToClose!!!!!')
- console.log('tabsToClose!!!!!', goodInd)
- console.log('tabsToClose!!!!!', tabsToClose.map(it => it.label))
- vscode.window.tabGroups.close(tabsToClose).then();
- }
- } catch (e) {
- }
- }
- module.exports = { restoreByArr };
|