forked from microsoft/python-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFString.cs
More file actions
86 lines (76 loc) · 3.34 KB
/
Copy pathFString.cs
File metadata and controls
86 lines (76 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Python.Parsing.Ast {
public class FString : Expression {
private readonly Node[] _children;
private readonly string _openQuotes;
public FString(Node[] children, string openQuotes, string unparsed) {
_children = children;
_openQuotes = openQuotes;
Unparsed = unparsed;
}
public override IEnumerable<Node> GetChildNodes() {
return _children;
}
public readonly string Unparsed;
public override string NodeName => "f-string expression";
public override void Walk(PythonWalker walker) {
if (walker.Walk(this)) {
foreach (var child in _children) {
child.Walk(walker);
}
}
walker.PostWalk(this);
}
public async override Task WalkAsync(PythonWalkerAsync walker, CancellationToken cancellationToken = default) {
if (await walker.WalkAsync(this, cancellationToken)) {
foreach (var child in _children) {
await child.WalkAsync(walker, cancellationToken);
}
}
await walker.PostWalkAsync(this, cancellationToken);
}
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
var verbatimPieces = this.GetVerbatimNames(ast);
var verbatimComments = this.GetListWhiteSpace(ast);
if (verbatimPieces != null) {
// string+ / bytes+, such as "abc" "abc", which can spawn multiple lines, and
// have comments in between the peices.
for (var i = 0; i < verbatimPieces.Length; i++) {
if (verbatimComments != null && i < verbatimComments.Length) {
format.ReflowComment(res, verbatimComments[i]);
}
res.Append(verbatimPieces[i]);
}
} else {
format.ReflowComment(res, this.GetPreceedingWhiteSpaceDefaultNull(ast));
if (this.GetExtraVerbatimText(ast) != null) {
res.Append(this.GetExtraVerbatimText(ast));
} else {
RecursiveAppendRepr(res, ast, format);
}
}
}
private void RecursiveAppendRepr(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
res.Append('f');
res.Append(_openQuotes);
foreach (var child in _children) {
AppendChild(res, ast, format, child);
}
res.Append(_openQuotes);
}
private static void AppendChild(StringBuilder res, PythonAst ast, CodeFormattingOptions format, Node child) {
if (child is ConstantExpression expr) {
// Non-Verbatim AppendCodeString for ConstantExpression adds quotes around string
// Remove those quotes
var childStrBuilder = new StringBuilder();
child.AppendCodeString(childStrBuilder, ast, format);
res.Append(childStrBuilder.ToString().Trim('\''));
} else {
child.AppendCodeString(res, ast, format);
}
}
}
}