Skip to content

Commit

Permalink
clean dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
chen56 committed Apr 24, 2024
1 parent 07bf11e commit efef967
Showing 1 changed file with 0 additions and 297 deletions.
297 changes: 0 additions & 297 deletions bin/notecli.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:args/command_runner.dart';
import 'package:code_builder/code_builder.dart' as code;
import 'package:code_builder/code_builder.dart';
Expand All @@ -17,7 +16,6 @@ import 'package:analyzer/dart/analysis/utilities.dart' as analyzer_util;
import 'package:watcher/watcher.dart';
import 'package:yaml_edit/yaml_edit.dart' show YamlEditor;

import 'package:you_dart/src/core.dart';

const String _libRoot = "lib";
const String _notesRoot = "lib/pages";
Expand Down Expand Up @@ -206,24 +204,6 @@ $fields
}
}

// // Generated by note_dev_gen.dart, please don't edit!
// // ignore_for_file: library_prefixes, non_constant_identifier_names
//
// import 'package:you_note_dart/note.dart';
// import 'package:flutter_web/1.welcome/page.dart' deferred as welcome_;
// import 'package:flutter_web/1.welcome/page.g.dart' as welcome_g;
// import 'package:flutter_web/note_app.dart';
//
// mixin PathsMixin {
// final Note welcome = put2(
// "/1.welcome",
// (shortTitle: "welcome",),
// welcome_g.noteInfo,
// () => welcome_
// .loadLibrary()
// .then((value) => welcome_.page, onError: onError));
// }

class NoteLib {
final FileSystem fs;
final NotesGenerator noteGenerator;
Expand Down Expand Up @@ -301,25 +281,6 @@ class NoteLib {
}
}

enum _CellType { header, body, tail }

typedef _CellInfo = ({
String cellType,
int offset,
int end,
List<Statement> cellStatements,
List<
({
String nodeType,
AstNode node,
})> specialNodes,
});

typedef _NoteInfo = ({
String code,
List<_CellInfo> cells,
});

class PageData {
final NoteLib noteLib;
final DartFormatter fmt;
Expand All @@ -344,214 +305,6 @@ class PageData {

get file => noteLib.file;

_NoteInfo _collectInfo() {
var buildFunc = unit.declarations.whereType<FunctionDeclaration>().where((e) => "${e.name}" == "build");

if (buildFunc.isEmpty) {
_log(" ${noteLib.file.path} [build] function not found, so it is a empty note");
return (
code: content,
cells: [
(
cellType: _CellType.header.name,
offset: 0,
end: unit.end,
cellStatements: [],
specialNodes: [],
)
],
);
}

var buildBody = buildFunc.first.functionExpression.body;
assert(buildBody is BlockFunctionBody, "build() func only support Block Function, but(${buildBody.runtimeType})");
var buildBodyBlock = (buildBody as BlockFunctionBody).block;

List<_CellInfo> body = [];
List<Statement> cellStatements = [];
int offset = buildBodyBlock.offset + 1;
for (var st in buildBodyBlock.statements) {
var statementType = _cellStatementType(st);

// log("statement runtimeType:${st.runtimeType} - statementType:$statementType - offset:${st.offset} len:${st.length} end:${st.end} file.len:${content.length} ,unit.len:${unit.length} ");
// log("---${content.toString().safeSubstring(st.offset, st.offset + 20)}---");

if (statementType == _CellStatementType.normal) {
cellStatements.add(st);
continue;
}
if (statementType == _CellStatementType.line) {
// Submit previously collected statements first
// Cell boundary line without builder : code above line statements
// The line [cellStatementType.line] was ignored

body.add((
cellType: _CellType.body.name,
offset: offset,
end: st.offset,
cellStatements: cellStatements,
specialNodes: _collectRunInCellStatements(cellStatements),
));
//reset collect
cellStatements = [];

// The starting point of the new cell is located below the line statement
offset = st.end;
continue;
}

throw Exception("not here! statementType:$statementType statement:$st");
}

// Finally, add a collectCellStatements to cell, as the last cell
body.add((
cellType: _CellType.body.name,
offset: offset,
end: buildBodyBlock.rightBracket.offset,
cellStatements: cellStatements,
specialNodes: _collectRunInCellStatements(cellStatements),
));

// build(BuildContext context, Pen pen, MainCell print){
// ↑_____________________________________________________↑_______________
// findBuild.offset buildBodyBlock.leftBracket.end
//
// } // build end
// ↑_____________________________________________________
// buildBodyBlock.rightBracket.offset
//
// } // build end
// _↑_____________________________________________________
// findBuild.end

return (
code: content,
cells: [
(
cellType: _CellType.header.name,
offset: 0,
end: buildBodyBlock.leftBracket.end,
cellStatements: [],
specialNodes: [],
),
...body,
(
cellType: _CellType.tail.name,
offset: buildBodyBlock.rightBracket.offset,
end: unit.end,
cellStatements: [],
specialNodes: [],
)
],
);
}

/// _cellStatementType.line :
/// ```dart
/// print.next(title:const Text("new cell-----------------"));
/// ```
static _CellStatementType _cellStatementType(Statement statement) {
if (statement is! ExpressionStatement) {
return _CellStatementType.normal;
}
var expression = statement.expression;

if (expression is! MethodInvocation) {
return _CellStatementType.normal;
}
// print(
// "_cellStatementType expression.target?.staticType: ${expression.target}");
// print.next(title:const Text("new cell-----------------"))
if (expression.target?.toString() != "print") {
return _CellStatementType.normal;
}
// print(
// "_cellStatementType expression.methodName.name: ${expression.methodName.name}");
if (expression.methodName.name != "next") {
return _CellStatementType.normal;
}

return _CellStatementType.line;
}

/*
// ignore: always_use_package_imports
import 'page.dart';
final noteInfo = (
meta: page,
cells: [
///
(cellType: 'header', offset: 0, end: 255),
],
code:
""
);
*/
@Deprecated("直接动态获取代码")
Future<PageData> _gen(_NoteInfo source) async {
var cells = source.cells.map((e) {
var comment = e.cellStatements.map((e) => strings.safeSubstring(e.toString().replaceAll("\n", " "), 0, 30));

var specialNodes = e.specialNodes.map((e) => """
(
nodeType: '${e.nodeType}',
offset: ${e.node.offset},
end: ${e.node.end},
)
""").join(",");
return """
/// $comment
(
cellType:'${e.cellType}',
offset:${e.offset},
end:${e.end},
specialNodes: <({
String nodeType,
int end,
int offset,
})>[ $specialNodes ] ,
)
""";
}).join(",");
Library lib = Library((b) => b
..comments.addAll(["Generated by github.com/chen56/note, please don't edit! "])
..body.add(
code.Block((b) => b
..statements.addAll([
Code('''
noteInfo() => (
cells: [ $cells ],
);
'''),
])),
));

DartEmitter emitter = DartEmitter(
allocator: Allocator.none,
orderDirectives: true,
useNullSafetySyntax: true,
);
String content = lib.accept(emitter).toString();
File writeTo = noteLib.file.parent.childFile("note.g.dart");
// write file
// 写2次文件,方便调试,如果格式化出错,还可以看下上面未格式化的版本看看哪错了
await writeTo.writeAsString(content);
await writeTo.writeAsString(fmt.format(content));
return this;
}

static List<({String nodeType, AstNode node})> _collectRunInCellStatements(List<Statement> topLevelCellStatements) {
List<({String nodeType, AstNode node})> collected = List.empty(growable: true);
List<AstVisitor> collectors = [_FindRunInCell(collected), _FindMateSampleStatement(collected)];
for (var st in topLevelCellStatements) {
for (AstVisitor collector in collectors) {
st.visitChildren(collector);
}
}
return collected;
}

@override
String toString() {
return "$file";
Expand All @@ -563,56 +316,6 @@ _log(Object? o) {
print("${DateTime.now()} - $o");
}

enum _CellStatementType {
/// cell split line statement
line,

/// normal statement , not a cell split statement
normal,
}

Statement _findFirstParentStatement(AstNode node) {
if (node is Statement) return node;
return _findFirstParentStatement(node.parent!);
}

class _FindRunInCell extends GeneralizingAstVisitor {
static const String nodeType = "Pen.runInCurrentCell";
final List<
({
String nodeType,
AstNode node,
})> collect;

_FindRunInCell(this.collect);

@override
visitMethodInvocation(MethodInvocation node) {
var targetType = node.target?.staticType?.getDisplayString(withNullability: false);
if (targetType == "Pen" && node.methodName.name == "runInCurrentCell") {
collect.add((nodeType: nodeType, node: _findFirstParentStatement(node)));
}
return super.visitMethodInvocation(node);
}
}

class _FindMateSampleStatement extends GeneralizingAstVisitor {
static const String nodeType = "MateSample.new.firstParentStatement";

final List<({String nodeType, AstNode node})> collect;

_FindMateSampleStatement(this.collect);

@override
visitInstanceCreationExpression(InstanceCreationExpression node) {
// todo careful , this api is change from name to name2, not check
if ("${node.constructorName.type.name2}" == "MateSample") {
collect.add((nodeType: nodeType, node: _findFirstParentStatement(node)));
}
return super.visitInstanceCreationExpression(node);
}
}

class Pubspec {
static const _yamlePathAssets = ["flutter", "assets"];
late final YamlEditor _yamlEditor;
Expand Down

0 comments on commit efef967

Please sign in to comment.