forked from redhat-developer/lsp4ij
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Formatting fails when position end is out of range of the document
Fixes redhat-developer#183 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
67324b3
commit 5d5b05b
Showing
2 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/java/com/redhat/devtools/lsp4ij/LSPIJUtils_toOffsetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij; | ||
|
||
import com.intellij.openapi.editor.Document; | ||
import com.intellij.openapi.editor.impl.DocumentImpl; | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase; | ||
import org.eclipse.lsp4j.Position; | ||
|
||
/** | ||
* Tests for {@link LSPIJUtils#toOffset(Position, Document)}. | ||
*/ | ||
public class LSPIJUtils_toOffsetTest extends BasePlatformTestCase { | ||
|
||
public void testValidPositionWithOneLine() { | ||
assertOffset("foo", 0, 0, 0, 'f'); | ||
assertOffset("foo", 0, 1, 1, 'o'); | ||
assertOffset("foo", 0, 2, 2, 'o'); | ||
} | ||
|
||
public void testValidPositionWithTwoLines() { | ||
assertOffset("foo\nbar", 0, 0, 0, 'f'); | ||
assertOffset("foo\nbar", 0, 1, 1, 'o'); | ||
assertOffset("foo\nbar", 0, 2, 2, 'o'); | ||
|
||
assertOffset("foo\nbar", 1, 0, 4, 'b'); | ||
assertOffset("foo\nbar", 1, 1, 5, 'a'); | ||
assertOffset("foo\nbar", 1, 2, 6, 'r'); | ||
} | ||
|
||
public void testInvalidLineWithOneLine() { | ||
assertOffset("foo", 999999, 0, 0, 'f'); | ||
assertOffset("foo", 999999, 1, 1, 'o'); | ||
assertOffset("foo", 999999, 2, 2, 'o'); | ||
} | ||
|
||
public void testInvalidLineWithTwoLines() { | ||
assertOffset("foo\nbar", 999999, 0, 4, 'b'); | ||
assertOffset("foo\nbar", 999999, 1, 5, 'a'); | ||
assertOffset("foo\nbar", 999999, 2, 6, 'r'); | ||
} | ||
|
||
public void testInvalidCharacterWithOneLine() { | ||
assertOffset("foo", 0, 999999, 2, 'o'); | ||
} | ||
|
||
public void testInvalidCharacterWithTwoLines() { | ||
assertOffset("foo\nbar", 0, 999999, 2, 'o'); | ||
assertOffset("foo\nbar", 1, 999999, 6, 'r'); | ||
} | ||
|
||
public void assertOffset(String content, int line, int character, int expectedOffset, char expectedChar) { | ||
assertOffset(content, new Position(line, character),expectedOffset, expectedChar); | ||
} | ||
|
||
public void assertOffset(String content, Position position, int expectedOffset, char expectedChar) { | ||
Document document = new DocumentImpl(content); | ||
int actualOffset = LSPIJUtils.toOffset(position, document); | ||
assertEquals(expectedOffset, actualOffset); | ||
// get the character from the given offset | ||
char actualChar = document.getCharsSequence().charAt(expectedOffset); | ||
assertEquals(expectedChar, actualChar); | ||
} | ||
|
||
|
||
} |