Skip to content

Commit

Permalink
fix[note]: the first line of a multi-line note is not indented
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Feb 1, 2024
1 parent 22b0114 commit 370b1f5
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.FileUtils;
import com.zfoo.protocol.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* EN: When generating the protocol, the document comments and field comments of the protocol will use this class
Expand Down Expand Up @@ -68,15 +67,20 @@ public static String classNote(short protocolId, CodeLanguage language) {
return classNote;
}

public static String fieldNote(short protocolId, String fieldName, CodeLanguage language) {
public static List<String> fieldNote(short protocolId, String fieldName, CodeLanguage language) {
var protocolNote = protocolNoteMap.get(protocolId);
var fieldNoteMap = protocolNote.getValue();
var fieldNote = fieldNoteMap.get(fieldName);
if (StringUtils.isBlank(fieldNote)) {
return StringUtils.EMPTY;
return Collections.emptyList();
}
fieldNote = formatNote(language, fieldNote);
return fieldNote;
var multipleLineNotes = fieldNote.split(FileUtils.LS_REGEX);
var notes = new ArrayList<String>();
for(var oneLineNote : multipleLineNotes) {
var formatFieldNote = formatNote(language, oneLineNote);
notes.add(formatFieldNote);
}
return notes;
}

private static String formatNote(CodeLanguage language, String fieldNote) {
Expand All @@ -87,14 +91,14 @@ private static String formatNote(CodeLanguage language, String fieldNote) {
case TypeScript:
case CSharp:
case Protobuf:
fieldNote = StringUtils.format("// {}", fieldNote).replace("\n", "\n// ");
fieldNote = StringUtils.format("// {}", fieldNote);
break;
case Lua:
fieldNote = StringUtils.format("-- {}", fieldNote).replace("\n", "\n-- ");
fieldNote = StringUtils.format("-- {}", fieldNote);
break;
case Python:
case GdScript:
fieldNote = StringUtils.format("# {}", fieldNote).replace("\n", "\n# ");
fieldNote = StringUtils.format("# {}", fieldNote);
break;
case Enhance:
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ private static String fieldDefinition(ProtocolRegistration registration) {

var propertyFullName = StringUtils.format("{} {};", propertyType, propertyName);
// 生成注释
var filedNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Cpp);
if (StringUtils.isNotBlank(filedNote)) {
cppBuilder.append(TAB + TAB).append(filedNote).append(LS);
var fieldNotes = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Cpp);
for(var fieldNote : fieldNotes) {
cppBuilder.append(TAB + TAB).append(fieldNote).append(LS);
}
cppBuilder.append(TAB + TAB).append(propertyFullName).append(LS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ private static String fieldDefinition(ProtocolRegistration registration) {
var propertyType = toCsClassName(field.getGenericType().getTypeName());
var propertyFullName = StringUtils.format("public {} {};", propertyType, fieldName);
// 生成注释
var fieldNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.CSharp);
if (StringUtils.isNotBlank(fieldNote)) {
var fieldNotes = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.CSharp);
for(var fieldNote : fieldNotes) {
csBuilder.append(TAB + TAB).append(fieldNote).append(LS);
}
csBuilder.append(TAB + TAB).append(propertyFullName).append(LS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ private static String fieldDefinition(ProtocolRegistration registration) {
IFieldRegistration fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
var fieldName = field.getName();
// 生成注释
var fieldNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.GdScript);
if (StringUtils.isNotBlank(fieldNote)) {
var fieldNotes = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.GdScript);
for(var fieldNote : fieldNotes) {
gdBuilder.append(fieldNote).append(LS);
}
var fieldType = gdSerializer(fieldRegistration.serializer()).fieldType(field, fieldRegistration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ private static String fieldDefinition(ProtocolRegistration registration) {

var propertyFullName = StringUtils.format("{} {}", fieldName, fieldType);
// 生成注释
var filedNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Go);
if (StringUtils.isNotBlank(filedNote)) {
goBuilder.append(TAB).append(filedNote).append(LS);
var fieldNotes = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Go);
for(var fieldNote : fieldNotes) {
goBuilder.append(TAB).append(fieldNote).append(LS);
}
goBuilder.append(TAB).append(propertyFullName).append(LS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ private static String fieldDefinition(ProtocolRegistration registration) {
var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
var fieldName = field.getName();
// 生成注释
var fieldNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.JavaScript);
if (StringUtils.isNotBlank(fieldNote)) {
var fieldNotes = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.JavaScript);
for(var fieldNote : fieldNotes) {
fieldDefinitionBuilder.append(TAB).append(fieldNote).append(LS);
}
var triple = jsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ private static String valueOfMethod(ProtocolRegistration registration) {
var fieldRegistration = fieldRegistrations[i];
var fieldName = field.getName();
// 生成注释
var fieldNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Lua);
if (StringUtils.isNotBlank(fieldNote)) {
var fieldNotes = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Lua);
for(var fieldNote : fieldNotes) {
luaBuilder.append(TAB + TAB).append(fieldNote).append(LS);
}
var fieldDefaultValue = luaSerializer(fieldRegistration.serializer()).fieldDefaultValue(field, fieldRegistration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ private static String fieldDefinition(ProtocolRegistration registration) {
var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
var fieldName = field.getName();
// 生成注释
var fieldNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Python);
if (StringUtils.isNotBlank(fieldNote)) {
var fieldNotes = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Python);
for(var fieldNote : fieldNotes) {
pyBuilder.append(TAB).append(fieldNote).append(LS);
}
var fieldDefaultValue = pySerializer(fieldRegistration.serializer()).fieldDefaultValue(field, fieldRegistration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ private static String fieldDefinition(ProtocolRegistration registration) {
var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
var fieldName = field.getName();
// 生成注释
var fieldNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.TypeScript);
if (StringUtils.isNotBlank(fieldNote)) {
var fieldNotes = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.TypeScript);
for(var fieldNote : fieldNotes) {
fieldDefinitionBuilder.append(TAB).append(fieldNote).append(LS);
}
var triple = tsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
Expand Down

0 comments on commit 370b1f5

Please sign in to comment.