Skip to content

Commit 45d0689

Browse files
committed
Fixing the bug related to lose file after changing file path due to reverting project (#TODO: filename change. See diff)
1 parent f92b1cf commit 45d0689

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

src/com/reveal/codetimemachine/Commits3DView.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import com.intellij.openapi.project.Project;
1212
import com.intellij.openapi.ui.ComboBox;
1313
import com.intellij.openapi.vfs.VirtualFile;
14+
import com.intellij.psi.PsiFile;
15+
import com.intellij.psi.search.EverythingGlobalScope;
16+
import com.intellij.psi.search.FilenameIndex;
1417
import com.intellij.ui.EditorTextField;
1518
import com.reveal.metrics.CKNumberReader;
1619
import com.reveal.metrics.MaxCKNumber;
17-
import com.reveal.metrics.Metrics;
1820
import org.jetbrains.annotations.NotNull;
1921
import org.jetbrains.annotations.Nullable;
2022

@@ -360,6 +362,8 @@ public void actionPerformed(ActionEvent e)
360362
else
361363
gitHelper.checkoutCommitID(commitList.get(TTMWindow.activeCommit_cIndex).getCommitID());
362364

365+
updateVirtualFileIfNeeded();
366+
363367
checkoutProjectLatestCommitBtn.setText("*"+CHECKOUT_LATEST_PROJECT_COMMIT_BUTTON_TEXT);
364368
CodeTimeMachineAction.getCodeTimeMachine(project).getToolWindow().hide(null);
365369
}
@@ -394,6 +398,29 @@ public void actionPerformed(ActionEvent e)
394398
this.add(checkoutProjectLatestCommitBtn); // As there's no layout, we should set Bound it. we'll do this in "ComponentResized()" event
395399
}
396400

401+
void updateVirtualFileIfNeeded()
402+
{
403+
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
404+
// if after reverting project, package names or ... changes the virtualFile path get invalid.
405+
// So we search for the new path of file.
406+
// Since we assume filename has not changed, we use valid part of old virtualFile (the filename).
407+
if(document==null)
408+
{
409+
PsiFile[] filesByName = FilenameIndex.getFilesByName(project, virtualFile.getName(), new EverythingGlobalScope(project));
410+
if(filesByName.length>0)
411+
{
412+
virtualFile = filesByName[0].getVirtualFile();
413+
document = FileDocumentManager.getInstance().getDocument(virtualFile);
414+
415+
if(document == null)
416+
{
417+
// It means that the filename has also changed.
418+
//#TODO: in such case, we can't renew virtualFile variable.
419+
}
420+
}
421+
}
422+
}
423+
397424
private void addMouseMotionListener()
398425
{
399426
this.addMouseMotionListener(new MouseMotionListener()

0 commit comments

Comments
 (0)