/jEdit/tags/jedit-4-3-pre15/macros/Java/Java_File_Save.bsh

# · Unknown · 142 lines · 132 code · 10 blank · 0 comment · 0 complexity · 430f74a33e2eb0a6c60fd7305930ff8c MD5 · raw file

  1. /*
  2. * Java_File_Save.bsh - a BeanShell macro for saving new java files.
  3. *
  4. * Copyright (C) 2004 Nicholas O'Leary nol@deferential.net
  5. *
  6. * :mode=beanshell:tabSize=3:indentSize=3:maxLineLen=0:noTabs=true:
  7. * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
  8. *
  9. * {{{ License
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with the jEdit program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. * }}}
  24. *
  25. * Notes:
  26. * Only the first 250 lines of the buffer are scanned for a suitable
  27. * class or interface declaration.
  28. *
  29. * Changes:
  30. * 17-May-04: Only scans if the edit mode is either 'java' or the default mode
  31. * : Ignores declarations that are in multiline comments
  32. * 08-Jun-04: If an infinite loop is hit (1000 iterations) in the comment
  33. * : parsing, it now opens the default save dialog, rather than
  34. * : just returning.
  35. * $Id: Java_File_Save.bsh 12063 2008-03-02 06:58:51Z k_satoda $
  36. */
  37. // Check this is a new file
  38. if (buffer.isNewFile() && buffer.getPath() != null)
  39. {
  40. // Only look further if the mode is 'java', or still the default
  41. String buffer_mode = buffer.getMode().toString();
  42. if (buffer_mode.equals("java") || buffer_mode.equals(jEdit.getProperty("buffer.defaultMode","")))
  43. {
  44. String fullpath = buffer.getPath();
  45. VFS vfs = VFSManager.getVFSForPath(fullpath);
  46. // Split into constituent parts
  47. String path = vfs.getParentOfPath(fullpath);
  48. String name = vfs.getFileName(fullpath);
  49. // At most, check the first 250 lines - this sounds reasonable to me
  50. int maxLine = Math.min(buffer.getLineCount(),250);
  51. import java.util.regex.Pattern;
  52. import java.util.regex.Matcher;
  53. // Build the regex - based on the offical java language spec.
  54. Pattern regex = Pattern.compile("^\\s*(public|protected|private|static|abstract|final|native|synchronized|transient|volatile|strictfp)?\\s*(class|interface)\\s*([^ {/]*)");
  55. boolean inComment = false;
  56. for(int i=0;i<maxLine;i++)
  57. {
  58. String txt = buffer.getLineText(i);
  59. int count = 0;
  60. // See if this line has a the start or finish of a multiline comment
  61. while (txt.indexOf("/*")!=-1 || txt.indexOf("*/")!=-1)
  62. {
  63. // A little paranoia on my part
  64. count++;
  65. if (count==1000)
  66. {
  67. Log.log(Log.ERROR,BeanShell.class,"Infinite loop:["+txt+"]");
  68. buffer.save(view,null,true);
  69. return;
  70. }
  71. // Look for the next starting comment if we're not in a comment
  72. if (!inComment)
  73. {
  74. int commentStartIndex = txt.indexOf("/*");
  75. if (commentStartIndex != -1)
  76. {
  77. inComment = true;
  78. if (commentStartIndex+2 == txt.length())
  79. txt = "";
  80. else
  81. txt = txt.substring(commentStartIndex+2);
  82. }
  83. }
  84. // Look for the next ending comment if we are in a comment
  85. if (inComment)
  86. {
  87. int commentEndIndex = txt.indexOf("*/");
  88. if (commentEndIndex != -1)
  89. {
  90. inComment = false;
  91. if (commentEndIndex+2 == txt.length())
  92. txt = "";
  93. else
  94. txt = txt.substring(commentEndIndex+2);
  95. } else {
  96. continue;
  97. }
  98. }
  99. }
  100. // We now know if the remainder of the line is in a comment or not
  101. if (!inComment)
  102. {
  103. Matcher matcher = regex.matcher(txt);
  104. if (matcher.matches())
  105. {
  106. // Extract the class/interface name
  107. name = matcher.group(3)+".java";
  108. break;
  109. }
  110. }
  111. }
  112. // Open the VFSBrowser
  113. String[] files = GUIUtilities.showVFSFileDialog(view,path+name,
  114. VFSBrowser.SAVE_DIALOG,false);
  115. if(files == null)
  116. return false;
  117. buffer.save(view,files[0],true);
  118. return;
  119. }
  120. }
  121. // This isn't a file that has been scanned, so just do a normal save
  122. buffer.save(view,null,true);
  123. /*
  124. Macro index data (in DocBook format)
  125. <listitem>
  126. <para><filename>Java_File_Save.bsh</filename></para>
  127. <abstract><para>Acts as a wrapper script to the Save As action. If the buffer
  128. is a new file, it scans the first 250 lines for a Java class or interface
  129. declaration. On finding one, it extracts the appropriate filename to be
  130. used in the Save As dialog.</para></abstract>
  131. </listitem>
  132. */