Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Improved generated comments #16

Merged
merged 4 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
878 changes: 815 additions & 63 deletions example/libclang-example/generated_bindings.dart

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example/libclang-example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ ffigen:
unsigned long long: 8
enum: 4

# True by default
extract-comments: true
# Doc Comments for generated binings: Can be full, brief(default) or none
comments: full
13 changes: 10 additions & 3 deletions lib/src/code_generator/struc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ class Struc extends Binding {
s.write(arrayHelper.declarationString(w));
helpers.add(arrayHelper);
} else {
const depth = ' ';
if (m.dartDoc != null) {
s.write(depth + '/// ');
s.writeAll(m.dartDoc.split('\n'), '\n' + depth + '/// ');
s.write('\n');
}
if (m.type.isPrimitive) {
s.write(' @${m.type.getCType(w)}()\n');
s.write('$depth@${m.type.getCType(w)}()\n');
}
s.write(' ${m.type.getDartType(w)} ${m.name};\n\n');
s.write('$depth${m.type.getDartType(w)} ${m.name};\n\n');
}
}
s.write('}\n\n');
Expand All @@ -88,10 +94,11 @@ class Struc extends Binding {
}

class Member {
final String dartDoc;
final String name;
final Type type;

const Member({this.name, this.type});
const Member({this.name, this.type, this.dartDoc});
}

// Helper bindings for struct array.
Expand Down
18 changes: 9 additions & 9 deletions lib/src/config_provider/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class Config {
/// If typedef of supported types(int8_t) should be directly used.
bool useSupportedTypedefs;

/// If tool should extract doc comment from bindings.
bool extractComments;
/// Extracted Doc comment type.
String comment;

/// Manually creating configurations.
///
Expand All @@ -77,7 +77,7 @@ class Config {
this.enumClassFilters,
this.sort = false,
this.useSupportedTypedefs = true,
this.extractComments = true,
this.comment,
});

Config._();
Expand Down Expand Up @@ -243,13 +243,13 @@ class Config {
extractedResult: (dynamic result) =>
useSupportedTypedefs = result as bool,
),
strings.extractComments: Specification<bool>(
description: 'whether or not to extract comments from bindings',
strings.comments: Specification<String>(
description: 'Type of comment to extract',
isRequired: false,
validator: booleanValidator,
extractor: booleanExtractor,
defaultValue: true,
extractedResult: (dynamic result) => extractComments = result as bool,
validator: commentValidator,
extractor: commentExtractor,
defaultValue: strings.brief,
extractedResult: (dynamic result) => comment = result as String,
),
};
}
Expand Down
20 changes: 19 additions & 1 deletion lib/src/config_provider/spec_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ bool filterValidator(String name, dynamic value) {
for (final subkey in value[key].keys) {
if (subkey == strings.matches || subkey == strings.names) {
if (value[key][subkey] is! YamlList) {
_logger.severe("Expected '$name -> $key -> $subkey' to be a List.");
_logger
.severe("Expected '$name -> $key -> $subkey' to be a List.");
_result = false;
}
} else {
Expand Down Expand Up @@ -239,3 +240,20 @@ SupportedNativeType nativeSupportedType(int value, {bool signed = true}) {
'Unsupported value given to sizemap, Allowed values for sizes are: 1, 2, 4, 8');
}
}

String commentExtractor(dynamic value) => value as String;

bool commentValidator(String name, dynamic value) {
if (value is! String) {
_logger.severe("Expected value of key '$name' to be a String.");
return false;
} else {
if (strings.commentTypeSet.contains(value as String)) {
return true;
} else {
_logger.severe(
"Value of key '$name' must be one of the following - ${strings.commentTypeSet}.");
return false;
}
}
}
Loading