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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/**
Create_Constructor.bsh - a BeanShell macro for the jEdit text editor that
creates a constructor containing the selected variables. This code has the
similar limitations as Get_Class_Name; it merely looks backwards for the nearest
occurance of the keyword 'class," etc.
Copyright (C) 2004 Thomas Galvin - software@thomas-galvin.com
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.
*/
//Localization
final static String UnevenNumberMessage = jEdit.getProperty("macro.rs.CreateConstructor.UnevenNumber.message", "Uneven number of type names and variables.");
final static String NotEditableMessage = jEdit.getProperty("macro.rs.general.ErrorNotEditableDialog.message", "Buffer is not editable");
// Process
boolean JAVA_MODE = buffer.getMode().getName().toLowerCase().equals("java");
String UNDEFINED = "UNKNOWN_CLASS";
void setCaret(int selectionStart, int selectionEnd)
{
textArea.setCaretPosition(selectionStart);
textArea.moveCaretPosition(selectionEnd);
}
String getClassName()
{
int selectionStart;
int selectionEnd;
if(textArea.getSelection().length != 0){ // if there are selections exists
selectionStart = textArea.getSelection(0).getStart();
selectionEnd = textArea.getSelection(0).getEnd();
}
else{ // if no selection
selectionStart = textArea.getCaretPosition();
selectionEnd = textArea.getCaretPosition();
}
String text = textArea.getText();
int index = text.lastIndexOf("class", selectionStart);
if(index != -1)
{
textArea.setCaretPosition(index);
int lineNumber = textArea.getCaretLine();
int lineEnd = textArea.getLineEndOffset(lineNumber);
String lineText = text.substring(index, lineEnd);
StringTokenizer tokenizer = new StringTokenizer(lineText);
tokenizer.nextToken(); //eat "class"
if(tokenizer.hasMoreTokens())
{
setCaret(selectionStart, selectionEnd);
return tokenizer.nextToken();
}
}
setCaret(selectionStart, selectionEnd);
String fileClassName = buffer.getName();
int index = fileClassName.lastIndexOf('.');
if(index != -1)
{
fileClassName = fileClassName.substring(0, index);
if(fileClassName.toLowerCase().indexOf("untitled") == -1)
{
return fileClassName;
}
}
return UNDEFINED;
}
public String createJavaConstructor(String className, String[] typeNames, String[] variableNames)
{
if(typeNames.length != variableNames.length)
{
Macro.message(view, UnevenNumberMessage);
return "";
}
String args = "";
String body = "";
for(int i = 0; i < typeNames.length; i++)
{
args += typeNames[i] + " " + variableNames[i];
if(i+1 < typeNames.length)
{
args += ",\n";
}
body += "this." + variableNames[i] + " = " + variableNames[i] + ";\n";
}
String code =
"/**\n" + "Basic constructor for " + className + "\n*/\n" +
"public " + className +
"(" + args + ")" + "\n" +
"{" + "\n" +
body +
"}" + "\n";
return code;
}
public String createCppConstructor(String className, String[] typeNames, String[] variableNames)
{
if(typeNames.length != variableNames.length)
{
Macro.message(view, UnevenNumberMessage);
return "";
}
String args = "";
String body = "";
for(int i = 0; i < typeNames.length; i++)
{
String setVariable = variableNames[i] + "Value";
args += typeNames[i] + "& " + setVariable;
body += variableNames[i] + "(" + setVariable + ")";
if(i+1 < typeNames.length)
{
args += ",\n";
body += ",";
}
body += "\n";
}
String code =
"/*\n" + "Basic constructor for " + className + "\n*/\n" +
className + "::" + className +
"(" + args + ")" + "\n" +
"throw()\n" +
" : " + body +
"{" + "\n" +
"}" + "\n";
return code;
}
void parseSelection()
{
int selectionStart;
int selectionEnd;
if(textArea.getSelection().length != 0){ // if there are selections exists
selectionStart = textArea.getSelection(0).getStart();
selectionEnd = textArea.getSelection(0).getEnd();
}
else{ // if no selection
selectionStart = textArea.getCaretPosition();
selectionEnd = textArea.getCaretPosition();
}
textArea.setCaretPosition(selectionStart);
int startLine = textArea.getCaretLine();
textArea.setCaretPosition(selectionEnd);
int endLine = textArea.getCaretLine();
Vector typeNames = new Vector();
Vector variableNames = new Vector();
for(int i = startLine; i <= endLine; i++)
{
String lineText = textArea.getLineText(i);
if( lineText != null && !lineText.equals("") )
{
lineText = lineText.trim();
if( lineText.endsWith(";") )
{
lineText = lineText.substring( 0, lineText.length() -1 );
}
StringTokenizer tokenizer = new StringTokenizer(lineText);
int tokenCount = tokenizer.countTokens();
if(tokenCount >= 2)
{
int numGarbage = tokenCount - 2;
for (int i = 0; i < numGarbage; i++)
{
tokenizer.nextToken();
}
String type = tokenizer.nextToken();
String variable = tokenizer.nextToken();
if(type != null &&
type.compareTo("") != 0 &&
variable != null &&
variable.compareTo("") != 0 )
{
typeNames.add(type);
variableNames.add(variable);
}
}
}
}
int size = typeNames.size();
String [] types = new String[size];
String [] variables = new String[size];
for(int i = 0; i < size; i++)
{
types[i] = typeNames.get(i).toString();
variables[i] = variableNames.get(i).toString();
}
String code = "\n";
if(JAVA_MODE)
{
code += createJavaConstructor(getClassName(), types, variables);
textArea.setCaretPosition(selectionEnd);
textArea.setSelectedText(code);
}
else
{
code += createCppConstructor(getClassName(), types, variables);
textArea.setCaretPosition(selectionEnd);
textArea.setSelectedText(code);
}
textArea.setCaretPosition(selectionEnd);
textArea.moveCaretPosition(selectionEnd + code.length(), true);
textArea.indentSelectedLines();
}
parseSelection();
|