Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 6a2b5a7

Browse files
committed
Merge PR #162
1 parent 58c9371 commit 6a2b5a7

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/Analysis/Engine/Test/LineFormatterTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ public void UnaryOperators(string code, string expected) {
208208
AssertSingleLineFormat(code, expected);
209209
}
210210

211-
[TestMethod, Priority(0)]
212-
public void EqualsWithTypeHints() {
213-
AssertSingleLineFormat("def foo(x:int=3,x=100.)", "def foo(x: int = 3, x=100.)");
211+
[DataRow("def foo(x:int=3,x=100.)", "def foo(x: int = 3, x=100.)")]
212+
[DataRow("def foo(x:Union[int,str]=3,x=100.)", "def foo(x: Union[int, str] = 3, x=100.)")]
213+
[DataTestMethod, Priority(0)]
214+
public void EqualsWithTypeHints(string code, string expected) {
215+
AssertSingleLineFormat(code, expected);
214216
}
215217

216218
[TestMethod, Priority(0)]

src/LanguageServer/Impl/Implementation/LineFormatter.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,35 @@ public TextEdit[] FormatLine(int line) {
149149
builder.Append(token);
150150
break;
151151

152-
case TokenKind.Assign when token.IsInsideFunctionArgs && prev?.PrevNonIgnored?.Kind != TokenKind.Colon:
153-
builder.Append(token);
152+
case TokenKind.Assign when token.IsInsideFunctionArgs:
153+
// Search backwards through the tokens looking for a colon for this argument,
154+
// indicating that there's a type hint and spacing should surround the equals.
155+
for (var p = token.PrevNonIgnored; p != null; p = p.PrevNonIgnored) {
156+
if (p == token.Inside) {
157+
// Hit the surrounding left parenthesis, so stop the search.
158+
builder.Append(token);
159+
break;
160+
}
161+
162+
if (p.Inside != token.Inside) {
163+
// Inside another grouping than the =, so skip over it.
164+
continue;
165+
}
166+
167+
if (p.Kind == TokenKind.Comma) {
168+
// Found a comma, indicating the end of another argument, so stop.
169+
builder.Append(token);
170+
break;
171+
}
172+
173+
if (p.Kind == TokenKind.Colon) {
174+
// Found a colon before hitting another argument or the opening parenthesis, so add spacing.
175+
AppendTokenEnsureWhiteSpacesAround(builder, token);
176+
break;
177+
}
178+
}
154179
break;
180+
155181
case TokenKind.Assign:
156182
AppendTokenEnsureWhiteSpacesAround(builder, token);
157183
break;

0 commit comments

Comments
 (0)