From 0d10203e828b311545fed199641b028d203d28cf Mon Sep 17 00:00:00 2001 From: Scott Wells Date: Tue, 10 Dec 2024 16:45:45 -0600 Subject: [PATCH] A bit more clean-up and simplification. --- .../LSPCodeBlockProviderFixtureTestCase.java | 66 +++++++++++-------- .../LSPFoldingRangeFixtureTestCase.java | 40 ++++++----- 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java index f1d0f604f..f235a559a 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java @@ -15,6 +15,7 @@ import com.intellij.openapi.editor.CaretModel; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Trinity; import com.intellij.psi.PsiFile; import com.intellij.testFramework.EditorTestUtil; import com.redhat.devtools.lsp4ij.JSONUtils; @@ -42,38 +43,13 @@ protected LSPCodeBlockProviderFixtureTestCase(String... fileNamePatterns) { protected void assertCodeBlocks(@NotNull String fileName, @NotNull String fileBody, @NotNull String mockFoldingRangesJson) { - // Gather the raw token offsets - int rawCaretOffset = fileBody.indexOf(CARET_TOKEN); - assertFalse("No " + CARET_TOKEN + " found.", rawCaretOffset == -1); - int rawStartOffset = fileBody.indexOf(START_TOKEN); - assertFalse("No " + START_TOKEN + " found.", rawStartOffset == -1); - int rawEndOffset = fileBody.indexOf(END_TOKEN); - assertFalse("No " + END_TOKEN + " found.", rawEndOffset == -1); - - // Compute final offsets as appropriate based on relative token positioning - int caretOffset = rawCaretOffset; - if (rawCaretOffset > rawStartOffset) caretOffset -= START_TOKEN.length(); - if (rawCaretOffset > rawEndOffset) caretOffset -= END_TOKEN.length(); - int startOffset = rawStartOffset; - if (rawStartOffset > rawCaretOffset) startOffset -= CARET_TOKEN.length(); - if (rawStartOffset > rawEndOffset) startOffset -= END_TOKEN.length(); - int endOffset = rawEndOffset; - if (rawEndOffset > rawCaretOffset) endOffset -= CARET_TOKEN.length(); - if (rawEndOffset > rawStartOffset) endOffset -= START_TOKEN.length(); - - // Remove the tokens - fileBody = fileBody - .replace(CARET_TOKEN, "") - .replace(START_TOKEN, "") - .replace(END_TOKEN, ""); - MockLanguageServer.INSTANCE.setTimeToProceedQueries(100); List mockFoldingRanges = JSONUtils.getLsp4jGson().fromJson(mockFoldingRangesJson, new TypeToken>() { }.getType()); MockLanguageServer.INSTANCE.setFoldingRanges(mockFoldingRanges); Project project = myFixture.getProject(); - PsiFile file = myFixture.configureByText(fileName, fileBody); + PsiFile file = myFixture.configureByText(fileName, stripTokens(fileBody)); Editor editor = myFixture.getEditor(); // Initialize the language server @@ -89,6 +65,12 @@ protected void assertCodeBlocks(@NotNull String fileName, CaretModel caretModel = editor.getCaretModel(); + // Derive the caret, start, and end offsets from tokens in the file body + Trinity offsets = getOffsets(fileBody); + int caretOffset = offsets.getFirst(); + int startOffset = offsets.getSecond(); + int endOffset = offsets.getThird(); + caretModel.moveToOffset(caretOffset); CodeBlockUtil.moveCaretToCodeBlockStart(project, editor, false); assertEquals(startOffset, caretModel.getOffset()); @@ -97,4 +79,36 @@ protected void assertCodeBlocks(@NotNull String fileName, CodeBlockUtil.moveCaretToCodeBlockEnd(project, editor, false); assertEquals(endOffset, caretModel.getOffset()); } + + @NotNull + private static String stripTokens(@NotNull String fileBody) { + return fileBody + .replace(CARET_TOKEN, "") + .replace(START_TOKEN, "") + .replace(END_TOKEN, ""); + } + + @NotNull + private static Trinity<@NotNull Integer, @NotNull Integer, @NotNull Integer> getOffsets(@NotNull String fileBody) { + // Gather the raw token offsets + int rawCaretOffset = fileBody.indexOf(CARET_TOKEN); + assertFalse("No " + CARET_TOKEN + " found.", rawCaretOffset == -1); + int rawStartOffset = fileBody.indexOf(START_TOKEN); + assertFalse("No " + START_TOKEN + " found.", rawStartOffset == -1); + int rawEndOffset = fileBody.indexOf(END_TOKEN); + assertFalse("No " + END_TOKEN + " found.", rawEndOffset == -1); + + // Adjust final offsets as appropriate based on relative token positioning + int caretOffset = rawCaretOffset; + if (rawCaretOffset > rawStartOffset) caretOffset -= START_TOKEN.length(); + if (rawCaretOffset > rawEndOffset) caretOffset -= END_TOKEN.length(); + int startOffset = rawStartOffset; + if (rawStartOffset > rawCaretOffset) startOffset -= CARET_TOKEN.length(); + if (rawStartOffset > rawEndOffset) startOffset -= END_TOKEN.length(); + int endOffset = rawEndOffset; + if (rawEndOffset > rawCaretOffset) endOffset -= CARET_TOKEN.length(); + if (rawEndOffset > rawStartOffset) endOffset -= START_TOKEN.length(); + + return Trinity.create(caretOffset, startOffset, endOffset); + } } diff --git a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPFoldingRangeFixtureTestCase.java b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPFoldingRangeFixtureTestCase.java index b471ea0b7..bca39c071 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPFoldingRangeFixtureTestCase.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPFoldingRangeFixtureTestCase.java @@ -51,15 +51,12 @@ protected LSPFoldingRangeFixtureTestCase(String... fileNamePatterns) { protected void assertFoldingRanges(@NotNull String fileName, @NotNull String fileBody, @NotNull String mockFoldingRangesJson) { - // Derive the expected text ranges from the tokenized file body - List expectedTextRanges = getExpectedTextRanges(fileBody); - MockLanguageServer.INSTANCE.setTimeToProceedQueries(100); List mockFoldingRanges = JSONUtils.getLsp4jGson().fromJson(mockFoldingRangesJson, new TypeToken>() { }.getType()); MockLanguageServer.INSTANCE.setFoldingRanges(mockFoldingRanges); - PsiFile file = myFixture.configureByText(fileName, removeTokens(fileBody)); + PsiFile file = myFixture.configureByText(fileName, stripTokens(fileBody)); Editor editor = myFixture.getEditor(); // Initialize the language server @@ -75,7 +72,8 @@ protected void assertFoldingRanges(@NotNull String fileName, FoldingModel foldingModel = editor.getFoldingModel(); FoldRegion[] foldRegions = foldingModel.getAllFoldRegions(); - // We only need to check against start because we confirmed that start and end are the same length above + // Derive the expected text ranges from the tokenized file body + List expectedTextRanges = getExpectedTextRanges(fileBody); assertEquals(expectedTextRanges.size(), foldRegions.length); for (int i = 0; i < foldRegions.length; i++) { @@ -95,6 +93,11 @@ protected void assertFoldingRanges(@NotNull String fileName, } } + @NotNull + private static String stripTokens(@NotNull String fileBody) { + return fileBody.replaceAll(TOKEN_PATTERN.pattern(), ""); + } + @NotNull private static List getExpectedTextRanges(@NotNull String fileBody) { // Gather raw start and end token offsets @@ -131,28 +134,28 @@ private static List getExpectedTextRanges(@NotNull String fileBody) { rawEndOffsets.add(rawEndOffset); } - // Compute final offsets as appropriate based on relative token positioning + // Adjust final offsets as appropriate based on relative token positioning List startOffsets = new ArrayList<>(rawStartOffsets.size()); for (int i = 0; i < rawStartOffsets.size(); i++) { - int rawStartOffset = rawStartOffsets.get(i); - int startOffset = rawStartOffset; - for (Integer otherRawStartOffset : rawStartOffsets) { - if (rawStartOffset > otherRawStartOffset) startOffset -= START_TOKEN_LENGTH; + int currentRawStartOffset = rawStartOffsets.get(i); + int startOffset = currentRawStartOffset; + for (Integer rawStartOffset : rawStartOffsets) { + if (currentRawStartOffset > rawStartOffset) startOffset -= START_TOKEN_LENGTH; } for (int rawEndOffset : rawEndOffsets) { - if (rawStartOffset > rawEndOffset) startOffset -= END_TOKEN_LENGTH; + if (currentRawStartOffset > rawEndOffset) startOffset -= END_TOKEN_LENGTH; } startOffsets.add(startOffset); } List endOffsets = new ArrayList<>(rawEndOffsets.size()); for (int i = 0; i < rawEndOffsets.size(); i++) { - int rawEndOffset = rawEndOffsets.get(i); - int endOffset = rawEndOffset; + int currentRawEndOffset = rawEndOffsets.get(i); + int endOffset = currentRawEndOffset; for (int rawStartOffset : rawStartOffsets) { - if (rawEndOffset > rawStartOffset) endOffset -= START_TOKEN_LENGTH; + if (currentRawEndOffset > rawStartOffset) endOffset -= START_TOKEN_LENGTH; } - for (Integer otherRawEndOffset : rawEndOffsets) { - if (rawEndOffset > otherRawEndOffset) endOffset -= END_TOKEN_LENGTH; + for (Integer rawEndOffset : rawEndOffsets) { + if (currentRawEndOffset > rawEndOffset) endOffset -= END_TOKEN_LENGTH; } endOffsets.add(endOffset); } @@ -166,9 +169,4 @@ private static List getExpectedTextRanges(@NotNull String fileBody) { } return expectedTextRanges; } - - @NotNull - private static String removeTokens(@NotNull String fileBody) { - return fileBody.replaceAll(TOKEN_PATTERN.pattern(), ""); - } }