Skip to content

Commit 6d1065e

Browse files
authored
Fix indentation formatting for params with attributes (#642)
Fixes issue where parameters that start with attributes are not properly indented onto the next line like ones without attributes. Before: ```swift // func qux(a: String, b: String, @stringbuilder c: () -> String) func qux( a: String, b: String, @stringbuilder c: () -> String ) ``` After: ```swift // func qux(a: String, b: String, @stringbuilder c: () -> String) func qux( a: String, b: String, @stringbuilder c: () -> String ) ``` Resolves: rdar://108472125
1 parent 52847fc commit 6d1065e

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

src/components/DocumentationTopic/PrimaryContent/DeclarationSource.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ export default {
140140
// if we find some text ending with ", " and the next token is the start
141141
// of a new param, update this token text to replace the space with a
142142
// newline followed by 4 spaces
143-
if (token.text && token.text.endsWith(', ')
144-
&& nextToken && nextToken.kind === TokenKind.externalParam) {
143+
const isStartOfParam = ({ kind }) => (
144+
kind === TokenKind.attribute || kind === TokenKind.externalParam
145+
);
146+
if (token.text && token.text.endsWith(', ') && nextToken && isStartOfParam(nextToken)) {
145147
newToken.text = `${token.text.trimEnd()}\n${indent}`;
146148
indentedParams = true;
147149
}

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,4 +774,105 @@ func foobarbaz() -> Int`,
774774
func foo(bar: @escaping () -> ()) -> Int`,
775775
);
776776
});
777+
778+
it('adds newlines for params that start with attribute tokens', () => {
779+
const param = name => ([
780+
{
781+
kind: TokenKind.externalParam,
782+
text: name,
783+
},
784+
{
785+
kind: TokenKind.text,
786+
text: ': ',
787+
},
788+
{
789+
kind: TokenKind.typeIdentifier,
790+
text: 'Int',
791+
},
792+
]);
793+
const attributeParam = name => ([
794+
{
795+
kind: TokenKind.attribute,
796+
text: '@StringBuilder',
797+
},
798+
{
799+
kind: TokenKind.text,
800+
text: ' ',
801+
},
802+
{
803+
kind: TokenKind.externalParam,
804+
text: name,
805+
},
806+
{
807+
kind: TokenKind.text,
808+
text: ': () -> ',
809+
},
810+
{
811+
kind: TokenKind.typeIdentifier,
812+
text: 'String',
813+
},
814+
]);
815+
const tokens = (paramA, paramB) => ([
816+
{
817+
kind: TokenKind.keyword,
818+
text: 'func',
819+
},
820+
{
821+
kind: TokenKind.text,
822+
text: ' ',
823+
},
824+
{
825+
kind: TokenKind.identifier,
826+
text: 'qux',
827+
},
828+
{
829+
kind: TokenKind.text,
830+
text: '(',
831+
},
832+
...paramA,
833+
{
834+
kind: TokenKind.text,
835+
text: ', ',
836+
},
837+
...paramB,
838+
{
839+
kind: TokenKind.text,
840+
text: ')',
841+
},
842+
]);
843+
844+
// Before:
845+
// func qux(a: Int, @StringBuilder b: () -> String)
846+
//
847+
// After:
848+
// func qux(
849+
// a: Int,
850+
// @StringBuilder b: () -> String
851+
// )
852+
const wrapper = mountWithTokens(tokens(param('a'), attributeParam('b')));
853+
const tokenComponents = wrapper.findAll(Token);
854+
expect(getText(tokenComponents)).toBe(
855+
`func qux(
856+
a: Int,
857+
@StringBuilder b: () -> String
858+
)`,
859+
);
860+
861+
// Before:
862+
// func qux(@StringBuilder a: () -> String, b: Int)
863+
//
864+
// After:
865+
// func qux(
866+
// @StringBuilder a: () -> String,
867+
// b: Int
868+
// )
869+
const wrapper2 = mountWithTokens(tokens(attributeParam('a'), param('b')));
870+
const tokenComponents2 = wrapper2.findAll(Token);
871+
expect(getText(tokenComponents2)).toBe(
872+
`func qux(
873+
@StringBuilder a: () -> String,
874+
b: Int
875+
)`,
876+
);
877+
});
777878
});

0 commit comments

Comments
 (0)