2

I client of mine asked to have a lot of comments in the code - including css files.

So I thought about using CSScomb to reorganize the css code and add comments automatically. It doesn't make sense but why not.

So the idea would be to change the config file which states:

[
    "font",
    "font-family"
],
[
    "padding",
    "margin"
]
...

but get an output that writes:

/* FONT STYLE */
font: ....;
font-family: ....;

Any ideas ?

YannickHelmut
  • 555
  • 1
  • 7
  • 20

1 Answers1

1

DIY Group Comments

You can add this functionality yourself by editing the sort-order.js file in the CSScomb package.

  1. With Sublime Text open, from the menu choose Preferences > Browse Packages…; This will open the Packages folder.
  2. From the Packages folder, navigate to CSScomb/node_modules/csscomb/lib/options/sort-order.js.
  3. Create a copy of this file for retrieval in case you want to revert your changes. I gave my copy a name of sort-order-original.js.
    Create a copy of this file in a different directory for retrieval in case you want to revert your changes. I moved my copy up one level into a new directory ../originals/options/sort-order.js.
    Note: If you simply rename the copy in the existing directory it may get included and run by the module; so, it is safest to just move it into a new location.
  4. Open sort-order.js in Sublime Text for editing.
  5. Consult this diff for the necessary changes to be made.
    Consult this diff for the necessary changes to be made. (This latest version adds new logic to prevent the group comments from being duplicated when running CSScomb multiple times.)
  6. If you feel comfortable with these changes, copy & paste them in their entirety to replace the contents of sort-order.js. You may choose to edit further to suit your needs. Save.
    Note: Essentially, these changes are extending each sorted object with an additional property that contains a CSS comment optionally provided by you in your User Settings. I will show you where to add the comments in the next step.
  7. Now, you're ready to add comments by group. From the Sublime Text menu, choose Preferences > Package Settings > CSScomb > Settings – User. If you haven't already configured your own settings, you can get started by copying the contents of the Settings – Default into Settings – User.
  8. In the user settings file, find the "sort-order" property. It is either an array of property names or an array of arrays of property names. By default, CSScomb will add a blank line between the nested array groups, but we've modified the file that does this.
  9. You may now optionally add a comment as the first property of any nested array. The sort-order.js file will detect this and output it at the top of the group. If no comment is defined, the default blank line is output instead.

Example User Settings:

"sort-order": [
    [
        "/* LAYOUT */",
        "position",
        "z-index",
        "top",
        "right",
        "bottom",
        "left"
    ],
    [
        "/* DISPLAY */",
        "display",
        …
        "flex-align"
    ],
    [
        "/* TYPOGRAPHY */",
        "font",
        …
        "line-height"
    ],
    [
        …
    ]
]

Before running CSScomb:

.selector {
    font-family: Arial;
    line-height: 1;
    position: relative;
    display: block;
    background-color: red;
}

After running CSScomb:

.selector {

    /* LAYOUT */
    position: relative;

    /* DISPLAY */
    display: block;

    /* TYPOGRAPHY */
    font-family: Arial;
    line-height: 1;

    background-color: red;
}
gfullam
  • 11,531
  • 5
  • 50
  • 64
  • Very nice, thank you so much ! One last problem: if I launch it twice... it will add these comments a second time... any solution for that ? – YannickHelmut Nov 13 '15 at 10:08
  • Not in this implementation. But you could play around with `sort-order.js` or another lib file to see about adding some sort of logic check for duplicate comments. – gfullam Nov 13 '15 at 12:24
  • Ok I've been trying to find a self-made solution for 2 days and I'm stuck. Meaning that your solution can only be applied one time at the end of the compilation - so my whole idea (not your solution) becomes useless... – YannickHelmut Nov 15 '15 at 20:56
  • I'll post a solution for you. ;) stand by. – gfullam Nov 15 '15 at 23:16
  • @YannickHelmut I have updated my answer. See the latest diff which adds new logic to prevent the group comments from being duplicated when running CSScomb multiple times. Enjoy! – gfullam Nov 16 '15 at 21:56
  • You may hate me. But I can't get rid of those empty lines between the groups with you new code (worked on the previous one but now I'm lost again...) Thank you so much for your solution, I think I'll use it on all projects, makes code more readable ! – YannickHelmut Nov 17 '15 at 15:18
  • The empty line is hard coded into `sort-order.js`, it is the default group delimiter even if you don't use my modified version. You could create a setting for it on User Settings that you read in instead of the hard coded line break. Maybe I'll do that at some point. – gfullam Nov 17 '15 at 16:40
  • If you want to remove the hard coded empty line inserted before each group comment, comment out or remove line 372: `sc0.unshift(sc0[sc0.length - 1]);` As mentioned in the previous comment, you could insert a variable delimiter here with some additional work. – gfullam Nov 17 '15 at 18:30