2323
2424package processing .app .tools ;
2525
26- import java .awt .*;
27- import java .awt .datatransfer .*;
26+ import org .fife .ui .rsyntaxtextarea .Token ;
27+ import processing .app .Editor ;
28+ import processing .app .syntax .SketchTextArea ;
2829
2930import javax .swing .text .BadLocationException ;
3031import javax .swing .text .Segment ;
31-
32- import org .fife .ui .rsyntaxtextarea .Token ;
33-
34- import processing .app .*;
35- import processing .app .syntax .*;
32+ import java .awt .*;
33+ import java .awt .datatransfer .Clipboard ;
34+ import java .awt .datatransfer .StringSelection ;
3635
3736/**
3837 * Format for Discourse Tool
39- * <p/ >
38+ * <p>
4039 * Original code by <A HREF="http://usuarios.iponet.es/imoreta">owd</A>.
4140 * Revised and updated for revision 0108 by Ben Fry (10 March 2006).
4241 * This code may later be moved to its own 'Tool' plugin, but is included
4342 * with release 0108+ while features for the "Tools" menu are in testing.
44- * <p/ >
43+ * <p>
4544 * Updated for 0122 to simply copy the code directly to the clipboard,
4645 * rather than opening a new window.
47- * <p/ >
46+ * <p>
4847 * Updated for 0144 to only format the selected lines.
49- * <p/ >
48+ * <p>
5049 * Updated for 1.5.8 - Simplification, using RSyntaxTextArea TokenImpl formatter (08 dec 2014 - Ricardo JL Rufino)
51- * <p/ >
50+ * <p>
5251 * Notes from the original source:
5352 * Discourse.java This is a dirty-mix source.
5453 * NOTE that: No macs and no keyboard. Unreliable source.
5756 */
5857public class DiscourseFormat {
5958
60- private Editor editor ;
61- // JTextArea of the actual Editor
62- private SketchTextArea textarea ;
63- private boolean html ;
64-
59+ private final Editor editor ;
60+ private final SketchTextArea textarea ;
61+ private final boolean html ;
6562
6663 /**
6764 * Creates a new window with the formated (YaBB tags) sketchcode
@@ -74,12 +71,10 @@ public DiscourseFormat(Editor editor, boolean html) {
7471 this .html = html ;
7572 }
7673
77-
7874 /**
7975 * Format and render sketch code.
8076 */
8177 public void show () {
82- // [code] tag cancels other tags, using [quote]
8378 StringBuilder cf = new StringBuilder (html ? "<pre>\n " : "[code]\n " );
8479
8580 int selStart = textarea .getSelectionStart ();
@@ -105,6 +100,7 @@ public void show() {
105100 stopLine --;
106101 }
107102 } catch (BadLocationException e ) {
103+ // ignore
108104 }
109105 }
110106
@@ -117,22 +113,21 @@ public void show() {
117113
118114 StringSelection formatted = new StringSelection (cf .toString ());
119115 Clipboard clipboard = Toolkit .getDefaultToolkit ().getSystemClipboard ();
120- clipboard .setContents (formatted , new ClipboardOwner () {
121- public void lostOwnership (Clipboard clipboard , Transferable contents ) {
122- // i don't care about ownership
123- }
124- });
116+ clipboard .setContents (formatted , (clipboard1 , contents ) -> {
117+ // i don't care about ownership
118+ });
125119 Clipboard unixclipboard = Toolkit .getDefaultToolkit ().getSystemSelection ();
126120 if (unixclipboard != null ) unixclipboard .setContents (formatted , null );
127121
128122 editor .statusNotice ("Code formatted for " + (html ? "HTML" : "the Arduino forum" ) + " has been copied to the clipboard." );
129123 }
130124
131125 /**
132- * Append a char to a StringBuilder while escaping for proper display in HTML.
133- * @param c input char to escape
134- * @param buffer StringBuilder to append html-safe version of c to.
135- */
126+ * Append a char to a StringBuilder while escaping for proper display in HTML.
127+ *
128+ * @param c input char to escape
129+ * @param buffer StringBuilder to append html-safe version of c to.
130+ */
136131 private void appendToHTML (char c , StringBuilder buffer ) {
137132 if (!html ) {
138133 buffer .append (c );
@@ -149,45 +144,32 @@ private void appendToHTML(char c, StringBuilder buffer) {
149144 }
150145 }
151146
152- // A terrible headache...
153- public void appendFormattedLine (StringBuilder cf , int line ) {
147+ private void appendFormattedLine (StringBuilder buffer , int line ) {
154148 Segment segment = new Segment ();
155149
156- // get line text from parent text area
157150 textarea .getTextLine (line , segment );
158-
159- char [] segmentArray = segment .array ;
160- int segmentOffset = segment .offset ;
161- int segmentCount = segment .count ;
162- // int width = 0;
163151
164152 if (!html ) {
153+ char [] segmentArray = segment .array ;
154+ int segmentOffset = segment .offset ;
155+ int segmentCount = segment .count ;
156+
165157 for (int j = 0 ; j < segmentCount ; j ++) {
166158 char c = segmentArray [j + segmentOffset ];
167- appendToHTML (c , cf );
168- // int charWidth;
169- // if (c == '\t') {
170- // charWidth = (int) painter.nextTabStop(width, j) - width;
171- // } else {
172- // charWidth = fm.charWidth(c);
173- // }
174- // width += charWidth;
159+ appendToHTML (c , buffer );
175160 }
161+ return ;
162+ }
176163
177- } else {
178-
179- Token tokenList = textarea .getTokenListForLine (line );
180-
181- while (tokenList != null ){
182- if (tokenList .getType () == Token .NULL ){
183- cf .append ('\n' );
184- }else if (tokenList .isPaintable ()){
185- tokenList .appendHTMLRepresentation (cf , textarea , false );
186- }
187-
188- tokenList = tokenList .getNextToken ();
164+ Token tokenList = textarea .getTokenListForLine (line );
165+
166+ while (tokenList != null ) {
167+ if (tokenList .getType () != Token .NULL ) {
168+ tokenList .appendHTMLRepresentation (buffer , textarea , false );
189169 }
190-
170+ tokenList = tokenList . getNextToken ();
191171 }
172+
173+ buffer .append ('\n' );
192174 }
193175}
0 commit comments