KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > GlobalTag


1 /*
2  * GlobalTag.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: GlobalTag.java,v 1.2 2004/05/21 16:50:24 piso Exp $
6  *
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 (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import gnu.regexp.RE;
25 import gnu.regexp.REMatch;
26 import gnu.regexp.UncheckedRE;
27 import java.util.List JavaDoc;
28 import javax.swing.undo.CompoundEdit JavaDoc;
29
30 public final class GlobalTag extends Tag
31 {
32     private final String JavaDoc filename;
33
34     private GlobalTag(String JavaDoc name, String JavaDoc signature, String JavaDoc filename)
35     {
36         super(name, signature);
37         this.filename = filename;
38     }
39
40     private GlobalTag(String JavaDoc name, String JavaDoc signature, String JavaDoc filename,
41         String JavaDoc canonicalSignature)
42     {
43         super(name, signature);
44         this.filename = filename;
45         this.canonicalSignature = canonicalSignature;
46     }
47
48     public final String JavaDoc getFileName()
49     {
50         return filename;
51     }
52
53     // Return tag name from line in tag file.
54
public static String JavaDoc getTagName(Line line)
55     {
56         int i = line.getText().indexOf(Tagger.separatorChar);
57         if (i < 0) // Should never happen.
58
return line.getText();
59         else
60             return line.substring(0, i);
61     }
62
63     // Construct global tag from one line of text in tag file.
64
public static GlobalTag makeGlobalTag(String JavaDoc s)
65     {
66         int index = s.indexOf(Tagger.separatorChar);
67         if (index < 0)
68             return null;
69         String JavaDoc name = s.substring(0, index);
70         s = s.substring(index + 1);
71         index = s.indexOf(Tagger.separatorChar);
72         if (index < 0)
73             return null;
74         String JavaDoc filename = s.substring(0, index);
75         s = s.substring(index + 1);
76         index = s.indexOf(Tagger.separatorChar);
77         if (index < 0) {
78             // No canonical signature.
79
String JavaDoc signature = s;
80             return new GlobalTag(name, signature, filename);
81         }
82         String JavaDoc signature = s.substring(0, index);
83         String JavaDoc canonicalSignature = s.substring(index + 1);
84         return new GlobalTag(name, signature, filename, canonicalSignature);
85     }
86
87     public String JavaDoc getClassName()
88     {
89         // Java.
90
int index = name.indexOf('.');
91         if (index >= 0)
92             return name.substring(0, index);
93         // C++, Perl.
94
index = name.indexOf("::");
95         if (index >= 0)
96             return name.substring(0, index);
97         // C, Lisp.
98
return null;
99     }
100
101     public String JavaDoc getMethodName()
102     {
103         // Java
104
int index = name.indexOf('.');
105         if (index >= 0)
106             return name.substring(index + 1);
107         // C++
108
index = name.indexOf("::");
109         if (index >= 0)
110             return name.substring(index + 2);
111         return name;
112     }
113
114     public String JavaDoc getLongName()
115     {
116         String JavaDoc s = signature.trim();
117         if (s.startsWith("DEFUN")) {
118             // Emacs source.
119
return name;
120         }
121         if (s.startsWith("(")) {
122             // Lisp.
123
s = s.substring(1).trim();
124             // First word should be "defun" or "defvar" or some such...
125
int end = 0;
126             final int limit = s.length();
127             for (int i = 0; i < limit; i++) {
128                 char c = s.charAt(i);
129                 if (c == ' ' || c == '\t') {
130                     end = i;
131                     break;
132                 }
133             }
134             String JavaDoc definer = s.substring(0, end);
135             s = s.substring(end).trim();
136             FastStringBuffer sb = new FastStringBuffer('(');
137             sb.append(definer);
138             sb.append(' ');
139             if (definer.equals("defgeneric") || definer.equals("defmethod")) {
140                 sb.append(s);
141                 return sb.toString();
142             }
143             for (int i = 0; i < limit; i++) {
144                 char c = s.charAt(i);
145                 if (c == ' ' || c == '\t') {
146                     sb.append(s.substring(0, i));
147                     sb.append(" ...");
148                     return sb.toString();
149                 }
150                 if (c == ')') {
151                     sb.append(s.substring(0, i + 1));
152                     return sb.toString();
153                 }
154             }
155             sb.append(s);
156             sb.append(" ...");
157             return sb.toString();
158         }
159         if (name.startsWith("class "))
160             return name;
161         // Strip comment if any.
162
// BUG! Only really relevant to Java-like languages.
163
int index = s.indexOf("//");
164         if (index >= 0)
165             s = s.substring(0, index).trim();
166         index = s.lastIndexOf(')');
167         if (index >= 0)
168             s = s.substring(0, index + 1);
169         if (s.endsWith("("))
170             s = s.substring(0, s.length() - 1);
171         // Try to substitute the fully qualified method name.
172
// Try Java first.
173
String JavaDoc separator = ".";
174         int sepLength = 1;
175         index = name.indexOf(separator);
176         if (index >= 0) {
177             String JavaDoc methodName = name.substring(index + sepLength);
178             index = s.indexOf(methodName);
179             if (index >= 0) {
180                 String JavaDoc head = s.substring(0, index);
181                 String JavaDoc tail = s.substring(index + methodName.length());
182                 s = head + name + tail;
183             }
184         } else {
185             // C++.
186
separator = "::";
187             sepLength = 2;
188             index = name.indexOf(separator);
189             if (index >= 0) {
190                 String JavaDoc methodName = name.substring(index + sepLength);
191                 index = s.indexOf(methodName);
192                 if (index >= 0) {
193                     String JavaDoc head = s.substring(0, index);
194                     String JavaDoc tail = s.substring(index + methodName.length());
195                     s = head + name + tail;
196                 }
197             }
198         }
199         return s;
200     }
201
202     public String JavaDoc toString()
203     {
204         // JavaScript.
205
if (signature.trim().startsWith("function "))
206             return signature + " " + File.getInstance(filename).getName();
207         else
208             return super.toString();
209     }
210
211     public void gotoTag(Editor editor)
212     {
213         editor.setWaitCursor();
214         Buffer buf = Editor.getBuffer(File.getInstance(filename));
215         if (buf != null) {
216             editor.makeNext(buf);
217             editor.activate(buf);
218             Position pos = findSignatureInCurrentBuffer(buf, signature);
219             if (pos != null) {
220                 CompoundEdit JavaDoc compoundEdit = editor.beginCompoundEdit();
221                 editor.addUndo(SimpleEdit.FOLD);
222                 editor.unfoldMethod(pos.getLine());
223                 editor.moveDotTo(pos);
224                 TagCommands.centerTag(editor);
225                 editor.endCompoundEdit(compoundEdit);
226                 editor.updateDisplay();
227             }
228         }
229         editor.setDefaultCursor();
230     }
231
232     private static Position findSignatureInCurrentBuffer(Buffer buffer,
233         String JavaDoc signature)
234     {
235         final List JavaDoc localTags = buffer.getTags(true);
236         if (localTags == null)
237             return null;
238         final int limit = localTags.size();
239         for (int i = 0; i < limit; i++) {
240             LocalTag localTag = (LocalTag) localTags.get(i);
241             if (localTag.getSignature().equals(signature))
242                 return localTag.getPosition();
243         }
244         // We did not find an exact match. The signature may have changed.
245
// Look for a substring containing the function name and argument list
246
// only.
247
RE re = new UncheckedRE("\\w+\\s*\\(.*\\)");
248         REMatch match = re.getMatch(signature);
249         if (match != null) {
250             String JavaDoc sub = match.toString();
251             for (int i = 0; i < limit; i++) {
252                 LocalTag localTag = (LocalTag) localTags.get(i);
253                 if (localTag.getSignature().indexOf(sub) >= 0)
254                     return localTag.getPosition();
255             }
256         }
257         return null;
258     }
259 }
260
Popular Tags