()
| 2060 | |
| 2061 | |
| 2062 | public void handleAutoFormat() { |
| 2063 | final String source = getText(); |
| 2064 | |
| 2065 | try { |
| 2066 | final String formattedText = createFormatter().format(source); |
| 2067 | // save current (rough) selection point |
| 2068 | int selectionEnd = getSelectionStop(); |
| 2069 | |
| 2070 | // boolean wasVisible = |
| 2071 | // textarea.getSelectionStopLine() >= textarea.getFirstLine() && |
| 2072 | // textarea.getSelectionStopLine() < textarea.getLastLine(); |
| 2073 | |
| 2074 | // make sure the caret would be past the end of the text |
| 2075 | if (formattedText.length() < selectionEnd - 1) { |
| 2076 | selectionEnd = formattedText.length() - 1; |
| 2077 | } |
| 2078 | |
| 2079 | if (formattedText.equals(source)) { |
| 2080 | statusNotice(Language.text("editor.status.autoformat.no_changes")); |
| 2081 | |
| 2082 | } else { // replace with new bootiful text |
| 2083 | startCompoundEdit(); |
| 2084 | // selectionEnd hopefully at least in the neighborhood |
| 2085 | int scrollPos = textarea.getVerticalScrollPosition(); |
| 2086 | textarea.setText(formattedText); |
| 2087 | setSelection(selectionEnd, selectionEnd); |
| 2088 | |
| 2089 | // Put the scrollbar position back, otherwise it jumps on each format. |
| 2090 | // Since we're not doing a good job of maintaining position anyway, |
| 2091 | // a more complicated workaround here is fairly pointless. |
| 2092 | // http://code.google.com/p/processing/issues/detail?id=1533 |
| 2093 | if (scrollPos != textarea.getVerticalScrollPosition()) { |
| 2094 | textarea.setVerticalScrollPosition(scrollPos); |
| 2095 | } |
| 2096 | stopCompoundEdit(); |
| 2097 | sketch.setModified(true); |
| 2098 | statusNotice(Language.text("editor.status.autoformat.finished")); |
| 2099 | } |
| 2100 | |
| 2101 | } catch (final Exception e) { |
| 2102 | statusError(e); |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | |
| 2107 | abstract public String getCommentPrefix(); |
no test coverage detected