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