diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToWordStartAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToWordStartAction.java index d7026415b463..34ceda57714b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToWordStartAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToWordStartAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,12 +24,15 @@ */ package com.intellij.openapi.editor.actions; +import com.intellij.openapi.editor.CaretModel; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.SelectionModel; import com.intellij.openapi.editor.actionSystem.EditorAction; import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.util.text.CharArrayUtil; public class DeleteToWordStartAction extends TextComponentEditorAction { public DeleteToWordStartAction() { @@ -45,10 +48,31 @@ public class DeleteToWordStartAction extends TextComponentEditorAction { } private static void deleteToWordStart(Editor editor) { - int endOffset = editor.getCaretModel().getOffset(); - EditorActionUtil.moveCaretToPreviousWord(editor, false); - int startOffset = editor.getCaretModel().getOffset(); + final CaretModel caretModel = editor.getCaretModel(); + int endOffset = caretModel.getOffset(); + + // The logic is as follows: + // 1. Check are there white-space symbols starting at the current caret position going backwards. Delete them if any; + // 2. Otherwise locate previous word start and delete the text up to it; + // Example: + // 'test string ' -> 'test string' + // 'test string' -> 'test ' + Document document = editor.getDocument(); + final SelectionModel selectionModel = editor.getSelectionModel(); + int startOffset = -1; + if (!selectionModel.hasSelection() && !selectionModel.hasBlockSelection()) { + int i = CharArrayUtil.shiftBackward(document.getCharsSequence(), Math.max(0, endOffset - 1), " \t\n"); + if (i >= 0 && i < endOffset - 1) { + startOffset = i + 1; // We need offset of the first white space symbol, not offset of the last non-white space symbol before it. + } + } + + if (startOffset < 0) { + EditorActionUtil.moveCaretToPreviousWord(editor, false); + startOffset = caretModel.getOffset(); + } + document.deleteString(startOffset, endOffset); } } diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java index 15aab99d6495..f86a7c80bbbe 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -514,69 +514,69 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest } } protected static void backspace() { - doAction(IdeActions.ACTION_EDITOR_BACKSPACE); + executeAction(IdeActions.ACTION_EDITOR_BACKSPACE); } protected static void delete() { - doAction(IdeActions.ACTION_EDITOR_DELETE); + executeAction(IdeActions.ACTION_EDITOR_DELETE); } protected static void home() { - doAction(IdeActions.ACTION_EDITOR_MOVE_LINE_START); + executeAction(IdeActions.ACTION_EDITOR_MOVE_LINE_START); } protected static void end() { - doAction(IdeActions.ACTION_EDITOR_MOVE_LINE_END); + executeAction(IdeActions.ACTION_EDITOR_MOVE_LINE_END); } protected static void copy() { - doAction(IdeActions.ACTION_EDITOR_COPY); + executeAction(IdeActions.ACTION_EDITOR_COPY); } protected static void paste() { - doAction(IdeActions.ACTION_EDITOR_PASTE); + executeAction(IdeActions.ACTION_EDITOR_PASTE); } protected static void moveCaretToPreviousWordWithSelection() { - doAction("EditorPreviousWordWithSelection"); + executeAction("EditorPreviousWordWithSelection"); } protected static void moveCaretToNextWordWithSelection() { - doAction("EditorNextWordWithSelection"); + executeAction("EditorNextWordWithSelection"); } protected static void cutLineBackward() { - doAction("EditorCutLineBackward"); + executeAction("EditorCutLineBackward"); } protected static void cutToLineEnd() { - doAction("EditorCutLineEnd"); + executeAction("EditorCutLineEnd"); } protected static void killToWordStart() { - doAction("EditorKillToWordStart"); + executeAction("EditorKillToWordStart"); } protected static void killToWordEnd() { - doAction("EditorKillToWordEnd"); + executeAction("EditorKillToWordEnd"); } protected static void killRegion() { - doAction("EditorKillRegion"); + executeAction("EditorKillRegion"); } protected static void killRingSave() { - doAction("EditorKillRingSave"); + executeAction("EditorKillRingSave"); } protected static void unindent() { - doAction("EditorUnindentSelection"); + executeAction("EditorUnindentSelection"); } protected static void lineComment() { new CommentByLineCommentHandler().invoke(getProject(), getEditor(), getFile()); } - private static void doAction(@NotNull final String actionId) { + protected static void executeAction(@NotNull final String actionId) { CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { @Override public void run() { diff --git a/platform/util/src/com/intellij/util/text/CharArrayUtil.java b/platform/util/src/com/intellij/util/text/CharArrayUtil.java index c5244ba4ca7b..e5b5c958e4c4 100644 --- a/platform/util/src/com/intellij/util/text/CharArrayUtil.java +++ b/platform/util/src/com/intellij/util/text/CharArrayUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -198,17 +198,17 @@ public class CharArrayUtil { return shiftForward(new CharArrayCharSequence(buffer), offset, chars); } - public static int shiftBackward(CharSequence buffer, int offset, String chars) { + public static int shiftBackward(CharSequence buffer, int offset, String charsToSkip) { if (offset >= buffer.length()) return offset; while (true) { if (offset < 0) break; char c = buffer.charAt(offset); int i; - for (i = 0; i < chars.length(); i++) { - if (c == chars.charAt(i)) break; + for (i = 0; i < charsToSkip.length(); i++) { + if (c == charsToSkip.charAt(i)) break; } - if (i == chars.length()) break; + if (i == charsToSkip.length()) break; offset--; } return offset;