From 63e8272a76e9fc7fe1b81354b82d7b1d4397319e Mon Sep 17 00:00:00 2001 From: "Denis.Zhdanov" Date: Fri, 13 Jan 2012 15:20:06 +0400 Subject: [PATCH] IDEA-79691 Option to keep cursor on screen when scrolling with Ctrl+Up/Down --- .../editor/actions/EditorActionUtil.java | 51 ++++++++++++------- .../editor/actions/ScrollDownAction.java | 4 +- .../actions/ScrollDownAndMoveAction.java | 40 +++++++++++++++ .../editor/actions/ScrollUpAction.java | 4 +- .../editor/actions/ScrollUpAndMoveAction.java | 40 +++++++++++++++ .../src/messages/ActionsBundle.properties | 2 + .../src/idea/PlatformActions.xml | 2 + 7 files changed, 120 insertions(+), 23 deletions(-) create mode 100644 platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollDownAndMoveAction.java create mode 100644 platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollUpAndMoveAction.java diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/EditorActionUtil.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/EditorActionUtil.java index de36566c2bb8..04582bc98034 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/EditorActionUtil.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/EditorActionUtil.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. @@ -55,27 +55,38 @@ public class EditorActionUtil { private EditorActionUtil() { } - public static void scrollRelatively(Editor editor, int lineShift) { + /** + * Tries to change given editor's viewport position in vertical dimension by the given number of visual lines. + * + * @param editor target editor which viewport position should be changed + * @param lineShift defines viewport position's change length + * @param moveCaret flag that identifies whether caret should be moved if its current position becomes off-screen + */ + public static void scrollRelatively(Editor editor, int lineShift, boolean moveCaret) { if (lineShift != 0) { editor.getScrollingModel().scrollVertically( editor.getScrollingModel().getVerticalScrollOffset() + lineShift * editor.getLineHeight() ); } - //Rectangle viewRectangle = editor.getScrollingModel().getVisibleArea(); - //int lineNumber = editor.getCaretModel().getVisualPosition().line; - //if (viewRectangle != null) { - // VisualPosition startPos = editor.xyToVisualPosition(new Point(0, viewRectangle.y)); - // int start = startPos.line + 1; - // VisualPosition endPos = editor.xyToVisualPosition(new Point(0, viewRectangle.y + viewRectangle.height)); - // int end = endPos.line - 2; - //if (lineNumber < start) { - // editor.getCaretModel().moveCaretRelatively(0, start - lineNumber, false, false, true); - //} - //else if (lineNumber > end) { - // editor.getCaretModel().moveCaretRelatively(0, end - lineNumber, false, false, true); - //} - //} + if (!moveCaret) { + return; + } + + Rectangle viewRectangle = editor.getScrollingModel().getVisibleArea(); + int lineNumber = editor.getCaretModel().getVisualPosition().line; + if (viewRectangle != null) { + VisualPosition startPos = editor.xyToVisualPosition(new Point(0, viewRectangle.y)); + int start = startPos.line + 1; + VisualPosition endPos = editor.xyToVisualPosition(new Point(0, viewRectangle.y + viewRectangle.height)); + int end = endPos.line - 2; + if (lineNumber < start) { + editor.getCaretModel().moveCaretRelatively(0, start - lineNumber, false, false, true); + } + else if (lineNumber > end) { + editor.getCaretModel().moveCaretRelatively(0, end - lineNumber, false, false, true); + } + } } public static void moveCaretRelativelyAndScroll(Editor editor, @@ -215,9 +226,11 @@ public class EditorActionUtil { } if (isCamel) { - if (firstIsIdentifierPart && secondIsIdentifierPart && - (Character.isLowerCase(prev) && Character.isUpperCase(current) || prev != '_' && current == '_' || - Character.isUpperCase(prev) && Character.isUpperCase(current) && Character.isLowerCase(next))) { + if (firstIsIdentifierPart + && (Character.isLowerCase(prev) && Character.isUpperCase(current) + || prev != '_' && current == '_' + || Character.isUpperCase(prev) && Character.isUpperCase(current) && Character.isLowerCase(next))) + { return true; } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollDownAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollDownAction.java index 09a4b6ea7317..80990c251578 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollDownAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollDownAction.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. @@ -36,7 +36,7 @@ public class ScrollDownAction extends InactiveEditorAction { private static class Handler extends EditorActionHandler { @Override public void execute(Editor editor, DataContext dataContext) { - EditorActionUtil.scrollRelatively(editor, 1); + EditorActionUtil.scrollRelatively(editor, 1, false); } } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollDownAndMoveAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollDownAndMoveAction.java new file mode 100644 index 000000000000..29bbedfba770 --- /dev/null +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollDownAndMoveAction.java @@ -0,0 +1,40 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.editor.actions; + +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.actionSystem.EditorActionHandler; + +/** + * Moves editor viewport one visual line down. Caret is also moved one line down if it becomes off-screen + * + * @author Denis Zhdanov + * @since 1/13/12 1:22 PM + */ +public class ScrollDownAndMoveAction extends InactiveEditorAction { + + public ScrollDownAndMoveAction() { + super(new Handler()); + } + + private static class Handler extends EditorActionHandler { + @Override + public void execute(Editor editor, DataContext dataContext) { + EditorActionUtil.scrollRelatively(editor, 1, true); + } + } +} diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollUpAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollUpAction.java index 422613413f82..f2214cdfaf1f 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollUpAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollUpAction.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. @@ -36,7 +36,7 @@ public class ScrollUpAction extends InactiveEditorAction { private static class Handler extends EditorActionHandler { @Override public void execute(Editor editor, DataContext dataContext) { - EditorActionUtil.scrollRelatively(editor, -1); + EditorActionUtil.scrollRelatively(editor, -1, false); } } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollUpAndMoveAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollUpAndMoveAction.java new file mode 100644 index 000000000000..919e121323dd --- /dev/null +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/ScrollUpAndMoveAction.java @@ -0,0 +1,40 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.editor.actions; + +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.actionSystem.EditorActionHandler; + +/** + * Moves editor viewport one visual line up. Caret is also moved one line up if it becomes off-screen. + * + * @author Denis Zhdanov + * @since 1/13/12 1:21 PM + */ +public class ScrollUpAndMoveAction extends InactiveEditorAction { + + public ScrollUpAndMoveAction() { + super(new Handler()); + } + + private static class Handler extends EditorActionHandler { + @Override + public void execute(Editor editor, DataContext dataContext) { + EditorActionUtil.scrollRelatively(editor, -1, true); + } + } +} diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index efc5af738f96..b32e2251622f 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -89,6 +89,8 @@ action.EditorTab.text=Tab action.EmacsStyleIndent.text=Emacs Tab action.EditorScrollUp.text=Scroll Up action.EditorScrollDown.text=Scroll Down +action.EditorScrollUpAndMove.text=Scroll Up and Move if Necessary +action.EditorScrollDownAndMove.text=Scroll Down and Move if Necessary action.EditorScrollTop.text=Scroll to Top action.EditorScrollBottom.text=Scroll to Bottom action.EditorMoveUpAndScroll.text=Move Up and Scroll diff --git a/platform/platform-resources/src/idea/PlatformActions.xml b/platform/platform-resources/src/idea/PlatformActions.xml index 8f16cfe5b38d..cfe8b1fa5ef4 100644 --- a/platform/platform-resources/src/idea/PlatformActions.xml +++ b/platform/platform-resources/src/idea/PlatformActions.xml @@ -51,6 +51,8 @@ + +