Fix mapped position when breaking up indented code.

[SVN r83122]
This commit is contained in:
Daniel James
2013-02-24 11:23:02 +00:00
parent d7a8b2de2c
commit 4ff7f3a9fb
2 changed files with 70 additions and 9 deletions
+35 -7
View File
@@ -296,14 +296,42 @@ namespace quickbook
case mapped_file_section::empty:
return section->original_pos;
case mapped_file_section::indented: {
// Indented doesn't really work, but that's okay because we
// currently don't break up indented code.
unsigned newlines = std::count(
this->source().begin() + section->our_pos,
this->source().begin() + pos, '\n');
boost::string_ref::size_type our_line = section->our_pos;
unsigned newline_count = 0;
return pos - section->our_pos + section->original_pos +
newlines * section->indentation;
for(boost::string_ref::size_type i = section->our_pos;
i != pos; ++i)
{
if (source()[i] == '\n') {
our_line = i + 1;
++newline_count;
}
}
if (newline_count == 0)
return pos - section->our_pos + section->original_pos;
boost::string_ref::size_type original_line =
section->original_pos;
while(newline_count > 0) {
if (original->source()[original_line] == '\n')
--newline_count;
++original_line;
}
for(unsigned i = section->indentation; i > 0; --i) {
if (original->source()[original_line] == '\n' ||
original->source()[original_line] == '\0') break;
assert(original->source()[original_line] == ' ' ||
original->source()[original_line] == '\t');
++original_line;
}
assert(original->source()[original_line] ==
source()[our_line]);
return original_line + (pos - our_line);
}
default:
assert(false);
+35 -2
View File
@@ -183,7 +183,7 @@ void indented_map_tests()
quickbook::file_position(2,4));
// TODO: Shouldn't this be (3,1)? Does it matter?
BOOST_TEST_EQ(f1->position_of(f1->source().end()),
quickbook::file_position(3,4));
quickbook::file_position(3,1));
}
{
@@ -209,7 +209,7 @@ void indented_map_tests()
BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 11),
quickbook::file_position(2,4));
BOOST_TEST_EQ(f1->position_of(f1->source().end()),
quickbook::file_position(3,4));
quickbook::file_position(3,1));
}
{
@@ -235,9 +235,42 @@ void indented_map_tests()
}
}
void indented_map_tests2()
{
boost::string_ref source(
" Code line1\n"
"\n"
" Code line2\n");
quickbook::file_ptr fake_file = new quickbook::file(
"(fake file)", source, 105u);
quickbook::mapped_file_builder builder;
{
builder.start(fake_file);
builder.unindent_and_add(fake_file->source());
quickbook::file_ptr f1 = builder.release();
BOOST_TEST_EQ(f1->source(),
boost::string_ref("Code line1\n\nCode line2\n"));
BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
quickbook::file_position(1,4));
BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 1),
quickbook::file_position(1,5));
BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 5),
quickbook::file_position(1,9));
BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 10),
quickbook::file_position(1,14));
BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 11),
quickbook::file_position(2,1));
BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 12),
quickbook::file_position(3,4));
}
}
int main()
{
simple_map_tests();
indented_map_tests();
indented_map_tests2();
return boost::report_errors();
}