Skip to content

Commit

Permalink
Show line numbers at link when there are filename duplicates in "Defi…
Browse files Browse the repository at this point in the history
…ned in:" section (docs) (#6280)
  • Loading branch information
wooster0 authored and Serdar Dogruyol committed Jul 28, 2018
1 parent 97344a3 commit 077920d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src/compiler/crystal/tools/doc/generator.cr
Expand Up @@ -360,7 +360,13 @@ class Crystal::Doc::Generator
filename[@base_dir.size..-1]
end

record RelativeLocation, filename : String, line_number : Int32, url : String? do
class RelativeLocation
property show_line_number
getter filename, line_number, url

def initialize(@filename : String, @line_number : Int32, @url : String?, @show_line_number : Bool)
end

def to_json(builder : JSON::Builder)
builder.object do
builder.field "filename", filename
Expand All @@ -369,6 +375,7 @@ class Crystal::Doc::Generator
end
end
end

SRC_SEP = "src#{File::SEPARATOR}"

def relative_locations(type)
Expand All @@ -386,7 +393,16 @@ class Crystal::Doc::Generator
filename = filename[1..-1] if filename.starts_with? File::SEPARATOR
filename = filename[4..-1] if filename.starts_with? SRC_SEP

locations << RelativeLocation.new(filename, location.line_number, url)
show_line_number = locations.any? do |location|
if location.filename == filename
location.show_line_number = true
true
else
false
end
end

locations << RelativeLocation.new(filename, location.line_number, url, show_line_number)
end
locations
end
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/crystal/tools/doc/html/type.html
Expand Up @@ -43,9 +43,11 @@ <h2>Alias Definition</h2>
<h2>Defined in:</h2>
<% locations.each do |location| %>
<% if url = location.url %>
<a href="<%= url %>#L<%= location.line_number %>" target="_blank"><%= location.filename %></a>
<a href="<%= url %>#L<%= location.line_number %>" target="_blank">
<%= location.filename %><% if location.show_line_number %>:<%= location.line_number %><% end %>
</a>
<% else %>
<%= location.filename %>
<%= location.filename %><% if location.show_line_number %>:<%= location.line_number %><% end %>
<% end %>
<br/>
<% end %>
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/types.cr
Expand Up @@ -705,7 +705,8 @@ module Crystal
# Adds a location to this type.
def add_location(location : Location)
locations = @locations ||= [] of Location
locations << location
# The `unless` prevents that identical links are being generated in the "Defined in:" section in the docs because of macros
locations << location unless locations.any? { |loc| loc.filename == location.filename && loc.line_number == location.line_number }
end

getter(types) { {} of String => NamedType }
Expand Down

0 comments on commit 077920d

Please sign in to comment.