mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[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
This commit is contained in:
@@ -767,6 +767,9 @@
|
||||
|
||||
<define name="CodeListing">
|
||||
<element name="CodeListing">
|
||||
<attribute name="language">
|
||||
<data type="string" />
|
||||
</attribute>
|
||||
<zeroOrMore>
|
||||
<ref name="zCodeLineNumbered"/>
|
||||
</zeroOrMore>
|
||||
|
||||
@@ -199,15 +199,19 @@ public:
|
||||
|
||||
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 {};
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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() };
|
||||
}
|
||||
|
||||
|
||||
@@ -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&b=1&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>]
|
||||
|
||||
|
||||
@@ -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 {
|
||||
@@ -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.
|
||||
|
||||
@@ -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 {
|
||||
@@ -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>]
|
||||
|
||||
Reference in New Issue
Block a user