PageRenderTime 71ms CodeModel.GetById 67ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/macros/Java/Create_Constructor.bsh

#
Unknown | 221 lines | 184 code | 37 blank | 0 comment | 0 complexity | d0526e650fe1acfe15ba40f53cc77207 MD5 | raw file
  1/**
  2Create_Constructor.bsh - a BeanShell macro for the jEdit text editor  that 
  3creates a constructor containing the selected variables.  This code has the 
  4similar limitations as Get_Class_Name; it merely looks backwards for the nearest 
  5occurance of the keyword 'class," etc.
  6
  7Copyright (C)  2004 Thomas Galvin - software@thomas-galvin.com
  8
  9This program is free software; you can redistribute it and/or
 10modify it under the terms of the GNU General Public License
 11as published by the Free Software Foundation; either version 2
 12of the License, or any later version.
 13
 14This program is distributed in the hope that it will be useful,
 15but WITHOUT ANY WARRANTY; without even the implied warranty of
 16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17GNU General Public License for more details.
 18*/
 19
 20boolean JAVA_MODE = buffer.getMode().getName().toLowerCase().equals("java");
 21String UNDEFINED = "UNKNOWN_CLASS";
 22
 23void setCaret(int selectionStart, int selectionEnd)
 24{
 25  textArea.setCaretPosition(selectionStart);
 26  textArea.moveCaretPosition(selectionEnd);
 27}
 28
 29String getClassName()
 30{
 31  int selectionStart = textArea.getSelectionStart();
 32  int selectionEnd = textArea.getSelectionEnd();
 33  
 34  String text = textArea.getText();
 35  int index = text.lastIndexOf("class", selectionStart);
 36  if(index != -1)
 37  {
 38    textArea.setCaretPosition(index);
 39    int lineNumber = textArea.getCaretLine();
 40    int lineEnd = textArea.getLineEndOffset(lineNumber);
 41    String lineText = text.substring(index, lineEnd);
 42    
 43    StringTokenizer tokenizer = new StringTokenizer(lineText);
 44    tokenizer.nextToken(); //eat "class"
 45    if(tokenizer.hasMoreTokens())
 46    {
 47      setCaret(selectionStart, selectionEnd);
 48      return tokenizer.nextToken();
 49    }
 50  }
 51  setCaret(selectionStart, selectionEnd);
 52  
 53  String fileClassName = buffer.getName();
 54  int index = fileClassName.lastIndexOf('.');
 55  if(index != -1)
 56  {
 57    fileClassName = fileClassName.substring(0, index);
 58    if(fileClassName.toLowerCase().indexOf("untitled") == -1)
 59    {
 60      return fileClassName;
 61    }
 62  }
 63  
 64  return UNDEFINED;
 65}
 66
 67public String createJavaConstructor(String className, String[] typeNames, String[] variableNames)
 68{
 69  if(typeNames.length != variableNames.length)
 70  {
 71    Macro.message(view, "Uneven number of type names and variables.");
 72    return "";
 73  }
 74  
 75  String args = "";
 76  String body = "";
 77  
 78  for(int i = 0; i < typeNames.length; i++)
 79  {
 80    args += typeNames[i] + " " + variableNames[i];
 81    if(i+1 < typeNames.length)
 82    {
 83      args += ",\n";
 84    }
 85    
 86    body += "this." + variableNames[i] + " = " + variableNames[i] + ";\n";
 87  }
 88  
 89  String code = 
 90  "/**\n" + "Basic constructor for " + className + "\n*/\n" + 
 91  "public " + className + 
 92  "(" +  args + ")" + "\n" + 
 93  "{" + "\n" + 
 94    body + 
 95  "}" + "\n";
 96  
 97  return code;
 98}
 99
100public String createCppConstructor(String className, String[] typeNames, String[] variableNames)
101{
102  if(typeNames.length != variableNames.length)
103  {
104    Macro.message(view, "Uneven number of type names and variables.");
105    return "";
106  }
107  
108  String args = "";
109  String body = "";
110  
111  for(int i = 0; i < typeNames.length; i++)
112  {
113    String setVariable = variableNames[i] + "Value";
114    
115    args += typeNames[i] + "& " + setVariable;
116    body += variableNames[i] + "(" + setVariable + ")";
117    
118    if(i+1 < typeNames.length)
119    {
120      args += ",\n";
121      body += ",";
122    }
123    
124    body += "\n";
125  }
126  
127  String code = 
128  "/*\n" + "Basic constructor for " + className + "\n*/\n" + 
129  className + "::" + className +  
130  "(" +  args + ")" + "\n" + 
131  "throw()\n" + 
132  " : " + body + 
133  "{" + "\n" + 
134  "}" + "\n";
135  
136  return code;
137}
138
139void parseSelection()
140{
141  int selectionStart = textArea.getSelectionStart();
142  int selectionEnd = textArea.getSelectionEnd();
143  
144  textArea.setCaretPosition(selectionStart);
145  int startLine = textArea.getCaretLine();
146  
147  textArea.setCaretPosition(selectionEnd);
148  int endLine = textArea.getCaretLine();
149  
150  Vector typeNames = new Vector();
151  Vector variableNames = new Vector();
152  
153  for(int i = startLine; i <= endLine; i++)
154  {
155    String lineText = textArea.getLineText(i);
156    if( lineText != null && !lineText.equals("") )
157    {
158      lineText = lineText.trim();
159      if( lineText.endsWith(";") )
160      {
161        lineText = lineText.substring( 0, lineText.length() -1 );
162      }
163      
164      StringTokenizer tokenizer = new StringTokenizer(lineText);
165      int tokenCount = tokenizer.countTokens();
166      if(tokenCount >= 2)
167      {
168        int numGarbage = tokenCount - 2;
169        for (int i = 0; i < numGarbage; i++)
170        {
171          tokenizer.nextToken();
172        }
173        
174        String type = tokenizer.nextToken();
175        String variable = tokenizer.nextToken();
176        
177        if(type != null &&
178           type.compareTo("") != 0 &&
179           variable != null &&
180           variable.compareTo("") != 0 )
181        {
182          typeNames.add(type);
183          variableNames.add(variable);
184        }
185      }
186    }
187  }
188  
189  int size = typeNames.size();
190  String [] types = new String[size];
191  String [] variables = new String[size];
192  
193  for(int i = 0; i < size; i++)
194  {
195    types[i] = typeNames.get(i).toString();
196    variables[i] = variableNames.get(i).toString();
197  }
198  
199  String code = "\n";
200  if(JAVA_MODE)
201  {
202    code += createJavaConstructor(getClassName(), types, variables);
203    textArea.setCaretPosition(selectionEnd);
204    textArea.setSelectedText(code);
205  }
206  else
207  {
208    code += createCppConstructor(getClassName(), types, variables);
209    textArea.setCaretPosition(selectionEnd);
210    textArea.setSelectedText(code);
211  }
212  
213  textArea.setCaretPosition(selectionEnd);
214  textArea.moveCaretPosition(selectionEnd + code.length(), true);
215  textArea.indentSelectedLines();
216}
217
218if( buffer.isReadOnly() )
219	Macros.error( view, "Buffer is read-only." );
220else
221	parseSelection();