Home Download Buy Blog Forum Support

Simple style guideline plugin

Simple style guideline plugin

Postby kallisti5 on Thu Mar 08, 2012 3:58 pm

Looking to make a simple style guideline plugin

[*]spaces at end of line should be highlighted
[*]spaces at beginning of lines (instead of tabs) should be highlighted
[*]lines over 80 characters long should be highlighted
[*]single newlines (vs two newlines) after '}' at column 0 should be highlighted.

Any thoughts on how to accomplish these? I'm thinking a regex check run on each line should be enough... just have no idea how to use plugin system.
kallisti5
 
Posts: 4
Joined: Thu Mar 08, 2012 3:55 pm

Re: Simple style guideline plugin

Postby facelessuser on Thu Mar 08, 2012 5:14 pm

If I understand correctly, you could just install RegReplace and add the following replacements to the reg_replace.sublime-settings file (copy it to Package/User before modifying):

Code: Select all
         "remove_trailing_spaces": {
            "find": "[ \\t]+$",
            "replace": "",
            "greedy": true,
            "case": true
         },
         "remove_leading_spaces": {
            "find": "^[ ]+",
            "replace": "",
            "greedy": true
         },
         "80_chars_limit": {
            "find": "^([^\\r\\n]{80})([^\\r\\n]+)$",
            "replace": "\\1\\n\\2",
            "greedy": true
         },
         "remove_excessive_newlines_after_curly_end_column_0": {
            "find": "^(\\}[^\\r\\n]*)((\\r?\\n){2,})",
            "replace": "\\1\\n",
            "greedy": true
         },


And then add this command to your Packages/User/Default.sublime-commands file:

Code: Select all
   {
      "caption": "Check Format: Evaluate Code Format",
      "command": "reg_replace",
      "args": {
         "replacements": ["80_chars_limit", "remove_trailing_spaces", "remove_leading_spaces", "remove_excessive_newlines_after_curly_end_column_0"],
         "find_only": true
      }
   },


It should highlight all instances of your rules, and allow you replace them with the defined replacements. Granted the regex should find all of the instances, but the replacements are kind of limited, like if you wanted to split a line greater than 80 chars and indent it the same as the first line, you are tough out of luck without writing a custom plugin to do this.

edit:
If you want to break a line that is greater than 80*2 into small pieces, you can enable multi-pass (it will sweep the file executing the regex until it can pass the file without finding anymore instances, or it hits its internal limit of sweeps)

Code: Select all
   {
      "caption": "Check Format: Evaluate Code Format",
      "command": "reg_replace",
      "args": {
         "replacements": ["80_chars_limit", "remove_trailing_spaces", "remove_leading_spaces", "remove_excessive_newlines_after_curly_end_column_0"],
         "find_only": true,
         "multi_pass": true
      }
   },


edit:
Thinking about this more, I may allow for custom highlights on save in RegReplace in the future. Currently it allows replacements on save, but a highlight only feature might be nice too; one that cleans up after itself.
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests