Comments (7)
import 'package:code_builder/code_builder.dart';
class CustomEmitter extends DartEmitter {
CustomEmitter() : super(Allocator.simplePrefixing(), true, false);
@OverRide
StringSink visitExpression(Expression expression, [StringSink? output]) {
var sink = output ?? StringBuffer();
if (expression is CodeExpression) {
expression.code.accept(this, sink);
} else if (expression is LiteralExpression) {
final value = expression.literal.toString();
if (value.startsWith("'") && value.endsWith("'")) {
sink.write('"' + value.substring(1, value.length - 1) + '"');
} else {
sink.write(value);
}
} else {
super.visitExpression(expression, sink);
}
return sink;
}
@OverRide
StringSink visitRecordLiteral(RecordLiteral literal, [StringSink? output]) {
var sink = output ?? StringBuffer();
sink.write('(');
var needsComma = false;
for (var field in literal.fields) {
if (needsComma) {
sink.write(', ');
}
field.accept(this, sink);
needsComma = true;
}
if (needsComma) {
sink.write(','); // Add trailing comma for named fields
}
sink.write(')');
return sink;
}
}
void main() {
var emitter = CustomEmitter();
var spec = Code('''
import 'package:lang_generator/lang_generator.dart';
@AppLangConfig(jsonExport: true)
@AppLangImporter(
path: "lib/app_lang_source.en_US.json",
locale: "en_US",
)
class AppLangSource {
var incomeTransaction = (
fields: (
amount: (tr_TR: "Fiyat", en_US: "Amount"),
paymentDate: (tr_TR: "Ödeme Tarihi", en_US: "Payment Date"),
),
actions: (
update: (tr_TR: "Düzenle", en_US: "Edit"),
detail: (tr_TR: "Detay", en_US: "Detail"),
addTag: (tr_TR: "Etiket Ekle", en_US: "Add Tag"),
),
validations: (
amount: (
notNull: (tr_TR: "Tutar girmelisiniz", en_US: "You must enter an amount"),
lessThen: (tr_TR: "Tutar 100'den küçük olmalı", en_US: "Amount must be less than 100"),
),
),
);
var expenseTransaction = (
fields: (
amount: (tr_TR: "Fiyat", en_US: "Amount"),
paymentDate: (tr_TR: "Ödeme Tarihi", en_US: "Payment Date"),
),
);
}
''');
print(spec.accept(emitter));
}
from code_builder.
The CustomEmitter class inherits from DartEmitter: -----
The visitExpression method overrides the default behavior to ensure double quotes are retained.
The visitRecordLiteral method is customized to add a trailing comma after each named field.
from code_builder.
@AkashKeote Thank you so much. It seems you are using different version of code_builder. But you gave me the idea and I did it.
code_builder: ^4.10.0
import 'package:code_builder/code_builder.dart';
import 'package:code_builder/src/specs/expression.dart';
// region [p]
class CustomEmitter extends DartEmitter {
CustomEmitter()
: super(
allocator: Allocator.simplePrefixing(),
orderDirectives: true,
useNullSafetySyntax: false,
);
@override
StringSink visitLiteralExpression(LiteralExpression expression, [StringSink? output]) {
output ??= StringBuffer();
final escaped = expression.literal.replaceAll('\\\'', '\'').replaceAll('\\n', '\n');
if (escaped.startsWith("'") && escaped.endsWith("'")) {
return output..write('"${escaped.substring(1, escaped.length - 1)}"');
}
return output..write(escaped);
}
@override
StringSink visitLiteralRecordExpression(LiteralRecordExpression expression, [StringSink? output]) {
final out = output ??= StringBuffer();
out.write('(');
_visitAll<Object?>(expression.positionalFieldValues, out, (value) {
_acceptLiteral(value, out);
});
if (expression.namedFieldValues.isNotEmpty) {
if (expression.positionalFieldValues.isNotEmpty) {
out.write(', ');
}
} else if (expression.positionalFieldValues.length == 1) {
out.write(',');
}
_visitAll<MapEntry<String, Object?>>(expression.namedFieldValues.entries, out, (entry) {
out.write('${entry.key}: ');
_acceptLiteral(entry.value, out);
});
if (expression.namedFieldValues.isNotEmpty) {
out.write(',');
}
return out..write(')');
}
/// Helper method improving on [StringSink.writeAll].
///
/// For every `Spec` in [elements], executing [visit].
///
/// If [elements] is at least 2 elements, inserts [separator] delimiting them.
StringSink _visitAll<T>(
Iterable<T> elements,
StringSink output,
void Function(T) visit, [
String separator = ', ',
]) {
// Basically, this whole method is an improvement on
// output.writeAll(specs.map((s) => s.accept(visitor));
//
// ... which would allocate more StringBuffer(s) for a one-time use.
if (elements.isEmpty) {
return output;
}
final iterator = elements.iterator..moveNext();
visit(iterator.current);
while (iterator.moveNext()) {
output.write(separator);
visit(iterator.current);
}
return output;
}
void _acceptLiteral(Object? literalOrSpec, StringSink output) {
if (literalOrSpec is Spec) {
literalOrSpec.accept(this, output);
return;
}
literal(literalOrSpec).accept(this, output);
}
}
// endregion
from code_builder.
need your help how to contribute on the gssoc as my exam is over now and i am in 2nd year dont have much idea about pull request and fork button not here
from code_builder.
Fork Button on the home page of the repo.
You can make changes and then you can create pull request on your forked repo.
![image](https://private-user-images.githubusercontent.com/7986394/335861920-4c83d003-269a-4fb8-bb18-4ea56fede46a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg3OTQwNjcsIm5iZiI6MTcxODc5Mzc2NywicGF0aCI6Ii83OTg2Mzk0LzMzNTg2MTkyMC00YzgzZDAwMy0yNjlhLTRmYjgtYmIxOC00ZWE1NmZlZGU0NmEucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYxOSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MTlUMTA0MjQ3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MjMzZjYyNmZjNmZhMTUwNWI2YTU3OTY3ZjU3OGFlNDQ2YTkzMmYxMWVkOTIwMzFmZjJlNzY2YTVlNWM5NTdmNyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.hJuYEF989OaBo35BZp4kdvOeVVc8veIsRRkxHDzZ9Zg)
from code_builder.
thanku so much sir this will help me a lot
from code_builder.
@AkashKeote You are welcome. You helped me a lot, too. Thank you. 👍
from code_builder.
Related Issues (20)
- Allow defining an EnumValue with named parameters
- Reduce the use of trailing commas
- Add support for extension types HOT 4
- How to add generics to typedef?
- Testing only code (ie: matchers) should be exported through a specific library HOT 1
- Add external key word for the FieldBuilder HOT 1
- late keyword is not working when defining a specific type HOT 2
- Records are not supported
- Reverse conversion: Dart syntax into code_builder syntax
- how can i create switch statement? when write Code("case A: "), i meet 'case' can't be used as an identifier because it's a keyword.
- Allow omitting parentheses for Expression.asA(Expression other) HOT 2
- update to allow for an unnamed library HOT 3
- Support emitting to a part file (`.g.dart`) HOT 3
- Generated extension class doesn't have blank line between members
- build_web_compilers:entrypoint on web/main.dart:
- `LiteralMapExpression` needs support for collection-if expression
- Add private constructor option HOT 1
- `literalString` \r\n(CRLF) causes an error
- Is it possible to generate a Library object from string data?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from code_builder.