Skip to content

Commit 13b72d7

Browse files
authored
Merge pull request #191 from znz/feature/drop-source-location-line
source_location の行番号を削除して生成ドキュメントの差分肥大化を防ぐ
2 parents 00e29d6 + 2821d75 commit 13b72d7

6 files changed

Lines changed: 59 additions & 4 deletions

File tree

lib/bitclust/entry.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def deserializer
117117
when '[LibraryEntry]' then "restore_libraries(h['#{@name}'])"
118118
when '[ClassEntry]' then "restore_classes(h['#{@name}'])"
119119
when '[MethodEntry]' then "restore_methods(h['#{@name}'])"
120-
when 'Location' then "h['#{@name}']&.tap { |loc| break if loc.empty?; break Location.new(*loc.split(?:)) }"
120+
when 'Location' then "h['#{@name}']&.tap { |loc| break if loc.empty?; break Location.new(loc.split(?:).first, nil) }"
121121
else
122122
raise "must not happen: @type=#{@type.inspect}"
123123
end
@@ -135,7 +135,7 @@ def serializer
135135
when '[LibraryEntry]' then "serialize_entries(@#{@name})"
136136
when '[ClassEntry]' then "serialize_entries(@#{@name})"
137137
when '[MethodEntry]' then "serialize_entries(@#{@name})"
138-
when 'Location' then "@#@name.to_s"
138+
when 'Location' then "(@#{@name} && @#{@name}.file).to_s"
139139
else
140140
raise "must not happen: @type=#{@type.inspect}"
141141
end

lib/bitclust/functionreferenceparser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def entry(header, body)
6161
f.name = h.name
6262
f.params = h.params
6363
f.source = body.join('')
64-
f.source_location = body[0]&.location&.tap {|loc| break Location.new(@path, loc.line - 1) }
64+
f.source_location = body[0]&.location && Location.new(@path, nil)
6565
}
6666
end
6767

lib/bitclust/subcommands/statichtml_command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def canonical_url(current_url)
9191
end
9292

9393
def edit_url(location)
94-
"#{@edit_base_url}/#{location.file}#L#{location.line}".sub(/([^:])\/\/+/, "\\1/")
94+
"#{@edit_base_url}/#{location.file}".sub(/([^:])\/\/+/, "\\1/")
9595
end
9696

9797
def encodename_package(str)

test/test_functionreferenceparser.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@ def test_parse_file(data)
4848
assert_equal data[:expected], result.collect(&:source)
4949
}
5050
end
51+
52+
# source_location points at the file only; the line number is no longer
53+
# recorded (it would only churn the generated diffs).
54+
def test_source_location_has_no_line_number
55+
@db.transaction {
56+
result = @parser.parse_file(@path, "test.c", {"version" => "2.6.0"})
57+
location = result.first.source_location
58+
assert_equal @path, location.file
59+
assert_nil location.line
60+
}
61+
end
5162
end

test/test_methoddatabase.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ def test_dynamic_include
5757
@db.get_class("B").dynamically_included.map{|m| m.name})
5858
end
5959

60+
# source_location must be stored without a line number so that regenerating
61+
# the database does not churn the persisted entries (and the generated HTML).
62+
def test_source_location_serialized_without_line_number
63+
entry = @db.search_methods(BitClust::MethodNamePattern.new(nil, nil, 'at_exit')).records.first.entry
64+
entry.source_location = BitClust::Location.new('refm/api/src/_builtin/Kernel', 42)
65+
assert_equal('refm/api/src/_builtin/Kernel', entry._get_properties['source_location'])
66+
end
67+
68+
def test_source_location_roundtrip_drops_line_number
69+
entry = @db.search_methods(BitClust::MethodNamePattern.new(nil, nil, 'at_exit')).records.first.entry
70+
entry.source_location = BitClust::Location.new('refm/api/src/_builtin/Kernel', 42)
71+
entry._set_properties(entry._get_properties)
72+
assert_equal('refm/api/src/_builtin/Kernel', entry.source_location.file)
73+
assert_nil(entry.source_location.line)
74+
end
75+
6076
private
6177
def setup_files
6278
FileUtils.mkdir_p("#{@root}/_builtin")

test/test_statichtml_command.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'test/unit'
2+
require 'bitclust'
3+
require 'bitclust/subcommands/statichtml_command'
4+
5+
class TestStatichtmlURLMapperEx < Test::Unit::TestCase
6+
def setup
7+
@urlmapper = BitClust::Subcommands::StatichtmlCommand::URLMapperEx.new(
8+
:suffix => 'html',
9+
:edit_base_url => 'https://github.com/rurema/doctree/edit/master',
10+
)
11+
end
12+
13+
# The edit URL must not contain a line number, so that shifting source lines
14+
# does not churn the generated HTML diff.
15+
def test_edit_url_has_no_line_number
16+
location = BitClust::Location.new('refm/api/src/_builtin/Array', 42)
17+
assert_equal('https://github.com/rurema/doctree/edit/master/refm/api/src/_builtin/Array',
18+
@urlmapper.edit_url(location))
19+
end
20+
21+
# A location restored from the database has no line number (nil); the edit URL
22+
# must still be a clean file link without a trailing "#L".
23+
def test_edit_url_without_line_in_location
24+
location = BitClust::Location.new('refm/api/src/_builtin/Array', nil)
25+
assert_equal('https://github.com/rurema/doctree/edit/master/refm/api/src/_builtin/Array',
26+
@urlmapper.edit_url(location))
27+
end
28+
end

0 commit comments

Comments
 (0)