justinfx / MayaSublime

Send selected Python and MEL code snippets from SublimeText to Maya via commandPort
MIT License
149 stars 38 forks source link

Comments destroy multi-line send to maya #21

Closed peti196 closed 10 years ago

peti196 commented 10 years ago

When there's a // type comment in the code, it's sent to Maya so that it ignores the rest of the line causing errors. I saw there might be an attempt to remove them like RX_COMMENT = re.compile(r'^\s*(//|#)') but it doesn't seem to work for me. Sublime build 3059, Maya 2013 X64 sp2

justinfx commented 10 years ago

Can you please give a code snippet example that reproduces the problem? The RX_COMMENT is currently being used to filter out both python and mel style comments from the lines that are sent over the socket.

This MEL snippet works fine for me:

// here is some comments
print "FOO";
// and some
// more multi-
// line comments
print "BAR";
// DONE

As well as this equivalent python snippet:


# here is some comments
print "FOO";
# and some
# more multi-
# line comments
print "BAR";
# DONE

I am not sure what kind of error you are running into. As far as I know, there shouldn't be a reason to send comment lines.

peti196 commented 10 years ago

Hi, thank you.

It doesn't cut the comments from the end of the line. just to be simple (MEL):

print "foo"; //this prints foo print "bar"; //and this prints bar

About every MEl code I've ever seen in programmed this style. This translates to 'print "foo"; //this prints foo print "bar"; //and this prints bar' when it's sent so it only prints 'foo'.

Same thing goes where it's actually really needed:

if (someFunction($someVariable)`&& $someOtherVariable == $antoherVariable) //when the object is red and it's square shaped { fucntion();

<1800 more lines...> } else if (`someFunction($someVariable`)`&& $someOtherVariable == $yetAntoherVariable) //when the object is red but NOT square shaped { fucntion(); <1800 more lines...> } else //in any other case { exit "damn"; }
peti196 commented 10 years ago

Well, I solved the problem with the least elegant method. I stole inserted this code I stole from Markus Jarderot http://stackoverflow.com/questions/241327/python-snippet-to-remove-c-and-c-comments/241506#241506 : def commentremover(text): def replacer(match): s = match.group(0) if s.startswith('/'): return "" else: return s pattern = re.compile( r'//.?$|/_.?_/|\'(?:.|[^\'])\'|"(?:.|[^\"])_"', re.DOTALL | re.MULTILINE ) return re.sub(pattern, replacer, text)

and modified this line in your code:

snips.extend((line if isPython else comment_remover(line.strip()))

So now it deletes the comments and only the comments, not the ones within a string (for example "//192.168.0.1/c/users/" remains in the code) Also, /these kind of multi-line comments/ aren't handled by mayaSublime but fortunately Maya handles it as a comment so that's not a real problem.

justinfx commented 10 years ago

I was looking at the problem and saw the same issue of dealing with comment characters that are part of the string. But I was concerned about using a complex regex that strips them out, for fear that it might miss a case and break syntax unexpectedly.

Let me test your snippets a bit first before merging them as a fix

Thanks! On Feb 20, 2014 4:49 AM, "peti196" notifications@github.com wrote:

Well, I solved the problem with the least elegant method. I stole inserted this code I stole from Markus Jarderot http://stackoverflow.com/questions/241327/python-snippet-to-remove-c-and-c-comments/241506#241506: def commentremover(text): def replacer(match): s = match.group(0) if s.startswith('/'): return "" else: return s pattern = re.compile( r'//.?$|/_.?_/|\'(?:.|[^\'])\'|"(?:.|[^\"])_"', re.DOTALL | re.MULTILINE ) return re.sub(pattern, replacer, text)

and modified this line in your code:

snips.extend((line if isPython else comment_remover(line.strip()))

So now it deletes the comments and only the comments, not the ones within a string (for example "//192.168.0.1/c/users/" remains in the code) Also, /these kind of multi-line comments/ aren't handled by mayaSublime but fortunately Maya handles it as a comment so that's not a real problem.

Reply to this email directly or view it on GitHubhttps://github.com/justinfx/MayaSublime/issues/21#issuecomment-35512621 .

justinfx commented 10 years ago

This regex snippet breaks if the comment contains newline literal characters:

string $foo = "foo";
if ($foo == "foo") //this is a \n comment
{
    print "FOO";
}
else if ($foo == "bar") //this is a \n\n\n comment
{
    /* 
    multi
    line
    stuff
    */
    print "BAR";
}
else //in any other case
{
    print "NONE";
}

Which formats into:

string $foo = "foo";
if ($foo == "foo") 
 comment
{
    print "FOO";
}
else if ($foo == "bar") 

 comment
{

    print "BAR";
}
else 
{
    print "NONE";
}

It is kind of the reason I am hesitant to introduce a complex regex to strip various comment formats, as opposed to my current simple one that just ignores lines. I understand my current implementation breaks when the MEL code contains trailing line snippets, but I still think if any solution is added it should be one that isn't too faulty.

Will keep testing options.

justinfx commented 10 years ago

Can you check out the changes I just committed to the topic branch? I think instead of trying to successfully pull out trailing line comments, and multi-line comments which potentially can break the original source, it might be better to just fix how the source is joined and sent to Maya.

I am joining the MEL source on "\r" (carriage return) which seems to work properly for complexly commented source code. If it works fine for you, I will merge it into master.

peti196 commented 10 years ago

Genious. It works fine up until 8089 characters. Is it a limitation of the Telnet, Maya or Sublime?

justinfx commented 10 years ago

Yea the commandPort has a limit value as part of its configuration. The default used to be 4096 I thought. On Feb 25, 2014 1:17 AM, "peti196" notifications@github.com wrote:

Genious. It works fine up until 8089 characters. Is it a limitation of the Telnet, Maya or Sublime?

Reply to this email directly or view it on GitHubhttps://github.com/justinfx/MayaSublime/issues/21#issuecomment-35880492 .

justinfx commented 10 years ago

Merged into master