PageRenderTime 79ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Unknown | 92 lines | 77 code | 15 blank | 0 comment | 0 complexity | c205519e9522637872b12dc588eaa7ff 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. Get_Class_Name.bsh - a BeanShell macro for the jEdit text editor replaces the
  3. selected text with the current class name.
  4. This macro walks though several steps in an attempt to guess the correct class
  5. name.
  6. First, it searches backwards for the nearest occurance of the keyword 'class,'
  7. so it should work for inner class definitions. It does not, however, do any
  8. fancy code parsing, so if you run this macro at any point after an inner class
  9. definition, it will return the name of the inner class, not the parent class.
  10. If that fails, it attempts to guess the class name from the file name; i.e. it
  11. would return "FooBar" if the buffer was named "FooBar.cc." If this fails (if,
  12. for example, the buffer is untitled), it returns an empty string.
  13. Copyright (C) 2004 Thomas Galvin - software@thomas-galvin.com
  14. This program is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU General Public License
  16. as published by the Free Software Foundation; either version 2
  17. of the License, or any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. */
  23. void setCaret(int selectionStart, int selectionEnd)
  24. {
  25. textArea.setCaretPosition(selectionStart);
  26. textArea.moveCaretPosition(selectionEnd);
  27. }
  28. String getClassName()
  29. {
  30. int selectionStart = textArea.getSelectionStart();
  31. int selectionEnd = textArea.getSelectionEnd();
  32. String text = textArea.getText();
  33. int index = text.lastIndexOf("class", selectionStart);
  34. if(index != -1)
  35. {
  36. textArea.setCaretPosition(index);
  37. int lineNumber = textArea.getCaretLine();
  38. int lineEnd = textArea.getLineEndOffset(lineNumber);
  39. String lineText = text.substring(index, lineEnd);
  40. StringTokenizer tokenizer = new StringTokenizer(lineText);
  41. tokenizer.nextToken(); //eat "class"
  42. if(tokenizer.hasMoreTokens())
  43. {
  44. setCaret(selectionStart, selectionEnd);
  45. return tokenizer.nextToken();
  46. }
  47. }
  48. String fileClassName = buffer.getName();
  49. int index = fileClassName.lastIndexOf('.');
  50. if(index != -1)
  51. {
  52. fileClassName = fileClassName.substring(0, index);
  53. if(fileClassName.toLowerCase().indexOf("untitled") == -1)
  54. {
  55. return fileClassName;
  56. }
  57. }
  58. setCaret(selectionStart, selectionEnd);
  59. String className = buffer.getName();
  60. int index = name.lastIndexOf('.');
  61. if(index != -1)
  62. {
  63. className = className.substring(0, index);
  64. if(className.toLowerCase().indexOf("untitled") == -1)
  65. {
  66. return className;
  67. }
  68. }
  69. return "";
  70. }
  71. String className = getClassName();
  72. if( buffer.isReadOnly() )
  73. Macros.error( view, "Buffer is read-only." );
  74. else if( className != null && !className.equals("") )
  75. {
  76. textArea.setSelectedText(className);
  77. }