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.util.Collections;
031: import java.util.List;
032: import java.util.Set;
033: import java.util.prefs.Preferences;
034: import javax.swing.JComponent;
035: import org.jruby.ast.ConstDeclNode;
036: import org.jruby.ast.Node;
037: import org.jruby.ast.NodeTypes;
038: import org.netbeans.modules.gsf.api.CompilationInfo;
039: import org.netbeans.modules.gsf.api.OffsetRange;
040: import org.netbeans.modules.ruby.AstUtilities;
041: import org.netbeans.modules.ruby.hints.spi.AstRule;
042: import org.netbeans.modules.ruby.hints.spi.Description;
043: import org.netbeans.modules.ruby.hints.spi.Fix;
044: import org.netbeans.modules.ruby.hints.spi.HintSeverity;
045: import org.netbeans.modules.ruby.hints.spi.RuleContext;
046: import org.netbeans.modules.ruby.lexer.LexUtilities;
047: import org.openide.util.NbBundle;
048:
049: /**
050: * Check constant names to see if they conform to standard Ruby conventions
051: *
052: * @author Tor Norbye
053: */
054: public class ConstantNames implements AstRule {
055: public ConstantNames() {
056: }
057:
058: public boolean appliesTo(CompilationInfo info) {
059: return true;
060: }
061:
062: public Set<Integer> getKinds() {
063: return Collections.singleton(NodeTypes.CONSTDECLNODE);
064: }
065:
066: public void run(RuleContext context, List<Description> result) {
067: Node node = context.node;
068: CompilationInfo info = context.compilationInfo;
069:
070: assert (node.nodeId == NodeTypes.CONSTDECLNODE);
071: String name = ((ConstDeclNode) node).getName();
072:
073: for (int i = 0; i < name.length(); i++) {
074: if (Character.isLowerCase(name.charAt(i))) {
075: String displayName = NbBundle.getMessage(
076: ConstantNames.class, "InvalidConstantName");
077: OffsetRange range = AstUtilities.getNameRange(node);
078: range = LexUtilities.getLexerOffsets(info, range);
079: if (range != OffsetRange.NONE) {
080: List<Fix> fixList = Collections.emptyList();
081: Description desc = new Description(this ,
082: displayName, info.getFileObject(), range,
083: fixList, 1600);
084: result.add(desc);
085: }
086: return;
087: }
088: }
089: }
090:
091: public String getId() {
092: return "Constant_Names"; // NOI18N
093: }
094:
095: public String getDisplayName() {
096: return NbBundle.getMessage(ConstantNames.class,
097: "ConstantNameWarning");
098: }
099:
100: public String getDescription() {
101: return NbBundle.getMessage(ConstantNames.class,
102: "ConstantNameWarningDesc");
103: }
104:
105: public boolean getDefaultEnabled() {
106: return false;
107: }
108:
109: public HintSeverity getDefaultSeverity() {
110: return HintSeverity.WARNING;
111: }
112:
113: public boolean showInTasklist() {
114: return true;
115: }
116:
117: public JComponent getCustomizer(Preferences node) {
118: return null;
119: }
120: }
|