Skip to content

Commit 63d1104

Browse files
authored
Don't apply "smart indent" to parent symbols (#41)
1 parent 68dc3be commit 63d1104

File tree

8 files changed

+144
-3
lines changed

8 files changed

+144
-3
lines changed

src/components/DocumentationTopic.vue

+5
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ export default {
235235
type: Array,
236236
required: false,
237237
},
238+
symbolKind: {
239+
type: String,
240+
required: false,
241+
},
238242
variants: {
239243
type: Array,
240244
default: () => ([]),
@@ -255,6 +259,7 @@ export default {
255259
identifier: this.identifier,
256260
languages: new Set(Object.keys(this.languagePaths)),
257261
interfaceLanguage: this.interfaceLanguage,
262+
symbolKind: this.symbolKind,
258263
};
259264
},
260265
data() {

src/components/DocumentationTopic/PrimaryContent/DeclarationGroup.vue

+9-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
</p>
2020
<Source
2121
:tokens="declaration.tokens"
22-
:simple-indent="isSwift && !isCocoaApi"
23-
:smart-indent="isCocoaApi"
22+
:simple-indent="shouldSimpleIndent"
23+
:smart-indent="shouldSmartIndent"
2424
:language="interfaceLanguage"
2525
/>
2626
</div>
@@ -30,6 +30,7 @@
3030
import DeclarationSource from 'docc-render/components/DocumentationTopic/PrimaryContent/DeclarationSource.vue';
3131
import Language from 'docc-render/constants/Language';
3232
import { APIChangesMultipleLines } from 'docc-render/mixins/apiChangesHelpers';
33+
import { isParentSymbolKind } from 'docc-render/utils/symbols';
3334

3435
/**
3536
* Renders a code source with an optional caption.
@@ -47,6 +48,9 @@ export default {
4748
interfaceLanguage: {
4849
default: () => Language.swift.key.api,
4950
},
51+
symbolKind: {
52+
default: () => undefined,
53+
},
5054
},
5155
props: {
5256
declaration: {
@@ -79,7 +83,9 @@ export default {
7983
return this.declaration.platforms.join(', ');
8084
},
8185
isSwift: ({ interfaceLanguage }) => interfaceLanguage === Language.swift.key.api,
82-
isCocoaApi: ({ languages }) => languages.has(Language.objectiveC.key.api),
86+
shouldSimpleIndent: ({ isSwift, shouldSmartIndent }) => isSwift && !shouldSmartIndent,
87+
shouldSmartIndent: ({ languages, symbolKind }) => languages.has(Language.objectiveC.key.api)
88+
&& !isParentSymbolKind(symbolKind),
8389
},
8490
};
8591
</script>

src/constants/SymbolKind.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* This source file is part of the Swift.org open source project
3+
*
4+
* Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
* Licensed under Apache License v2.0 with Runtime Library Exception
6+
*
7+
* See https://swift.org/LICENSE.txt for license information
8+
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
// Note: this is not an exhaustive/complete definition of all the various kinds
12+
// that may be emitted by DocC in Render JSON at the moment—only the ones that
13+
// the renderer cares about for the time being.
14+
export default {
15+
class: 'class',
16+
enum: 'enum',
17+
protocol: 'protocol',
18+
struct: 'struct',
19+
uid: 'uid',
20+
};

src/utils/symbols.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* This source file is part of the Swift.org open source project
3+
*
4+
* Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
* Licensed under Apache License v2.0 with Runtime Library Exception
6+
*
7+
* See https://swift.org/LICENSE.txt for license information
8+
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
import SymbolKind from 'docc-render/constants/SymbolKind';
11+
12+
// Returns whether or not the given symbol kind can be considered a potential
13+
// "parent" of other symbols.
14+
//
15+
// Classes, enums, protocols, and structs can be thought of as parent symbols
16+
// that may contain child symbol members for any of its
17+
// properties/functions/initializers/etc for example.
18+
//
19+
// eslint-disable-next-line import/prefer-default-export
20+
export function isParentSymbolKind(kind) {
21+
switch (kind) {
22+
case SymbolKind.class:
23+
case SymbolKind.enum:
24+
case SymbolKind.protocol:
25+
case SymbolKind.struct:
26+
case SymbolKind.uid:
27+
return true;
28+
default:
29+
return false;
30+
}
31+
}

src/views/DocumentationTopic.vue

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export default {
8989
platforms,
9090
required: isRequirement,
9191
roleHeading,
92+
symbolKind,
9293
title = '',
9394
tags = [],
9495
} = {},
@@ -122,6 +123,7 @@ export default {
122123
title,
123124
topicSections,
124125
seeAlsoSections,
126+
symbolKind,
125127
variantOverrides,
126128
variants,
127129
extendsTechnology,

tests/unit/components/DocumentationTopic.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ const propsData = {
120120
],
121121
references: {},
122122
roleHeading: 'Thing',
123+
symbolKind: 'thing',
123124
title: 'FooKit',
124125
variants: [
125126
{
@@ -176,6 +177,11 @@ describe('DocumentationTopic', () => {
176177
expect(wrapper.vm._provided.interfaceLanguage).toEqual(propsData.interfaceLanguage);
177178
});
178179

180+
it('provides the symbol kind', () => {
181+
// eslint-disable-next-line no-underscore-dangle
182+
expect(wrapper.vm._provided.symbolKind).toEqual(propsData.symbolKind);
183+
});
184+
179185
it('renders a root div', () => {
180186
expect(wrapper.is('div.doc-topic')).toBe(true);
181187
});

tests/unit/components/DocumentationTopic/PrimaryContent/DeclarationGroup.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,30 @@ describe('DeclarationGroup', () => {
110110

111111
expect(wrapper.classes()).toContain(multipleLinesClass);
112112
});
113+
114+
it('does not apply a "smart indent" for Objective-C classes/structs/etc', () => {
115+
['class', 'enum', 'protocol', 'struct'].forEach((symbolKind) => {
116+
const source = createWrapper({
117+
provide: {
118+
interfaceLanguage: 'occ',
119+
languages: new Set(['occ']),
120+
symbolKind,
121+
},
122+
}).find(DeclarationSource);
123+
expect(source.exists()).toBe(true);
124+
expect(source.props('smartIndent')).toBe(false);
125+
});
126+
});
127+
128+
it('applies a "smart indent" for other Objective-C symbols', () => {
129+
const source = createWrapper({
130+
provide: {
131+
interfaceLanguage: 'occ',
132+
languages: new Set(['occ']),
133+
symbolKind: 'instm',
134+
},
135+
}).find(DeclarationSource);
136+
expect(source.exists()).toBe(true);
137+
expect(source.props('smartIndent')).toBe(true);
138+
});
113139
});

tests/unit/utils/symbols.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* This source file is part of the Swift.org open source project
3+
*
4+
* Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
* Licensed under Apache License v2.0 with Runtime Library Exception
6+
*
7+
* See https://swift.org/LICENSE.txt for license information
8+
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
import { isParentSymbolKind } from 'docc-render/utils/symbols';
11+
import SymbolKind from 'docc-render/constants/SymbolKind';
12+
13+
describe('isParentSymbolKind', () => {
14+
it('returns true for kind "class"', () => {
15+
expect(isParentSymbolKind(SymbolKind.class)).toBe(true);
16+
});
17+
18+
it('returns true for kind "enum"', () => {
19+
expect(isParentSymbolKind(SymbolKind.enum)).toBe(true);
20+
});
21+
22+
it('returns true for kind "protocol"', () => {
23+
expect(isParentSymbolKind(SymbolKind.protocol)).toBe(true);
24+
});
25+
26+
it('returns true for kind "struct"', () => {
27+
expect(isParentSymbolKind(SymbolKind.struct)).toBe(true);
28+
});
29+
30+
it('returns true for kind "uid"', () => {
31+
expect(isParentSymbolKind(SymbolKind.uid)).toBe(true);
32+
});
33+
34+
it('returns false for other kind values', () => {
35+
expect(isParentSymbolKind(SymbolKind.unknown)).toBe(false);
36+
expect(isParentSymbolKind('clm')).toBe(false);
37+
expect(isParentSymbolKind('instm')).toBe(false);
38+
expect(isParentSymbolKind('method')).toBe(false);
39+
expect(isParentSymbolKind('initializer')).toBe(false);
40+
expect(isParentSymbolKind('func')).toBe(false);
41+
expect(isParentSymbolKind('function')).toBe(false);
42+
expect(isParentSymbolKind('property')).toBe(false);
43+
expect(isParentSymbolKind('')).toBe(false);
44+
});
45+
});

0 commit comments

Comments
 (0)