Created "quick fix" for error "syntax is not proto2".
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/Messages.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/Messages.java
new file mode 100644
index 0000000..03ea039
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/Messages.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ *
+ * http://www.eclipse.org/legal/epl-v10.html
+ */
+package com.google.eclipse.protobuf.ui.quickfix;
+
+import org.eclipse.osgi.util.NLS;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Messages extends NLS {
+
+ public static String changeToProto2;
+ public static String changeToProto2Label;
+
+ static {
+ Class<Messages> targetType = Messages.class;
+ NLS.initializeMessages(targetType.getName(), targetType);
+ }
+
+ private Messages() {}
+}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/Messages.properties b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/Messages.properties
new file mode 100644
index 0000000..54e5126
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/Messages.properties
@@ -0,0 +1,2 @@
+changeToProto2=Change syntax to "proto2".
+changeToProto2Label=Change syntax to "proto2"
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickAssistProcessor.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickAssistProcessor.java
index 15aa610..a5dc287 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickAssistProcessor.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickAssistProcessor.java
@@ -10,6 +10,7 @@
package com.google.eclipse.protobuf.ui.quickfix;
import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
import com.google.inject.Inject;
@@ -27,10 +28,21 @@
@Inject private SpellingCorrectionProcessor spellingCorrectionProcessor;
- @Override public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
+ @Override public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext context) {
List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
- proposals.addAll(asList(spellingCorrectionProcessor.computeQuickAssistProposals(invocationContext)));
- proposals.addAll(asList(super.computeQuickAssistProposals(invocationContext)));
+ proposals.addAll(spellingFixes(context));
+ proposals.addAll(asList(super.computeQuickAssistProposals(context)));
return proposals.toArray(new ICompletionProposal[proposals.size()]);
}
+
+ private List<ICompletionProposal> spellingFixes(IQuickAssistInvocationContext context) {
+ ICompletionProposal[] spellingFixes = spellingCorrectionProcessor.computeQuickAssistProposals(context);
+ if (spellingFixes.length == 0) return emptyList();
+ if (spellingFixes.length == 1 && isNoCompletionsProposal(spellingFixes[0])) return emptyList();
+ return asList(spellingFixes);
+ }
+
+ private boolean isNoCompletionsProposal(ICompletionProposal p) {
+ return p.getClass().getSimpleName().equals("NoCompletionsProposal");
+ }
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
index 59fc306..04f7973 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
@@ -1,29 +1,42 @@
/*
* Copyright (c) 2011 Google Inc.
- *
- * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
- * Public License v1.0 which accompanies this distribution, and is available at
- *
+ *
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ *
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.google.eclipse.protobuf.ui.quickfix;
-import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider;
+import static com.google.eclipse.protobuf.ui.quickfix.Messages.*;
+import static com.google.eclipse.protobuf.validation.ProtobufJavaValidator.SYNTAX_IS_NOT_PROTO2_ERROR_CODE;
+
+import com.google.eclipse.protobuf.protobuf.Syntax;
+import com.google.eclipse.protobuf.ui.labeling.Images;
+import com.google.inject.Inject;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.xtext.ui.editor.model.edit.*;
+import org.eclipse.xtext.ui.editor.quickfix.*;
+import org.eclipse.xtext.validation.Issue;
/**
* @author alruiz@google.com (Alex Ruiz)
*/
public class ProtobufQuickfixProvider extends DefaultQuickfixProvider {
-// @Fix(MyJavaValidator.INVALID_NAME)
-// public void capitalizeName(final Issue issue, IssueResolutionAcceptor acceptor) {
-// acceptor.accept(issue, "Capitalize name", "Capitalize the name.", "upcase.png", new IModification() {
-// public void apply(IModificationContext context) throws BadLocationException {
-// IXtextDocument xtextDocument = context.getXtextDocument();
-// String firstLetter = xtextDocument.get(issue.getOffset(), 1);
-// xtextDocument.replace(issue.getOffset(), 1, firstLetter.toUpperCase());
-// }
-// });
-// }
-
+ @Inject private Images images;
+
+ @Fix(SYNTAX_IS_NOT_PROTO2_ERROR_CODE)
+ public void makeSyntaxProto2(final Issue issue, IssueResolutionAcceptor acceptor) {
+ String image = images.imageFor(Syntax.class);
+ acceptor.accept(issue, changeToProto2Label, changeToProto2, image, new ISemanticModification() {
+ public void apply(EObject element, IModificationContext context) throws Exception {
+ if (!(element instanceof Syntax)) return;
+ Syntax syntax = (Syntax) element;
+ syntax.setName("proto2");
+ }
+ });
+ }
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
index 9de1574..1ede6e7 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
@@ -30,6 +30,9 @@
*/
public class ProtobufJavaValidator extends AbstractProtobufJavaValidator {
+ public static final String SYNTAX_IS_NOT_PROTO2_ERROR_CODE = "syntaxIsNotProto2";
+ public static final String TAG_NUMBER_IS_NOT_UNIQUE_ERROR_CODE = "tagNumberIsNotUnique";
+
@Inject private FieldOptions fieldOptions;
@Inject private ImportUriResolver uriResolver;
@Inject private IQualifiedNameProvider qualifiedNameProvider;
@@ -71,7 +74,7 @@
String name = syntax.getName();
if ("proto2".equals(name)) return;
String msg = (name == null) ? expectedSyntaxIdentifier : format(unrecognizedSyntaxIdentifier, name);
- error(msg, SYNTAX__NAME);
+ error(msg, syntax, SYNTAX__NAME, SYNTAX_IS_NOT_PROTO2_ERROR_CODE);
}
@Check public void checkTagNumberIsUnique(Field field) {
@@ -87,7 +90,7 @@
if (other.getIndex() != index) continue;
QualifiedName messageName = qualifiedNameProvider.getFullyQualifiedName(message);
String msg = format(fieldNumberAlreadyUsed, index, messageName.toString(), other.getName());
- error(msg, FIELD__INDEX);
+ error(msg, field, FIELD__INDEX, TAG_NUMBER_IS_NOT_UNIQUE_ERROR_CODE);
break;
}
}