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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.javascript.hints;
040:
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.List;
044: import java.util.Set;
045: import java.util.prefs.Preferences;
046: import javax.swing.JComponent;
047: import org.mozilla.javascript.Node;
048: import org.netbeans.editor.BaseDocument;
049: import org.netbeans.editor.Utilities;
050: import org.netbeans.modules.gsf.api.CompilationInfo;
051: import org.netbeans.modules.gsf.api.Error;
052: import org.netbeans.modules.gsf.api.OffsetRange;
053: import org.netbeans.modules.javascript.editing.AstUtilities;
054: import org.netbeans.modules.javascript.editing.lexer.LexUtilities;
055: import org.netbeans.modules.javascript.hints.spi.Description;
056: import org.netbeans.modules.javascript.hints.spi.EditList;
057: import org.netbeans.modules.javascript.hints.spi.ErrorRule;
058: import org.netbeans.modules.javascript.hints.spi.Fix;
059: import org.netbeans.modules.javascript.hints.spi.HintSeverity;
060: import org.netbeans.modules.javascript.hints.spi.PreviewableFix;
061: import org.netbeans.modules.javascript.hints.spi.RuleContext;
062: import org.openide.util.NbBundle;
063:
064: /**
065: * Test for equality (==) mistyped as assignment (=)?
066: *
067: * @author Tor Norbye
068: */
069: public class AccidentalAssignment implements ErrorRule {
070:
071: public Set<String> getCodes() {
072: return Collections.singleton("msg.equal.as.assign"); // NOI18N
073: }
074:
075: public void run(RuleContext context, Error error,
076: List<Description> result) {
077: CompilationInfo info = context.compilationInfo;
078: BaseDocument doc = context.doc;
079:
080: Node node = (Node) error.getParameters()[0];
081: int astOffset = error.getStartPosition();
082: OffsetRange astRange = AstUtilities.getRange(node);
083: OffsetRange range = LexUtilities
084: .getLexerOffsets(info, astRange);
085: if (range != OffsetRange.NONE) {
086: range = StrictWarning.limitErrorToLine(doc, range,
087: astOffset);
088:
089: List<Fix> fixList = new ArrayList<Fix>(2);
090: fixList.add(new ConvertAssignmentFix(info, node, error,
091: false));
092: fixList.add(new ConvertAssignmentFix(info, node, error,
093: true));
094: Description desc = new Description(this , getDisplayName(),
095: info.getFileObject(), range, fixList, 500);
096: result.add(desc);
097: }
098: }
099:
100: public boolean appliesTo(CompilationInfo compilationInfo) {
101: return true;
102: }
103:
104: public String getDisplayName() {
105: return NbBundle.getMessage(AccidentalAssignment.class,
106: "AccidentalAssignment");
107: }
108:
109: public boolean showInTasklist() {
110: return true;
111: }
112:
113: public HintSeverity getDefaultSeverity() {
114: return HintSeverity.WARNING;
115: }
116:
117: public String getId() {
118: return "AccidentalAssignment"; // NOI18N
119: }
120:
121: public String getDescription() {
122: return NbBundle.getMessage(AccidentalAssignment.class,
123: "AccidentalAssignmentDesc");
124: }
125:
126: public boolean getDefaultEnabled() {
127: return true;
128: }
129:
130: public JComponent getCustomizer(Preferences node) {
131: return null;
132: }
133:
134: private static class ConvertAssignmentFix implements PreviewableFix {
135:
136: private CompilationInfo info;
137: private Node assignment;
138: private Error error;
139: private boolean ignore;
140:
141: public ConvertAssignmentFix(CompilationInfo info,
142: Node assignment, Error error, boolean ignore) {
143: this .info = info;
144: this .assignment = assignment;
145: this .error = error;
146: this .ignore = ignore;
147: }
148:
149: public String getDescription() {
150: return ignore ? NbBundle.getMessage(
151: AccidentalAssignment.class,
152: "AccidentalAssignmentIgnore") : NbBundle
153: .getMessage(AccidentalAssignment.class,
154: "AccidentalAssignmentFix");
155: }
156:
157: public void implement() throws Exception {
158: EditList edits = getEditList();
159: if (edits != null) {
160: edits.apply();
161: }
162: }
163:
164: public EditList getEditList() throws Exception {
165: // TODO - offer to add double parentheses around the expression to "really mean it"
166: BaseDocument doc = (BaseDocument) info.getDocument();
167: EditList list = new EditList(doc);
168: OffsetRange astRange = AstUtilities
169: .getNameRange(assignment);
170: OffsetRange lexRange = LexUtilities.getLexerOffsets(info,
171: astRange);
172: if (lexRange == OffsetRange.NONE) {
173: return list;
174: }
175:
176: if (ignore) {
177: list.replace(lexRange.getStart(), 0, "(", false, 0);
178: list.replace(lexRange.getEnd(), 0, ")", false, 0);
179: } else {
180: int startOffset = lexRange.getStart();
181: int lineEnd = Utilities.getRowEnd(doc, startOffset);
182: String line = doc.getText(startOffset, lineEnd
183: - startOffset);
184: int dashIndex = line.indexOf('=');
185: if (dashIndex == -1) {
186: int astOffset = error.getStartPosition();
187: int lexOffset = LexUtilities.getLexerOffset(info,
188: astOffset);
189: if (lexOffset == -1) {
190: return list;
191: }
192: startOffset = Utilities.getRowStart(doc, lexOffset);
193: lineEnd = Utilities.getRowEnd(doc, startOffset);
194: line = doc.getText(startOffset, lineEnd
195: - startOffset);
196: dashIndex = line.indexOf('=');
197: }
198: if (dashIndex != -1) {
199: list.replace(startOffset + dashIndex, 0, "=",
200: false, 0);
201: }
202: }
203:
204: return list;
205: }
206:
207: public boolean isSafe() {
208: return false;
209: }
210:
211: public boolean isInteractive() {
212: return false;
213: }
214:
215: public boolean canPreview() {
216: return true;
217: }
218: }
219: }
|