1 19 20 package org.netbeans.modules.languages.features; 21 22 import java.util.Iterator ; 23 import org.netbeans.api.languages.ASTItem; 24 import org.netbeans.api.languages.ASTPath; 25 import org.netbeans.api.languages.LanguagesManager; 26 import org.netbeans.api.languages.ParseException; 27 import org.netbeans.api.languages.ASTToken; 28 import java.awt.event.ActionEvent ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.JTextComponent ; 35 import org.netbeans.api.lexer.Token; 36 import org.netbeans.api.lexer.TokenHierarchy; 37 import org.netbeans.api.lexer.TokenSequence; 38 import org.netbeans.editor.BaseAction; 39 import org.netbeans.modules.editor.NbEditorDocument; 40 import org.netbeans.api.languages.ASTNode; 41 import org.netbeans.api.languages.ParseException; 42 import org.netbeans.api.languages.ASTToken; 43 import org.netbeans.modules.languages.Feature; 44 import org.netbeans.modules.languages.Language; 45 import org.netbeans.modules.languages.LanguagesManagerImpl; 46 import org.netbeans.modules.languages.LanguagesManagerImpl; 47 import org.netbeans.modules.languages.ParserManagerImpl; 48 import org.openide.ErrorManager; 49 50 54 public class FormatAction extends BaseAction { 55 56 public FormatAction () { 57 super("Format"); 58 } 59 60 public void actionPerformed (ActionEvent e, JTextComponent component) { 61 try { 62 NbEditorDocument doc = (NbEditorDocument) component.getDocument (); 63 ASTNode root = ParserManagerImpl.get (doc).getAST (); 64 if (root == null) return; 65 StringBuilder sb = new StringBuilder (); 66 Map <String ,String > indents = new HashMap <String ,String > (); 67 indents.put ("i", ""); indents.put ("ii", " "); indent ( 70 root, 71 new ArrayList <ASTItem> (), 72 sb, 73 indents, 74 null, 75 false, 76 doc 77 ); 78 doc.remove (0, doc.getLength ()); 79 doc.insertString (0, sb.toString (), null); 80 } catch (ParseException ex) { 81 ErrorManager.getDefault ().notify (ex); 82 } catch (BadLocationException ex) { 83 ErrorManager.getDefault ().notify (ex); 84 } 85 } 86 87 104 105 private static void indent ( 106 ASTItem item, 107 List <ASTItem> path, 108 StringBuilder sb, 109 Map <String ,String > indents, 110 ASTToken whitespace, 111 boolean firstIndented, 112 NbEditorDocument document 113 ) { 114 try { 115 Language language = LanguagesManagerImpl.get ().getLanguage (item.getMimeType ()); 116 117 path.add (item); 118 ASTPath path2 = ASTPath.create (path); 119 Iterator <ASTItem> it = item.getChildren ().iterator (); 120 while (it.hasNext ()) { 121 ASTItem e = it.next (); 122 123 String indent = null; 125 if (e instanceof ASTToken) { 126 ASTToken token = (ASTToken) e; 127 if (language.getSkipTokenTypes ().contains (token.getType ())) { whitespace = (ASTToken) e; 129 firstIndented = false; 130 continue; 131 } 132 } 133 Feature format = language.getFeature ("FORMAT", path2); 134 if (format != null) 135 indent = (String ) format.getValue (); 136 137 143 if (indent != null) { 144 if (indent.equals ("NewLine")) 145 sb.append ("\n"); 146 } 147 148 if (e instanceof ASTToken) 150 sb.append (((ASTToken) e).getIdentifier ()); 151 else 152 indent ( 153 (ASTNode) e, 154 path, 155 sb, 156 indents, 157 whitespace, 158 firstIndented || indent != null, 159 null 160 ); 161 } path.remove (path.size () - 1); 163 } catch (ParseException ex) { 164 } 165 } 166 167 private static String chars (int length) { 168 StringBuilder sb = new StringBuilder (); 169 for (int i = 0; i < length; i++) sb.append (' '); 170 return sb.toString (); 171 } 172 } 173 | Popular Tags |