Skip to content

Commit d9f8231

Browse files
authored
Option to skip elements with no comments (SourceDocs#76)
1 parent 54182a4 commit d9f8231

File tree

13 files changed

+61
-9
lines changed

13 files changed

+61
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ Typing `sourcedocs help <command>` we get a list of all options for that command
132132
-m, --module-name-path Include the module name as part of the output folder path
133133
-c, --clean Delete output folder before generating documentation
134134
-l, --collapsible Put methods, properties and enum cases inside collapsible blocks
135-
-t, --table-of-contents Generate a table of contents with properties and methods for each type
135+
-t, --table-of-contents Generate a table of contents with properties and methods for each type
136+
-s, --skip-empty Do not generate documentation for files with no comments.
136137
-r, --reproducible-docs Generate documentation that is reproducible: only depends on the sources.
137138
For example, this will avoid adding timestamps on the generated files.
138139
-h, --help Show help information

Sources/SourceDocsCLI/GenerateCommand.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ extension SourceDocs {
5555
help: "Generate a table of contents with properties and methods for each type")
5656
var tableOfContents = false
5757

58+
@Flag(name: .shortAndLong,
59+
help: "Do not generate documentation for files with no comments.")
60+
var skipEmpty = false
61+
5862
@Argument(help: "List of arguments to pass to xcodebuild")
5963
var xcodeArguments: [String] = []
6064

@@ -74,7 +78,7 @@ extension SourceDocs {
7478
minimumAccessLevel: minACL, includeModuleNameInPath: moduleNamePath,
7579
clean: clean, collapsibleBlocks: collapsible,
7680
tableOfContents: tableOfContents, xcodeArguments: xcodeArguments,
77-
reproducibleDocs: reproducibleDocs)
81+
reproducibleDocs: reproducibleDocs, skipEmpty: skipEmpty)
7882

7983
try DocumentationGenerator(options: options).run()
8084
}

Sources/SourceDocsLib/DocumentationGenerator/DocumentationGenerator.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import SourceKittenFramework
2626
/// - xcodeArguments: Array of `String` arguments to pass to xcodebuild. Defaults to an empty array.
2727
/// - reproducibleDocs: generate documentation that is reproducible: only depends on the sources.
2828
/// For example, this will avoid adding timestamps on the generated files. Defaults to false.
29+
/// - skipEmpty: Do not generate documentation for files with no comments.
2930
public struct DocumentOptions {
3031
public let allModules: Bool
3132
public let spmModule: String?
@@ -41,13 +42,14 @@ public struct DocumentOptions {
4142
public let tableOfContents: Bool
4243
public let xcodeArguments: [String]
4344
public let reproducibleDocs: Bool
45+
public let skipEmpty: Bool
4446

4547
public init(allModules: Bool, spmModule: String?, moduleName: String?,
4648
linkBeginningText: String = "", linkEndingText: String = ".md",
4749
inputFolder: String, outputFolder: String,
4850
minimumAccessLevel: AccessLevel = .public, includeModuleNameInPath: Bool = false,
4951
clean: Bool = false, collapsibleBlocks: Bool = false, tableOfContents: Bool = false,
50-
xcodeArguments: [String] = [], reproducibleDocs: Bool = false) {
52+
xcodeArguments: [String] = [], reproducibleDocs: Bool = false, skipEmpty: Bool = false) {
5153
self.allModules = allModules
5254
self.spmModule = spmModule
5355
self.moduleName = moduleName
@@ -62,6 +64,7 @@ public struct DocumentOptions {
6264
self.tableOfContents = tableOfContents
6365
self.xcodeArguments = xcodeArguments
6466
self.reproducibleDocs = reproducibleDocs
67+
self.skipEmpty = skipEmpty
6568
}
6669
}
6770

@@ -157,7 +160,8 @@ public final class DocumentationGenerator {
157160
private func process(dictionary: SwiftDocDictionary) {
158161
let markdownOptions = MarkdownOptions(collapsibleBlocks: options.collapsibleBlocks,
159162
tableOfContents: options.tableOfContents,
160-
minimumAccessLevel: options.minimumAccessLevel)
163+
minimumAccessLevel: options.minimumAccessLevel,
164+
skipEmpty: options.skipEmpty)
161165

162166
if let value: String = dictionary.get(.kind), let kind = SwiftDeclarationKind(rawValue: value) {
163167
if kind == .struct, let item = MarkdownObject(dictionary: dictionary, options: markdownOptions) {

Sources/SourceDocsLib/DocumentationGenerator/Elements/MarkdownEnum.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct MarkdownEnum: SwiftDocDictionaryInitializable, MarkdownConvertible {
3434
let first = substructure.first else {
3535
return nil
3636
}
37-
return MarkdownEnumCaseElement(dictionary: first)
37+
return MarkdownEnumCaseElement(dictionary: first, options: options)
3838
}
3939
properties = structure.compactMap { MarkdownVariable(dictionary: $0, options: options) }
4040
methods = structure.compactMap { MarkdownMethod(dictionary: $0, options: options) }
@@ -43,6 +43,11 @@ struct MarkdownEnum: SwiftDocDictionaryInitializable, MarkdownConvertible {
4343
properties = []
4444
methods = []
4545
}
46+
47+
let topLevelDoc: String? = dictionary.get(.documentationComment)
48+
guard !options.skipEmpty || topLevelDoc?.isEmpty == false || !cases.isEmpty || !properties.isEmpty || !methods.isEmpty else {
49+
return nil
50+
}
4651
}
4752

4853
var tableOfContents: String {
@@ -107,9 +112,17 @@ struct MarkdownEnumCaseElement: SwiftDocDictionaryInitializable, MarkdownConvert
107112
let dictionary: SwiftDocDictionary
108113

109114
init?(dictionary: SwiftDocDictionary) {
115+
fatalError("Not supported")
116+
}
117+
118+
init?(dictionary: SwiftDocDictionary, options: MarkdownOptions) {
110119
guard dictionary.isKind([.enumelement]) else {
111120
return nil
112121
}
122+
let topLevelDoc: String? = dictionary.get(.documentationComment)
123+
guard !options.skipEmpty || topLevelDoc?.isEmpty == false else {
124+
return nil
125+
}
113126
self.dictionary = dictionary
114127
}
115128

Sources/SourceDocsLib/DocumentationGenerator/Elements/MarkdownMethod.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ struct MarkdownMethod: SwiftDocDictionaryInitializable, MarkdownConvertible {
2727
guard dictionary.accessLevel >= options.minimumAccessLevel && dictionary.isKind(methods) else {
2828
return nil
2929
}
30+
let topLevelDoc: String? = dictionary.get(.documentationComment)
31+
guard !options.skipEmpty || topLevelDoc?.isEmpty == false else {
32+
return nil
33+
}
3034
self.dictionary = dictionary
3135
self.options = options
3236

Sources/SourceDocsLib/DocumentationGenerator/Elements/MarkdownObject.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ struct MarkdownObject: SwiftDocDictionaryInitializable, MarkdownConvertible {
3434
properties = []
3535
methods = []
3636
}
37+
38+
let topLevelDoc: String? = dictionary.get(.documentationComment)
39+
guard !options.skipEmpty || topLevelDoc?.isEmpty == false || !properties.isEmpty || !methods.isEmpty else {
40+
return nil
41+
}
3742
}
3843

3944
var elementType: String {

Sources/SourceDocsLib/DocumentationGenerator/Elements/MarkdownProtocol.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ struct MarkdownProtocol: SwiftDocDictionaryInitializable, MarkdownConvertible {
3434
properties = []
3535
methods = []
3636
}
37+
38+
let topLevelDoc: String? = dictionary.get(.documentationComment)
39+
guard !options.skipEmpty || topLevelDoc?.isEmpty == false || !properties.isEmpty || !methods.isEmpty else {
40+
return nil
41+
}
3742
}
3843

3944
var markdown: String {

Sources/SourceDocsLib/DocumentationGenerator/Elements/MarkdownTypealias.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ struct MarkdownTypealias: SwiftDocDictionaryInitializable, MarkdownConvertible {
2121
guard dictionary.accessLevel >= options.minimumAccessLevel && dictionary.isKind([.typealias]) else {
2222
return nil
2323
}
24+
let topLevelDoc: String? = dictionary.get(.documentationComment)
25+
guard !options.skipEmpty || topLevelDoc?.isEmpty == false else {
26+
return nil
27+
}
2428
self.dictionary = dictionary
2529
self.options = options
2630
}

Sources/SourceDocsLib/DocumentationGenerator/Elements/MarkdownVariable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ struct MarkdownVariable: SwiftDocDictionaryInitializable, MarkdownConvertible {
2222
guard dictionary.accessLevel >= options.minimumAccessLevel && dictionary.isKind(variables) else {
2323
return nil
2424
}
25+
let topLevelDoc: String? = dictionary.get(.documentationComment)
26+
guard !options.skipEmpty || topLevelDoc?.isEmpty == false else {
27+
return nil
28+
}
2529
self.dictionary = dictionary
2630
self.options = options
2731
}

Sources/SourceDocsLib/DocumentationGenerator/MarkdownIndex.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct MarkdownOptions {
1313
var collapsibleBlocks: Bool
1414
var tableOfContents: Bool
1515
var minimumAccessLevel: AccessLevel
16+
var skipEmpty: Bool
1617
}
1718

1819
class MarkdownIndex {

0 commit comments

Comments
 (0)