Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Adding support for multiple lineComment prefixes #3121

Merged
merged 6 commits into from
Mar 16, 2013
Merged

Adding support for multiple lineComment prefixes #3121

merged 6 commits into from
Mar 16, 2013

Conversation

TomMalbran
Copy link
Contributor

There are a few languages like PHP that support 2 types of line comment ("//" and "#").

To address this, I made lineComment be an array for every language and saved also as an array in Language. It will also return an array when asking for a linePrefix.

lineCommentPrefix can work with multiple line prefixes and uncomment any line comment prefix defined in the array if it is found at the start of the line as before, but it will always comment using the first line comment prefix.

blockCommentPrefixSuffix can also work multiple line prefixes and uncomment any line comment prefix defined in the array. This also fixes a bug where in PHP if there was a comment using # and you would Toggle Block Comment inside that line comment it could uncomment any block comment found before that line.

@ghost ghost assigned redmunds Mar 13, 2013
@njx
Copy link

njx commented Mar 13, 2013

To @redmunds

@peterflynn
Copy link
Member

Two quick thoughts:

  • We should add a unit test or two for this.
  • We should probably make the LanguageManager API accept a single string or an array, to avoid breaking backwards compatibility.

@TomMalbran
Copy link
Contributor Author

I made setLineCommentSyntax take a string or an array of strings and added a new test in LanguageManager.

I was thinking of adding some tests to EditorCommandHandlers, but it would require to change the JavaScript or CSS line comment syntax for those tests, or I should do it in PHP mode, but could be broken if PHP is removed from the core?

function _matchExpressions(string, expressions) {
var matchAny = false;
expressions.forEach(function (exp) {
matchAny = matchAny || string.match(exp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop searches for every expression no matter what. It should stop searching after a match is found.

@redmunds
Copy link
Contributor

Done with initial review.

*/
function _getLinePrefix(string, expressions, prefixes) {
var result = null;
expressions.forEach(function (exp, index) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is needed here to cover the entire array to cover cases where the line prefixes are prefixes of each other and find the best match. Peter mentioned a case in #3056 where in Clojure people use ; and ;; for line commenting, and this should cover that case.

@TomMalbran
Copy link
Contributor Author

@redmunds Thanks for the review. I fixed your comments, except the ones I commented on, so please read my replies and let me know what should be done. Thanks.

// A line is commented out if it starts with 0-N whitespace chars, then "//"
if (!line.match(lineExp) && line.match(/\S/)) {
// A line is commented out if it starts with 0-N whitespace chars, then a line comment prefix
console.log(_matchExpressions(line, lineExp));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove this call to console.log().

@redmunds
Copy link
Contributor

Uncommenting is not working correctly in a .js file (Mac 10.8). I select 3 lines and press Cmd-/ and the lines are commented with line comments. Selecting the same 3 lines an pressing Cmd-/ again, only the first line is uncommented.

@redmunds
Copy link
Contributor

Done with second review.

@TomMalbran
Copy link
Contributor Author

Oops, I left an unwanted break from the previews commit. It should work for every line now.

I added a couple of new tests for EditorCommandHandlers with a new special language. Not sure if it would need more, since those cover the basic difference and the others all still work. And added comments for Clojure since line uncommenting can uncomment any case now.

I am not sure if I like the change of Language.prototype.getLineCommentPrefix to return always an array, since now blockCommentPrefixSuffix needs to do a double check with the prefixes, null and empty, since blockComment can call it with an empty array and lineComment can call it with undefined (it wouldn't be nice to make lineComment to pass an empty array). This is because blockComment needs to do an extra check (first if block) for languages with line prefixes.

@redmunds
Copy link
Contributor

I am not sure if I like the change of Language.prototype.getLineCommentPrefix to return always an array...

Seems like it's easier to always pass empty arrays everywhere and not have to check for null anywhere. Why not just fix the places that pass null/undefined?

* Returns the prefix to use for line comments.
* @return {string} The prefix
* Returns an array of prefixes to use for line comments.
* @return {Array.<string>} The prefixes
*/
Language.prototype.getLineCommentPrefix = function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to should be changed to getLineCommentPrefixes now that there can be multiple.

@@ -449,26 +450,37 @@ define(function (require, exports, module) {
* @return {boolean} Whether line comments are supported
*/
Language.prototype.hasLineCommentSyntax = function () {
return Boolean(this._lineCommentSyntax);
return Boolean(this._lineCommentSyntax.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent of the code would be more clear to return a boolean expression than to convert an integer to Boolean type:

    return (this._lineCommentSyntax.length > 0);

@redmunds
Copy link
Contributor

Done with review.

@TomMalbran
Copy link
Contributor Author

@redmunds all done.

@redmunds
Copy link
Contributor

Looks good. Merging.

redmunds added a commit that referenced this pull request Mar 16, 2013
Adding support for multiple lineComment prefixes
@redmunds redmunds merged commit e2ab2f2 into adobe:master Mar 16, 2013
@TomMalbran
Copy link
Contributor Author

Great. Thanks :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants