Skip to content

Commit e8c5de4

Browse files
authored
Begin extracting file structure information from ModelElement (#3408)
1 parent 24af8a1 commit e8c5de4

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

lib/src/generator/file_structure.dart

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:dartdoc/src/model/model_element.dart';
6+
7+
const _html = 'html';
8+
const _md = 'md';
9+
10+
enum FileStructureMode {
11+
htmlOriginal,
12+
mdOriginal,
13+
}
14+
15+
/// This class defines an interface to allow [ModelElement]s and [Generator]s
16+
/// to get information about the desired on-disk representation of a single
17+
/// [ModelElement]. None of these getters should be considered valid unless
18+
/// [modelElement.isCanonical] is true. Implementations of this class can
19+
/// define which elements have their own files and which do not, how
20+
/// to lay them out on disk, and how to link different pages in the structure
21+
/// together.
22+
abstract class FileStructure {
23+
factory FileStructure(FileStructureMode mode, ModelElement modelElement) {
24+
switch (mode) {
25+
case FileStructureMode.htmlOriginal:
26+
return _FileStructureHtml(modelElement);
27+
case FileStructureMode.mdOriginal:
28+
return _FileStructureMd(modelElement);
29+
}
30+
}
31+
32+
/// Link to the [ModelElement] the information for this [FileStructure]
33+
/// applies to.
34+
// TODO(jcollins): consider not carrying a reference to a ModelElement and
35+
// calculating necessary bits at construction time. Might be challenging
36+
// if we want to calculate [hasIndependentFile] based on documentation
37+
// length or other variables not always available.
38+
ModelElement get modelElement;
39+
40+
/// True if an independent file should be created for this `ModelElement`.
41+
bool get hasIndependentFile;
42+
43+
/// Returns a string suitable for use as an in-page anchor for this element in
44+
/// its [ModelElement.enclosingElement] page.
45+
String get htmlId;
46+
47+
/// Returns a link fragment relative to the HTML base for this `modelElement`.
48+
/// Scrubbed of platform-local path separators. Must include an anchor
49+
/// if [hasIndependentFile] is false.
50+
String get href;
51+
52+
/// The file name for an independent file. Only valid if [hasIndependentFile]
53+
/// is `true`. May contain platform-local path separators. Includes
54+
/// the [fileType] and the [modelElement.enclosingElement]'s [dirName].
55+
String get fileName;
56+
57+
/// The directory name that should contain any elements enclosed by
58+
/// [modelElement].
59+
String get dirName;
60+
61+
/// A type (generally "html" or "md") to be appended to the file name.
62+
String get fileType;
63+
}
64+
65+
class _FileStructureHtml implements FileStructure {
66+
_FileStructureHtml(this.modelElement);
67+
68+
@override
69+
// TODO: implement fileName
70+
String get fileName => throw UnimplementedError();
71+
72+
@override
73+
String get fileType => _html;
74+
75+
@override
76+
// TODO: implement hasIndependentFile
77+
bool get hasIndependentFile => throw UnimplementedError();
78+
79+
@override
80+
// TODO: implement href
81+
String get href => throw UnimplementedError();
82+
83+
@override
84+
// TODO: implement htmlId
85+
String get htmlId => throw UnimplementedError();
86+
87+
@override
88+
final ModelElement modelElement;
89+
90+
@override
91+
// TODO: implement dirName
92+
String get dirName => throw UnimplementedError();
93+
}
94+
95+
class _FileStructureMd implements FileStructure {
96+
_FileStructureMd(this.modelElement);
97+
98+
@override
99+
// TODO: implement fileName
100+
String get fileName => throw UnimplementedError();
101+
102+
@override
103+
// TODO: implement fileType
104+
String get fileType => _md;
105+
106+
@override
107+
// TODO: implement hasIndependentFile
108+
bool get hasIndependentFile => throw UnimplementedError();
109+
110+
@override
111+
// TODO: implement href
112+
String get href => throw UnimplementedError();
113+
114+
@override
115+
// TODO: implement htmlId
116+
String get htmlId => throw UnimplementedError();
117+
118+
@override
119+
final ModelElement modelElement;
120+
121+
@override
122+
// TODO: implement dirName
123+
String get dirName => throw UnimplementedError();
124+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:dartdoc/src/generator/file_structure.dart';
6+
import 'package:test/test.dart';
7+
import 'package:test_reflective_loader/test_reflective_loader.dart';
8+
9+
import '../dartdoc_test_base.dart';
10+
import '../src/utils.dart';
11+
12+
void main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(FileStructureTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class FileStructureTest extends DartdocTestBase {
20+
@override
21+
String get libraryName => 'file_structure_test';
22+
23+
void test_createFileStructureForVariable() async {
24+
var library = await bootPackageWithLibrary('''
25+
var globalVar = 123;
26+
''');
27+
var globalVar = library.properties.named('globalVar');
28+
var htmlStructure =
29+
FileStructure(FileStructureMode.htmlOriginal, globalVar);
30+
var mdStructure = FileStructure(FileStructureMode.mdOriginal, globalVar);
31+
expect(htmlStructure.fileType, equals('html'));
32+
expect(mdStructure.fileType, equals('md'));
33+
}
34+
}

0 commit comments

Comments
 (0)