1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-03-05 15:32:51 +01:00

Fix arithmetic for scrolling columns

This commit is contained in:
Matt Ellis 2020-09-16 08:50:01 +01:00
parent 53a687fd53
commit df3a533515
No known key found for this signature in database
GPG Key ID: FA6025D54131324B
3 changed files with 74 additions and 9 deletions
src/com/maddyhome/idea/vim/helper
test/org/jetbrains/plugins/ideavim

View File

@ -231,7 +231,7 @@ public class EditorHelper {
*/
public static int getVisualColumnAtRightOfScreen(final @NotNull Editor editor, int visualLine) {
final Rectangle area = getVisibleArea(editor);
return getFullVisualColumn(editor, area.x + area.width, editor.visualLineToY(visualLine), area.x, area.x + area.width);
return getFullVisualColumn(editor, area.x + area.width - 1, editor.visualLineToY(visualLine), area.x, area.x + area.width);
}
/**
@ -707,16 +707,18 @@ public class EditorHelper {
// of columns. It also works with inline inlays and folds. It is slightly inaccurate for proportional fonts, but is
// still a good solution. Besides, what kind of monster uses Vim with proportional fonts?
final int standardColumnWidth = EditorUtil.getPlainSpaceWidth(editor);
final int something = ((point.x - (screenWidth / 2)) / standardColumnWidth) * standardColumnWidth;
EditorHelper.scrollHorizontally(editor, something);
final int x = point.x - (screenWidth / standardColumnWidth / 2 * standardColumnWidth);
EditorHelper.scrollHorizontally(editor, x);
}
public static void scrollColumnToRightOfScreen(@NotNull Editor editor, int visualLine, int visualColumn) {
var inlay = editor.getInlayModel().getInlineElementAt(new VisualPosition(visualLine, visualColumn + 1));
int inlayWidth = inlay != null && inlay.isRelatedToPrecedingText() ? inlay.getWidthInPixels() : 0;
final int columnRightX = editor.visualPositionToXY(new VisualPosition(visualLine, visualColumn + 1)).x;
// Scroll to the start of the next column, minus a screenwidth
final int nextColumnX = editor.visualPositionToXY(new VisualPosition(visualLine, visualColumn + 1)).x;
final int screenWidth = EditorHelper.getVisibleArea(editor).width;
EditorHelper.scrollHorizontally(editor, columnRightX + inlayWidth - 1 - screenWidth);
EditorHelper.scrollHorizontally(editor, nextColumnX + inlayWidth - screenWidth);
}
/**

View File

@ -111,10 +111,8 @@ class ScrollLastScreenColumnActionTest : VimTestCase() {
assertVisibleLineBounds(0, 99 - availableColumns + 1, 99)
// We have to assert the location of the inlay
Assert.assertTrue("Inlay bounds must be greater than last text location",
inlay.bounds!!.x > (visibleArea.x + textWidth))
Assert.assertTrue("Inlay bounds must be less than width of screen",
inlay.bounds!!.x + inlay.bounds!!.width <= (visibleArea.x + visibleArea.width + 1))
Assert.assertEquals(visibleArea.x + textWidth, inlay.bounds!!.x)
Assert.assertEquals(visibleArea.x + visibleArea.width, inlay.bounds!!.x + inlay.bounds!!.width)
}
fun `test last screen column does not include subsequent inline inlay associated with following text`() {

View File

@ -0,0 +1,65 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2020 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jetbrains.plugins.ideavim.helper
import com.maddyhome.idea.vim.helper.EditorHelper
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.Assert
class EditorHelperTest : VimTestCase() {
fun `test scroll column to left of screen`() {
configureByColumns(100)
EditorHelper.scrollColumnToLeftOfScreen(myFixture.editor, 0, 2)
val visibleArea = myFixture.editor.scrollingModel.visibleArea
val columnWidth = visibleArea.width / screenWidth
Assert.assertEquals(2 * columnWidth, visibleArea.x)
}
fun `test scroll column to right of screen`() {
configureByColumns(100)
val column = screenWidth + 2
EditorHelper.scrollColumnToRightOfScreen(myFixture.editor, 0, column)
val visibleArea = myFixture.editor.scrollingModel.visibleArea
val columnWidth = visibleArea.width / screenWidth
Assert.assertEquals((column - screenWidth + 1) * columnWidth, visibleArea.x)
}
fun `test scroll column to middle of screen with even number of columns`() {
configureByColumns(200)
// For an 80 column screen, moving a column to the centre should position it in column 41 (1 based) - 40 columns on
// the left, mid point, 39 columns on the right
// Put column 100 into position 41 -> offset is 59 columns
EditorHelper.scrollColumnToMiddleOfScreen(myFixture.editor, 0, 99)
val visibleArea = myFixture.editor.scrollingModel.visibleArea
val columnWidth = visibleArea.width / screenWidth
Assert.assertEquals(59 * columnWidth, visibleArea.x)
}
fun `test scroll column to middle of screen with odd number of columns`() {
configureByColumns(200)
setEditorVisibleSize(81, 25)
// For an 81 column screen, moving a column to the centre should position it in column 41 (1 based) - 40 columns on
// the left, mid point, 40 columns on the right
// Put column 100 into position 41 -> offset is 59 columns
EditorHelper.scrollColumnToMiddleOfScreen(myFixture.editor, 0, 99)
val visibleArea = myFixture.editor.scrollingModel.visibleArea
val columnWidth = visibleArea.width / screenWidth
Assert.assertEquals(59 * columnWidth, visibleArea.x)
}
}