Skip to content

Commit

Permalink
move to getter
Browse files Browse the repository at this point in the history
  • Loading branch information
takumma committed Jun 10, 2024
1 parent 3e443a2 commit cc24570
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
32 changes: 7 additions & 25 deletions dartdoc_test/lib/dartdoc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

import 'dart:io';

import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:dartdoc_test/src/resource.dart';

import 'src/extractor.dart';
Expand All @@ -26,13 +24,8 @@ class DartDocTest {

Future<void> run() async {
final rootFolder = Directory.current.absolute.path;
final contextCollection = AnalysisContextCollection(
includedPaths: [rootFolder],
resourceProvider: PhysicalResourceProvider.INSTANCE,
);

final ctx = contextCollection.contextFor(rootFolder);
final session = ctx.currentSession;
final session = currentContext.currentSession;

// TODO: add `include` and `exclude` options
final files = Directory(rootFolder)
Expand Down Expand Up @@ -62,25 +55,14 @@ void main() {
''';
}

String wrapCodeSample(DocumentationCodeSample sample) {
final fileName = sample.comment.span.file.url?.path.split('/').last;

var result = <String>[];
result.addAll(sample.comment.imports);
result.add('');
result.add('import "./$fileName";\n');
result.add('void main() {');
result.add(sample.code.split('\n').map((l) => ' $l').join('\n'));
result.add('}');

return result.join('\n');
}

void writeCodeSamples(String filePath, List<DocumentationCodeSample> samples) {
for (final (i, s) in samples.indexed) {
final path = filePath.replaceAll('.dart', '_sample_$i.dart');
final code = wrapCodeSample(s);
print(code);
resourceProvider.setOverlay(path, content: code, modificationStamp: 0);
print(s.wrappedCode);
resourceProvider.setOverlay(
path,
content: s.wrappedCode,
modificationStamp: 0,
);
}
}
14 changes: 14 additions & 0 deletions dartdoc_test/lib/src/extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ class DocumentationCodeSample {
this.imports = const [],
this.hasMain = false,
});

String get wrappedCode {
final fileName = comment.span.file.url?.path.split('/').last;

final buf = StringBuffer();
buf.writeAll(comment.imports, '\n');
buf.writeln();
buf.writeln('import "$fileName";');
buf.writeln();
buf.writeln('void main() {');
buf.writeAll(code.split('\n').map((l) => ' $l'), '\n');
buf.writeln('}');
return buf.toString();
}
}

List<DocumentationCodeSample> extractFile(
Expand Down
2 changes: 1 addition & 1 deletion dartdoc_test/lib/src/resource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final resourceProvider =

final _current = Directory.current;

void currentContext = AnalysisContextCollection(
final currentContext = AnalysisContextCollection(
resourceProvider: resourceProvider,
includedPaths: [_current.absolute.path],
).contextFor(_current.absolute.path);

0 comments on commit cc24570

Please sign in to comment.