From fb0c462097f6fcfad1e0ef84e276e2021534b298 Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Mon, 1 Jun 2015 15:38:48 +0300 Subject: [PATCH] make 'Move Caret to Text End' action work properly for RTL text case --- .../openapi/actionSystem/IdeActions.java | 6 +++++- .../openapi/editor/actions/TextEndAction.java | 10 ++++++++-- .../actions/TextEndWithSelectionAction.java | 12 ++++++++--- .../openapi/editor/impl/EditorRtlTest.java | 20 +++++++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java b/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java index e05ceed942db..2913268aef81 100644 --- a/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java +++ b/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -31,6 +31,10 @@ public interface IdeActions { @NonNls String ACTION_EDITOR_ENTER = "EditorEnter"; @NonNls String ACTION_EDITOR_START_NEW_LINE = "EditorStartNewLine"; @NonNls String ACTION_EDITOR_SPLIT = "EditorSplitLine"; + @NonNls String ACTION_EDITOR_TEXT_START = "EditorTextStart"; + @NonNls String ACTION_EDITOR_TEXT_END = "EditorTextEnd"; + @NonNls String ACTION_EDITOR_TEXT_START_WITH_SELECTION = "EditorTextStartWithSelection"; + @NonNls String ACTION_EDITOR_TEXT_END_WITH_SELECTION = "EditorTextEndWithSelection"; @NonNls String ACTION_EDITOR_MOVE_LINE_START = "EditorLineStart"; @NonNls String ACTION_EDITOR_MOVE_LINE_END = "EditorLineEnd"; @NonNls String ACTION_EDITOR_MOVE_LINE_START_WITH_SELECTION = "EditorLineStartWithSelection"; diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/TextEndAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/TextEndAction.java index ae96512c6511..24973bb6e2a8 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/TextEndAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/TextEndAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -21,6 +21,7 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.ScrollType; import com.intellij.openapi.editor.ScrollingModel; import com.intellij.openapi.editor.actionSystem.EditorActionHandler; +import com.intellij.openapi.editor.impl.EditorImpl; import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory; import com.intellij.openapi.project.Project; @@ -38,7 +39,12 @@ public class TextEndAction extends TextComponentEditorAction { public void execute(Editor editor, DataContext dataContext) { editor.getCaretModel().removeSecondaryCarets(); int offset = editor.getDocument().getTextLength(); - editor.getCaretModel().moveToOffset(offset); + if (editor instanceof EditorImpl && ((EditorImpl)editor).myUseNewRendering) { + editor.getCaretModel().moveToLogicalPosition(editor.offsetToLogicalPosition(offset).leanForward(true)); + } + else { + editor.getCaretModel().moveToOffset(offset); + } editor.getSelectionModel().removeSelection(); ScrollingModel scrollingModel = editor.getScrollingModel(); diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/TextEndWithSelectionAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/TextEndWithSelectionAction.java index e7ecbbe33dcb..34420145edae 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/TextEndWithSelectionAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/TextEndWithSelectionAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -27,6 +27,7 @@ package com.intellij.openapi.editor.actions; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.editor.*; import com.intellij.openapi.editor.actionSystem.EditorActionHandler; +import com.intellij.openapi.editor.impl.EditorImpl; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -46,7 +47,7 @@ public class TextEndWithSelectionAction extends TextComponentEditorAction { caret = carets.get(0) == editor.getCaretModel().getPrimaryCaret() ? carets.get(carets.size() - 1) : carets.get(0); } LogicalPosition leadSelectionPosition = editor.visualToLogicalPosition(caret.getLeadSelectionPosition()); - LogicalPosition targetPosition = editor.offsetToLogicalPosition(endOffset); + LogicalPosition targetPosition = editor.offsetToLogicalPosition(endOffset).leanForward(true); editor.getSelectionModel().setBlockSelection(leadSelectionPosition, targetPosition); } else { @@ -54,7 +55,12 @@ public class TextEndWithSelectionAction extends TextComponentEditorAction { caret = carets.get(0); } int selectionStart = caret.getLeadSelectionOffset(); - caret.moveToOffset(endOffset); + if (editor instanceof EditorImpl && ((EditorImpl)editor).myUseNewRendering) { + editor.getCaretModel().moveToLogicalPosition(editor.offsetToLogicalPosition(endOffset).leanForward(true)); + } + else { + caret.moveToOffset(endOffset); + } caret.setSelection(selectionStart, endOffset); } ScrollingModel scrollingModel = editor.getScrollingModel(); diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/EditorRtlTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/EditorRtlTest.java index 8ee63be92766..a1288b6f251b 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/EditorRtlTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/EditorRtlTest.java @@ -406,6 +406,26 @@ public class EditorRtlTest extends AbstractEditorTest { checkSelection(1, 2); } + public void testTextStartEnd() throws Exception { + init("rr"); + myEditor.getCaretModel().moveToOffset(1); + executeAction(IdeActions.ACTION_EDITOR_TEXT_START); + assertVisualCaretLocation(0, false); + executeAction(IdeActions.ACTION_EDITOR_TEXT_END); + assertVisualCaretLocation(2, false); + } + + public void testTextStartEndWithSelection() throws Exception { + init("rr"); + myEditor.getCaretModel().moveToOffset(1); + executeAction(IdeActions.ACTION_EDITOR_TEXT_START_WITH_SELECTION); + assertVisualCaretLocation(0, false); + checkSelection(0, 1); + executeAction(IdeActions.ACTION_EDITOR_TEXT_END_WITH_SELECTION); + assertVisualCaretLocation(2, false); + checkSelection(1, 2); + } + private void init(String text) throws IOException { initText(text.replace(RTL_CHAR_REPRESENTATION, RTL_CHAR)); }