()
| 1713 | |
| 1714 | |
| 1715 | public void handleAutoFormat() { |
| 1716 | final String source = getText(); |
| 1717 | |
| 1718 | try { |
| 1719 | final String formattedText = createFormatter().format(source); |
| 1720 | // save current (rough) selection point |
| 1721 | int selectionEnd = getSelectionStop(); |
| 1722 | |
| 1723 | // boolean wasVisible = |
| 1724 | // textarea.getSelectionStopLine() >= textarea.getFirstLine() && |
| 1725 | // textarea.getSelectionStopLine() < textarea.getLastLine(); |
| 1726 | |
| 1727 | // make sure the caret would be past the end of the text |
| 1728 | if (formattedText.length() < selectionEnd - 1) { |
| 1729 | selectionEnd = formattedText.length() - 1; |
| 1730 | } |
| 1731 | |
| 1732 | if (formattedText.equals(source)) { |
| 1733 | statusNotice(Language.text("editor.status.autoformat.no_changes")); |
| 1734 | |
| 1735 | } else { // replace with new bootiful text |
| 1736 | startCompoundEdit(); |
| 1737 | // selectionEnd hopefully at least in the neighborhood |
| 1738 | int scrollPos = textarea.getVerticalScrollPosition(); |
| 1739 | textarea.setText(formattedText); |
| 1740 | setSelection(selectionEnd, selectionEnd); |
| 1741 | |
| 1742 | // Put the scrollbar position back, otherwise it jumps on each format. |
| 1743 | // Since we're not doing a good job of maintaining position anyway, |
| 1744 | // a more complicated workaround here is fairly pointless. |
| 1745 | // https://github.com/processing/processing/issues/1571 |
| 1746 | if (scrollPos != textarea.getVerticalScrollPosition()) { |
| 1747 | textarea.setVerticalScrollPosition(scrollPos); |
| 1748 | } |
| 1749 | stopCompoundEdit(); |
| 1750 | sketch.setModified(true); |
| 1751 | statusNotice(Language.text("editor.status.autoformat.finished")); |
| 1752 | } |
| 1753 | |
| 1754 | } catch (final Exception e) { |
| 1755 | statusError(e); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | |
| 1760 | abstract public String getCommentPrefix(); |
no test coverage detected