Skip to content
This repository has been archived by the owner on Dec 23, 2022. It is now read-only.

Commit

Permalink
Allow optional fields in proto3
Browse files Browse the repository at this point in the history
  • Loading branch information
jvolkman committed Feb 25, 2021
1 parent baa4fc3 commit cb2fa06
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ public void testOptionOrOptionalForProto2() {
assertSameElements(completions, "option", "optional");
}

public void testOptionForProto3() {
public void testOptionOrOptionalForProto3() {
withSyntax("proto3");
setInput("message Foo {", " repeated int32 field1 = 1;", " opt" + CARET_TAG, "}");
assertTrue(completeWithUniqueChoice());
assertResult("message Foo {", " repeated int32 field1 = 1;", " option ", "}");
Collection<String> completions = getCompletionItemsAsStrings();
assertNotNull(completions);
assertSameElements(completions, "option", "optional");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ message ExtensionRangesTest {

message FieldLabelsTest {
int32 valid = 1;
<error descr="The 'optional' keyword should not be specified in proto3 (fields are optional by default)">optional</error> int32 invalid_optional = 2;
optional int32 valid_optional = 2;
<error descr="Required fields are not supported in proto3">required</error> int32 invalid_required = 3;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,13 @@ private static void annotateExtensionsStatement(
}

/*
* Fields cannot be marked required or optional.
* Fields cannot be marked required.
* Field names must be unique after converting to lowercase and removing underscores.
* Enum types must be defined in proto3 files.
*/
private static void annotateField(PbField field, AnnotationHolder holder) {
PbFieldLabel label = field.getDeclaredLabel();
if (label != null && "optional".equals(label.getText())) {
holder.newAnnotation(HighlightSeverity.ERROR, PbLangBundle.message("proto3.optional.fields"))
.range(label)
.create();
} else if (label != null && "required".equals(label.getText())) {
if (label != null && "required".equals(label.getText())) {
holder.newAnnotation(HighlightSeverity.ERROR, PbLangBundle.message("proto3.required.fields"))
.range(label)
.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private static class NonTopLevelKeywords extends CompletionProvider<CompletionPa
.collect(Collectors.toList());

private static final List<LookupElement> PROTO3_FIELD_LABELS =
Stream.of("repeated")
Stream.of("optional", "repeated")
.map(PbCompletionContributor::lookupElementWithSpace)
.collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ proto3.messageset=MessageSet is not supported in proto3
proto3.weak.imports=Weak imports are not supported in proto3
proto3.extension.ranges=Extension ranges are not supported in proto3
proto3.required.fields=Required fields are not supported in proto3
proto3.optional.fields=The 'optional' keyword should not be specified in proto3 (fields are optional by default)
proto3.extensions=Only option extensions are allowed in proto3
proto3.default.values=Default values are not supported in proto3
proto3.group.fields=Group fields are not supported in proto3
Expand Down

0 comments on commit cb2fa06

Please sign in to comment.