|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + ******************************************************************************/ |
| 11 | +package com.redhat.devtools.lsp4ij; |
| 12 | + |
| 13 | +import com.intellij.openapi.editor.Document; |
| 14 | +import com.intellij.openapi.editor.impl.DocumentImpl; |
| 15 | +import com.intellij.testFramework.fixtures.BasePlatformTestCase; |
| 16 | +import org.eclipse.lsp4j.Position; |
| 17 | +import org.eclipse.lsp4j.Range; |
| 18 | +import org.eclipse.lsp4j.TextEdit; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests for {@link LSPIJUtils#applyEdits(Document, List)} with multiple text edits at the same position. |
| 27 | + * According to LSP spec: "if multiple inserts have the same position, the order in the array |
| 28 | + * defines the order in which the inserted strings appear in the resulting text." |
| 29 | + * |
| 30 | + * This method is used for formatting, where text edits must be sorted by position and |
| 31 | + * same-position edits must preserve array order. |
| 32 | + */ |
| 33 | +public class LSPIJUtils_applyEdits_SamePositionTest extends BasePlatformTestCase { |
| 34 | + |
| 35 | + /** |
| 36 | + * Test that multiple insertions at the same position maintain array order. |
| 37 | + */ |
| 38 | + public void testMultipleInsertsAtSamePosition() { |
| 39 | + Document document = new DocumentImpl("world"); |
| 40 | + |
| 41 | + List<TextEdit> edits = new ArrayList<>(); |
| 42 | + // Insert three strings at position 0:0 in order: "Hello ", ", ", "beautiful " |
| 43 | + // Expected result: "Hello , beautiful world" (in that exact order) |
| 44 | + edits.add(new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "Hello ")); |
| 45 | + edits.add(new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), ", ")); |
| 46 | + edits.add(new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "beautiful ")); |
| 47 | + |
| 48 | + String result = LSPIJUtils.applyEdits(document, edits); |
| 49 | + assertEquals("Hello , beautiful world", result); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test multiple insertions at the same position with mixed line/character positions. |
| 54 | + */ |
| 55 | + public void testMultipleInsertsAtSamePositionMidLine() { |
| 56 | + Document document = new DocumentImpl("start end"); |
| 57 | + |
| 58 | + List<TextEdit> edits = new ArrayList<>(); |
| 59 | + // Insert three strings at position 0:6 (between "start " and "end") |
| 60 | + // In order: "one ", "two ", "three " |
| 61 | + edits.add(new TextEdit(new Range(new Position(0, 6), new Position(0, 6)), "one ")); |
| 62 | + edits.add(new TextEdit(new Range(new Position(0, 6), new Position(0, 6)), "two ")); |
| 63 | + edits.add(new TextEdit(new Range(new Position(0, 6), new Position(0, 6)), "three ")); |
| 64 | + |
| 65 | + String result = LSPIJUtils.applyEdits(document, edits); |
| 66 | + assertEquals("start one two three end", result); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test that edits maintain order even when mixed with edits at different positions. |
| 71 | + */ |
| 72 | + public void testMixedPositionEditsWithSamePositionInserts() { |
| 73 | + Document document = new DocumentImpl("line1\nline2\nline3\n"); |
| 74 | + |
| 75 | + List<TextEdit> edits = new ArrayList<>(); |
| 76 | + // Multiple edits: |
| 77 | + // - Two inserts at 0:0 (should maintain order: "A", "B") |
| 78 | + // - One insert at 1:0 |
| 79 | + // - Two inserts at 2:0 (should maintain order: "X", "Y") |
| 80 | + edits.add(new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "A")); |
| 81 | + edits.add(new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "B")); |
| 82 | + edits.add(new TextEdit(new Range(new Position(1, 0), new Position(1, 0)), "C")); |
| 83 | + edits.add(new TextEdit(new Range(new Position(2, 0), new Position(2, 0)), "X")); |
| 84 | + edits.add(new TextEdit(new Range(new Position(2, 0), new Position(2, 0)), "Y")); |
| 85 | + |
| 86 | + String result = LSPIJUtils.applyEdits(document, edits); |
| 87 | + assertEquals("ABline1\nCline2\nXYline3\n", result); |
| 88 | + } |
| 89 | +} |
0 commit comments