Source Code Cross Referenced for ExpandSameLineDef.java in  » IDE-Netbeans » ruby » org » netbeans » modules » ruby » hints » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » ruby » org.netbeans.modules.ruby.hints 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * Portions Copyrighted 2007 Sun Microsystems, Inc.
027:         */
028:        package org.netbeans.modules.ruby.hints;
029:
030:        import java.io.IOException;
031:        import java.util.ArrayList;
032:        import java.util.Collections;
033:        import java.util.HashSet;
034:        import java.util.List;
035:        import java.util.Set;
036:        import java.util.prefs.Preferences;
037:        import javax.swing.JComponent;
038:        import javax.swing.text.BadLocationException;
039:        import org.jruby.ast.Node;
040:        import org.jruby.ast.NodeTypes;
041:        import org.jruby.ast.NodeTypes;
042:        import org.jruby.lexer.yacc.ISourcePosition;
043:        import org.netbeans.modules.gsf.api.CompilationInfo;
044:        import org.netbeans.modules.gsf.api.OffsetRange;
045:        import org.netbeans.api.lexer.Token;
046:        import org.netbeans.api.lexer.TokenId;
047:        import org.netbeans.api.lexer.TokenSequence;
048:        import org.netbeans.editor.BaseDocument;
049:        import org.netbeans.editor.Utilities;
050:        import org.netbeans.modules.ruby.AstPath;
051:        import org.netbeans.modules.ruby.AstUtilities;
052:        import org.netbeans.modules.ruby.hints.spi.AstRule;
053:        import org.netbeans.modules.ruby.hints.spi.Description;
054:        import org.netbeans.modules.ruby.hints.spi.EditList;
055:        import org.netbeans.modules.ruby.hints.spi.Fix;
056:        import org.netbeans.modules.ruby.hints.spi.HintSeverity;
057:        import org.netbeans.modules.ruby.hints.spi.PreviewableFix;
058:        import org.netbeans.modules.ruby.hints.spi.RuleContext;
059:        import org.netbeans.modules.ruby.lexer.LexUtilities;
060:        import org.netbeans.modules.ruby.lexer.RubyTokenId;
061:        import org.openide.util.Exceptions;
062:        import org.openide.util.NbBundle;
063:
064:        /**
065:         * Hint which adds a fix to lines containing a "single-line" definition
066:         * of a method or a class, and offers to expand it into a multi-line
067:         * definition, e.g. replacing
068:         * <pre>
069:         *    def foo; bar; end
070:         * </pre>
071:         * with
072:         * <pre>
073:         *    def foo
074:         *      bar
075:         *    end
076:         * </pre>
077:         * <p>
078:         * NOTE - this hint is only activated for the line under the caret!
079:         *
080:         * @todo Filter out the case where you have a def inside a class on the same line!
081:         * @todo Apply this tip to brace blocks as well - and offer both expand and collapse!
082:         * @todo Why doesn't this work on line begins? E.g. add "def foo; bar; end" and put the
083:         *   caret to the left of "def"; it doesn't activate
084:         * @todo See James Moore's comment about formatting multi-line statements
085:         * 
086:         * @author Tor Norbye
087:         */
088:        public class ExpandSameLineDef implements  AstRule {
089:            public ExpandSameLineDef() {
090:            }
091:
092:            public boolean appliesTo(CompilationInfo info) {
093:                // Skip for RHTML files for now - isn't implemented properly
094:                return info.getFileObject().getMIMEType().equals("text/x-ruby");
095:            }
096:
097:            public Set<Integer> getKinds() {
098:                Set<Integer> integers = new HashSet<Integer>();
099:                integers.add(NodeTypes.CLASSNODE);
100:                integers.add(NodeTypes.DEFNNODE);
101:                integers.add(NodeTypes.DEFSNODE);
102:                return integers;
103:            }
104:
105:            public void run(RuleContext context, List<Description> result) {
106:                Node node = context.node;
107:                AstPath path = context.path;
108:                CompilationInfo info = context.compilationInfo;
109:                BaseDocument doc = context.doc;
110:
111:                // Look for use of deprecated fields
112:                if (node.nodeId == NodeTypes.DEFNNODE
113:                        || node.nodeId == NodeTypes.DEFSNODE
114:                        || node.nodeId == NodeTypes.CLASSNODE) {
115:                    ISourcePosition pos = node.getPosition();
116:                    try {
117:                        if (doc == null) {
118:                            // Run on a file that was just closed
119:                            return;
120:                        }
121:
122:                        int start = pos.getStartOffset();
123:                        int end = pos.getEndOffset();
124:                        int length = doc.getLength();
125:                        if (Utilities.getRowEnd(doc, Math.min(start, length)) == Utilities
126:                                .getRowEnd(doc, Math.min(end, length))) {
127:                            // Block is on a single line
128:                            // TODO - add a hint to turn off this hint?
129:                            // Should be a utility or infrastructure option!
130:                            Node root = AstUtilities.getRoot(info);
131:                            if (path.leaf() != node) {
132:                                path = new AstPath(root, node);
133:                            }
134:                            List<Fix> fixList = Collections
135:                                    .<Fix> singletonList(new ExpandLineFix(
136:                                            info, path));
137:
138:                            OffsetRange range = new OffsetRange(pos
139:                                    .getStartOffset(), pos.getEndOffset());
140:                            Description desc = new Description(this ,
141:                                    getDisplayName(), info.getFileObject(),
142:                                    range, fixList, 150);
143:                            result.add(desc);
144:
145:                            // Exit; don't process children such that a def inside a class all
146:                            // on the same line only produces a single suggestion for the outer block
147:                            return;
148:                        }
149:                    } catch (BadLocationException ex) {
150:                        Exceptions.printStackTrace(ex);
151:                    }
152:                }
153:            }
154:
155:            public void cancel() {
156:                // Does nothing
157:            }
158:
159:            public String getId() {
160:                return "Expand_Same_Line_Def"; // NOI18N
161:            }
162:
163:            public String getDisplayName() {
164:                return NbBundle.getMessage(ExpandSameLineDef.class,
165:                        "ExpandLine");
166:            }
167:
168:            public String getDescription() {
169:                return NbBundle.getMessage(ExpandSameLineDef.class,
170:                        "ExpandLineDesc");
171:            }
172:
173:            private static class ExpandLineFix implements  PreviewableFix {
174:
175:                private CompilationInfo info;
176:
177:                private AstPath path;
178:
179:                ExpandLineFix(CompilationInfo info, AstPath path) {
180:                    this .info = info;
181:                    this .path = path;
182:                }
183:
184:                public String getDescription() {
185:                    String code = path.leaf().nodeId == NodeTypes.DEFNNODE ? "def"
186:                            : "class";
187:                    return NbBundle.getMessage(ExpandSameLineDef.class,
188:                            "ExpandLineFix", code);
189:                }
190:
191:                private void findLineBreaks(Node node, Set<Integer> offsets) {
192:                    if (node.nodeId == NodeTypes.NEWLINENODE) {
193:                        offsets.add(node.getPosition().getStartOffset());
194:                    }
195:                    @SuppressWarnings(value="unchecked")
196:                    List<Node> list = node.childNodes();
197:
198:                    for (Node child : list) {
199:                        findLineBreaks(child, offsets);
200:                    }
201:                }
202:
203:                /** 
204:                 * Try to split a line like
205:                 *   class FooController; def rescue_action(e) raise e end; end 
206:                 * into multiple lines. We can use lexical tokens like ";" as a clue
207:                 * to where to put newlines, but we want to use the AST too such that
208:                 * we see that we need a newline between the argument (e) and raise in the
209:                 * above line.
210:                 * <p>
211:                 * By using both we'll get some offsets in the same area so we'll need
212:                 * to be careful when applying our ;-to-\n replacements and our \n insertions
213:                 * so we don't get multiple newlines for places where both the AST and
214:                 * the semicolons suggest we need newlines.
215:                 */
216:                public void implement() throws Exception {
217:                    getEditList().apply();
218:                }
219:
220:                public EditList getEditList() throws Exception {
221:                    BaseDocument doc = (BaseDocument) info.getDocument();
222:                    ISourcePosition pos = path.leaf().getPosition();
223:                    int startOffset = pos.getStartOffset();
224:                    int endOffset = pos.getEndOffset();
225:                    if (endOffset > doc.getLength()) {
226:                        if (startOffset > doc.getLength()) {
227:                            startOffset = doc.getLength();
228:                        }
229:                        endOffset = doc.getLength();
230:                    }
231:
232:                    // Look through the document and find the statement separators (;);
233:                    // at these locations I'll replace the ; with a newline and then
234:                    // apply a formatter
235:                    Set<Integer> offsetSet = new HashSet<Integer>();
236:                    findLineBreaks(path.leaf(), offsetSet);
237:
238:                    // Add in ; replacements
239:                    TokenSequence<? extends RubyTokenId> ts = LexUtilities
240:                            .getRubyTokenSequence(doc, endOffset);
241:                    if (ts != null) {
242:                        // Traverse sequence in reverse order such that my offset list is in decreasing order
243:                        ts.move(endOffset);
244:                        while (ts.movePrevious() && ts.offset() > startOffset) {
245:                            Token<? extends RubyTokenId> token = ts.token();
246:                            TokenId id = token.id();
247:
248:                            if (id == RubyTokenId.IDENTIFIER
249:                                    && ";".equals(token.text().toString())) { // NOI18N
250:                                offsetSet.add(ts.offset());
251:                            } else if (id == RubyTokenId.CLASS
252:                                    || id == RubyTokenId.DEF
253:                                    || id == RubyTokenId.END) {
254:                                offsetSet.add(ts.offset());
255:                            }
256:                        }
257:                    }
258:
259:                    List<Integer> offsets = new ArrayList<Integer>(offsetSet);
260:                    Collections.sort(offsets);
261:                    // Ensure that we go in high to lower order such that I edit the
262:                    // document from bottom to top (so offsets don't have to be adjusted
263:                    // to account for our own edits along the way)
264:                    Collections.reverse(offsets);
265:
266:                    EditList edits = new EditList(doc);
267:
268:                    if (offsets.size() > 0) {
269:                        // TODO: Create a ModificationResult here and process it
270:                        // The following is the WRONG way to do it...
271:                        // I've gotta use a ModificationResult instead!
272:
273:                        List<Integer> newlines = new ArrayList<Integer>();
274:
275:                        try {
276:                            // Process offsets from back to front such that I can
277:                            // modify the document without worrying that the other offsets
278:                            // need to be adjusted
279:                            int prev = -1;
280:                            for (int offset : offsets) {
281:                                // We might get some dupes since we add offsets from both
282:                                // the AST newline nodes and semicolons discovered in the lexical token hierarchy
283:                                if (offset == prev) {
284:                                    continue;
285:                                }
286:                                prev = offset;
287:                                if (";".equals(doc.getText(offset, 1))) { // NOI18N
288:                                    edits.replace(offset, 1, null, false, 1);
289:                                    if (newlines.contains(offset + 2)) {
290:                                        continue;
291:                                    }
292:                                }
293:                                if (newlines.contains(offset + 1)
294:                                        || newlines.contains(offset)) {
295:                                    continue;
296:                                }
297:                                edits.replace(offset, 0, "\n", false, 2); // NOI18N
298:                                newlines.add(offset);
299:                            }
300:
301:                            edits.format();
302:                        } catch (BadLocationException ble) {
303:                            Exceptions.printStackTrace(ble);
304:                        }
305:                    }
306:
307:                    return edits;
308:                }
309:
310:                public boolean isSafe() {
311:                    return true;
312:                }
313:
314:                public boolean isInteractive() {
315:                    return false;
316:                }
317:
318:                public boolean canPreview() {
319:                    return true;
320:                }
321:            }
322:
323:            public boolean getDefaultEnabled() {
324:                return true;
325:            }
326:
327:            public HintSeverity getDefaultSeverity() {
328:                return HintSeverity.CURRENT_LINE_WARNING;
329:            }
330:
331:            public boolean showInTasklist() {
332:                return false;
333:            }
334:
335:            public JComponent getCustomizer(Preferences node) {
336:                return null;
337:            }
338:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.