KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > ruby > Ruby


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages.ruby;
21
22 import org.netbeans.api.languages.ASTToken;
23 import org.netbeans.api.languages.CharInput;
24 import org.netbeans.api.languages.ASTPath;
25 import org.netbeans.api.languages.ASTToken;
26 import org.netbeans.api.languages.ASTNode;
27 import org.netbeans.modules.editor.NbEditorDocument;
28 import org.openide.ErrorManager;
29 import java.util.Iterator JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
32
33
34 public class Ruby {
35     
36     private static String JavaDoc MIME_TYPE = "text/x-ruby";
37         
38     public static void performDeleteCurrentMethod (ASTNode node, JTextComponent JavaDoc comp) {
39         NbEditorDocument doc = (NbEditorDocument)comp.getDocument();
40         int position = comp.getCaretPosition();
41         ASTPath path = node.findPath(position);
42         ASTNode methodNode = null;
43         for (Iterator JavaDoc iter = path.listIterator(); iter.hasNext(); ) {
44             Object JavaDoc obj = iter.next();
45             if (!(obj instanceof ASTNode))
46                 break;
47             ASTNode n = (ASTNode) obj;
48             if ("Method_Def".equals(n.getNT())) { // NOI18N
49
methodNode = n;
50             } // if
51
} // for
52
if (methodNode != null) {
53             try {
54                 doc.remove(methodNode.getOffset(), methodNode.getLength());
55             } catch (BadLocationException JavaDoc e) {
56                 ErrorManager.getDefault().notify(e);
57             }
58         }
59     }
60      
61     public static boolean enabledDeleteCurrentMethod (ASTNode node, JTextComponent JavaDoc comp) {
62         NbEditorDocument doc = (NbEditorDocument)comp.getDocument();
63         int position = comp.getCaretPosition();
64         ASTPath path = node.findPath(position);
65         if (path == null) {
66             return false;
67         }
68         for (Iterator JavaDoc iter = path.listIterator(); iter.hasNext(); ) {
69             Object JavaDoc obj = iter.next();
70             if (!(obj instanceof ASTNode))
71                 return false;
72             ASTNode n = (ASTNode) obj;
73             if ("Method_Def".equals(n.getNT())) { // NOI18N
74
return true;
75             } // if
76
} // for
77
return false;
78     }
79     
80     public static Object JavaDoc[] analyzeDelimitedInput (CharInput input, String JavaDoc mimeType) {
81         int startIndex = input.getIndex();
82         input.read(); // "%" character
83
char inputType = input.read();
84         char delimiter;
85         if (inputType == 'Q' || inputType == 'q' || inputType == 'r' || inputType == 'x' ||
86                 inputType == 'W' || inputType == 'w') {
87             delimiter = input.read();
88         } else {
89             delimiter = inputType;
90             inputType = 'Q';
91         }
92         char endDelimiter;
93         switch (delimiter) {
94             case '{':
95                 endDelimiter = '}';
96                 break;
97             case '<':
98                 endDelimiter = '>';
99                 break;
100             case '(':
101                 endDelimiter = ')';
102                 break;
103             case '[':
104                 endDelimiter = ']';
105                 break;
106             default:
107                 endDelimiter = delimiter;
108         }
109         boolean nested = delimiter != endDelimiter;
110         int level = 1;
111         if (input.eof()) {
112             return null;
113         }
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
115         while (!input.eof()) {
116             char c = input.read();
117             if (nested && c == delimiter) {
118                 level++;
119             }
120             if (c == endDelimiter) {
121                 level--;
122                 if (level == 0) {
123                     // String str = buf.toString();
124
String JavaDoc str = delimiterToType(inputType);
125                     // input.getString(startIndex, input.getIndex()); // [PENDING] input.getString() not implemented
126
return new Object JavaDoc[] {
127                         ASTToken.create (
128                             MIME_TYPE,
129                             delimiterToType (inputType),
130                             input.getString (startIndex, input.getIndex ()),
131                             0
132                         ),
133                         new Integer JavaDoc (-1)// [PENDING] set proper constant for DEFAULT_STATE
134
};
135                 }
136             }
137             buf.append(c);
138         }
139         return new Object JavaDoc[] {null, new Integer JavaDoc (-1)}; // [PENDING] set proper constant for DEFAULT_STATE
140
}
141     
142     private static String JavaDoc delimiterToType(char delimiter) {
143         String JavaDoc type = null;
144         switch (delimiter) {
145             case 'Q':
146             case 'W': // [PENDING] array elements
147
type = "double_quoted_string";
148                 break;
149             case 'q':
150             case 'w': // [PENDING] array elements
151
type = "single_quoted_string";
152                 break;
153             case 'r':
154                 type = "regular_expression";
155                 break;
156             case 'x':
157                 type = "shell_command";
158                 break;
159         }
160         return type;
161     }
162     
163 }
164
Popular Tags