Index: editor/libsrc/org/netbeans/editor/ActionFactory.java =================================================================== RCS file: /cvs/editor/libsrc/org/netbeans/editor/ActionFactory.java,v retrieving revision 1.70 diff -u -u -r1.70 ActionFactory.java --- editor/libsrc/org/netbeans/editor/ActionFactory.java 24 Oct 2005 11:50:54 -0000 1.70 +++ editor/libsrc/org/netbeans/editor/ActionFactory.java 6 Nov 2005 06:16:21 -0000 @@ -866,6 +866,117 @@ } } + + /** + * Reindent the current line. + * + * @todo Fix it such that it doesn't modify the selection + */ + public static class ReindentLineAction extends BaseAction { + + static final long serialVersionUID =1L; + + public ReindentLineAction() { + // TODO: figure out what these flags are all about + super(BaseKit.reindentLineAction, + ABBREV_RESET | MAGIC_POSITION_RESET | UNDO_MERGE_RESET); + //putValue ("helpID", ReindentLineAction.class.getName ()); + } + + public void actionPerformed(ActionEvent evt, JTextComponent target) { + if (target != null) { + if (!target.isEditable() || !target.isEnabled()) { + target.getToolkit().beep(); + return; + } + + // Todo: operate iterately over all the lines in the + // selection + + // Note: there is a Utilities.reformat(doc, pos) + // method - is that all I need? + + + // This code is copied from FormatAction in this + // same file - I just tweaked the code which used + // to look at the selection to compute the line bounds + // instead. Finally, I adjust the caret position to + // the first non-whitespace character instead of + // column 0. + + Caret caret = target.getCaret(); + BaseDocument doc = (BaseDocument)target.getDocument(); + GuardedDocument gdoc = (doc instanceof GuardedDocument) + ? (GuardedDocument)doc : null; + + doc.atomicLock(); + try { + + int caretLine = Utilities.getLineOffset(doc, caret.getDot()); + int startPos; + Position endPosition; + + + /* + if (caret.isSelectionVisible()) { + startPos = target.getSelectionStart(); + endPosition = doc.createPosition(target.getSelectionEnd()); + } else { + startPos = 0; + endPosition = doc.createPosition(doc.getLength()); + } + */ + startPos = Utilities.getRowStart(doc, caret.getDot()); + endPosition = + doc.createPosition(Utilities.getRowEnd(doc, caret.getDot())); + + + + int pos = startPos; + if (gdoc != null) { + pos = gdoc.getGuardedBlockChain().adjustToBlockEnd(pos); + } + + while (pos < endPosition.getOffset()) { + int stopPos = endPosition.getOffset(); + if (gdoc != null) { // adjust to start of the next guarded block + stopPos = gdoc.getGuardedBlockChain().adjustToNextBlockStart(pos); + if (stopPos == -1) { + stopPos = endPosition.getOffset(); + } + } + + int reformattedLen = doc.getFormatter().reformat(doc, pos, stopPos); + pos = pos + reformattedLen; + + if (gdoc != null) { // adjust to end of current block + pos = gdoc.getGuardedBlockChain().adjustToBlockEnd(pos); + } + } + + // Restore the line + pos = Utilities.getRowStartFromLineOffset(doc, caretLine); + if (pos >= 0) { + + pos = Utilities.getRowFirstNonWhite(doc, pos); + + caret.setDot(pos); + } + } catch (GuardedException e) { + target.getToolkit().beep(); + } catch (BadLocationException e) { + if (System.getProperty("netbeans.debug.exceptions") != null) { // NOI18N + e.printStackTrace(); + } + } finally { + doc.atomicUnlock(); + } + + } + } + } + + public static class AdjustWindowAction extends BaseAction { int percentFromWindowTop; Index: editor/libsrc/org/netbeans/editor/BaseKit.java =================================================================== RCS file: /cvs/editor/libsrc/org/netbeans/editor/BaseKit.java,v retrieving revision 1.140 diff -u -u -r1.140 BaseKit.java --- editor/libsrc/org/netbeans/editor/BaseKit.java 21 Oct 2005 13:48:17 -0000 1.140 +++ editor/libsrc/org/netbeans/editor/BaseKit.java 6 Nov 2005 06:16:21 -0000 @@ -144,6 +144,9 @@ /** Shift line right action */ public static final String shiftLineRightAction = "shift-line-right"; // NOI18N + /** Reindent Line action */ + public static final String reindentLineAction = "reindent-line"; // NOI18N + /** Shift line left action */ public static final String shiftLineLeftAction = "shift-line-left"; // NOI18N @@ -639,6 +642,7 @@ new ActionFactory.RedoAction(), new ActionFactory.WordMatchAction(wordMatchNextAction, true), new ActionFactory.WordMatchAction(wordMatchPrevAction, false), + new ActionFactory.ReindentLineAction(), new ActionFactory.ShiftLineAction(shiftLineLeftAction, false), new ActionFactory.ShiftLineAction(shiftLineRightAction, true), new ActionFactory.AdjustWindowAction(adjustWindowTopAction, 0), Index: editor/libsrc/org/netbeans/editor/Bundle.properties =================================================================== RCS file: /cvs/editor/libsrc/org/netbeans/editor/Bundle.properties,v retrieving revision 1.64 diff -u -u -r1.64 Bundle.properties --- editor/libsrc/org/netbeans/editor/Bundle.properties 2 Nov 2005 14:59:27 -0000 1.64 +++ editor/libsrc/org/netbeans/editor/Bundle.properties 6 Nov 2005 06:16:21 -0000 @@ -96,6 +96,7 @@ paste-from-clipboard=Paste popup-remove-selection=Delete redo=Redo +reindent-line=Re-indent Line remove-line=Delete Line remove-line-begin=Delete Preceding Characters in Line remove-selection=Delete Selection Index: ide/defaults/src/org/netbeans/modules/defaults/Emacs-keybindings.xml =================================================================== RCS file: /cvs/ide/defaults/src/org/netbeans/modules/defaults/Emacs-keybindings.xml,v retrieving revision 1.8 diff -u -u -r1.8 Emacs-keybindings.xml --- ide/defaults/src/org/netbeans/modules/defaults/Emacs-keybindings.xml 31 Oct 2005 15:33:44 -0000 1.8 +++ ide/defaults/src/org/netbeans/modules/defaults/Emacs-keybindings.xml 6 Nov 2005 06:16:22 -0000 @@ -49,7 +49,8 @@ - + +