Skip to content

Commit

Permalink
Refactor source mappings in output code
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Jan 5, 2015
1 parent afe8248 commit bd615e5
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 120 deletions.
170 changes: 72 additions & 98 deletions inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,14 @@ namespace Sass {

void Inspect::operator()(Media_Block* media_block)
{
// if (ctx) ctx->source_map.add_mapping(media_block);
// source_map.add_mapping(media_block);
append_to_buffer("@media ");
// if (ctx) ctx->source_map.add_end_mapping(media_block);
// source_map.add_end_mapping(media_block);
append_to_buffer("@media", media_block, " ");
media_block->media_queries()->perform(this);
media_block->block()->perform(this);
}

void Inspect::operator()(Feature_Block* feature_block)
{
if (ctx) ctx->source_map.add_mapping(feature_block);
source_map.add_mapping(feature_block);
append_to_buffer("@supports ");
if (ctx) ctx->source_map.add_end_mapping(feature_block);
source_map.add_end_mapping(feature_block);
append_to_buffer("@supports", feature_block, " ");
feature_block->feature_queries()->perform(this);
feature_block->block()->perform(this);
}
Expand All @@ -102,18 +94,18 @@ namespace Sass {
void Inspect::operator()(Declaration* dec)
{
if (dec->value()->concrete_type() == Expression::NULL_VAL) return;
if (ctx) ctx->source_map.add_mapping(dec->property());
source_map.add_mapping(dec->property());
if (ctx) ctx->source_map.add_open_mapping(dec->property());
source_map.add_open_mapping(dec->property());
dec->property()->perform(this);
if (ctx) ctx->source_map.add_end_mapping(dec->property());
source_map.add_end_mapping(dec->property());
if (ctx) ctx->source_map.add_close_mapping(dec->property());
source_map.add_close_mapping(dec->property());
append_to_buffer(": ");
if (ctx) ctx->source_map.add_mapping(dec->value());
source_map.add_mapping(dec->value());
if (ctx) ctx->source_map.add_open_mapping(dec->value());
source_map.add_open_mapping(dec->value());
dec->value()->perform(this);
if (dec->is_important()) append_to_buffer(" !important");
if (ctx) ctx->source_map.add_end_mapping(dec->value());
source_map.add_end_mapping(dec->value());
if (ctx) ctx->source_map.add_close_mapping(dec->value());
source_map.add_close_mapping(dec->value());
append_to_buffer(";");
}

Expand All @@ -129,16 +121,12 @@ namespace Sass {
void Inspect::operator()(Import* import)
{
if (!import->urls().empty()) {
if (ctx) ctx->source_map.add_mapping(import);
source_map.add_mapping(import);
append_to_buffer("@import ");
append_to_buffer("@import", import, " ");
import->urls().front()->perform(this);
append_to_buffer(";");
for (size_t i = 1, S = import->urls().size(); i < S; ++i) {
append_to_buffer(ctx->linefeed);
if (ctx) ctx->source_map.add_mapping(import);
source_map.add_mapping(import);
append_to_buffer("@import ");
append_to_buffer("@import", import, " ");
import->urls()[i]->perform(this);
append_to_buffer(";");
}
Expand All @@ -147,36 +135,28 @@ namespace Sass {

void Inspect::operator()(Import_Stub* import)
{
if (ctx) ctx->source_map.add_mapping(import);
source_map.add_mapping(import);
append_to_buffer("@import ");
append_to_buffer("@import", import, " ");
append_to_buffer(import->file_name());
append_to_buffer(";");
}

void Inspect::operator()(Warning* warning)
{
if (ctx) ctx->source_map.add_mapping(warning);
source_map.add_mapping(warning);
append_to_buffer("@warn ");
append_to_buffer("@warn", warning, " ");
warning->message()->perform(this);
append_to_buffer(";");
}

void Inspect::operator()(Error* error)
{
if (ctx) ctx->source_map.add_mapping(error);
source_map.add_mapping(error);
append_to_buffer("@error ");
append_to_buffer("@error", error, " ");
error->message()->perform(this);
append_to_buffer(";");
}

void Inspect::operator()(Debug* debug)
{
if (ctx) ctx->source_map.add_mapping(debug);
source_map.add_mapping(debug);
append_to_buffer("@debug ");
append_to_buffer("@debug", debug, " ");
debug->value()->perform(this);
append_to_buffer(";");
}
Expand All @@ -188,7 +168,7 @@ namespace Sass {

void Inspect::operator()(If* cond)
{
append_to_buffer("@if ");
append_to_buffer("@if", cond, " ");
cond->predicate()->perform(this);
cond->consequent()->perform(this);
if (cond->alternative()) {
Expand All @@ -201,7 +181,7 @@ namespace Sass {

void Inspect::operator()(For* loop)
{
append_to_buffer("@for ");
append_to_buffer("@for", loop, " ");
append_to_buffer(loop->variable());
append_to_buffer(" from ");
loop->lower_bound()->perform(this);
Expand All @@ -212,7 +192,7 @@ namespace Sass {

void Inspect::operator()(Each* loop)
{
append_to_buffer("@each ");
append_to_buffer("@each", loop, " ");
append_to_buffer(loop->variables()[0]);
for (size_t i = 1, L = loop->variables().size(); i < L; ++i) {
append_to_buffer(", ");
Expand All @@ -225,31 +205,31 @@ namespace Sass {

void Inspect::operator()(While* loop)
{
append_to_buffer("@while ");
append_to_buffer("@while", loop, " ");
loop->predicate()->perform(this);
loop->block()->perform(this);
}

void Inspect::operator()(Return* ret)
{
append_to_buffer("@return ");
append_to_buffer("@return", ret, " ");
ret->value()->perform(this);
append_to_buffer(";");
}

void Inspect::operator()(Extension* extend)
{
append_to_buffer("@extend ");
append_to_buffer("@extend", extend, " ");
extend->selector()->perform(this);
append_to_buffer(";");
}

void Inspect::operator()(Definition* def)
{
if (def->type() == Definition::MIXIN) {
append_to_buffer("@mixin ");
append_to_buffer("@mixin", def, " ");
} else {
append_to_buffer("@function ");
append_to_buffer("@function", def, " ");
}
append_to_buffer(def->name());
def->parameters()->perform(this);
Expand All @@ -258,7 +238,8 @@ namespace Sass {

void Inspect::operator()(Mixin_Call* call)
{
append_to_buffer(string("@include ") += call->name());
append_to_buffer("@include", call, " ");
append_to_buffer(call->name());
if (call->arguments()) {
call->arguments()->perform(this);
}
Expand All @@ -271,9 +252,7 @@ namespace Sass {

void Inspect::operator()(Content* content)
{
if (ctx) ctx->source_map.add_mapping(content);
source_map.add_mapping(content);
append_to_buffer("@content;");
append_to_buffer("@content", content, ";");
}

void Inspect::operator()(Map* map)
Expand Down Expand Up @@ -484,11 +463,7 @@ namespace Sass {

void Inspect::operator()(String_Constant* s)
{
if (ctx) ctx->source_map.add_mapping(s);
source_map.add_mapping(s);
append_to_buffer(s->needs_unquoting() ? unquote(s->value()) : s->value());
if (ctx) ctx->source_map.add_end_mapping(s);
source_map.add_end_mapping(s);
append_to_buffer(s->needs_unquoting() ? unquote(s->value()) : s->value(), s);
}

void Inspect::operator()(Feature_Query* fq)
Expand Down Expand Up @@ -543,26 +518,26 @@ namespace Sass {
void Inspect::operator()(Media_Query_Expression* mqe)
{
if (mqe->is_interpolated()) {
if (ctx) ctx->source_map.add_mapping(mqe->feature());
source_map.add_mapping(mqe->feature());
if (ctx) ctx->source_map.add_open_mapping(mqe->feature());
source_map.add_open_mapping(mqe->feature());
mqe->feature()->perform(this);
if (ctx) ctx->source_map.add_end_mapping(mqe->feature());
source_map.add_end_mapping(mqe->feature());
if (ctx) ctx->source_map.add_close_mapping(mqe->feature());
source_map.add_close_mapping(mqe->feature());
}
else {
append_to_buffer("(");
if (ctx) ctx->source_map.add_mapping(mqe->feature());
source_map.add_mapping(mqe->feature());
if (ctx) ctx->source_map.add_open_mapping(mqe->feature());
source_map.add_open_mapping(mqe->feature());
mqe->feature()->perform(this);
if (ctx) ctx->source_map.add_end_mapping(mqe->feature());
source_map.add_end_mapping(mqe->feature());
if (ctx) ctx->source_map.add_close_mapping(mqe->feature());
source_map.add_close_mapping(mqe->feature());
if (mqe->value()) {
append_to_buffer(": ");
if (ctx) ctx->source_map.add_mapping(mqe->value());
source_map.add_mapping(mqe->value());
if (ctx) ctx->source_map.add_open_mapping(mqe->value());
source_map.add_open_mapping(mqe->value());
mqe->value()->perform(this);
if (ctx) ctx->source_map.add_end_mapping(mqe->value());
source_map.add_end_mapping(mqe->value());
if (ctx) ctx->source_map.add_close_mapping(mqe->value());
source_map.add_close_mapping(mqe->value());
}
append_to_buffer(")");
}
Expand Down Expand Up @@ -642,32 +617,24 @@ namespace Sass {

void Inspect::operator()(Selector_Placeholder* s)
{
append_to_buffer(s->name());
append_to_buffer(s->name(), s);
}

void Inspect::operator()(Type_Selector* s)
{
if (ctx) ctx->source_map.add_mapping(s);
source_map.add_mapping(s);
append_to_buffer(s->name());
if (ctx) ctx->source_map.add_end_mapping(s);
source_map.add_end_mapping(s);
append_to_buffer(s->name(), s);
}

void Inspect::operator()(Selector_Qualifier* s)
{
if (ctx) ctx->source_map.add_mapping(s);
source_map.add_mapping(s);
append_to_buffer(s->name());
if (ctx) ctx->source_map.add_end_mapping(s);
source_map.add_end_mapping(s);
append_to_buffer(s->name(), s);
}

void Inspect::operator()(Attribute_Selector* s)
{
append_to_buffer("[");
if (ctx) ctx->source_map.add_mapping(s);
source_map.add_mapping(s);
if (ctx) ctx->source_map.add_open_mapping(s);
source_map.add_open_mapping(s);
append_to_buffer(s->name());
if (!s->matcher().empty()) {
append_to_buffer(s->matcher());
Expand All @@ -676,18 +643,14 @@ namespace Sass {
}
// append_to_buffer(s->value());
}
if (ctx) ctx->source_map.add_end_mapping(s);
source_map.add_end_mapping(s);
if (ctx) ctx->source_map.add_close_mapping(s);
source_map.add_close_mapping(s);
append_to_buffer("]");
}

void Inspect::operator()(Pseudo_Selector* s)
{
if (ctx) ctx->source_map.add_mapping(s);
source_map.add_mapping(s);
append_to_buffer(s->name());
if (ctx) ctx->source_map.add_end_mapping(s);
source_map.add_end_mapping(s);
append_to_buffer(s->name(), s);
if (s->expression()) {
s->expression()->perform(this);
append_to_buffer(")");
Expand All @@ -696,11 +659,7 @@ namespace Sass {

void Inspect::operator()(Wrapped_Selector* s)
{
if (ctx) ctx->source_map.add_mapping(s);
source_map.add_mapping(s);
append_to_buffer(s->name());
if (ctx) ctx->source_map.add_end_mapping(s);
source_map.add_end_mapping(s);
append_to_buffer(s->name(), s);
s->selector()->perform(this);
append_to_buffer(")");
}
Expand Down Expand Up @@ -734,18 +693,18 @@ namespace Sass {
void Inspect::operator()(Selector_List* g)
{
if (g->empty()) return;
if (ctx) ctx->source_map.add_mapping((*g)[0]);
source_map.add_mapping((*g)[0]);
if (ctx) ctx->source_map.add_open_mapping((*g)[0]);
source_map.add_open_mapping((*g)[0]);
(*g)[0]->perform(this);
if (ctx) ctx->source_map.add_end_mapping((*g)[0]);
source_map.add_end_mapping((*g)[0]);
if (ctx) ctx->source_map.add_close_mapping((*g)[0]);
source_map.add_close_mapping((*g)[0]);
for (size_t i = 1, L = g->length(); i < L; ++i) {
append_to_buffer(", ");
if (ctx) ctx->source_map.add_mapping((*g)[i]);
source_map.add_mapping((*g)[i]);
if (ctx) ctx->source_map.add_open_mapping((*g)[i]);
source_map.add_open_mapping((*g)[i]);
(*g)[i]->perform(this);
if (ctx) ctx->source_map.add_end_mapping((*g)[i]);
source_map.add_end_mapping((*g)[i]);
if (ctx) ctx->source_map.add_close_mapping((*g)[i]);
source_map.add_close_mapping((*g)[i]);
}
}

Expand Down Expand Up @@ -802,4 +761,19 @@ namespace Sass {
source_map.update_column(text);
}

void Inspect::append_to_buffer(const string& text, AST_Node* node)
{
if (ctx) ctx->source_map.add_open_mapping(node);
source_map.add_open_mapping(node);
append_to_buffer(text);
if (ctx) ctx->source_map.add_close_mapping(node);
source_map.add_close_mapping(node);
}

void Inspect::append_to_buffer(const string& text, AST_Node* node, const string& tail)
{
append_to_buffer(text, node);
append_to_buffer(tail);
}

}
2 changes: 2 additions & 0 deletions inspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace Sass {
public:
void append_indent_to_buffer();
void append_to_buffer(const string& text);
void append_to_buffer(const string& text, AST_Node* node);
void append_to_buffer(const string& text, AST_Node* node, const string& tail);

public:

Expand Down
6 changes: 3 additions & 3 deletions output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Sass {
{ }
virtual ~Output() { };

string get_buffer()
string get_buffer(void)
{
Inspect comments(ctx);
size_t size_com = top_comments.size();
Expand Down Expand Up @@ -86,9 +86,9 @@ namespace Sass {

void append_to_buffer(const string& data, AST_Node* node)
{
ctx->source_map.add_mapping(node);
ctx->source_map.add_open_mapping(node);
append_to_buffer(data);
ctx->source_map.add_end_mapping(node);
ctx->source_map.add_close_mapping(node);
}

};
Expand Down
Loading

0 comments on commit bd615e5

Please sign in to comment.