Skip to content

Commit

Permalink
Issue 822 - Fixed a boundary case issue when a close brace is typed a…
Browse files Browse the repository at this point in the history
…t the very end of a file that would have resulted in an IndexOutOfBoundsException. (redhat-developer#825)

fix: Fixed a boundary case issue when a close brace is typed at the very end of a file that would have resulted in an IndexOutOfBoundsException. (redhat-developer#825)

Fixes redhat-developer#822
  • Loading branch information
SCWells72 authored Feb 10, 2025
1 parent fff525c commit 025e3ef
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,14 @@ private static Result handleCloseBraceTyped(@NotNull Project project,
int beforeOffset = offset - 1;
TextRange codeBlockRange = LSPCodeBlockProvider.getCodeBlockRange(editor, file, beforeOffset);
if (codeBlockRange != null) {
int startOffset = codeBlockRange.getStartOffset();
int endOffset = codeBlockRange.getEndOffset();

// Make sure the range includes the brace pair
Document document = editor.getDocument();
CharSequence documentChars = document.getCharsSequence();

// Constrain the offsets to the valid range of the file
int startOffset = Math.max(codeBlockRange.getStartOffset(), 0);
int endOffset = Math.min(codeBlockRange.getEndOffset(), documentChars.length() - 1);

// Make sure the range includes the brace pair
if ((startOffset > 0) && (documentChars.charAt(startOffset) != openBraceChar)) {
startOffset--;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,77 @@ export class Foo {
);
}

// BOUNDARY TESTS

// language=json
private static final String BOUNDARY_MOCK_SELECTION_RANGE_JSON = """
[
{
"range": {
"start": {
"line": 1,
"character": 0
},
"end": {
"line": 1,
"character": 1
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 15
},
"end": {
"line": 1,
"character": 1
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 1,
"character": 1
}
}
}
}
}
]
""";

// language=json
private static final String BOUNDARY_MOCK_FOLDING_RANGE_JSON = "[]";

// language=json
private static final String BOUNDARY_MOCK_RANGE_FORMATTING_JSON = "[]";

// No language injection here because there are syntax errors
private static final String BOUNDARY_FILE_BODY_BEFORE = """
function foo() {
// type }"""; // NOTE: It's critical that this happen at the VERY end of the file to confirm the fix for 822

// Confirms the fix for https://github.com/redhat-developer/lsp4ij/issues/822
public void testCloseBraceAtEndOfFile() {
assertOnTypeFormatting(
TEST_FILE_NAME,
BOUNDARY_FILE_BODY_BEFORE,
// language=typescript
"""
function foo() {
}""",
BOUNDARY_MOCK_SELECTION_RANGE_JSON,
BOUNDARY_MOCK_FOLDING_RANGE_JSON,
BOUNDARY_MOCK_RANGE_FORMATTING_JSON,
clientConfiguration -> clientConfiguration.format.onTypeFormatting.clientSide.formatOnCloseBrace = true
);
}

// COMPLEX TESTS

// language=json
Expand Down

0 comments on commit 025e3ef

Please sign in to comment.