1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/**
ToggleHeaderSource.bsh V1.6
by Alan Ezust
$Id: Toggle_Header_Source.bsh 22405 2012-10-26 17:13:58Z ezust $
A jedit beanshell macro that toggles your current buffer
between the header file (.hh?) and the source file (.c(c|pp|xx)?).
Enables you to switch the current text
buffer between C/C++ header and sourcecode
file. If the file does not already exist, it opens a new buffer
of that name for you.
New to 1.2: If another buffer is already open with the requested
filename, it is selected, even if located in another directory.
New to 1.3: If another EditPane is already open with the
requested buffer, it is selected and focused.
New to 1.4: Instead of throwing an exception, treats
files with no extension as header files, and tries to
open .cpp file with same basename. Searches in
current project for header/source files too.
New to 1.5: Supports multiple, default header extensions.
New to 1.6: Does nothing when not in the correct mode (c or c++)
Fixed a bug where it did not work without projectviewer installed.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the jEdit program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
import projectviewer.*;
import projectviewer.vpt.*;
import java.util.*;
/* Set these properties via the console beanshell to something else if cpp,h are not
your favorite extensions. e.g.:
jEdit.setProperty("default.extension.c", "cc");
jEdit.setProperty("default.extension.h", "hh");
*/
String defaultSourceExtension = jEdit.getProperty("default.extension.c", "cpp");
String defaultHeaderExtension = jEdit.getProperty("default.extension.h", "h");
String[] sourceExtensions = new String[]{"cpp", "cxx", "cc", "c"};
String[] headerExtensions = new String[]{"h", "hh", "hpp", "hxx"};
/** Looks for an existing file in the current project, if ProjectViewer is
installed and a 'current' project exists.
@return the absolute path if found, NULL if not.
*/
String findBufferInProject(String fileName) {
fileName = MiscUtilities.getFileName(fileName);
VPTProject project = ProjectViewer.getActiveProject(view);
Collection nodes = project.getOpenableNodes();
for (VPTNode node: nodes) {
String path = node.getNodePath();
if ((path.endsWith("\\" + fileName)) || (path.endsWith("/" + fileName)))
return path;
}
return null;
}
/** Looks for an existing file in the same directory, or
an already open buffer with the given name in another directory.
@return the absolute path if found, NULL if not.
*/
String findBuffer(String fileName) {
File f = new File(fileName);
if (f.canRead()) return f.getPath();
String fn2 = f.getName();
Buffer[] bufs = jEdit.getBuffers();
bn = bufs.length;
for (int i=0; i<bn; ++i) {
Buffer b = bufs[i];
if (b.getName().equals(fn2)) return b.getPath();
}
return null;
}
/** Returns true if PV is installed and a project is selected (current)
*/
boolean activeProjectExists()
{
EditPlugin p = jEdit.getPlugin("projectviewer.ProjectPlugin",false);
if(p == null)
return false;
VPTProject project = ProjectViewer.getActiveProject(view);
return (project != null);
}
/** Given a header file, iterates through all sourcefile extensions checking if the sourcefile exists.
@return the absolute path of the sourcefile for this header
*/
String getSourceFile(String baseName)
{
int numExt = sourceExtensions.length;
// First try open buffers
for (int i=numExt-1; i>=0; --i)
{
String ext = sourceExtensions[i];
String tryFile = baseName + "." + ext;
String path = findBuffer(tryFile);
if (path != null) return path;
}
// Then try current project
if (activeProjectExists())
{
for (int i=numExt-1; i>=0; --i)
{
String ext = sourceExtensions[i];
String tryFile = baseName + "." + ext;
String path = findBufferInProject(tryFile);
if (path != null) return path;
}
}
return baseName + "." + defaultSourceExtension;
}
String getHeaderFile(String baseName)
{
int numExt = headerExtensions.length;
for (int i=numExt-1; i>=0; --i)
{
String ext = headerExtensions[i];
String tryFile = baseName + "." + ext;
String path = findBuffer(tryFile);
if (path != null) return path;
}
// Try no extension at all:
String path = findBuffer(baseName);
if (path != null) return path;
// Then try current project
if (activeProjectExists()) {
for (int i=numExt-1; i>=0; --i)
{
String ext = headerExtensions[i];
String tryFile = baseName + "." + ext;
path = findBufferInProject(tryFile);
if (path != null) return path;
}
// No extension at all in project:
path = findBufferInProject(baseName);
if (path != null) return path;
}
return baseName + "." + defaultHeaderExtension;
}
boolean isSourceFile(String extension)
{
for (int i=0; i<sourceExtensions.length; ++i) {
if (extension.equals(sourceExtensions[i])) return true;
}
return false;
}
void toggleHeaderSource()
{
String mode = buffer.getMode().toString();
if (! (mode.equals("c") || mode.equals("c++"))) return;
String currentFile = buffer.getPath();
int pos = currentFile.lastIndexOf('.');
String path = null;
if (pos < 0) {
path = getSourceFile(currentFile);
}
else {
String baseName = currentFile.substring(0, pos);
String extension = currentFile.substring(pos+1);
if (isSourceFile(extension))
{
path = getHeaderFile(baseName);
if (path == null)
path = baseName + "." + defaultHeaderExtension;
}
else
{
path = getSourceFile(baseName);
}
}
if (path == null) return;
// see if it is already open in another editpane
panes = view.getEditPanes();
if (panes.length > 1)
for (int i=panes.length - 1; i >= 0; --i) {
buf = panes[i].getBuffer();
if (buf.getName().equals(path)
|| buf.getPath().equals(path)) {
panes[i].focusOnTextArea();
return;
}
}
jEdit.openFile(view, path);
}
toggleHeaderSource();
|