23
23
24
24
package processing .app .tools ;
25
25
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 ;
28
29
29
30
import javax .swing .text .BadLocationException ;
30
31
import 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 ;
36
35
37
36
/**
38
37
* Format for Discourse Tool
39
- * <p/ >
38
+ * <p>
40
39
* Original code by <A HREF="http://usuarios.iponet.es/imoreta">owd</A>.
41
40
* Revised and updated for revision 0108 by Ben Fry (10 March 2006).
42
41
* This code may later be moved to its own 'Tool' plugin, but is included
43
42
* with release 0108+ while features for the "Tools" menu are in testing.
44
- * <p/ >
43
+ * <p>
45
44
* Updated for 0122 to simply copy the code directly to the clipboard,
46
45
* rather than opening a new window.
47
- * <p/ >
46
+ * <p>
48
47
* Updated for 0144 to only format the selected lines.
49
- * <p/ >
48
+ * <p>
50
49
* Updated for 1.5.8 - Simplification, using RSyntaxTextArea TokenImpl formatter (08 dec 2014 - Ricardo JL Rufino)
51
- * <p/ >
50
+ * <p>
52
51
* Notes from the original source:
53
52
* Discourse.java This is a dirty-mix source.
54
53
* NOTE that: No macs and no keyboard. Unreliable source.
57
56
*/
58
57
public class DiscourseFormat {
59
58
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 ;
65
62
66
63
/**
67
64
* Creates a new window with the formated (YaBB tags) sketchcode
@@ -74,12 +71,10 @@ public DiscourseFormat(Editor editor, boolean html) {
74
71
this .html = html ;
75
72
}
76
73
77
-
78
74
/**
79
75
* Format and render sketch code.
80
76
*/
81
77
public void show () {
82
- // [code] tag cancels other tags, using [quote]
83
78
StringBuilder cf = new StringBuilder (html ? "<pre>\n " : "[code]\n " );
84
79
85
80
int selStart = textarea .getSelectionStart ();
@@ -105,6 +100,7 @@ public void show() {
105
100
stopLine --;
106
101
}
107
102
} catch (BadLocationException e ) {
103
+ // ignore
108
104
}
109
105
}
110
106
@@ -117,22 +113,21 @@ public void show() {
117
113
118
114
StringSelection formatted = new StringSelection (cf .toString ());
119
115
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
+ });
125
119
Clipboard unixclipboard = Toolkit .getDefaultToolkit ().getSystemSelection ();
126
120
if (unixclipboard != null ) unixclipboard .setContents (formatted , null );
127
121
128
122
editor .statusNotice ("Code formatted for " + (html ? "HTML" : "the Arduino forum" ) + " has been copied to the clipboard." );
129
123
}
130
124
131
125
/**
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
+ */
136
131
private void appendToHTML (char c , StringBuilder buffer ) {
137
132
if (!html ) {
138
133
buffer .append (c );
@@ -149,45 +144,32 @@ private void appendToHTML(char c, StringBuilder buffer) {
149
144
}
150
145
}
151
146
152
- // A terrible headache...
153
- public void appendFormattedLine (StringBuilder cf , int line ) {
147
+ private void appendFormattedLine (StringBuilder buffer , int line ) {
154
148
Segment segment = new Segment ();
155
149
156
- // get line text from parent text area
157
150
textarea .getTextLine (line , segment );
158
-
159
- char [] segmentArray = segment .array ;
160
- int segmentOffset = segment .offset ;
161
- int segmentCount = segment .count ;
162
- // int width = 0;
163
151
164
152
if (!html ) {
153
+ char [] segmentArray = segment .array ;
154
+ int segmentOffset = segment .offset ;
155
+ int segmentCount = segment .count ;
156
+
165
157
for (int j = 0 ; j < segmentCount ; j ++) {
166
158
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 );
175
160
}
161
+ return ;
162
+ }
176
163
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 );
189
169
}
190
-
170
+ tokenList = tokenList . getNextToken ();
191
171
}
172
+
173
+ buffer .append ('\n' );
192
174
}
193
175
}
0 commit comments