Add code to the `context`
FWeinb opened this issue · 8 comments
Currently the extracted code is usesless. see
We need to take into account that it is possible to have "{" and "}" inside the code snipped.
Yeah, that's what I suspected when I first raised the possibility of previewing the code.
Ping @SassDoc/owners.
I propose the following:
You match the function declaration (only the name and arguments) with a regex, like before.
Then, assuming you have the whole code buffer in a buffer
variable, and a cursor
variable set to the first {
position:
var start = cursor; // Remember the start position
cursor++; // Pass the first brace
var depth = 1;
var length = buffer.length;
for (; cursor < length && depth > 0; cursor++) {
switch (buffer[cursor]) {
case '{':
depth++;
break;
case '}':
depth--;
break;
}
}
// Now `cursor` is at the last matching brace
var code = buffer.substring(start, cursor);
This is not tested, but I think there's not much to tweak to make it work in your parser.
Note this is totally ignoring the eventual braces in strings and comments.
What if there are several functions/mixins in the same file? You have to stop whenever you find a new function/mixin declaration.
It stops when the matching brace is found. If you don't nest mixins/functions, that's okay. And even if it can be nested, the result will be consistent.
@valeriangalliat We could possibly extend the detection of quotes and comments. Thanks for the initial version. Will work form there.
It looks pretty nice. If you build a pre-release, we can try it out.
Fixed in version 0.2.3
. Just reinstall scss-comment-parser and it should work.