/plugins/Beauty/tags/beauty-0.5.0/src/docs/index.html

# · HTML · 347 lines · 346 code · 1 blank · 0 comment · 0 complexity · f2b3baa962875328cd9dc62bce96b094 MD5 · raw file

  1. <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>
  5. Beauty Plugin
  6. </title>
  7. </head>
  8. <body bgcolor="#FFFFFF">
  9. <table summary="Header" bgcolor="#CCCCFF" cellspacing="0" border="0" width="100%" cols="2">
  10. <tr width="100%">
  11. <td valign="TOP">
  12. <strong>
  13. <font size="+2">Beauty</font>
  14. </strong>
  15. </td>
  16. <td valign="TOP" align="RIGHT">
  17. <font size="-1"><p><strong>Version 0.5.0 (@@tstamp@@)</strong> <p>Dale Anson</font>
  18. </td>
  19. </tr>
  20. </table>
  21. <table summary="Introduction" border="0" cellspacing="0" cellspacing="0" width="100%">
  22. <tr align="CENTER" width="100%">
  23. <td bgcolor="#7FB2FF" width="100%">
  24. <strong>
  25. <font color="#FFFFFF" size="+1"><a name="intro">Introduction</a></font>
  26. </strong>
  27. </td>
  28. </tr>
  29. </table>
  30. <p>
  31. The Beauty plugin is a general framework for code beautifiers and provides several built-in beautifiers. While there are several existing plugins for beautifying/formatting various source file formats, this plugin aims to consolidate those formatters into a standard API so that beautifying can be performed in a general way, similar to how the SideKick plugin provides a general framework for code browsers.
  32. <p>
  33. There are several beautifiers/formatters included:
  34. <ul>
  35. <li>HTML: works very well, even when presented with poorly formatted html. Delegates to the CSS beautifier to format style blocks within an HTML file. Delegates to the included custom javascript beautifier to format script blocks within an HTML file. Delegates to the included custom java beautifier to format java scriptlet blocks within an HTML file. This does a pretty good job on JSP files, although it will choke on a few things, like EL code within style or script blocks.</li>
  36. <li>JSP: this is new for Beauty 0.5 and takes care of the shortcomings of the HTML beautifier for JSP files. This beautifier handles cleaning up jsp files, including script and style blocks and will even beautify java code withing scriptlet blocks.
  37. <li>CSS: works very well. The JSP beautifier delegates to this beautifier to clean up style blocks within JSP files.</li>
  38. <li>Java: works very well, formats strictly to Sun's formatting standard only. Needs work on doing line wrapping per Sun's guidelines.</li>
  39. <li>JavaCC: needs work, formats to a combination of Sun's Java formatting standard and my own style for javacc files.</li>
  40. <li>JSON: works very well. JSON files are usually machine-generated in a single long line, so it is nice to be able to format them quickly to a more human-readable format.</li>
  41. <li>Beanshell: Works very well. It generally follows Sun's formatting standard for java files, although beanshell code is slightly different from java code, so there are some minor differences.</li>
  42. </ul>
  43. <p>
  44. Two other plugins have been updated to support the Beauty framework:
  45. <ul>
  46. <li>AStyle: a nice, configurable beautifier for Java, C, and C++ code.
  47. <li>XmlIndenter: part of the XML plugin, this beautifier works on all xml-based files. Use this for well-formed xml dialects like docbook files, Ant files, and so on, use the HTML beautifier for those xml-like files that are not necessarily well-formed.
  48. </ul>
  49. <p>
  50. With the 0.4 release, Beauty has the ability to <a href="#custom">configure a custom beautifier</a> per mode, which allows padding of various items, clean up of extra blank lines, and the ability to use the jEdit indenter. I've included custom beautifiers for
  51. <ul>
  52. <li>Ada</li>
  53. <li>Java</li>
  54. <li>Javascript</li>
  55. <li>CSS</li>
  56. </ul>
  57. <p>
  58. (Note: the custom CSS beautifier is still included, but the built-in CSS beautifier introduced in Beauty 0.5 does a much better job.)
  59. <p>
  60. (Another note: The built in Java beautifier does a much better job than the custom Java beautifier, but the built in Java beautifier requires grammatically correct Java code, where the custom beautifier does not. The custom beautifier is used by the JSP beautifier for JSP pages to format Java scriptlet blocks.)
  61. <p>
  62. Making custom beautifiers is very easy to do, and you are encouraged to create and share beautifiers for any language that jEdit supports.
  63. <p>
  64. All other modes can default to use the built-in jEdit indenter, which works pretty well for most modes. To turn on this option, check the box in the Beauty options.
  65. <p>
  66. <table summary="Configuration" border="0" cellspacing="0" cellspacing="0" width="100%">
  67. <tr align="CENTER" width="100%">
  68. <td bgcolor="#7FB2FF" width="100%">
  69. <strong>
  70. <font color="#FFFFFF" size="+1"><a name="usage">Usage</a></font>
  71. </strong>
  72. </td>
  73. </tr>
  74. </table>
  75. <p>
  76. To beautify a buffer, go to Plugins - Beauty - Beautify Buffer. This action is mappable as a keyboard shortcut. Go to Utilities - Global Options - Shortcuts - Plugin: Beauty. I've mapped Ctrl+B for this action.
  77. <p>
  78. <table summary="Configuration" border="0" cellspacing="0" cellspacing="0" width="100%">
  79. <tr align="CENTER" width="100%">
  80. <td bgcolor="#7FB2FF" width="100%">
  81. <strong>
  82. <font color="#FFFFFF" size="+1"><a name="config">Configuration</a></font>
  83. </strong>
  84. </td>
  85. </tr>
  86. </table>
  87. <p>
  88. Configuration depends on the specific beautifier. Generally, beautifiers will honor the settings for the buffer and/or mode. So, if you're editing an html file in html mode, then the html beautifier will be used to format the file, and the html beautifier will use the right line enders and tab settings. These settings are controlled by the settings for the buffer and the mode:
  89. <p>
  90. <ul>
  91. <li>Line separators: as set for the current buffer.
  92. <li>Tab width: as set for the current buffer.
  93. <li>Indent width: as set for the current buffer.
  94. <li>Soft tabs: in this initial release, all tabs are converted to soft tabs. This should be fixed in the next release to honor the soft tab setting for the buffer.
  95. </ul>
  96. <p>
  97. To set which beautifier to use per mode, use the Beauty option settings under the Plugins - Plugin Options menu.
  98. <p>
  99. <strong>CSS Beautifier</strong>
  100. <p>
  101. The CSS beautifier introduced in Beauty 0.5 has some additional configuration options. See the plugin option pane for details.
  102. <p>
  103. <strong>JSP Beautifier</strong>
  104. <p>
  105. In addition to the buffer settings mentioned above, the JSP beautifier has a few options that can be set in the plugin options. Additionally, some configuration of java scriptlets, style blocks, and javascript blocks is possible.
  106. <p>
  107. This beautifier delegates to the CSS beautifier for style blocks, so any configuration you make for the CSS beautifier applies to the style blocks within a JSP file.
  108. <p>
  109. Similarly, this beautifier delegates to the custom beautifiers for java and javascript blocks within a JSP file, so you can make adjustments via the custom beautifier dialog in the plugin options.
  110. <p>
  111. Just to clarify the Java beautifiers: Assuming you have the AStyle plugin installed, you have 3 choices for Java beautification:
  112. <ul>
  113. <li><code>java:custom</code></li>
  114. <li><code>java:astyle</code></li>
  115. <li><code>java:beauty</code></li>
  116. </ul>
  117. For regular Java files, you'll want to use either <code>java:astyle</code> or <code>java:beauty</code>. The <code>java:custom</code> beautifier is the one used by the JSP beautifier. It does not beautify as well as either <code>java:astyle</code> or <code>java:beauty</code> but will accept the Java code commonly found in JSP scriptlets where the other two won't.
  118. <p>
  119. <strong>Others</strong>
  120. <p>
  121. It is quite possible that new beautifiers are introduced as jEdit plugins for specific languages. Please consult the documentation for those beautifiers for any settings that might be possible.
  122. <p>
  123. <table summary="Custom Beautifier" border="0" cellspacing="0" cellspacing="0" width="100%">
  124. <tr align="CENTER" width="100%">
  125. <td bgcolor="#7FB2FF" width="100%">
  126. <strong>
  127. <font color="#FFFFFF" size="+1"><a name="custom">Custom Beautifier</a></font>
  128. </strong>
  129. </td>
  130. </tr>
  131. </table>
  132. <p>
  133. <b><i>Custom beautifiers can be shared. If you create one for a mode, post it on the jEdit mailing lists or add it to the jEdit plugin feature tracker or email it to me at danson@grafidog.com, and I'll include it in the next release of Beauty.</i></b>
  134. <p>
  135. To post the custom beautifier, just copy the appropriate properties file from <code>$USER_HOME/.jedit/plugins/beauty.BeautyPlugin</code>. To locate your <code>$USER_HOME</code>, in jEdit go to Utilities - Beanshell - Evaluate Beanshell Expression..., then enter
  136. <p>
  137. <code>System.getProperty("user.home")</code>
  138. <p>
  139. <p>
  140. To set up a custom beautifier:
  141. <ol>
  142. <li>
  143. Go to Plugins - Plugin Options - Beauty - Custom Beautifier
  144. </li>
  145. <li>
  146. Select the mode to configure. For your convenience, the mode will initially be set to the mode of the current buffer.
  147. </li>
  148. <li>
  149. Configure the beautifier, click "Apply".
  150. </li>
  151. <li>
  152. Go to Plugins - Plugin Options - Beauty - Modes and assign your newly created beautifier. You can assign the same beautifier to more than one mode, so you could create a beautifier for pl-sql and assign the same beautifier to both the sql-loader mode and the transact-sql mode.
  153. </li>
  154. </ol>
  155. <p>
  156. The configuration should need only a little explanation:
  157. <p>
  158. There are 2 tabs, "Padding" and "Indenting".
  159. <p>
  160. <b>Token Padding</b><br>
  161. This beautifier works by using jEdit syntax highlighting engine to tokenize the buffer, so the same tokens that are identified by the syntax highlighting are used by the custom beautifier. For example, "+" may be defined in the mode file as an operator. Checking the "before" and "after" for "Pad operators" will cause the beautifier to make sure that there is a space before and after all "+" characters. Functions, digits, operators, and keywords are defined in the mode file, so the quality of this tokenization really depends on the quality of the mode file.
  162. <p>
  163. Padding can be added before and/or after these tokens:<br>
  164. FUNCTION<br>
  165. DIGIT<br>
  166. KEYWORD1<br>
  167. KEYWORD2<br>
  168. KEYWORD3<br>
  169. KEYWORD4<br>
  170. OPERATOR
  171. <p>
  172. Labels are also defined in the mode file, and you may want to put labels on a separate line. This may or may not work well, again, it depends on the quality of the mode file. For example, in the Ada language, a label is defined as <tt>&lt;&lt;</tt>sometext<tt>&gt;&gt;</tt>, but in the Ada mode file, both <tt>&lt;&lt;</tt> and <tt>&gt;&gt;</tt> are defined as labels. This means that if you elect to put a label on a separate line, you'll end up with
  173. <br>
  174. <pre>
  175. &lt;&lt;sometext
  176. &gt;&gt;
  177. </pre>
  178. <br>
  179. In this case, either the mode file needs to be fixed, or use the "Insert line separator before" and "Insert line separator after" text fields, and enter <tt>&lt;&lt;</tt> in the before and <tt>&gt;&gt;</tt> in the after.
  180. <p>
  181. <b>Character padding</b><br>
  182. Four text fields are for setting up padding of individual characters. Just enter the characters, don't separate with a comma. If you do separate with a comma, then the padding will also be applied to any commas in your file.
  183. <p>
  184. <ul>
  185. <li>Pad before these characters</li>
  186. <li>Pad after these characters</li>
  187. <li>Don't pad before these characters</li>
  188. <li>Don't pad after these characters</li>
  189. </ul>
  190. <p>
  191. These are applied by the beautifier in this order, so if you say to pad before ":" and then say don't pad before ":", the don't pad wins.
  192. For example, you might use these when you want a space following each <tt>(</tt> and before each <tt>)</tt>.
  193. <p>
  194. The "don't pad" characters are handy for modes like javascript that define ".", ",", and ";" as operators. Adding these characters to the don't pad text fields will cause the beautifier to remove spaces before and after these characters even though they may be padded as operators.
  195. <p>
  196. <b>Line insertion</b><br>
  197. The next two text fields are for entering <b>regular expressions</b> to describe items that should have a line separator inserted either before and/or after:
  198. <p>
  199. <ul>
  200. <li>Insert line separators before these strings</li>
  201. <li>Insert line separators after these strings</li>
  202. </ul>
  203. <p>
  204. <b>Endless trouble can happen here, so be careful!</b>
  205. The "Insert line separators before" and "Insert line separators after" text fields can take a comma separated list of strings. Do not put spaces in this string unless you really mean it. <b>These strings are used as regular expressions</b>, but note that this is a comma separated list, so if your regex contains commas, you must escape the commas or things probably won't work as you'd want. <b>Warning: be sure to escape special regular expression characters within your regular expressions.</b>For example, if you want to insert a line separator before the start of a C-style comment, /*, enter "/\*" rather than "/*". Round, curly, and square brackets also need to be escaped. Other possible characters that might need escaped are ".", "?", "*", and "+". There may be more. Here's a good example: Suppose you are wanting to insert line separators in a json file after any of {, [, comma, ], and }. In the "Insert line separators after these strings" box, enter
  206. <code>
  207. \{,\[,\,,\],\}
  208. </code>
  209. <p>
  210. You can elect to have multiple blank lines collapsed into a single blank line.
  211. <p>
  212. The custom beautifier applies the configuration in order from top to bottom (mostly), so, for example, operators are padded before applying the 'don't pad before' configuration. Padding is only applied if there is no whitespace before or after the item to be padded, so additional whitespace is not added unnecessarily. Similarly, if you want to insert a line break before or after an item and there already is a line separator, no additional blank lines will be added. The exception to the "top to bottom" rule is padding of keywords. Keywords are padded last. This is to prevent the other pad/don't pad rules from removing padding from keywords. For example, if you have
  213. <p>
  214. <code>
  215. for (int i = 0; i &lt; a.length(); i++)
  216. </code>
  217. <p>
  218. You can set to not pad before <tt>(</tt> so that <code>a.length()</code> looks good and still have the space between <code>for (</code>.
  219. <p>
  220. <b>Indenting</b><br>
  221. Indenting is applied by the beautifier after padding. On the "Indenting" tab, you can have the custom beautifier use the jEdit indenter to do the line indentation. Each of the text fields on this tab correspond exactly to the properties of the same names used by the jEdit mode files. They are filled in initially with the values from the jEdit mode file for the mode you are working with. Unfortunately, the documentation for these in the jEdit help file is not very good, so it may take some experimentation to make these work as you'd like.
  222. <p>
  223. These are the supported indenting properties:
  224. <p>
  225. These take a list of characters:
  226. <ul>
  227. <li>Indent open brackets</li>
  228. <li>Indent close brackets</li>
  229. <li>Unaligned open brackets</li>
  230. <li>Unaligned close brackets</li>
  231. <li>Electric keys</li>
  232. </ul>
  233. <p>
  234. These take regular expressions:
  235. <p>
  236. <ul>
  237. <li>Indent next line</li>
  238. <li>Unindent this line</li>
  239. </ul>
  240. <p>
  241. These are true/false:
  242. <p>
  243. <ul>
  244. <li>Line up closing brackets</li>
  245. <li>Double bracket indent</li>
  246. </ul>
  247. <p>
  248. <table summary="For Developers" border="0" cellspacing="0" cellspacing="0" width="100%">
  249. <tr align="CENTER" width="100%">
  250. <td bgcolor="#7FB2FF" width="100%">
  251. <strong>
  252. <font color="#FFFFFF" size="+1"><a name="intro">For Developers</a></font>
  253. </strong>
  254. </td>
  255. </tr>
  256. </table>
  257. <p>
  258. While the ability to create a custom beautifier from within the Beauty plugin is fairly good, even better are beautifiers designed specifically with a strong understanding of the language to be beautified. This is why Beauty can delegate to the AStyle plugin and the XML plugin for beautification of Java, C, C++, XML, and HTML files.
  259. <p>
  260. Adding a new beautifier/formatter is quite simple:
  261. <ol>
  262. <li>Write a formatter that implements beauty.beautifiers.Beautifier. You'll need to implement only the
  263. <code>
  264. String beautify(String text)
  265. </code>
  266. method. Your formatter will be passed the complete contents of a buffer in the
  267. <code>
  268. text
  269. </code>
  270. parameter, and should return formatted text. The buffer contents will be replaced with this returned text.
  271. <li>Create a simple plugin that contains this formatter.
  272. <li>Add a
  273. <code>
  274. services.xml
  275. </code>
  276. file to your plugin like this:
  277. <p>
  278. <code>
  279. &lt;?xml version="1.0"?&gt;
  280. <br>
  281. &lt;!DOCTYPE SERVICES SYSTEM "services.dtd"&gt;
  282. <br>
  283. <br>
  284. &lt;SERVICES&gt;
  285. <br>
  286. &nbsp;&nbsp;&lt;SERVICE CLASS="beauty.beautifiers.Beautifier" NAME="lispy"&gt;
  287. <br>
  288. &nbsp;&nbsp;&nbsp;&nbsp;new lisp.lispy.Beautify();
  289. <br>
  290. &nbsp;&nbsp;&lt;/SERVICE&gt;
  291. <br>
  292. &lt;/SERVICES&gt;
  293. </code>
  294. <p>
  295. For
  296. <code>
  297. NAME
  298. </code>
  299. , either use the name of the editing mode that the beautifier supports, or a unique name for your plugin. The beanshell code must return an object that can be cast to
  300. <code>
  301. beauty.beautifiers.Beautifier
  302. </code>
  303. .
  304. <li>For each editing mode that your beautifier supports, add a line to your plugin properties file like this:
  305. <p>
  306. mode.MODENAME.beauty.beautifier.NAME
  307. <p>
  308. where
  309. <code>
  310. MODENAME
  311. </code>
  312. is the name of an editing mode that your beautifier supports, and
  313. <code>
  314. NAME
  315. </code>
  316. is the same name used in the services.xml file. So continuing the example, you might have:
  317. <p>
  318. &nbsp;&nbsp;&nbsp;
  319. <code>
  320. mode.lisp.beauty.beautifiers.lispy
  321. </code>
  322. <p>
  323. for your beautifier that supports the Lisp language.
  324. <li>Release your plugin.
  325. </ol>
  326. <table summary="Credits" border="0" cellspacing="0" cellspacing="0" width="100%">
  327. <tr align="CENTER" width="100%">
  328. <td bgcolor="#7FB2FF" width="100%">
  329. <strong>
  330. <font color="#FFFFFF" size="+1"><a name="intro">Credits</a></font>
  331. </strong>
  332. </td>
  333. </tr>
  334. </table>
  335. <p>
  336. I'd like to give credit to these people whose work I built off of:
  337. <p>
  338. <ul>
  339. <li>Sreenivasa Viswanadha for the Java 1.5 and javacc grammars (javacc.dev.java.net)</li>
  340. <li>Brian Goetz for the HTML grammar</li>
  341. <li>Patrick Niemeyer for the beanshell grammar (www.beanshell.org)</li>
  342. <li>Pieter from the PMD project for the jsp grammar (www.pmd.org)</li>
  343. <li>The original CSS grammar came from W3C (www.w3c.org)</li>
  344. </ul>
  345. </body>
  346. </html>