Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for multiline pasting #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jaakkonakaza
Copy link
Member

@jaakkonakaza jaakkonakaza commented Nov 26, 2024

Related YouTrack issue: https://youtrack.jetbrains.com/issue/SCL-20209/Scala-REPL-proper-multiline-paste-support-for-Scala-3-indentation-based-syntax

This PR is a suggestion of a temporary fix and contains information about the problem and suggestions for possible fixes.

Scala 3.4.2 updated their JLine version to 3.24.1. JLine has different behavior for different terminal types and if it can't detect a terminal it falls back to a "dumb terminal". JLine supports bracketed paste but they disabled it for dumb terminals in 3.18.0 (commit 542bfb6).

IntelliJ doesn't use bracketed paste for their integrated terminals. If using the "old terminal" in IntelliJ, it directly uses the system's terminal, therefore properly supporting bracketed paste and JLine detects the terminal type:

scala-cli
Welcome to Scala 3.5.2 (23.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                                                                                              
scala> sys.env.get("TERM")
val res0: Option[String] = Some(xterm-256color)

Scala REPL in IntelliJ:

scala> sys.env.get("TERM")
val res0: Option[String] = Some(dumb)

Switching to the "new terminal" (Enable New Terminal Beta), it still detects the correct terminal type, but because the pasting is handled by IntelliJ, it doesn't use bracketed paste.

The JLine terminal detection checks for TTY access, which fails for the IntelliJ Scala REPL. There are other instances of JLine problems in IntelliJ too ([1], [2]), so the issue is larger than just the REPL. The IntelliJ Scala plugin also sets the dumb flag because it does not work with non-dumb terminals.

As a temporary workaround I decided to just remove all empty lines from the input. In non-bracketed mode, empty lines finish the statement. Removing them should work in most cases, except when there is empty lines in multiline strings

Welcome to Scala 3.6.2-RC1 (22.0.2, Java OpenJDK 64-Bit Server VM).
Loaded A+ Courses module O1Library. Auto-imported package [o1] for your convenience.
Write a line (or more) of Scala and press Enter to run it. Use Up and Down to scroll through your earlier inputs.
Changes to the module are not loaded automatically. If you edit the files, restart the REPL with Meta+R or the icon on the left.
Type in expressions for evaluation. Or try :help.

scala> class Foo:
   def foo = ???
 
   def bar = ???
 
   val a = "a"
 
   val b =
     """
     b
 
 """
 
   val c =
     """
       |c
       |
       |""".stripMargin
 end Foo
// defined class Foo

scala> val foo = Foo()
val foo: Foo = Foo@2ace1cd3

scala> foo.b
val res0: String = "
    b
"

scala> foo.c
val res1: String = "
c

"

Possible fixes for the problem are:

  • Adding some flag to JLine to enable bracketed paste for dumb terminals
  • Fixing the bug in IntelliJ which breaks JLine (that I think has existed for 10 years)
  • Building our own REPL driver that doesn't use JLine and making a custom UI for it in IntelliJ

A possible fix on the IntelliJ side would be to use the same terminal logic that the new terminal uses, but it would require changes in the main IntelliJ codebase, the IntelliJ Scala plugin and possibly JLine.

The custom UI will also make it easier to extend the REPL, for example adding graphics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant