Skip to content

Commit

Permalink
Improve sourcemap output for prepended texts
Browse files Browse the repository at this point in the history
Fixes #879
Should work for all prepended texts (like top-comments)
  • Loading branch information
mgreter committed Mar 9, 2015
1 parent 5606d26 commit 22827bb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace Sass {
// prepend some text or token to the buffer
void Emitter::prepend_string(const string& text)
{
// update source-map for new text
wbuf.smap.prepend(Offset(text));
wbuf.buffer = text + wbuf.buffer;
}

Expand Down
26 changes: 24 additions & 2 deletions position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ namespace Sass {

using namespace std;

Offset::Offset(const char* string)
: line(0), column(0)
{
*this = inc(string, string + strlen(string));
}

Offset::Offset(const string& text)
: line(0), column(0)
{
*this = inc(text.c_str(), text.c_str() + text.size());
}

Offset::Offset(const size_t line, const size_t column)
: line(line), column(column) { }

Expand Down Expand Up @@ -34,7 +46,12 @@ namespace Sass {
return line != pos.line || column != pos.column;
}

const Offset Offset::operator+ (const Offset &off) const
void Offset::operator+= (const Offset &off)
{
*this = Offset(line + off.line, off.line > 0 ? off.column : off.column + column);
}

Offset Offset::operator+ (const Offset &off) const
{
return Offset(line + off.line, off.line > 0 ? off.column : off.column + column);
}
Expand Down Expand Up @@ -67,7 +84,7 @@ namespace Sass {
Position Position::inc(const char* begin, const char* end) const
{
Offset offset(line, column);
offset.inc(begin, end);
offset = offset.inc(begin, end);
return Position(file, offset);
}

Expand All @@ -81,6 +98,11 @@ namespace Sass {
return file == pos.file || line != pos.line || column != pos.column;
}

void Position::operator+= (const Offset &off)
{
*this = Position(file, line + off.line, off.line > 0 ? off.column : off.column + column);
}

const Position Position::operator+ (const Offset &off) const
{
return Position(file, line + off.line, off.line > 0 ? off.column : off.column + column);
Expand Down
6 changes: 5 additions & 1 deletion position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ namespace Sass {
class Offset {

public: // c-tor
Offset(const char* string);
Offset(const string& text);
Offset(const size_t line, const size_t column);

// return new position, incremented by the given string
Offset inc(const char* begin, const char* end) const;

public: // overload operators for position
void operator+= (const Offset &pos);
bool operator== (const Offset &pos) const;
bool operator!= (const Offset &pos) const;
const Offset operator+ (const Offset &off) const;
Offset operator+ (const Offset &off) const;

public: // overload output stream operator
// friend ostream& operator<<(ostream& strm, const Offset& off);
Expand All @@ -45,6 +48,7 @@ namespace Sass {
Position(const size_t file, const size_t line, const size_t column);

public: // overload operators for position
void operator+= (const Offset &off);
bool operator== (const Position &pos) const;
bool operator!= (const Position &pos) const;
const Position operator+ (const Offset &off) const;
Expand Down
18 changes: 18 additions & 0 deletions source_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ namespace Sass {
return result;
}

void SourceMap::prepend(const Offset& offset)
{
if (offset.line != 0 || offset.column != 0) {
for (Mapping& mapping : mappings) {
// move stuff on the first old line
if (mapping.generated_position.line == 0) {
mapping.generated_position.column += offset.column;
}
// make place for the new lines
mapping.generated_position.line += offset.line;
}
}
if (current_position.line == 0) {
current_position.column += offset.column;
}
current_position.line += offset.line;
}

void SourceMap::update_column(const string& str)
{
const ptrdiff_t new_line_count = std::count(str.begin(), str.end(), '\n');
Expand Down
3 changes: 3 additions & 0 deletions source_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "ast_fwd_decl.hpp"
#include "base64vlq.hpp"
#include "position.hpp"
#include "mapping.hpp"

namespace Sass {
Expand All @@ -22,6 +23,8 @@ namespace Sass {
void setFile(const string& str) {
file = str;
}
// void append(const Offset& offset);
void prepend(const Offset& offset);
void update_column(const string& str);
void add_open_mapping(AST_Node* node);
void add_close_mapping(AST_Node* node);
Expand Down

0 comments on commit 22827bb

Please sign in to comment.