Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code to the context #11

Closed
FWeinb opened this issue Aug 16, 2014 · 8 comments
Closed

Add code to the context #11

FWeinb opened this issue Aug 16, 2014 · 8 comments

Comments

@FWeinb
Copy link
Member

FWeinb commented Aug 16, 2014

Currently the extracted code is usesless. see

We need to take into account that it is possible to have "{" and "}" inside the code snipped.

@KittyGiraudel
Copy link
Member

Yeah, that's what I suspected when I first raised the possibility of previewing the code.

@KittyGiraudel
Copy link
Member

Ping @SassDoc/owners.

@valeriangalliat
Copy link
Member

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.

@KittyGiraudel
Copy link
Member

What if there are several functions/mixins in the same file? You have to stop whenever you find a new function/mixin declaration.

@valeriangalliat
Copy link
Member

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.

@FWeinb
Copy link
Member Author

FWeinb commented Aug 17, 2014

@valeriangalliat We could possibly extend the detection of quotes and comments. Thanks for the initial version. Will work form there.

FWeinb added a commit that referenced this issue Aug 17, 2014
@KittyGiraudel
Copy link
Member

It looks pretty nice. If you build a pre-release, we can try it out.

FWeinb added a commit that referenced this issue Aug 17, 2014
@FWeinb
Copy link
Member Author

FWeinb commented Aug 17, 2014

Fixed in version 0.2.3. Just reinstall scss-comment-parser and it should work.

@FWeinb FWeinb closed this as completed Aug 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants