File: Get_Class_Name.bsh

package info (click to toggle)
jedit 4.3.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 12,288 kB
  • ctags: 10,215
  • sloc: java: 88,754; xml: 81,655; makefile: 53; sh: 26
file content (92 lines) | stat: -rw-r--r-- 2,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
Get_Class_Name.bsh - a BeanShell macro for the jEdit text editor replaces the 
selected text with the current class name.

This macro walks though several steps in an attempt to guess the correct class
name.

First, it searches backwards for the nearest occurance of the keyword 'class,'
so it should work for inner class definitions.  It does not, however, do any 
fancy code parsing, so if you run this macro at any point after an inner class 
definition, it will return the name of the inner class, not the parent class.

If that fails, it attempts to guess the class name from the file name; i.e. it 
would return "FooBar" if the buffer was named "FooBar.cc."  If this fails (if, 
for example, the buffer is untitled), it returns an empty string.

Copyright (C)  2004 Thomas Galvin - software@thomas-galvin.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/

void setCaret(int selectionStart, int selectionEnd)
{
  textArea.setCaretPosition(selectionStart);
  textArea.moveCaretPosition(selectionEnd);
}

String getClassName()
{
  int selectionStart = textArea.getSelectionStart();
  int selectionEnd = textArea.getSelectionEnd();
  
  String text = textArea.getText();
  int index = text.lastIndexOf("class", selectionStart);
  if(index != -1)
  {
    textArea.setCaretPosition(index);
    int lineNumber = textArea.getCaretLine();
    int lineEnd = textArea.getLineEndOffset(lineNumber);
    String lineText = text.substring(index, lineEnd);
    
    StringTokenizer tokenizer = new StringTokenizer(lineText);
    tokenizer.nextToken(); //eat "class"
    if(tokenizer.hasMoreTokens())
    {
      setCaret(selectionStart, selectionEnd);
      return tokenizer.nextToken();
    }
  }
  
  
  String fileClassName = buffer.getName();
  int index = fileClassName.lastIndexOf('.');
  if(index != -1)
  {
    fileClassName = fileClassName.substring(0, index);
    if(fileClassName.toLowerCase().indexOf("untitled") == -1)
    {
      return fileClassName;
    }
  }
  setCaret(selectionStart, selectionEnd);
  
  String className = buffer.getName();
  int index = name.lastIndexOf('.');
  if(index != -1)
  {
    className = className.substring(0, index);
    if(className.toLowerCase().indexOf("untitled") == -1)
    {
      return className;
    }
  }
  
  return "";
}

String className = getClassName();
if( buffer.isReadOnly() )
	Macros.error( view, "Buffer is read-only." );
else if( className != null && !className.equals("") )
{
  textArea.setSelectedText(className);
}