diff --git a/bindings/xml/comment-xml-schema.rng b/bindings/xml/comment-xml-schema.rng index 6ee793715ff33..468efa042537e 100644 --- a/bindings/xml/comment-xml-schema.rng +++ b/bindings/xml/comment-xml-schema.rng @@ -767,6 +767,9 @@ + + + diff --git a/include/swift/Markup/AST.h b/include/swift/Markup/AST.h index 0238e16fe5abd..465766bb98e83 100644 --- a/include/swift/Markup/AST.h +++ b/include/swift/Markup/AST.h @@ -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 getChildren() const { return {}; diff --git a/lib/IDE/CommentConversion.cpp b/lib/IDE/CommentConversion.cpp index bcc7b595e937b..cce2738d38674 100644 --- a/lib/IDE/CommentConversion.cpp +++ b/lib/IDE/CommentConversion.cpp @@ -100,7 +100,9 @@ struct CommentToXMLConverter { } void printCodeBlock(const CodeBlock *CB) { - OS << ""; + OS << "getLanguage()); + OS << "\">"; SmallVector CodeLines; CB->getLiteralContent().split(CodeLines, "\n"); for (auto Line : CodeLines) { diff --git a/lib/Markup/AST.cpp b/lib/Markup/AST.cpp index a8ab20c7c6fa9..c430a3b43c62b 100644 --- a/lib/Markup/AST.cpp +++ b/lib/Markup/AST.cpp @@ -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 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(Node); - OS << "CodeBlock: Content="; + OS << "CodeBlock: "; + OS << "Language="; + simpleEscapingPrint(CB->getLanguage(), OS); + OS << " Content="; simpleEscapingPrint(CB->getLiteralContent(), OS); break; } diff --git a/lib/Markup/Markup.cpp b/lib/Markup/Markup.cpp index b49e2a5b98ad1..2edf6fa8362a4 100644 --- a/lib/Markup/Markup.cpp +++ b/lib/Markup/Markup.cpp @@ -108,7 +108,16 @@ ParseResult 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() }; } diff --git a/test/IDE/comment_extensions.swift b/test/IDE/comment_extensions.swift index 57e33f9f7485a..f5091520c1181 100644 --- a/test/IDE/comment_extensions.swift +++ b/test/IDE/comment_extensions.swift @@ -59,4 +59,3 @@ // CHECK: {{.*}}DocCommentAsXML=[urlWithQueryString()s:F14swift_ide_test18urlWithQueryStringFT_T_func urlWithQueryString()Brief.Test a link] // CHECK: {{.*}}DocCommentAsXML=[imageWithAmpersandsInTitleAndAlt()s:F14swift_ide_test32imageWithAmpersandsInTitleAndAltFT_T_func imageWithAmpersandsInTitleAndAlt()Brief.]]>] - diff --git a/test/Inputs/comment_to_something_conversion.swift b/test/Inputs/comment_to_something_conversion.swift index b701185819822..77be7e56b324a 100644 --- a/test/Inputs/comment_to_something_conversion.swift +++ b/test/Inputs/comment_to_something_conversion.swift @@ -115,7 +115,7 @@ enum A012_AttachToEntities { /// f0() // WOW! /// f0() // WOW! func f0() {} -// CHECK: DocCommentAsXML=[f0()s:FC14swift_ide_test9CodeBlock2f0FT_T_func f0()This is how you use this code.] +// CHECK: DocCommentAsXML=[f0()s:FC14swift_ide_test9CodeBlock2f0FT_T_func f0()This is how you use this code.] } @objc class EmptyComments { @@ -378,7 +378,7 @@ func f0(x: Int, y: Int, z: Int) {} var z = 3 */ func f1() {} -// CHECK: DocCommentAsXML=[f1()s:FC14swift_ide_test20IndentedBlockComment2f1FT_T_func f1()Brief.First paragraph line. Second paragraph line.Now for a code sample:] +// CHECK: DocCommentAsXML=[f1()s:FC14swift_ide_test20IndentedBlockComment2f1FT_T_func f1()Brief.First paragraph line. Second paragraph line.Now for a code sample:] /** Hugely indented brief. @@ -392,7 +392,7 @@ func f0(x: Int, y: Int, z: Int) {} var z = 3 */ func f2() {} -// CHECK: {{.*}}DocCommentAsXML=[f2()s:FC14swift_ide_test20IndentedBlockComment2f2FT_T_func f2()Hugely indented brief.First paragraph line. Second paragraph line.Now for a code sample:] +// CHECK: {{.*}}DocCommentAsXML=[f2()s:FC14swift_ide_test20IndentedBlockComment2f2FT_T_func f2()Hugely indented brief.First paragraph line. Second paragraph line.Now for a code sample:] } @objc class MultiLineBrief { @@ -405,3 +405,20 @@ func f0(x: Int, y: Int, z: Int) {} func f0() {} // CHECK: {{.*}}DocCommentAsXML=[f0()s:FC14swift_ide_test14MultiLineBrief2f0FT_T_func f0()Brief first line. Brief after softbreak.Some paragraph text.] } + +/// Brief. +/// +/// ``` +/// thisIsASwiftCodeExample() +/// ``` +func codeListingWithDefaultLangauge() {} +// CHECK: DocCommentAsXML=[codeListingWithDefaultLangauge()s:F14swift_ide_test30codeListingWithDefaultLangaugeFT_T_func codeListingWithDefaultLangauge()Brief.] CommentXMLValid + + +/// Brief. +/// +/// ```c++ +/// Something::Something::create(); +/// ``` +func codeListingWithOtherLanguage() {} +// CHECK: DocCommentAsXML=[codeListingWithOtherLanguage()s:F14swift_ide_test28codeListingWithOtherLanguageFT_T_func codeListingWithOtherLanguage()Brief.]