Skip to content

Commit f5cf52d

Browse files
[native_doc_dartifier] Extract Public API Signatures for LLM Context (#2370)
* field summary * Update public_abstractor.dart * finish getting summary * operator methods * ignore line * reduce 80 chars line * method to get the summary * Revert "method to get the summary" This reverts commit cc8c61e. * Dart like representation
1 parent 7944c5a commit f5cf52d

File tree

3 files changed

+421
-24
lines changed

3 files changed

+421
-24
lines changed

pkgs/native_doc_dartifier/lib/src/ast.dart

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Class {
99
final String extendedClass;
1010
final List<String> implementedInterfaces;
1111

12+
final List<Constructor> constructors;
1213
final List<Field> fields;
1314
final List<Method> methods;
14-
final List<Constructor> constructors;
1515
final List<Getter> getters;
1616
final List<Setter> setters;
1717

@@ -27,9 +27,28 @@ class Class {
2727
getters = [],
2828
setters = [];
2929

30-
@override
31-
String toString() =>
32-
'''- ${isInterface ? 'interface ' : ''}${isAbstract ? 'abstract ' : ''}class $name ${extendedClass.isNotEmpty ? 'extends $extendedClass ' : ''}${implementedInterfaces.isNotEmpty ? 'implements ${implementedInterfaces.join(', ')} ' : ''}''';
30+
String toDartLikeRepresentaion() => '''
31+
${isInterface ? 'interface ' : ''}${isAbstract ? 'abstract ' : ''}class $name ${extendedClass.isNotEmpty ? 'extends $extendedClass ' : ''}${implementedInterfaces.isNotEmpty ? 'implements ${implementedInterfaces.join(', ')} ' : ''}
32+
{
33+
${constructors.map((c) => '${c.toString()};').join('\n')}
34+
${fields.map((f) => '${f.toString()};').join('\n')}
35+
${methods.map((m) => '${m.toString()};').join('\n')}
36+
${getters.map((g) => '${g.toString()};').join('\n')}
37+
${setters.map((s) => '${s.toString()};').join('\n')}
38+
}
39+
''';
40+
41+
void addField(Field field) {
42+
fields.add(field);
43+
}
44+
45+
void addMethod(Method method) {
46+
methods.add(method);
47+
}
48+
49+
void addGetter(Getter getter) {
50+
getters.add(getter);
51+
}
3352
}
3453

3554
class Field {
@@ -38,46 +57,75 @@ class Field {
3857
final bool isStatic;
3958

4059
Field(this.name, this.type, {this.isStatic = false});
60+
61+
String toDartLikeRepresentaion() => '${isStatic ? 'static ' : ''}$type $name';
4162
}
4263

4364
class Method {
4465
final String name;
4566
final String returnType;
4667
final bool isStatic;
47-
final List<Param> parameters;
68+
final String parameters;
69+
final String typeParameters;
70+
final String operatorKeyword;
4871

4972
Method(
5073
this.name,
5174
this.returnType,
52-
this.isStatic, {
53-
this.parameters = const [],
75+
this.isStatic,
76+
this.parameters,
77+
this.typeParameters, {
78+
this.operatorKeyword = '',
5479
});
55-
}
5680

57-
class Param {
58-
final String name;
59-
final String type;
81+
String toDartLikeRepresentaion() {
82+
final staticPrefix = isStatic ? 'static ' : '';
83+
final operatorPrefix =
84+
operatorKeyword.isNotEmpty ? '$operatorKeyword ' : '';
6085

61-
Param(this.name, this.type);
86+
return '$staticPrefix$returnType $operatorPrefix$name'
87+
'$typeParameters$parameters';
88+
}
6289
}
6390

6491
class Constructor {
92+
final String className;
6593
final String name;
66-
final List<String> parameters;
94+
final String parameters;
95+
final String? factoryKeyword;
6796

68-
Constructor(this.name, {this.parameters = const []});
97+
Constructor(this.className, this.name, this.parameters, this.factoryKeyword);
98+
99+
String toDartLikeRepresentaion() {
100+
final constructorName = name.isNotEmpty ? '$className.$name' : className;
101+
return '${factoryKeyword != null ? '$factoryKeyword ' : ''}'
102+
'$constructorName$parameters';
103+
}
69104
}
70105

71106
class Getter {
72107
final String name;
73108
final String returnType;
109+
final bool isStatic;
74110

75-
Getter(this.name, this.returnType);
111+
Getter(this.name, this.returnType, this.isStatic);
112+
113+
String toDartLikeRepresentaion() {
114+
final staticPrefix = isStatic ? 'static ' : '';
115+
return '$staticPrefix$returnType get $name';
116+
}
76117
}
77118

78119
class Setter {
79120
final String name;
80121
final String parameterType;
122+
final bool isStatic;
123+
final String parameter;
124+
125+
Setter(this.name, this.parameterType, this.isStatic, this.parameter);
81126

82-
Setter(this.name, this.parameterType);
127+
String toDartLikeRepresentaion() {
128+
final staticPrefix = isStatic ? 'static ' : '';
129+
return '$staticPrefix$parameterType set $name($parameter)';
130+
}
83131
}

pkgs/native_doc_dartifier/lib/src/public_abstractor.dart

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/dart/analysis/utilities.dart';
56
import 'package:analyzer/dart/ast/ast.dart';
67
import 'package:analyzer/dart/ast/visitor.dart';
78
import 'ast.dart';
89

10+
String generateBindingsSummary(String sourceCode) {
11+
final abstractor = PublicAbstractor();
12+
parseString(content: sourceCode).unit.visitChildren(abstractor);
13+
final summary = abstractor.getRepresentation();
14+
return summary.replaceAll('jni\$_.', '').replaceAll('core\$_.', '');
15+
}
16+
917
class PublicAbstractor extends RecursiveAstVisitor<void> {
1018
final Map<String, Class> _classes = {};
1119

@@ -35,11 +43,110 @@ class PublicAbstractor extends RecursiveAstVisitor<void> {
3543
}
3644
}
3745

38-
String generateClassRepresentation() {
46+
@override
47+
void visitFieldDeclaration(FieldDeclaration node) {
48+
final className =
49+
(node.parent is ClassDeclaration)
50+
? (node.parent as ClassDeclaration).name.lexeme
51+
: '';
52+
53+
if (className.isEmpty || !_isPublic(className)) return;
54+
55+
for (final variable in node.fields.variables) {
56+
final fieldName = variable.name.lexeme;
57+
if (_isPublic(fieldName)) {
58+
_classes[className]!.addField(
59+
Field(
60+
fieldName,
61+
node.fields.type?.toSource() ?? 'dynamic',
62+
isStatic: node.isStatic,
63+
),
64+
);
65+
}
66+
}
67+
}
68+
69+
@override
70+
void visitMethodDeclaration(MethodDeclaration node) {
71+
final className =
72+
(node.parent is ClassDeclaration)
73+
? (node.parent as ClassDeclaration).name.lexeme
74+
: '';
75+
76+
final methodName = node.name.lexeme;
77+
78+
if (className.isEmpty || !_isPublic(className) || !_isPublic(methodName)) {
79+
return;
80+
}
81+
82+
final returnType = node.returnType?.toSource() ?? 'dynamic';
83+
final parameters = node.parameters?.toSource() ?? '()';
84+
final typeParameters = node.typeParameters?.toSource() ?? '';
85+
final operatorKeyword = node.operatorKeyword?.stringValue ?? '';
86+
87+
if (node.isGetter) {
88+
_classes[className]!.getters.add(
89+
Getter(methodName, returnType, node.isStatic),
90+
);
91+
return;
92+
}
93+
94+
if (node.isSetter) {
95+
_classes[className]!.setters.add(
96+
Setter(
97+
methodName,
98+
returnType,
99+
node.isStatic,
100+
node.parameters?.toSource() ?? '()',
101+
),
102+
);
103+
return;
104+
}
105+
106+
_classes[className]!.addMethod(
107+
Method(
108+
methodName,
109+
returnType,
110+
node.isStatic,
111+
parameters,
112+
typeParameters,
113+
operatorKeyword: operatorKeyword,
114+
),
115+
);
116+
}
117+
118+
@override
119+
void visitConstructorDeclaration(ConstructorDeclaration node) {
120+
final className =
121+
(node.parent is ClassDeclaration)
122+
? (node.parent as ClassDeclaration).name.lexeme
123+
: '';
124+
125+
final constructorName = node.name?.lexeme ?? '';
126+
127+
if (className.isEmpty ||
128+
!_isPublic(className) ||
129+
!_isPublic(constructorName)) {
130+
return;
131+
}
132+
133+
final parameters = node.parameters.toSource();
134+
135+
_classes[className]!.constructors.add(
136+
Constructor(
137+
className,
138+
constructorName,
139+
parameters,
140+
node.factoryKeyword?.stringValue,
141+
),
142+
);
143+
}
144+
145+
String getRepresentation() {
39146
final buffer = StringBuffer();
40147

41148
for (final classInfo in _classes.values) {
42-
buffer.writeln(classInfo.toString());
149+
buffer.writeln(classInfo.toDartLikeRepresentaion());
43150
buffer.writeln();
44151
}
45152
return buffer.toString();

0 commit comments

Comments
 (0)