Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ditto with additional lines #11336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 104 additions & 37 deletions spec/compiler/semantic/doc_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,117 @@ 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

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
Expand Down Expand Up @@ -148,21 +230,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 %(
Expand Down
16 changes: 11 additions & 5 deletions src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1090,12 +1090,18 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor

def check_ditto(node : Def | Assign | FunDef | Const | Macro, location : Location?) : Nil
return if [email protected]_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 appendix = stripped_doc.lchop?(":ditto:\n")
node.doc = "#{@last_doc}\n\n#{appendix.lchop('\n')}"
return
end
end

@last_doc = node.doc
end

def annotations_doc(annotations)
Expand Down