Skip to content

Commit

Permalink
A bit more clean-up and simplification.
Browse files Browse the repository at this point in the history
  • Loading branch information
SCWells72 committed Dec 10, 2024
1 parent 6fdf7fc commit 0d10203
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<FoldingRange> mockFoldingRanges = JSONUtils.getLsp4jGson().fromJson(mockFoldingRangesJson, new TypeToken<List<FoldingRange>>() {
}.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
Expand All @@ -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<Integer, Integer, Integer> 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());
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextRange> expectedTextRanges = getExpectedTextRanges(fileBody);

MockLanguageServer.INSTANCE.setTimeToProceedQueries(100);
List<FoldingRange> mockFoldingRanges = JSONUtils.getLsp4jGson().fromJson(mockFoldingRangesJson, new TypeToken<List<FoldingRange>>() {
}.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
Expand All @@ -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<TextRange> expectedTextRanges = getExpectedTextRanges(fileBody);
assertEquals(expectedTextRanges.size(), foldRegions.length);

for (int i = 0; i < foldRegions.length; i++) {
Expand All @@ -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<TextRange> getExpectedTextRanges(@NotNull String fileBody) {
// Gather raw start and end token offsets
Expand Down Expand Up @@ -131,28 +134,28 @@ private static List<TextRange> 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<Integer> 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<Integer> 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);
}
Expand All @@ -166,9 +169,4 @@ private static List<TextRange> getExpectedTextRanges(@NotNull String fileBody) {
}
return expectedTextRanges;
}

@NotNull
private static String removeTokens(@NotNull String fileBody) {
return fileBody.replaceAll(TOKEN_PATTERN.pattern(), "");
}
}

0 comments on commit 0d10203

Please sign in to comment.