1 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 ; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.JTextComponent ; 32 33 34 public class Ruby { 35 36 private static String MIME_TYPE = "text/x-ruby"; 37 38 public static void performDeleteCurrentMethod (ASTNode node, JTextComponent 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 iter = path.listIterator(); iter.hasNext(); ) { 44 Object obj = iter.next(); 45 if (!(obj instanceof ASTNode)) 46 break; 47 ASTNode n = (ASTNode) obj; 48 if ("Method_Def".equals(n.getNT())) { methodNode = n; 50 } } if (methodNode != null) { 53 try { 54 doc.remove(methodNode.getOffset(), methodNode.getLength()); 55 } catch (BadLocationException e) { 56 ErrorManager.getDefault().notify(e); 57 } 58 } 59 } 60 61 public static boolean enabledDeleteCurrentMethod (ASTNode node, JTextComponent 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 iter = path.listIterator(); iter.hasNext(); ) { 69 Object obj = iter.next(); 70 if (!(obj instanceof ASTNode)) 71 return false; 72 ASTNode n = (ASTNode) obj; 73 if ("Method_Def".equals(n.getNT())) { return true; 75 } } return false; 78 } 79 80 public static Object [] analyzeDelimitedInput (CharInput input, String mimeType) { 81 int startIndex = input.getIndex(); 82 input.read(); 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 buf = new StringBuffer (); 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 = delimiterToType(inputType); 125 return new Object [] { 127 ASTToken.create ( 128 MIME_TYPE, 129 delimiterToType (inputType), 130 input.getString (startIndex, input.getIndex ()), 131 0 132 ), 133 new Integer (-1) }; 135 } 136 } 137 buf.append(c); 138 } 139 return new Object [] {null, new Integer (-1)}; } 141 142 private static String delimiterToType(char delimiter) { 143 String type = null; 144 switch (delimiter) { 145 case 'Q': 146 case 'W': type = "double_quoted_string"; 148 break; 149 case 'q': 150 case 'w': 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 |