|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vegard IT GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Sebastian Thomschke (Vegard IT GmbH) - initial implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.lsp4e.test.completion; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import org.eclipse.jface.text.Document; |
| 21 | +import org.eclipse.jface.text.IDocument; |
| 22 | +import org.eclipse.lsp4e.operations.completion.LSCompletionProposal; |
| 23 | +import org.eclipse.lsp4e.operations.completion.LSCompletionProposalComparator; |
| 24 | +import org.eclipse.lsp4j.CompletionItem; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +public class LSCompletionProposalComparatorTest { |
| 28 | + |
| 29 | + /** |
| 30 | + * DocumentFilter length currently has top priority in LSCompletionProposalComparator. |
| 31 | + * This test encodes the expectation that, once proposals are considered matching, |
| 32 | + * sortText should decide the order, not the document filter length. |
| 33 | + */ |
| 34 | + @Test |
| 35 | + public void testDocumentFilterLengthDoesNotOverrideSortText() { |
| 36 | + IDocument document = new Document(""); |
| 37 | + |
| 38 | + final var item1 = new CompletionItem("p1"); |
| 39 | + item1.setSortText("B"); |
| 40 | + final var item2 = new CompletionItem("p2"); |
| 41 | + item2.setSortText("A"); |
| 42 | + |
| 43 | + final var proposalWithLongFilter = new StubProposal(document, item1, "longFilter"); |
| 44 | + final var proposalWithShortFilter = new StubProposal(document, item2, "x"); |
| 45 | + |
| 46 | + final var comparator = new LSCompletionProposalComparator(); |
| 47 | + final var proposals = new ArrayList<LSCompletionProposal>(); |
| 48 | + proposals.add(proposalWithLongFilter); |
| 49 | + proposals.add(proposalWithShortFilter); |
| 50 | + |
| 51 | + proposals.sort(comparator); |
| 52 | + |
| 53 | + // Desired order: sortText ascending -> "A", then "B" |
| 54 | + assertEquals("A", proposals.get(0).getSortText()); |
| 55 | + assertEquals("B", proposals.get(1).getSortText()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * CompletionProposalPopup.computeFilteredProposals filters an already sorted |
| 60 | + * list of proposals. To avoid surprising reordering if re-sorting is added |
| 61 | + * later, LSCompletionProposalComparator must not change the relative order |
| 62 | + * of proposals solely because their documentFilter length changes. |
| 63 | + * |
| 64 | + * This test verifies that changing the documentFilter does not change the |
| 65 | + * comparator outcome for otherwise identical proposals. |
| 66 | + */ |
| 67 | + @Test |
| 68 | + public void testFilteredProposalsShouldBeResortedWhenFilterChanges() { |
| 69 | + IDocument document = new Document(""); |
| 70 | + |
| 71 | + final var item1 = new CompletionItem("p1"); |
| 72 | + final var item2 = new CompletionItem("p2"); |
| 73 | + |
| 74 | + final int initialOffset = 1; |
| 75 | + final int updatedOffset = 2; |
| 76 | + |
| 77 | + final var proposalA = new VarFilterProposal(document, item1); |
| 78 | + final var proposalB = new VarFilterProposal(document, item2); |
| 79 | + |
| 80 | + // At the initial offset, proposalA has a longer filter than proposalB |
| 81 | + // so A should be ordered before B. |
| 82 | + proposalA.setFilterForOffset(initialOffset, "xx"); |
| 83 | + proposalB.setFilterForOffset(initialOffset, "x"); |
| 84 | + |
| 85 | + // After typing more, the filters swap lengths so the desired order |
| 86 | + // (if resorted) would become B before A. |
| 87 | + proposalA.setFilterForOffset(updatedOffset, "x"); |
| 88 | + proposalB.setFilterForOffset(updatedOffset, "xx"); |
| 89 | + |
| 90 | + final var comparator = new LSCompletionProposalComparator(); |
| 91 | + |
| 92 | + // Initial sort at invocation offset |
| 93 | + final var initiallySorted = new ArrayList<LSCompletionProposal>(); |
| 94 | + initiallySorted.add(proposalA); |
| 95 | + initiallySorted.add(proposalB); |
| 96 | + proposalA.setOffsetForSorting(initialOffset); |
| 97 | + proposalB.setOffsetForSorting(initialOffset); |
| 98 | + initiallySorted.sort(comparator); |
| 99 | + |
| 100 | + // Simulate JFace behaviour: keep the original order and just filter |
| 101 | + final var filteredWithoutResort = new ArrayList<>(initiallySorted); |
| 102 | + |
| 103 | + // Expected behaviour: update filters for the new offset and resort |
| 104 | + final var expectedResorted = new ArrayList<LSCompletionProposal>(); |
| 105 | + expectedResorted.add(proposalA); |
| 106 | + expectedResorted.add(proposalB); |
| 107 | + proposalA.setOffsetForSorting(updatedOffset); |
| 108 | + proposalB.setOffsetForSorting(updatedOffset); |
| 109 | + expectedResorted.sort(comparator); |
| 110 | + |
| 111 | + // We would like the filtered list to reflect the re-sorted order. |
| 112 | + assertEquals(expectedResorted, filteredWithoutResort); |
| 113 | + } |
| 114 | + |
| 115 | + private static class StubProposal extends LSCompletionProposal { |
| 116 | + |
| 117 | + private final String filter; |
| 118 | + |
| 119 | + StubProposal(IDocument document, CompletionItem item, String filter) { |
| 120 | + super(document, 0, item, null, null, false); |
| 121 | + this.filter = filter; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public String getDocumentFilter() { |
| 126 | + return filter; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public String getDocumentFilter(int offset) { |
| 131 | + return filter; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public int getRankCategory() { |
| 136 | + return 5; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int getRankScore() { |
| 141 | + return 0; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static class VarFilterProposal extends LSCompletionProposal { |
| 146 | + |
| 147 | + private final Map<Integer, String> filtersByOffset = new HashMap<>(); |
| 148 | + private int sortOffset; |
| 149 | + |
| 150 | + VarFilterProposal(IDocument document, CompletionItem item) { |
| 151 | + super(document, 0, item, null, null, false); |
| 152 | + } |
| 153 | + |
| 154 | + void setFilterForOffset(int offset, String filter) { |
| 155 | + filtersByOffset.put(Integer.valueOf(offset), filter); |
| 156 | + } |
| 157 | + |
| 158 | + void setOffsetForSorting(int offset) { |
| 159 | + this.sortOffset = offset; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String getDocumentFilter() { |
| 164 | + String filter = filtersByOffset.get(Integer.valueOf(sortOffset)); |
| 165 | + return filter != null ? filter : ""; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public String getDocumentFilter(int offset) { |
| 170 | + String filter = filtersByOffset.get(Integer.valueOf(offset)); |
| 171 | + return filter != null ? filter : ""; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public int getRankCategory() { |
| 176 | + return 5; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public int getRankScore() { |
| 181 | + return 0; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments