Skip to content

Commit

Permalink
[Markup] Slurp fenced code block info string
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bitjammer committed Dec 18, 2015
1 parent 02971fb commit e87be80
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
3 changes: 3 additions & 0 deletions bindings/xml/comment-xml-schema.rng
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,9 @@

<define name="CodeListing">
<element name="CodeListing">
<attribute name="language">
<data type="string" />
</attribute>
<zeroOrMore>
<ref name="zCodeLineNumbered"/>
</zeroOrMore>
Expand Down
10 changes: 7 additions & 3 deletions include/swift/Markup/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,19 @@ class Item final : public MarkupASTNode {

class CodeBlock final : public MarkupASTNode {
StringRef LiteralContent;
StringRef Language;

CodeBlock(StringRef LiteralContent)
CodeBlock(StringRef LiteralContent, StringRef Langauge)
: MarkupASTNode(ASTNodeKind::CodeBlock),
LiteralContent(LiteralContent) {}
LiteralContent(LiteralContent),
Language(Langauge) {}

public:
static CodeBlock *create(MarkupContext &MC, StringRef LiteralContent);
static CodeBlock *create(MarkupContext &MC, StringRef LiteralContent,
StringRef Language);

StringRef getLiteralContent() const { return LiteralContent; };
StringRef getLanguage() const { return Language; };

ArrayRef<const MarkupASTNode *> getChildren() const {
return {};
Expand Down
4 changes: 3 additions & 1 deletion lib/IDE/CommentConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ struct CommentToXMLConverter {
}

void printCodeBlock(const CodeBlock *CB) {
OS << "<CodeListing>";
OS << "<CodeListing language=\"";
appendWithXMLEscaping(OS, CB->getLanguage());
OS << "\">";
SmallVector<StringRef, 16> CodeLines;
CB->getLiteralContent().split(CodeLines, "\n");
for (auto Line : CodeLines) {
Expand Down
10 changes: 7 additions & 3 deletions lib/Markup/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ Code *Code::create(MarkupContext &MC, StringRef LiteralContent) {
return new (Mem) Code(LiteralContent);
}

CodeBlock *CodeBlock::create(MarkupContext &MC, StringRef LiteralContent) {
CodeBlock *CodeBlock::create(MarkupContext &MC, StringRef LiteralContent,
StringRef Language) {
void *Mem = MC.allocate(sizeof(CodeBlock), alignof(CodeBlock));
return new (Mem) CodeBlock(LiteralContent);
return new (Mem) CodeBlock(LiteralContent, Language);
}

List::List(ArrayRef<MarkupASTNode *> Children, bool IsOrdered)
Expand Down Expand Up @@ -460,7 +461,10 @@ void llvm::markup::dump(const MarkupASTNode *Node, llvm::raw_ostream &OS,
}
case llvm::markup::ASTNodeKind::CodeBlock: {
auto CB = cast<CodeBlock>(Node);
OS << "CodeBlock: Content=";
OS << "CodeBlock: ";
OS << "Language=";
simpleEscapingPrint(CB->getLanguage(), OS);
OS << " Content=";
simpleEscapingPrint(CB->getLiteralContent(), OS);
break;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/Markup/Markup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,16 @@ ParseResult<CodeBlock> parseCodeBlock(MarkupContext &MC, LineList &LL,
ParseState State) {
assert(cmark_node_get_type(State.Node) == CMARK_NODE_CODE_BLOCK
&& State.Event == CMARK_EVENT_ENTER);
return { CodeBlock::create(MC, getLiteralContent(MC, LL, State.Node)),

StringRef Language("swift");

if (auto FenceInfo = cmark_node_get_fence_info(State.Node)) {
StringRef FenceInfoStr(FenceInfo);
if (!FenceInfoStr.empty())
Language = MC.allocateCopy(FenceInfoStr);
}
return { CodeBlock::create(MC, getLiteralContent(MC, LL, State.Node),
Language),
State.next() };
}

Expand Down
1 change: 0 additions & 1 deletion test/IDE/comment_extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,3 @@
// 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>]

// 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>]

23 changes: 20 additions & 3 deletions test/Inputs/comment_to_something_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ enum A012_AttachToEntities {
/// f0() // WOW!
/// f0() // WOW!
func f0() {}
// 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>]
// 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>]
}

@objc class EmptyComments {
Expand Down Expand Up @@ -378,7 +378,7 @@ func f0(x: Int, y: Int, z: Int) {}
var z = 3
*/
func f1() {}
// 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>]
// 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>]
/**
Hugely indented brief.

Expand All @@ -392,7 +392,7 @@ func f0(x: Int, y: Int, z: Int) {}
var z = 3
*/
func f2() {}
// 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>]
// 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>]
}

@objc class MultiLineBrief {
Expand All @@ -405,3 +405,20 @@ func f0(x: Int, y: Int, z: Int) {}
func f0() {}
// 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>]
}

/// Brief.
///
/// ```
/// thisIsASwiftCodeExample()
/// ```
func codeListingWithDefaultLangauge() {}
// 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


/// Brief.
///
/// ```c++
/// Something::Something::create();
/// ```
func codeListingWithOtherLanguage() {}
// 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 comments on commit e87be80

Please sign in to comment.