redhat-developer / redhat-developer/vscode-java

Formatting code using Java string literals fails.

Open
#3,575 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
2.3k
Forks
546
Avg merge
20h 1m
Merged PRs (30d)
11

Description

Formatting code using Java string literals fails.

Environment
  • Operating System: W11 23H2, Ubuntu 24.04
  • JDK version: OpenJDK 22 2024-03-19 (build 22+36-2370)
  • Java extension version: v1.30.2024041208 (pre-release)

code: 1.88.1 (system setup)
commit: e170252f762678dec6ca2cc69aba1570769a5d39
date: 2024-04-10T17:41:02.734Z
Electron: 28.2.8
ElectronBuildId: 27744544
Chromium: 120.0.6099.291
Node.js: 18.18.2
V8: 12.0.267.19-electron.0
OS: Windows_NT x64 10.0.22631

Steps To Reproduce
  1. git clone https://github.com/Kamilake/Switch
  2. Format Switch.java
[Error - 오전 9:41:44] Request textDocument/formatting failed.
  Message: Internal error.
  Code: -32603 
java.util.concurrent.CompletionException: java.lang.IndexOutOfBoundsException: Index 2983 out of bounds for length 2983
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(Unknown Source)
	at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
	at java.base/java.util.concurrent.CompletableFuture$Completion.exec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: java.lang.IndexOutOfBoundsException: Index 2983 out of bounds for length 2983
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)
	at java.base/java.util.Objects.checkIndex(Unknown Source)
	at java.base/java.util.ArrayList.get(Unknown Source)
	at org.eclipse.jdt.internal.formatter.TokenManager.get(TokenManager.java:79)
	at org.eclipse.jdt.internal.formatter.TokenManager.findIndex(TokenManager.java:173)
	at org.eclipse.jdt.internal.formatter.TokenManager.firstIndexAfter(TokenManager.java:213)
	at org.eclipse.jdt.internal.formatter.linewrap.WrapPreparator.visit(WrapPreparator.java:756)
	at org.eclipse.jdt.core.dom.ConditionalExpression.accept0(ConditionalExpression.java:178)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChild(ASTNode.java:3382)
	at org.eclipse.jdt.core.dom.StringTemplateComponent.accept0(StringTemplateComponent.java:216)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:3405)
	at org.eclipse.jdt.core.dom.StringTemplateExpression.accept0(StringTemplateExpression.java:284)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChild(ASTNode.java:3382)
	at org.eclipse.jdt.core.dom.ReturnStatement.accept0(ReturnStatement.java:128)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:3405)
	at org.eclipse.jdt.core.dom.Block.accept0(Block.java:128)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChild(ASTNode.java:3382)
	at org.eclipse.jdt.core.dom.IfStatement.accept0(IfStatement.java:182)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:3405)
	at org.eclipse.jdt.core.dom.Block.accept0(Block.java:128)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChild(ASTNode.java:3382)
	at org.eclipse.jdt.core.dom.MethodDeclaration.accept0(MethodDeclaration.java:677)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:3405)
	at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:498)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:3405)
	at org.eclipse.jdt.core.dom.CompilationUnit.accept0(CompilationUnit.java:258)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3334)
	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.prepareWraps(DefaultCodeFormatter.java:434)
	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.prepareFormattedCode(DefaultCodeFormatter.java:230)
	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:184)
	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:168)
	at org.eclipse.jdt.ls.core.internal.handlers.FormatterHandler.format(FormatterHandler.java:116)
	at org.eclipse.jdt.ls.core.internal.handlers.FormatterHandler.format(FormatterHandler.java:103)
	at org.eclipse.jdt.ls.core.internal.handlers.FormatterHandler.formatting(FormatterHandler.java:74)
	at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$18(JDTLanguageServer.java:830)
	at org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:87)
	... 7 more
Current Result
public class Switch {
public static String handleSwitch(boolean enabled) {
return STR."{\"success\":\"true\", \"message\":\"Switch \{enabled ? "ON" : "OFF"}!\"}";}}
Expected Result
public class Switch {
    public static String handleSwitch(boolean enabled) {
        return STR."{\"success\":\"true\", \"message\":\"Switch \{enabled ? "ON" : "OFF"}!\"}";
    }
}
Additional Informations

If you need further information, please do not hesitate to contact me!

image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the referenced Switch.java file and start at FormatterHandler.format, following the stack through DefaultCodeFormatter and TokenManager. The work is done when formatting the Java string-template example no longer raises IndexOutOfBoundsException and produces the expected indentation shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.