Source Code Cross Referenced for RubyIndenter.java in  » IDE » J » org » armedbear » j » 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 » J » org.armedbear.j 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * RubyIndenter.java
003:         *
004:         * Copyright (C) 2002 Jens Luedicke <jens@irs-net.com>
005:         * based on PythonIndenter.java
006:         * $Id: RubyIndenter.java,v 1.1.1.1 2002/09/24 16:08:22 piso Exp $
007:         *
008:         * This program is free software; you can redistribute it and/or
009:         * modify it under the terms of the GNU General Public License
010:         * as published by the Free Software Foundation; either version 2
011:         * of the License, or (at your option) any later version.
012:         *
013:         * This program is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016:         * GNU General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU General Public License
019:         * along with this program; if not, write to the Free Software
020:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
021:         */
022:
023:        package org.armedbear.j;
024:
025:        public final class RubyIndenter {
026:            private static final RubyMode mode = RubyMode.getMode();
027:
028:            private final Line line;
029:            private final Buffer buffer;
030:            private final int indentSize;
031:
032:            public RubyIndenter(Line line, Buffer buffer) {
033:                this .line = line;
034:                this .buffer = buffer;
035:                indentSize = buffer.getIndentSize();
036:            }
037:
038:            public int getCorrectIndentation() {
039:                final String lineFirst = getFirstIdentifier(line);
040:                if (lineFirst.equals("def"))
041:                    return indentDef();
042:                if (lineFirst.equals("when"))
043:                    return indentWhen();
044:                if (lineFirst.equals("end"))
045:                    return indentEnd();
046:                final Line model = findModel(line);
047:                if (model == null)
048:                    return 0;
049:                final int modelIndent = buffer.getIndentation(model);
050:                final String lineText = trimSyntacticWhitespace(line.getText());
051:                if (lineText.startsWith("}"))
052:                    return Math.max(modelIndent - indentSize, 0);
053:                final String modelText = trimSyntacticWhitespace(model
054:                        .getText());
055:                if (modelText.endsWith("do") || modelText.endsWith("|")) {
056:                    // e.g. "do |foo|"
057:                    return modelIndent + indentSize;
058:                }
059:                final String modelFirst = getFirstIdentifier(modelText);
060:                final String[] indentAfter = { "begin", "class", "def", "if",
061:                        "else", "elsif", "for", "module", "unless", "when",
062:                        "while" };
063:                if (Utilities.isOneOf(modelFirst, indentAfter))
064:                    return modelIndent + indentSize;
065:                // Unindent if the current line starts with "else" or "elsif".
066:                final String[] unindent = { "else", "elsif" };
067:                if (Utilities.isOneOf(lineFirst, unindent))
068:                    return Math.max(0, modelIndent - indentSize);
069:                return modelIndent;
070:            }
071:
072:            // Scan backwards for line starting with "def" or "class" and indent
073:            // accordingly.
074:            private int indentDef() {
075:                for (Line model = line.previous(); model != null; model = model
076:                        .previous()) {
077:                    String modelFirst = getFirstIdentifier(model);
078:                    if (modelFirst.equals("def"))
079:                        return buffer.getIndentation(model);
080:                    if (modelFirst.equals("class"))
081:                        return buffer.getIndentation(model)
082:                                + buffer.getIndentSize();
083:                }
084:                return 0;
085:            }
086:
087:            // Scan backwards for line starting with "when" or "case" and indent
088:            // accordingly. This doesn't work correctly with nested case statements!
089:            private int indentWhen() {
090:                for (Line model = line.previous(); model != null; model = model
091:                        .previous()) {
092:                    String modelFirst = getFirstIdentifier(model);
093:                    if (modelFirst.equals("when") || modelFirst.equals("case"))
094:                        return buffer.getIndentation(model);
095:                }
096:                return 0;
097:            }
098:
099:            private int indentEnd() {
100:                for (Line model = line.previous(); model != null; model = model
101:                        .previous()) {
102:                    if (model.isBlank() || model.trim().startsWith("#"))
103:                        continue;
104:                    String modelFirst = getFirstIdentifier(model);
105:                    if (modelFirst.equals("def"))
106:                        return buffer.getIndentation(model);
107:                    return Math.max(buffer.getIndentation(model) - indentSize,
108:                            0);
109:                }
110:                return 0;
111:            }
112:
113:            // Return last non-blank line before this one.
114:            private static Line findModel(Line line) {
115:                for (Line model = line.previous(); model != null; model = model
116:                        .previous()) {
117:                    if (!model.isBlank() && !model.trim().startsWith("#"))
118:                        return model;
119:                }
120:                return null;
121:            }
122:
123:            // Replace syntactic whitespace (quotes and comments) with actual space
124:            // characters and return trimmed string.
125:            private static String trimSyntacticWhitespace(String s) {
126:                RubySyntaxIterator it = new RubySyntaxIterator(null);
127:                return new String(it.hideSyntacticWhitespace(s)).trim();
128:            }
129:
130:            // Never returns null.
131:            private static String getFirstIdentifier(Line line) {
132:                return getFirstIdentifier(trimSyntacticWhitespace(line
133:                        .getText()));
134:            }
135:
136:            // Never returns null.
137:            private static String getFirstIdentifier(String s) {
138:                FastStringBuffer sb = new FastStringBuffer();
139:                final int length = s.length();
140:                int i = 0;
141:                while (i < length && Character.isWhitespace(s.charAt(i)))
142:                    ++i;
143:                char c;
144:                while (i < length && !Character.isWhitespace(c = s.charAt(i))) {
145:                    sb.append(c);
146:                    ++i;
147:                }
148:                return sb.toString();
149:            }
150:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.