Revised DiscourseFormat to handle multiline comments. Also cleaned up code. Fixes #3970

This commit is contained in:
Federico Fissore 2015-10-28 15:12:28 +01:00
parent bf1537fa0c
commit 648625deb5
1 changed files with 41 additions and 59 deletions

View File

@ -23,32 +23,31 @@
package processing.app.tools; package processing.app.tools;
import java.awt.*; import org.fife.ui.rsyntaxtextarea.Token;
import java.awt.datatransfer.*; import processing.app.Editor;
import processing.app.syntax.SketchTextArea;
import javax.swing.text.BadLocationException; import javax.swing.text.BadLocationException;
import javax.swing.text.Segment; import javax.swing.text.Segment;
import java.awt.*;
import org.fife.ui.rsyntaxtextarea.Token; import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import processing.app.*;
import processing.app.syntax.*;
/** /**
* Format for Discourse Tool * Format for Discourse Tool
* <p/> * <p>
* Original code by <A HREF="http://usuarios.iponet.es/imoreta">owd</A>. * Original code by <A HREF="http://usuarios.iponet.es/imoreta">owd</A>.
* Revised and updated for revision 0108 by Ben Fry (10 March 2006). * 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 * 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. * with release 0108+ while features for the "Tools" menu are in testing.
* <p/> * <p>
* Updated for 0122 to simply copy the code directly to the clipboard, * Updated for 0122 to simply copy the code directly to the clipboard,
* rather than opening a new window. * rather than opening a new window.
* <p/> * <p>
* Updated for 0144 to only format the selected lines. * Updated for 0144 to only format the selected lines.
* <p/> * <p>
* Updated for 1.5.8 - Simplification, using RSyntaxTextArea TokenImpl formatter (08 dec 2014 - Ricardo JL Rufino) * Updated for 1.5.8 - Simplification, using RSyntaxTextArea TokenImpl formatter (08 dec 2014 - Ricardo JL Rufino)
* <p/> * <p>
* Notes from the original source: * Notes from the original source:
* Discourse.java This is a dirty-mix source. * Discourse.java This is a dirty-mix source.
* NOTE that: No macs and no keyboard. Unreliable source. * NOTE that: No macs and no keyboard. Unreliable source.
@ -57,11 +56,9 @@ import processing.app.syntax.*;
*/ */
public class DiscourseFormat { public class DiscourseFormat {
private Editor editor; private final Editor editor;
// JTextArea of the actual Editor private final SketchTextArea textarea;
private SketchTextArea textarea; private final boolean html;
private boolean html;
/** /**
* Creates a new window with the formated (YaBB tags) sketchcode * Creates a new window with the formated (YaBB tags) sketchcode
@ -74,12 +71,10 @@ public class DiscourseFormat {
this.html = html; this.html = html;
} }
/** /**
* Format and render sketch code. * Format and render sketch code.
*/ */
public void show() { public void show() {
// [code] tag cancels other tags, using [quote]
StringBuilder cf = new StringBuilder(html ? "<pre>\n" : "[code]\n"); StringBuilder cf = new StringBuilder(html ? "<pre>\n" : "[code]\n");
int selStart = textarea.getSelectionStart(); int selStart = textarea.getSelectionStart();
@ -105,6 +100,7 @@ public class DiscourseFormat {
stopLine--; stopLine--;
} }
} catch (BadLocationException e) { } catch (BadLocationException e) {
// ignore
} }
} }
@ -117,10 +113,8 @@ public class DiscourseFormat {
StringSelection formatted = new StringSelection(cf.toString()); StringSelection formatted = new StringSelection(cf.toString());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(formatted, new ClipboardOwner() { clipboard.setContents(formatted, (clipboard1, contents) -> {
public void lostOwnership(Clipboard clipboard, Transferable contents) {
// i don't care about ownership // i don't care about ownership
}
}); });
Clipboard unixclipboard = Toolkit.getDefaultToolkit().getSystemSelection(); Clipboard unixclipboard = Toolkit.getDefaultToolkit().getSystemSelection();
if (unixclipboard != null) unixclipboard.setContents(formatted, null); if (unixclipboard != null) unixclipboard.setContents(formatted, null);
@ -130,6 +124,7 @@ public class DiscourseFormat {
/** /**
* Append a char to a StringBuilder while escaping for proper display in HTML. * Append a char to a StringBuilder while escaping for proper display in HTML.
*
* @param c input char to escape * @param c input char to escape
* @param buffer StringBuilder to append html-safe version of c to. * @param buffer StringBuilder to append html-safe version of c to.
*/ */
@ -149,45 +144,32 @@ public class DiscourseFormat {
} }
} }
// A terrible headache... private void appendFormattedLine(StringBuilder buffer, int line) {
public void appendFormattedLine(StringBuilder cf, int line) {
Segment segment = new Segment(); Segment segment = new Segment();
// get line text from parent text area
textarea.getTextLine(line, segment); textarea.getTextLine(line, segment);
if (!html) {
char[] segmentArray = segment.array; char[] segmentArray = segment.array;
int segmentOffset = segment.offset; int segmentOffset = segment.offset;
int segmentCount = segment.count; int segmentCount = segment.count;
// int width = 0;
if (!html) {
for (int j = 0; j < segmentCount; j++) { for (int j = 0; j < segmentCount; j++) {
char c = segmentArray[j + segmentOffset]; char c = segmentArray[j + segmentOffset];
appendToHTML(c, cf); appendToHTML(c, buffer);
// int charWidth; }
// if (c == '\t') { return;
// charWidth = (int) painter.nextTabStop(width, j) - width;
// } else {
// charWidth = fm.charWidth(c);
// }
// width += charWidth;
} }
} else {
Token tokenList = textarea.getTokenListForLine(line); Token tokenList = textarea.getTokenListForLine(line);
while(tokenList != null){ while (tokenList != null) {
if(tokenList.getType() == Token.NULL){ if (tokenList.getType() != Token.NULL) {
cf.append('\n'); tokenList.appendHTMLRepresentation(buffer, textarea, false);
}else if(tokenList.isPaintable()){
tokenList.appendHTMLRepresentation(cf, textarea, false);
} }
tokenList = tokenList.getNextToken(); tokenList = tokenList.getNextToken();
} }
} buffer.append('\n');
} }
} }