summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interpreter/session/FileBackedHistory.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-03-15 02:56:39 +0000
committerPaul Phillips <paulp@improving.org>2011-03-15 02:56:39 +0000
commit7a29fc7de3465a064ffc6e0422d5df4bf39b36a3 (patch)
treef691e99c2358806c1d239856393f69278bbfaf02 /src/compiler/scala/tools/nsc/interpreter/session/FileBackedHistory.scala
parent048fe68a1f533a789bd39b80ce9793861d19df48 (diff)
downloadscala-7a29fc7de3465a064ffc6e0422d5df4bf39b36a3.tar.gz
scala-7a29fc7de3465a064ffc6e0422d5df4bf39b36a3.tar.bz2
scala-7a29fc7de3465a064ffc6e0422d5df4bf39b36a3.zip
Fix for a jline/history bug when the history fi...
Fix for a jline/history bug when the history file didn't already exist. Also expunged a bunch of history code which didn't get where it was going, including everything involving shutdown hooks. No review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/interpreter/session/FileBackedHistory.scala')
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/session/FileBackedHistory.scala57
1 files changed, 18 insertions, 39 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/session/FileBackedHistory.scala b/src/compiler/scala/tools/nsc/interpreter/session/FileBackedHistory.scala
index 57b0cd9104..5f11a0ea8f 100644
--- a/src/compiler/scala/tools/nsc/interpreter/session/FileBackedHistory.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/session/FileBackedHistory.scala
@@ -15,38 +15,12 @@ import FileBackedHistory._
trait FileBackedHistory extends JLineHistory with JPersistentHistory {
def maxSize: Int
protected lazy val historyFile: File = defaultFile
- protected def useShutdownHook = true
-
- // A tempfile holding only the last few lines of input
- private val bufferFile = File.makeTemp("scala_history")
- private var bufferWriter = bufferFile.printWriter()
-
- // flush to the permanent log every `flushFrequency` lines
- protected def flushFrequency: Int = if (useShutdownHook) 15 else 5
- private var flushCounter: Int = flushFrequency
-
- private def autoflush(): Unit = {
- flushCounter -= 1
- if (flushCounter <= 0) {
- flush()
- flushCounter = flushFrequency
- }
- }
+ private var isPersistent = true
locally {
load()
- if (useShutdownHook)
- sys addShutdownHook flush()
- }
- private def drainBufferFile() = {
- if (bufferWriter != null)
- bufferWriter.close()
-
- try bufferFile.lines().toList map (_ + "\n")
- finally bufferWriter = bufferFile.printWriter()
}
- private var isPersistent = true
def withoutSaving[T](op: => T): T = {
val saved = isPersistent
isPersistent = false
@@ -54,35 +28,40 @@ trait FileBackedHistory extends JLineHistory with JPersistentHistory {
finally isPersistent = saved
}
def addLineToFile(item: CharSequence): Unit = {
- if (isPersistent) {
- bufferWriter println item
- autoflush()
- }
+ if (isPersistent)
+ append(item + "\n")
}
+ /** Overwrites the history file with the current memory. */
protected def sync(): Unit = {
val lines = asStrings map (_ + "\n")
historyFile.writeAll(lines: _*)
}
+ /** Append one or more lines to the history file. */
+ protected def append(lines: String*): Unit = {
+ historyFile.appendAll(lines: _*)
+ }
def load(): Unit = {
- val lines = historyFile.lines().toIndexedSeq
+ if (!historyFile.canRead)
+ historyFile.createFile()
+
+ val lines: IndexedSeq[String] =
+ try historyFile.lines().toIndexedSeq
+ catch { case _: Exception => Vector() }
+
repldbg("Loading " + lines.size + " into history.")
- // bypass the tempfile buffer
+ // avoid writing to the history file
withoutSaving(lines takeRight maxSize foreach add)
- // truncate the main history file if it's too big.
+ // truncate the history file if it's too big.
if (lines.size > maxSize) {
repldbg("File exceeds maximum size: truncating to " + maxSize + " entries.")
sync()
}
moveToEnd()
}
- def flush(): Unit = {
- val toAppend = drainBufferFile()
- repldbg("Moving " + toAppend.size + " lines from buffer to permanent history.")
- historyFile.appendAll(toAppend: _*)
- }
+ def flush(): Unit = ()
def purge(): Unit = historyFile.truncate()
}