forked from facchinm/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscourseFormat.java
More file actions
175 lines (148 loc) · 5.48 KB
/
Copy pathDiscourseFormat.java
File metadata and controls
175 lines (148 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2005-06 Ignacio Manuel Gonzalez Moreta.
Copyright (c) 2006-08 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.tools;
import org.fife.ui.rsyntaxtextarea.Token;
import processing.app.Editor;
import processing.app.syntax.SketchTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.Segment;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
/**
* Format for Discourse Tool
* <p>
* Original code by <A HREF="http://usuarios.iponet.es/imoreta">owd</A>.
* Revised and updated for revision 0108 by Ben Fry (10 March 2006).
* This code may later be moved to its own 'Tool' plugin, but is included
* with release 0108+ while features for the "Tools" menu are in testing.
* <p>
* Updated for 0122 to simply copy the code directly to the clipboard,
* rather than opening a new window.
* <p>
* Updated for 0144 to only format the selected lines.
* <p>
* Updated for 1.5.8 - Simplification, using RSyntaxTextArea TokenImpl formatter (08 dec 2014 - Ricardo JL Rufino)
* <p>
* Notes from the original source:
* Discourse.java This is a dirty-mix source.
* NOTE that: No macs and no keyboard. Unreliable source.
* Only format processing code using fontMetrics.
* It works under my windows XP + PentiumIV + Processing 0091.
*/
public class DiscourseFormat {
private final Editor editor;
private final SketchTextArea textarea;
private final boolean html;
/**
* Creates a new window with the formated (YaBB tags) sketchcode
* from the actual Processing Tab ready to send to the processing discourse
* web (copy & paste)
*/
public DiscourseFormat(Editor editor, boolean html) {
this.editor = editor;
this.textarea = editor.getTextArea();
this.html = html;
}
/**
* Format and render sketch code.
*/
public void show() {
StringBuilder cf = new StringBuilder(html ? "<pre>\n" : "[code]\n");
int selStart = textarea.getSelectionStart();
int selStop = textarea.getSelectionEnd();
int startLine;
int stopLine;
try {
startLine = textarea.getLineOfOffset(selStart);
stopLine = textarea.getLineOfOffset(selStop);
} catch (BadLocationException e) {
return;
}
// If no selection, convert all the lines
if (selStart == selStop) {
startLine = 0;
stopLine = textarea.getLineCount() - 1;
} else {
// Make sure the selection doesn't end at the beginning of the last line
try {
if (textarea.getLineStartOffset(stopLine) == selStop) {
stopLine--;
}
} catch (BadLocationException e) {
// ignore
}
}
// Read the code line by line
for (int i = startLine; i <= stopLine; i++) {
appendFormattedLine(cf, i);
}
cf.append(html ? "\n</pre>" : "\n[/code]");
StringSelection formatted = new StringSelection(cf.toString());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(formatted, (clipboard1, contents) -> {
// i don't care about ownership
});
Clipboard unixclipboard = Toolkit.getDefaultToolkit().getSystemSelection();
if (unixclipboard != null) unixclipboard.setContents(formatted, null);
editor.statusNotice("Code formatted for " + (html ? "HTML" : "the Arduino forum") + " has been copied to the clipboard.");
}
/**
* Append a char to a StringBuilder while escaping for proper display in HTML.
*
* @param c input char to escape
* @param buffer StringBuilder to append html-safe version of c to.
*/
private void appendToHTML(char c, StringBuilder buffer) {
if (!html) {
buffer.append(c);
} else if (c == '<') {
buffer.append("<");
} else if (c == '>') {
buffer.append(">");
} else if (c == '&') {
buffer.append("&");
} else if (c > 127) {
buffer.append("&#").append((int) c).append(";"); // use unicode entity
} else {
buffer.append(c); // normal character
}
}
private void appendFormattedLine(StringBuilder buffer, int line) {
Segment segment = new Segment();
textarea.getTextLine(line, segment);
if (!html) {
char[] segmentArray = segment.array;
int segmentOffset = segment.offset;
int segmentCount = segment.count;
for (int j = 0; j < segmentCount; j++) {
char c = segmentArray[j + segmentOffset];
appendToHTML(c, buffer);
}
return;
}
Token tokenList = textarea.getTokenListForLine(line);
while (tokenList != null) {
if (tokenList.getType() != Token.NULL) {
tokenList.appendHTMLRepresentation(buffer, textarea, false);
}
tokenList = tokenList.getNextToken();
}
buffer.append('\n');
}
}