Skip to content

Commit 28de43b

Browse files
committed
[CodeCompletion] Trim whitespaces around the content
1 parent c633da0 commit 28de43b

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/IDE/CodeCompletionResultPrinter.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,21 @@ namespace {
6666
class AnnotatingDescriptionPrinter {
6767
raw_ostream &OS;
6868

69+
/// Print \p content enclosing with \p tag.
6970
void printWithTag(StringRef tag, StringRef content) {
71+
// Trim whitepsaces around the non-whitespace characters.
72+
// (i.e. " something " -> " <tag>something</tag> ".
73+
auto ltrimIdx = content.find_first_not_of(' ');
74+
auto rtrimIdx = content.find_last_not_of(' ') + 1;
75+
assert(ltrimIdx != StringRef::npos && rtrimIdx != StringRef::npos &&
76+
"empty or whitespace only element");
77+
78+
OS << content.substr(0, ltrimIdx);
7079
OS << "<" << tag << ">";
71-
swift::markup::appendWithXMLEscaping(OS, content);
80+
swift::markup::appendWithXMLEscaping(
81+
OS, content.substr(ltrimIdx, rtrimIdx - ltrimIdx));
7282
OS << "</" << tag << ">";
83+
OS << content.substr(rtrimIdx);
7384
}
7485

7586
void printTextChunk(CodeCompletionString::Chunk C) {

test/IDE/complete_annotation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func testMember(value: MyStruct) {
6868
// EXPR_MEMBER-DAG: Keyword[self]/CurrNominal: <keyword>self</keyword>; name=self
6969
// EXPR_MEMBER-DAG: Decl[InstanceVar]/CurrNominal: <name>propNormal</name>; name=propNormal
7070
// EXPR_MEMBER-DAG: Decl[InstanceVar]/CurrNominal: <name>propFunction</name>; name=propFunction
71-
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelNameParamName</name>(<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type>(<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user></callarg.type></callarg>)<keyword> rethrows</keyword>; name=labelNameParamName(label: (inout Int) throws -> MyStruct) rethrows
72-
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelName</name>(<callarg><callarg.label>label</callarg.label>: <callarg.type>(<attribute>@autoclosure </attribute>() -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Int</typeid.sys></callarg.type></callarg>); name=labelName(label: (@autoclosure () -> Int) -> Int)
71+
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelNameParamName</name>(<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type>(<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user></callarg.type></callarg>) <keyword>rethrows</keyword>; name=labelNameParamName(label: (inout Int) throws -> MyStruct) rethrows
72+
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelName</name>(<callarg><callarg.label>label</callarg.label>: <callarg.type>(<attribute>@autoclosure</attribute> () -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Int</typeid.sys></callarg.type></callarg>); name=labelName(label: (@autoclosure () -> Int) -> Int)
7373
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>sameName</name>(<callarg><callarg.label>label</callarg.label>: &amp;<callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>); name=sameName(label: &Int)
7474
// EXPR_MEMBER-DAG: Decl[InstanceMethod]/CurrNominal: <name>paramName</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>param</callarg.param>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>); name=paramName(param: Int)
7575
// EXPR_MEMBER: End completions
@@ -80,8 +80,8 @@ func testPostfix(value: MyStruct) {
8080
// EXPR_POSTFIX: Begin completions, 10 items
8181
// EXPR_POSTFIX-DAG: Decl[InstanceVar]/CurrNominal: <name>propNormal</name>; name=propNormal
8282
// EXPR_POSTFIX-DAG: Decl[InstanceVar]/CurrNominal: <name>propFunction</name>; name=propFunction
83-
// EXPR_POSTFIX-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelNameParamName</name>(<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type>(<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user></callarg.type></callarg>)<keyword> rethrows</keyword>; name=labelNameParamName(label: (inout Int) throws -> MyStruct) rethrows
84-
// EXPR_POSTFIX-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelName</name>(<callarg><callarg.label>label</callarg.label>: <callarg.type>(<attribute>@autoclosure </attribute>() -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Int</typeid.sys></callarg.type></callarg>); name=labelName(label: (@autoclosure () -> Int) -> Int)
83+
// EXPR_POSTFIX-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelNameParamName</name>(<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type>(<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user></callarg.type></callarg>) <keyword>rethrows</keyword>; name=labelNameParamName(label: (inout Int) throws -> MyStruct) rethrows
84+
// EXPR_POSTFIX-DAG: Decl[InstanceMethod]/CurrNominal: <name>labelName</name>(<callarg><callarg.label>label</callarg.label>: <callarg.type>(<attribute>@autoclosure</attribute> () -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Int</typeid.sys></callarg.type></callarg>); name=labelName(label: (@autoclosure () -> Int) -> Int)
8585
// EXPR_POSTFIX-DAG: Decl[InstanceMethod]/CurrNominal: <name>sameName</name>(<callarg><callarg.label>label</callarg.label>: &amp;<callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>); name=sameName(label: &Int)
8686
// EXPR_POSTFIX-DAG: Decl[InstanceMethod]/CurrNominal: <name>paramName</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>param</callarg.param>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>); name=paramName(param: Int)
8787
// EXPR_POSTFIX-DAG: Decl[Subscript]/CurrNominal: [<callarg><callarg.label>_</callarg.label> <callarg.param>param</callarg.param>: <callarg.type><typeid.sys>Int</typeid.sys></callarg.type></callarg>]; name=[param: Int]

test/SourceKit/CodeComplete/complete_annotateddescription.swift.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
key.kind: source.lang.swift.decl.function.method.instance,
2929
key.name: "labelName(label:)",
3030
key.sourcetext: ".labelName(label: <#T##(@autoclosure () -> Int) -> Int#>)",
31-
key.description: "<name>labelName</name>(<callarg><callarg.label>label</callarg.label>: <callarg.type>(<attribute>@autoclosure </attribute>() -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Int</typeid.sys></callarg.type></callarg>)",
31+
key.description: "<name>labelName</name>(<callarg><callarg.label>label</callarg.label>: <callarg.type>(<attribute>@autoclosure</attribute> () -&gt; <typeid.sys>Int</typeid.sys>) -&gt; <typeid.sys>Int</typeid.sys></callarg.type></callarg>)",
3232
key.typename: "Void",
3333
key.context: source.codecompletion.context.thisclass,
3434
key.typerelation: source.codecompletion.typerelation.unknown,
@@ -40,7 +40,7 @@
4040
key.kind: source.lang.swift.decl.function.method.instance,
4141
key.name: "labelNameParamName(label:)",
4242
key.sourcetext: ".labelNameParamName(label: <#T##(inout Int) throws -> MyStruct#>)",
43-
key.description: "<name>labelNameParamName</name>(<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type>(<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user></callarg.type></callarg>)<keyword> rethrows</keyword>",
43+
key.description: "<name>labelNameParamName</name>(<callarg><callarg.label>label</callarg.label> <callarg.param>param</callarg.param>: <callarg.type>(<keyword>inout</keyword> <typeid.sys>Int</typeid.sys>) <keyword>throws</keyword> -&gt; <typeid.user>MyStruct</typeid.user></callarg.type></callarg>) <keyword>rethrows</keyword>",
4444
key.typename: "Void",
4545
key.context: source.codecompletion.context.thisclass,
4646
key.typerelation: source.codecompletion.typerelation.unknown,

0 commit comments

Comments
 (0)