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