Skip to content

Commit e87be80

Browse files
committed
[Markup] Slurp fenced code block info string
Add a language property for Markup code blocks - the default is "swift" but will respect overrides such as the following: ```c++ Something::Somethign::create() ``` For code listings in doc comments written in other languages, such as when you want to compare usage against another language. rdar://problem/23948115
1 parent 02971fb commit e87be80

File tree

7 files changed

+50
-12
lines changed

7 files changed

+50
-12
lines changed

bindings/xml/comment-xml-schema.rng

+3
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,9 @@
767767

768768
<define name="CodeListing">
769769
<element name="CodeListing">
770+
<attribute name="language">
771+
<data type="string" />
772+
</attribute>
770773
<zeroOrMore>
771774
<ref name="zCodeLineNumbered"/>
772775
</zeroOrMore>

include/swift/Markup/AST.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,19 @@ class Item final : public MarkupASTNode {
199199

200200
class CodeBlock final : public MarkupASTNode {
201201
StringRef LiteralContent;
202+
StringRef Language;
202203

203-
CodeBlock(StringRef LiteralContent)
204+
CodeBlock(StringRef LiteralContent, StringRef Langauge)
204205
: MarkupASTNode(ASTNodeKind::CodeBlock),
205-
LiteralContent(LiteralContent) {}
206+
LiteralContent(LiteralContent),
207+
Language(Langauge) {}
206208

207209
public:
208-
static CodeBlock *create(MarkupContext &MC, StringRef LiteralContent);
210+
static CodeBlock *create(MarkupContext &MC, StringRef LiteralContent,
211+
StringRef Language);
209212

210213
StringRef getLiteralContent() const { return LiteralContent; };
214+
StringRef getLanguage() const { return Language; };
211215

212216
ArrayRef<const MarkupASTNode *> getChildren() const {
213217
return {};

lib/IDE/CommentConversion.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ struct CommentToXMLConverter {
100100
}
101101

102102
void printCodeBlock(const CodeBlock *CB) {
103-
OS << "<CodeListing>";
103+
OS << "<CodeListing language=\"";
104+
appendWithXMLEscaping(OS, CB->getLanguage());
105+
OS << "\">";
104106
SmallVector<StringRef, 16> CodeLines;
105107
CB->getLiteralContent().split(CodeLines, "\n");
106108
for (auto Line : CodeLines) {

lib/Markup/AST.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ Code *Code::create(MarkupContext &MC, StringRef LiteralContent) {
6262
return new (Mem) Code(LiteralContent);
6363
}
6464

65-
CodeBlock *CodeBlock::create(MarkupContext &MC, StringRef LiteralContent) {
65+
CodeBlock *CodeBlock::create(MarkupContext &MC, StringRef LiteralContent,
66+
StringRef Language) {
6667
void *Mem = MC.allocate(sizeof(CodeBlock), alignof(CodeBlock));
67-
return new (Mem) CodeBlock(LiteralContent);
68+
return new (Mem) CodeBlock(LiteralContent, Language);
6869
}
6970

7071
List::List(ArrayRef<MarkupASTNode *> Children, bool IsOrdered)
@@ -460,7 +461,10 @@ void llvm::markup::dump(const MarkupASTNode *Node, llvm::raw_ostream &OS,
460461
}
461462
case llvm::markup::ASTNodeKind::CodeBlock: {
462463
auto CB = cast<CodeBlock>(Node);
463-
OS << "CodeBlock: Content=";
464+
OS << "CodeBlock: ";
465+
OS << "Language=";
466+
simpleEscapingPrint(CB->getLanguage(), OS);
467+
OS << " Content=";
464468
simpleEscapingPrint(CB->getLiteralContent(), OS);
465469
break;
466470
}

lib/Markup/Markup.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,16 @@ ParseResult<CodeBlock> parseCodeBlock(MarkupContext &MC, LineList &LL,
108108
ParseState State) {
109109
assert(cmark_node_get_type(State.Node) == CMARK_NODE_CODE_BLOCK
110110
&& State.Event == CMARK_EVENT_ENTER);
111-
return { CodeBlock::create(MC, getLiteralContent(MC, LL, State.Node)),
111+
112+
StringRef Language("swift");
113+
114+
if (auto FenceInfo = cmark_node_get_fence_info(State.Node)) {
115+
StringRef FenceInfoStr(FenceInfo);
116+
if (!FenceInfoStr.empty())
117+
Language = MC.allocateCopy(FenceInfoStr);
118+
}
119+
return { CodeBlock::create(MC, getLiteralContent(MC, LL, State.Node),
120+
Language),
112121
State.next() };
113122
}
114123

test/IDE/comment_extensions.swift

-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,3 @@
5959
// CHECK: {{.*}}DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>urlWithQueryString()</Name><USR>s:F14swift_ide_test18urlWithQueryStringFT_T_</USR><Declaration>func urlWithQueryString()</Declaration><Abstract><Para>Brief.</Para></Abstract><Discussion><Para>Test <Link href="http://apple.com?a=1&amp;b=1&amp;c=abc">a link</Link></Para></Discussion></Function>]
6060

6161
// CHECK: {{.*}}DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>imageWithAmpersandsInTitleAndAlt()</Name><USR>s:F14swift_ide_test32imageWithAmpersandsInTitleAndAltFT_T_</USR><Declaration>func imageWithAmpersandsInTitleAndAlt()</Declaration><Abstract><Para>Brief.</Para></Abstract><Discussion><Para><rawHTML><![CDATA[<img src="http://apple.com" title="&&&" alt="&&&"\>]]></rawHTML></Para></Discussion></Function>]
62-

test/Inputs/comment_to_something_conversion.swift

+20-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ enum A012_AttachToEntities {
115115
/// f0() // WOW!
116116
/// f0() // WOW!
117117
func f0() {}
118-
// CHECK: DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>f0()</Name><USR>s:FC14swift_ide_test9CodeBlock2f0FT_T_</USR><Declaration>func f0()</Declaration><Abstract><Para>This is how you use this code.</Para></Abstract><Discussion><CodeListing><zCodeLineNumbered><![CDATA[f0() // WOW!]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[f0() // WOW!]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[f0() // WOW!]]></zCodeLineNumbered><zCodeLineNumbered></zCodeLineNumbered></CodeListing></Discussion></Function>]
118+
// CHECK: DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>f0()</Name><USR>s:FC14swift_ide_test9CodeBlock2f0FT_T_</USR><Declaration>func f0()</Declaration><Abstract><Para>This is how you use this code.</Para></Abstract><Discussion><CodeListing language="swift"><zCodeLineNumbered><![CDATA[f0() // WOW!]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[f0() // WOW!]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[f0() // WOW!]]></zCodeLineNumbered><zCodeLineNumbered></zCodeLineNumbered></CodeListing></Discussion></Function>]
119119
}
120120

121121
@objc class EmptyComments {
@@ -378,7 +378,7 @@ func f0(x: Int, y: Int, z: Int) {}
378378
var z = 3
379379
*/
380380
func f1() {}
381-
// CHECK: DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>f1()</Name><USR>s:FC14swift_ide_test20IndentedBlockComment2f1FT_T_</USR><Declaration>func f1()</Declaration><Abstract><Para>Brief.</Para></Abstract><Discussion><Para>First paragraph line. Second paragraph line.</Para><Para>Now for a code sample:</Para><CodeListing><zCodeLineNumbered><![CDATA[var x = 1]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[// var y = 2]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[var z = 3]]></zCodeLineNumbered><zCodeLineNumbered></zCodeLineNumbered></CodeListing></Discussion></Function>]
381+
// CHECK: DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>f1()</Name><USR>s:FC14swift_ide_test20IndentedBlockComment2f1FT_T_</USR><Declaration>func f1()</Declaration><Abstract><Para>Brief.</Para></Abstract><Discussion><Para>First paragraph line. Second paragraph line.</Para><Para>Now for a code sample:</Para><CodeListing language="swift"><zCodeLineNumbered><![CDATA[var x = 1]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[// var y = 2]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[var z = 3]]></zCodeLineNumbered><zCodeLineNumbered></zCodeLineNumbered></CodeListing></Discussion></Function>]
382382
/**
383383
Hugely indented brief.
384384

@@ -392,7 +392,7 @@ func f0(x: Int, y: Int, z: Int) {}
392392
var z = 3
393393
*/
394394
func f2() {}
395-
// CHECK: {{.*}}DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>f2()</Name><USR>s:FC14swift_ide_test20IndentedBlockComment2f2FT_T_</USR><Declaration>func f2()</Declaration><Abstract><Para>Hugely indented brief.</Para></Abstract><Discussion><Para>First paragraph line. Second paragraph line.</Para><Para>Now for a code sample:</Para><CodeListing><zCodeLineNumbered><![CDATA[var x = 1]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[// var y = 2]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[var z = 3]]></zCodeLineNumbered><zCodeLineNumbered></zCodeLineNumbered></CodeListing></Discussion></Function>]
395+
// CHECK: {{.*}}DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>f2()</Name><USR>s:FC14swift_ide_test20IndentedBlockComment2f2FT_T_</USR><Declaration>func f2()</Declaration><Abstract><Para>Hugely indented brief.</Para></Abstract><Discussion><Para>First paragraph line. Second paragraph line.</Para><Para>Now for a code sample:</Para><CodeListing language="swift"><zCodeLineNumbered><![CDATA[var x = 1]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[// var y = 2]]></zCodeLineNumbered><zCodeLineNumbered><![CDATA[var z = 3]]></zCodeLineNumbered><zCodeLineNumbered></zCodeLineNumbered></CodeListing></Discussion></Function>]
396396
}
397397

398398
@objc class MultiLineBrief {
@@ -405,3 +405,20 @@ func f0(x: Int, y: Int, z: Int) {}
405405
func f0() {}
406406
// CHECK: {{.*}}DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>f0()</Name><USR>s:FC14swift_ide_test14MultiLineBrief2f0FT_T_</USR><Declaration>func f0()</Declaration><Abstract><Para>Brief first line. Brief after softbreak.</Para></Abstract><Discussion><Para>Some paragraph text.</Para></Discussion></Function>]
407407
}
408+
409+
/// Brief.
410+
///
411+
/// ```
412+
/// thisIsASwiftCodeExample()
413+
/// ```
414+
func codeListingWithDefaultLangauge() {}
415+
// CHECK: DocCommentAsXML=[<Function file="{{.*}} line="{{.*}}" column="{{.*}}"><Name>codeListingWithDefaultLangauge()</Name><USR>s:F14swift_ide_test30codeListingWithDefaultLangaugeFT_T_</USR><Declaration>func codeListingWithDefaultLangauge()</Declaration><Abstract><Para>Brief.</Para></Abstract><Discussion><CodeListing language="swift"><zCodeLineNumbered><![CDATA[thisIsASwiftCodeExample()]]></zCodeLineNumbered><zCodeLineNumbered></zCodeLineNumbered></CodeListing></Discussion></Function>] CommentXMLValid
416+
417+
418+
/// Brief.
419+
///
420+
/// ```c++
421+
/// Something::Something::create();
422+
/// ```
423+
func codeListingWithOtherLanguage() {}
424+
// CHECK: DocCommentAsXML=[<Function file="{{.*}}" line="{{.*}}" column="{{.*}}"><Name>codeListingWithOtherLanguage()</Name><USR>s:F14swift_ide_test28codeListingWithOtherLanguageFT_T_</USR><Declaration>func codeListingWithOtherLanguage()</Declaration><Abstract><Para>Brief.</Para></Abstract><Discussion><CodeListing language="c++"><zCodeLineNumbered><![CDATA[Something::Something::create();]]></zCodeLineNumbered><zCodeLineNumbered></zCodeLineNumbered></CodeListing></Discussion></Function>]

0 commit comments

Comments
 (0)