From 849120c4267e267da59fba41f9ba4faf08852f96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Mon, 18 Oct 2021 19:10:45 +0200 Subject: [PATCH 1/3] Restructure ditto specs --- spec/compiler/semantic/doc_spec.cr | 76 +++++++++++++++--------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/spec/compiler/semantic/doc_spec.cr b/spec/compiler/semantic/doc_spec.cr index 54f3f60e4949..7ce3b90cda34 100644 --- a/spec/compiler/semantic/doc_spec.cr +++ b/spec/compiler/semantic/doc_spec.cr @@ -62,35 +62,52 @@ describe "Semantic: doc" do bar.doc.should eq("Hello") end - it "stores doc for const when using ditto" do - result = semantic %( - # A number - ONE = 1 + describe ":ditto:" do + it "stores doc for const" do + result = semantic %( + # A number + ONE = 1 - # :ditto: - TWO = 2 - ), wants_doc: true - program = result.program - program.types["ONE"].doc.should eq "A number" - program.types["TWO"].doc.should eq "A number" - end + # :ditto: + TWO = 2 + ), wants_doc: true + program = result.program + program.types["ONE"].doc.should eq "A number" + program.types["TWO"].doc.should eq "A number" + end - it "stores doc for def when using ditto" do - result = semantic %( - class Foo + it "stores doc for def" do + result = semantic %( + class Foo + # Hello + def bar + end + + # :ditto: + def bar2 + end + end + ), wants_doc: true + program = result.program + foo = program.types["Foo"] + bar = foo.lookup_defs("bar2").first + bar.doc.should eq("Hello") + end + + it "stores doc for macro" do + result = semantic %( # Hello - def bar + macro bar end # :ditto: - def bar2 + macro bar2 end - end - ), wants_doc: true - program = result.program - foo = program.types["Foo"] - bar = foo.lookup_defs("bar2").first - bar.doc.should eq("Hello") + ), wants_doc: true + program = result.program + bar2 = program.lookup_macros("bar2").as(Array(Macro)).first + bar2.doc.should eq("Hello") + end end it "stores doc for def with visibility" do @@ -148,21 +165,6 @@ describe "Semantic: doc" do bar.doc.should eq("Hello") end - it "stores doc for macro when using ditto" do - result = semantic %( - # Hello - macro bar - end - - # :ditto: - macro bar2 - end - ), wants_doc: true - program = result.program - bar2 = program.lookup_macros("bar2").as(Array(Macro)).first - bar2.doc.should eq("Hello") - end - {% for def_type in %w[def macro].map &.id %} it "overwrites doc for {{def_type}} when redefining" do result = semantic %( From 81416aa751abc25b10769dbc78e29902738c5d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Mon, 18 Oct 2021 19:11:10 +0200 Subject: [PATCH 2/3] Fix ditto with additional lines --- spec/compiler/semantic/doc_spec.cr | 65 +++++++++++++++++++ .../crystal/semantic/top_level_visitor.cr | 16 +++-- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/spec/compiler/semantic/doc_spec.cr b/spec/compiler/semantic/doc_spec.cr index 7ce3b90cda34..088c53cb5bfc 100644 --- a/spec/compiler/semantic/doc_spec.cr +++ b/spec/compiler/semantic/doc_spec.cr @@ -108,6 +108,71 @@ describe "Semantic: doc" do bar2 = program.lookup_macros("bar2").as(Array(Macro)).first bar2.doc.should eq("Hello") end + + it "amend previous doc" do + result = semantic %( + class Foo + # Hello + def bar + end + + # :ditto: + # + # World + def bar2 + end + end + ), wants_doc: true + program = result.program + foo = program.types["Foo"] + bar = foo.lookup_defs("bar2").first + bar.doc.should eq("Hello\n\nWorld") + end + + it "amend previous doc (without empty line)" do + result = semantic %( + class Foo + # Hello + def bar + end + + # :ditto: + # World + def bar2 + end + end + ), wants_doc: true + program = result.program + foo = program.types["Foo"] + bar = foo.lookup_defs("bar2").first + bar.doc.should eq("Hello\n\nWorld") + end + + it ":ditto: references last non-ditto doc" do + result = semantic %( + class Foo + # Hello + def bar + end + + # :ditto: + # + # World + def bar2 + end + + # :ditto: + # + # Crystal + def bar3 + end + end + ), wants_doc: true + program = result.program + foo = program.types["Foo"] + bar = foo.lookup_defs("bar3").first + bar.doc.should eq("Hello\n\nCrystal") + end end it "stores doc for def with visibility" do diff --git a/src/compiler/crystal/semantic/top_level_visitor.cr b/src/compiler/crystal/semantic/top_level_visitor.cr index 62ee7a8b260c..21f86d80b9b7 100644 --- a/src/compiler/crystal/semantic/top_level_visitor.cr +++ b/src/compiler/crystal/semantic/top_level_visitor.cr @@ -1090,12 +1090,18 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor def check_ditto(node : Def | Assign | FunDef | Const | Macro, location : Location?) : Nil return if !@program.wants_doc? - stripped_doc = node.doc.try &.strip - if stripped_doc == ":ditto:" - node.doc = @last_doc - else - @last_doc = node.doc + + if stripped_doc = node.doc.try &.strip + if stripped_doc == ":ditto:" + node.doc = @last_doc + return + elsif stripped_doc.starts_with?(":ditto:\n") + node.doc = "#{@last_doc}\n\n#{stripped_doc.lchop(":ditto:\n").lchop("\n")}" + return + end end + + @last_doc = node.doc end def annotations_doc(annotations) From ff12e8145bd88955e0ae0a51a9fa099ca249b8fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Mon, 18 Oct 2021 19:28:17 +0200 Subject: [PATCH 3/3] Update src/compiler/crystal/semantic/top_level_visitor.cr Co-authored-by: Sijawusz Pur Rahnama --- src/compiler/crystal/semantic/top_level_visitor.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/crystal/semantic/top_level_visitor.cr b/src/compiler/crystal/semantic/top_level_visitor.cr index 21f86d80b9b7..a57258f67df2 100644 --- a/src/compiler/crystal/semantic/top_level_visitor.cr +++ b/src/compiler/crystal/semantic/top_level_visitor.cr @@ -1095,8 +1095,8 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor if stripped_doc == ":ditto:" node.doc = @last_doc return - elsif stripped_doc.starts_with?(":ditto:\n") - node.doc = "#{@last_doc}\n\n#{stripped_doc.lchop(":ditto:\n").lchop("\n")}" + elsif appendix = stripped_doc.lchop?(":ditto:\n") + node.doc = "#{@last_doc}\n\n#{appendix.lchop('\n')}" return end end