Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

StringIndexOutOfBounds when using auto-complete #68

Closed
Nacoma opened this issue Feb 25, 2019 · 9 comments
Closed

StringIndexOutOfBounds when using auto-complete #68

Nacoma opened this issue Feb 25, 2019 · 9 comments
Labels

Comments

@Nacoma
Copy link

Nacoma commented Feb 25, 2019

I'm getting this exception when PHPStorm tries to use auto-completion with psalm-language-server. Let me know if there's anything I can add to help determine why this isn't working as intended.

2019-02-25 17:28:36,894 [1067849]  ERROR - plication.impl.ApplicationImpl - String index out of range: -1 
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(String.java:1967)
	at com.github.gtache.lsp.utils.DocumentUtils$.$anonfun$LSPPosToOffset$1(DocumentUtils.scala:82)
	at com.github.gtache.lsp.utils.DocumentUtils$.$anonfun$LSPPosToOffset$1$adapted(DocumentUtils.scala:78)
	at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:963)
	at com.github.gtache.lsp.utils.ApplicationUtils$.computableReadAction(ApplicationUtils.scala:26)
	at com.github.gtache.lsp.utils.DocumentUtils$.LSPPosToOffset(DocumentUtils.scala:78)
	at com.github.gtache.lsp.editor.EditorEventManager.$anonfun$diagnostics$4(EditorEventManager.scala:443)
	at com.github.gtache.lsp.editor.EditorEventManager.$anonfun$diagnostics$4$adapted(EditorEventManager.scala:437)
	at scala.collection.Iterator.foreach(Iterator.scala:937)
	at scala.collection.Iterator.foreach$(Iterator.scala:937)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1425)
	at scala.collection.IterableLike.foreach(IterableLike.scala:70)
	at scala.collection.IterableLike.foreach$(IterableLike.scala:69)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
	at com.github.gtache.lsp.editor.EditorEventManager.$anonfun$diagnostics$1(EditorEventManager.scala:437)
	at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:314)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
2019-02-25 17:28:36,894 [1067849]  ERROR - plication.impl.ApplicationImpl - PhpStorm 2018.3.4  Build #PS-183.5429.47 
2019-02-25 17:28:36,894 [1067849]  ERROR - plication.impl.ApplicationImpl - JDK: 1.8.0_152-release; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o 
2019-02-25 17:28:36,894 [1067849]  ERROR - plication.impl.ApplicationImpl - OS: Linux 
2019-02-25 17:28:36,894 [1067849]  ERROR - plication.impl.ApplicationImpl - Plugin to blame: LSP Support version: 1.5.2 
2019-02-25 17:28:36,894 [1067849]  ERROR - plication.impl.ApplicationImpl - Last Action: EditorChooseLookupItemReplace 
@gtache gtache added the bug label Feb 26, 2019
@Nacoma
Copy link
Author

Nacoma commented Feb 26, 2019

Possibly related, another exception being thrown from the same function in DocumentUtils

java.lang.IndexOutOfBoundsException: Wrong line: 253. Available lines count: 253
	at com.intellij.openapi.editor.impl.LineSet.checkLineIndex(LineSet.java:202)
	at com.intellij.openapi.editor.impl.LineSet.getLineStart(LineSet.java:187)
	at com.intellij.openapi.editor.impl.DocumentImpl.getLineStartOffset(DocumentImpl.java:1020)
	at com.intellij.util.DocumentUtil.getLineTextRange(DocumentUtil.java:104)
	at com.github.gtache.lsp.utils.DocumentUtils$.$anonfun$LSPPosToOffset$1(DocumentUtils.scala:81)
	at com.github.gtache.lsp.utils.DocumentUtils$.$anonfun$LSPPosToOffset$1$adapted(DocumentUtils.scala:78)
	at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:963)
	at com.github.gtache.lsp.utils.ApplicationUtils$.computableReadAction(ApplicationUtils.scala:26)
	at com.github.gtache.lsp.utils.DocumentUtils$.LSPPosToOffset(DocumentUtils.scala:78)
	at com.github.gtache.lsp.editor.EditorEventManager.$anonfun$diagnostics$4(EditorEventManager.scala:443)
	at com.github.gtache.lsp.editor.EditorEventManager.$anonfun$diagnostics$4$adapted(EditorEventManager.scala:437)
	at scala.collection.Iterator.foreach(Iterator.scala:937)
	at scala.collection.Iterator.foreach$(Iterator.scala:937)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1425)
	at scala.collection.IterableLike.foreach(IterableLike.scala:70)
	at scala.collection.IterableLike.foreach$(IterableLike.scala:69)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
	at com.github.gtache.lsp.editor.EditorEventManager.$anonfun$diagnostics$1(EditorEventManager.scala:437)
	at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:314)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

As a side note, this looks to be the same exception seen in #67

@gtache
Copy link
Owner

gtache commented Mar 1, 2019

Both should be fixed in #72.
Thanks for all the reports by the way!

@Nacoma
Copy link
Author

Nacoma commented Mar 5, 2019

The problem happening in the first trace is happening here still.

pos.getCharacter has the value -1.

val lineTextForPosition = if (lineText.nonEmpty) lineText.substring(0, min(lineText.length, pos.getCharacter)) else ""

I would expect that the author of psalm-language-server is using -1 to indicate EOL (which is standard in the language it's written in).

I've checked the LSP spec briefly and I have been unable to determine if negative values are permitted for positions.

@gtache
Copy link
Owner

gtache commented Mar 6, 2019

Ok, I looked a bit into psalm and it seems to use phan on the backend for diagnostics, and like you said phan probably uses -1 for this purpose, but I'm not familiar at all with php.
I could add a special case for -1, but all that the LSP specification says is that Position is zero-based and if the character offset is greater than the length of the line, it equals to the length of the line. So It seems to me that sending -1 is invalid.
Have you also checked the VSCode behaviour?

@Nacoma
Copy link
Author

Nacoma commented Mar 6, 2019

It’s difficult to say what the correct handling is.

I’ve opened an issue requesting clarification on the handling of negative positions.

@gtache
Copy link
Owner

gtache commented Mar 6, 2019

Great thanks !

@muglug
Copy link

muglug commented Mar 8, 2019

Hey! I created Psalm, let me know if I can help. Psalm doesn't use anything but PHP Parser on the backend. Psalm shouldn't be sending any -1 data - if it does, that's a bug that needs to be fixed in Psalm.

@Nacoma
Copy link
Author

Nacoma commented Mar 8, 2019

In LanguageServer/LanguageServer::emitIssues the value of $end_column - 1 is -1 with the following code snippet:

function test(): int {
    return (new Something())
        ->any();
}

I’m away from my computer but I can provide some more info later if necessary.

@Nacoma
Copy link
Author

Nacoma commented Apr 16, 2019

This appears to have been resolved.

@Nacoma Nacoma closed this as completed Apr 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants