Format and render sketch code.
()
| 76 | * Format and render sketch code. |
| 77 | */ |
| 78 | public void show() { |
| 79 | StringBuilder cf = new StringBuilder(html ? "<pre>\n" : "[code]\n"); |
| 80 | |
| 81 | int selStart = textarea.getSelectionStart(); |
| 82 | int selStop = textarea.getSelectionEnd(); |
| 83 | |
| 84 | int startLine; |
| 85 | int stopLine; |
| 86 | try { |
| 87 | startLine = textarea.getLineOfOffset(selStart); |
| 88 | stopLine = textarea.getLineOfOffset(selStop); |
| 89 | } catch (BadLocationException e) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // If no selection, convert all the lines |
| 94 | if (selStart == selStop) { |
| 95 | startLine = 0; |
| 96 | stopLine = textarea.getLineCount() - 1; |
| 97 | } else { |
| 98 | // Make sure the selection doesn't end at the beginning of the last line |
| 99 | try { |
| 100 | if (textarea.getLineStartOffset(stopLine) == selStop) { |
| 101 | stopLine--; |
| 102 | } |
| 103 | } catch (BadLocationException e) { |
| 104 | // ignore |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Read the code line by line |
| 109 | for (int i = startLine; i <= stopLine; i++) { |
| 110 | appendFormattedLine(cf, i); |
| 111 | } |
| 112 | |
| 113 | cf.append(html ? "\n</pre>" : "\n[/code]"); |
| 114 | |
| 115 | StringSelection formatted = new StringSelection(cf.toString()); |
| 116 | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 117 | clipboard.setContents(formatted, (clipboard1, contents) -> { |
| 118 | // i don't care about ownership |
| 119 | }); |
| 120 | Clipboard unixclipboard = Toolkit.getDefaultToolkit().getSystemSelection(); |
| 121 | if (unixclipboard != null) unixclipboard.setContents(formatted, null); |
| 122 | |
| 123 | editor.statusNotice("Code formatted for " + (html ? "HTML" : "the Arduino forum") + " has been copied to the clipboard."); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Append a char to a StringBuilder while escaping for proper display in HTML. |
no test coverage detected