/* * jEdit macro to open the root of the project the currently open * buffer belongs to in the jEdit file browser. * * Copyright 2010-2011 Ali Rantakari - http://hasseg.org * */ /** * Tries to find the root of the 'current project', assuming * all project files live under the same directory structure * and that they are all under the same version control * repository. * * For Git, Mercurial and Bazaar (and it seems Darcs?) this is * easy since they only have their 'dot-directory' at the root * of the structure but Subversion (and I assume CVS) put them * everywhere so we have to find the _last_ directory to have * one. * */ String findProjectRoot() { dir = new File(buffer.getDirectory()); previousDir = null; boolean previousDirHasSvnDir = false; boolean previousDirHasCvsDir = false; while (dir != null) { boolean hasSvnDir = false; boolean hasCvsDir = false; list = dir.list(); for (childName : list) { if (!hasSvnDir && childName.equals(".svn")) hasSvnDir = true; else if (!hasCvsDir && childName.equals("CVS")) hasCvsDir = true; else if (childName.equals(".git") || childName.equals(".hg") || childName.equals(".bzr") || childName.equals("_darcs") ) return dir.getAbsolutePath(); } if (previousDir != null && ((previousDirHasSvnDir && !hasSvnDir) || (previousDirHasCvsDir && !hasCvsDir)) ) return previousDir.getAbsolutePath(); previousDirHasSvnDir = hasSvnDir; previousDirHasCvsDir = hasCvsDir; previousDir = dir; dir = dir.getParentFile(); } return null; } root = findProjectRoot(); if (root == null) { VFSBrowser.browseDirectory(view, buffer.getDirectory()); view.getEditPane().focusOnTextArea(); view.getStatus().setMessageAndClear("Browser directory set to current buffer's location (cannot find project root.)."); } else { VFSBrowser.browseDirectory(view, root); view.getEditPane().focusOnTextArea(); view.getStatus().setMessageAndClear("Project root guessed; browser directory set to it."); }