Skip to content

Commit

Permalink
Merge pull request #9 from ARK4579/develop
Browse files Browse the repository at this point in the history
flutter widget parsing udpates
  • Loading branch information
ARK4579 authored Jun 5, 2021
2 parents 3064e6a + 6071341 commit 37ba54b
Show file tree
Hide file tree
Showing 44 changed files with 1,732 additions and 261 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# See https://www.dartlang.org/guides/libraries/private-files
*.exe

# Files and directories created by pub
.dart_tool/
Expand Down
4 changes: 2 additions & 2 deletions example/dj_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ void main() {
// Adding this line for demonstration only!
ImportDj(importStr: 'package:io/io.dart'),
FunctionDj(
description: 'Main entry point to this file!',
descriptionDj: 'Main entry point to this file!',
outputType: VariableType.Void,
name: 'main',
bodyCodeParts: [
FunctionCallDj(
name: 'print',
args: ["'Hellow World!'"],
args: ["'Hello World!'"],
),
],
),
Expand Down
29 changes: 29 additions & 0 deletions lib/main/djs/code_djs/base_widget_dj.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:dj/main/main.dart';

part 'base_widget_dj.g.dart';

@JsonSerializable()
class BaseWidgetDj extends CodePartDj {
@JsonKey(name: 'baseWidgetDjType')
final String baseWidgetDjType;

BaseWidgetDj({
required this.baseWidgetDjType,
descriptionDj,
CodePartDjType codePartDjType = CodePartDjType.BaseWidget,
}) : super(
descriptionDj: descriptionDj,
codePartDjType: codePartDjType,
);

factory BaseWidgetDj.fromJson(Map<String, dynamic> json) =>
_$BaseWidgetDjFromJson(json);
@override
Map<String, dynamic> toJson() => _$BaseWidgetDjToJson(this);

@override
List<String> toCode() {
return super.toCode();
}
}
76 changes: 76 additions & 0 deletions lib/main/djs/code_djs/base_widget_dj.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 154 additions & 0 deletions lib/main/djs/code_djs/class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:dj/main/main.dart';

part 'class.g.dart';

@JsonSerializable()
class ClassDj extends CodePartDj {
@JsonKey(name: 'name')
final String? name;

@JsonKey(name: 'baseName')
final String? baseName;

@JsonKey(name: 'fields')
final List<FieldDj>? fields;

@JsonKey(name: 'isExtends')
final bool? isExtends;

@JsonKey(name: 'isImplements')
final bool? isImplements;

@JsonKey(name: 'jsonSerializable')
final bool? jsonSerializable;

@JsonKey(name: 'functions')
final List<CodePartDj>? functions;

ClassDj({
descriptionDj,
this.name,
this.baseName,
this.fields,
this.isExtends,
this.isImplements,
this.jsonSerializable,
this.functions,
CodePartDjType codePartDjType = CodePartDjType.Class,
}) : super(
descriptionDj: descriptionDj,
codePartDjType: codePartDjType,
);

factory ClassDj.fromJson(Map<String, dynamic> json) =>
_$ClassDjFromJson(json);
@override
Map<String, dynamic> toJson() => _$ClassDjToJson(this);

List<String> _constructorCode() {
var codeLines = <String>[];

codeLines.add('$name({');

var superOnlyFields = <FieldDj>[];

fields?.forEach((field) {
String? comment;
var fieldLine = '${field.name}';
if (field.superOnly ?? false) {
superOnlyFields.add(field);
if (field.defaultValue != null) {
fieldLine = '$fieldLine = ${field.defaultValue}';
} else {
fieldLine = '$fieldLine';
}
} else if (field.hasDefaultValue) {
comment = 'ignoring defaultValue ${field.defaultValue}';
fieldLine = 'this.$fieldLine';
} else {
fieldLine = 'this.$fieldLine';
if ((field.isRequired ?? false) &&
(field.appliedDataType != 'dynamic')) {
fieldLine = 'required $fieldLine';
}
}
fieldLine += ',';
if (comment != null) {
fieldLine += ' // $comment';
}
if (!field.isPrivate) {
codeLines.add(fieldLine);
}
});

if (superOnlyFields.isEmpty) {
codeLines.add('});');
} else {
codeLines.add('}) : super(');
superOnlyFields.forEach((field) {
codeLines.add('${field.name}:${field.name},');
});
codeLines.add(');');
}

if (functions != null) {
codeLines.add('\n');
functions?.forEach((function) {
codeLines += function.toCode();
});
}

return codeLines;
}

@override
List<String> toCode() {
var _lines = super.toCode();

var _jsonSerializable = jsonSerializable ?? false;

if (name == null) return _lines;

var baseLine = '';
if ((isExtends ?? false) && (baseName ?? '').isNotEmpty) {
baseLine = 'extends $baseName';
}
var classStartLine = [
if (_jsonSerializable) '@JsonSerializable()',
'class $name $baseLine {'
];
_lines += classStartLine;

fields
?.where((field) => (!(field.superOnly ?? false) && !field.isPrivate))
.forEach((field) {
if (_jsonSerializable) {
_lines.add("@JsonKey(name: '${field.name}')");
}
_lines += field.toCode();
_lines.add('');
});

_lines = _lines + _constructorCode();

if (jsonSerializable ?? false) {
_lines.add('');
var jsFromLine1 = '$name.fromJson(Map<String, dynamic> json)';
var jsFromLine2 = '_\$${name}FromJson(json);';
var jsFromLine = 'factory $jsFromLine1 => $jsFromLine2';
_lines.add(jsFromLine);
}

if (jsonSerializable ?? false) {
_lines.add('');
_lines.add('@override');
_lines.add('Map<String, dynamic> toJson() => _\$${name}ToJson(this);');
}

// class end line
_lines.add('}');

return _lines;
}
}
93 changes: 93 additions & 0 deletions lib/main/djs/code_djs/class.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion lib/main/djs/code_djs/code_djs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@ export 'import.dart';
export 'function_call.dart';
export 'function.dart';
export 'if_else.dart';
export 'stateless_widget.dart';
export 'return.dart';
export 'field.dart';
export 'class.dart';
export 'enum.dart';
export 'map.dart';
export 'empty_line.dart';
export 'variable_declaration.dart';
export 'single_line.dart';
export 'export.dart';
export 'base_widget_dj.dart';
export 'manual_widget_dj.dart';
Loading

0 comments on commit 37ba54b

Please sign in to comment.