Skip to content

Commit eae7c0a

Browse files
committed
Improves @warn and @debug message to match ruby sass
Moved static get_cwd function to Sass::File namespace
1 parent 539dc70 commit eae7c0a

File tree

7 files changed

+82
-25
lines changed

7 files changed

+82
-25
lines changed

backtrace.hpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "position.hpp"
77
#endif
88

9+
#ifndef SASS_FILE
10+
#include "file.hpp"
11+
#endif
12+
913
namespace Sass {
1014

1115
using namespace std;
@@ -26,19 +30,35 @@ namespace Sass {
2630

2731
string to_string(bool warning = false)
2832
{
33+
size_t i = -1;
2934
stringstream ss;
35+
string cwd(Sass::File::get_cwd());
3036
Backtrace* this_point = this;
3137

3238
if (!warning) ss << endl << "Backtrace:";
3339
// the first tracepoint (which is parent-less) is an empty placeholder
3440
while (this_point->parent) {
35-
ss << endl
36-
<< "\t"
37-
<< (warning ? " " : "")
38-
<< this_point->path
39-
<< ":"
40-
<< this_point->position.line
41-
<< this_point->parent->caller;
41+
42+
// make path relative to the current directory
43+
string rel_path(Sass::File::resolve_relative_path(this_point->path, cwd, cwd));
44+
45+
if (warning) {
46+
ss << endl
47+
<< "\t"
48+
<< (++i == 0 ? "on" : "from")
49+
<< " line "
50+
<< this_point->position.line
51+
<< " of "
52+
<< rel_path;
53+
} else {
54+
ss << endl
55+
<< "\t"
56+
<< rel_path
57+
<< ":"
58+
<< this_point->position.line
59+
<< this_point->parent->caller;
60+
}
61+
4262
this_point = this_point->parent;
4363
}
4464

context.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#ifdef _WIN32
2-
#include <direct.h>
3-
#define getcwd _getcwd
42
#define PATH_SEP ';'
53
#else
6-
#include <unistd.h>
74
#define PATH_SEP ':'
85
#endif
96

@@ -359,15 +356,7 @@ namespace Sass {
359356

360357
string Context::get_cwd()
361358
{
362-
const size_t wd_len = 1024;
363-
char wd[wd_len];
364-
string cwd = getcwd(wd, wd_len);
365-
#ifdef _WIN32
366-
//convert backslashes to forward slashes
367-
replace(cwd.begin(), cwd.end(), '\\', '/');
368-
#endif
369-
if (cwd[cwd.length() - 1] != '/') cwd += '/';
370-
return cwd;
359+
return Sass::File::get_cwd();
371360
}
372361

373362
void register_function(Context& ctx, Signature sig, Native_Function f, Env* env)

context.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace Sass {
114114

115115
Context(Data);
116116
~Context();
117-
string get_cwd();
117+
static string get_cwd();
118118
void setup_color_map();
119119
string add_file(string);
120120
Block* parse_file();

eval.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "file.hpp"
12
#include "eval.hpp"
23
#include "ast.hpp"
34
#include "bind.hpp"
@@ -211,10 +212,9 @@ namespace Sass {
211212

212213
}
213214

214-
string prefix("WARNING: ");
215215
string result(unquote(message->perform(&to_string)));
216-
cerr << prefix << result;
217216
Backtrace top(backtrace, w->path(), w->position(), "");
217+
cerr << "WARNING: " << result;
218218
cerr << top.to_string(true);
219219
cerr << endl << endl;
220220
return 0;
@@ -243,15 +243,45 @@ namespace Sass {
243243

244244
}
245245

246-
string prefix("Error: ");
247246
string result(unquote(message->perform(&to_string)));
248-
cerr << prefix << result;
249247
Backtrace top(backtrace, e->path(), e->position(), "");
248+
cerr << "Error: " << result;
250249
cerr << top.to_string(true);
251250
cerr << endl << endl;
252251
return 0;
253252
}
254253

254+
Expression* Eval::operator()(Debug* d)
255+
{
256+
Expression* message = d->value()->perform(this);
257+
To_String to_string;
258+
259+
// try to use generic function
260+
if (env->has("@debug[f]")) {
261+
262+
Definition* def = static_cast<Definition*>((*env)["@debug[f]"]);
263+
// Block* body = def->block();
264+
// Native_Function func = def->native_function();
265+
Sass_C_Function c_func = def->c_function();
266+
267+
To_C to_c;
268+
union Sass_Value* c_args = sass_make_list(1, SASS_COMMA);
269+
sass_list_set_value(c_args, 0, message->perform(&to_c));
270+
Sass_Value* c_val = c_func(c_args, def->cookie());
271+
sass_delete_value(c_args);
272+
sass_delete_value(c_val);
273+
return 0;
274+
275+
}
276+
277+
string cwd(ctx.get_cwd());
278+
string result(unquote(message->perform(&to_string)));
279+
string rel_path(Sass::File::resolve_relative_path(d->path(), cwd, cwd));
280+
cerr << rel_path << ":" << d->position().line << ":" << " DEBUG: " << result;
281+
cerr << endl;
282+
return 0;
283+
}
284+
255285
Expression* Eval::operator()(List* l)
256286
{
257287
if (l->is_expanded()) return l;

file.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#ifdef _WIN32
2+
#include <direct.h>
3+
#define getcwd _getcwd
24
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
5+
#else
6+
#include <unistd.h>
37
#endif
48

59
#include <iostream>
@@ -28,6 +32,19 @@ namespace Sass {
2832
namespace File {
2933
using namespace std;
3034

35+
string get_cwd()
36+
{
37+
const size_t wd_len = 1024;
38+
char wd[wd_len];
39+
string cwd = getcwd(wd, wd_len);
40+
#ifdef _WIN32
41+
//convert backslashes to forward slashes
42+
replace(cwd.begin(), cwd.end(), '\\', '/');
43+
#endif
44+
if (cwd[cwd.length() - 1] != '/') cwd += '/';
45+
return cwd;
46+
}
47+
3148
// no physical check on filesystem
3249
// only a logical cleanup of a path
3350
string make_canonical_path (string path)

file.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Sass {
44
using namespace std;
55
struct Context;
66
namespace File {
7+
string get_cwd();
78
string base_name(string);
89
string dir_name(string);
910
string join_paths(string, string);

operation.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Sass {
2626
virtual T operator()(Import_Stub* x) = 0;
2727
virtual T operator()(Warning* x) = 0;
2828
virtual T operator()(Error* x) = 0;
29-
virtual T operator()(Debug* x) = 0;
29+
virtual T operator()(Debug* x) = 0;
3030
virtual T operator()(Comment* x) = 0;
3131
virtual T operator()(If* x) = 0;
3232
virtual T operator()(For* x) = 0;

0 commit comments

Comments
 (0)