Skip to content

Commit

Permalink
Further optimize LineListener.
Browse files Browse the repository at this point in the history
Using adapted source code from tut, we now filter out ansi color codes
from the byte output stream avoiding unnecessary intermediary strings.
  • Loading branch information
olafurpg committed May 23, 2019
1 parent 9706bf0 commit e246d85
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 37 deletions.
29 changes: 29 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,32 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```


# License notice for tut

Metals contains parts which are derived from
[tut](https://github.com/tpolecat/tut). We
include the text of the original license below:

```
The MIT License (MIT)
Copyright (c) 2013 Rob Norris
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```
115 changes: 79 additions & 36 deletions metals/src/main/scala/scala/meta/internal/metals/LineListener.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// This file contains adapted source code from tut, see NOTICE.md for LICENSE.
// Original source: https://github.com/tpolecat/tut/blob/e692c74afe7cb9f144f464b97f100c11367c7dfa/modules/core/src/main/scala/tut/AnsiFilterStream.scala
package scala.meta.internal.metals

import java.lang.StringBuilder
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import fansi.ErrorMode
import java.nio.CharBuffer
import scala.collection.mutable

/**
* Converts a stream of strings (with potential ANSI codes and newlines) into a callback for indvidual lines
Expand All @@ -18,57 +21,97 @@ class LineListener(onLine: String => Unit) {
def flush(): Unit = {
if (buffer.length() > 0) {
onLine(buffer.toString())
reset()
buffer = new StringBuilder()
}
}

def appendBytes(bytes: ByteBuffer): this.type = {
appendPlainString(toPlainString(bytes))
}

def appendString(text: String): this.type = {
appendPlainString(toPlainString(text))
appendBytes(
StandardCharsets.UTF_8.encode(CharBuffer.wrap(text.toCharArray()))
)
}

private def appendPlainString(text: String): this.type = {
def loop(start: Int): Unit = {
val newline = text.indexOf('\n', start)
if (newline < 0) {
buffer.append(text, start, text.length())
} else {
onLine(lineSubstring(text, start, newline))
loop(newline + 1)
def appendBytes(bytes: ByteBuffer): this.type = {
val chars = StandardCharsets.UTF_8.decode(bytes)
while (chars.hasRemaining()) {
val ch = chars.get()
// Strategy is, accumulate values as long as we're in a non-terminal state, then either discard
// them if we reach T (which means we accumulated an ANSI escape sequence) or print them out if
// we reach F.
state.apply(ch) match {
case F =>
stack.foreach { i =>
onCharacter(i)
}
onCharacter(ch)
resetState()
case T =>
resetState()
case L =>
flush()
resetState()
case s =>
stack += ch
state = s
}
}
loop(0)
this
}

private def lineSubstring(text: String, from: Int, to: Int): String = {
val end =
// Convert \r\n line breaks into \n line breaks.
if (to > 0 && text.charAt(to - 1) == '\r') to - 1
else to
if (buffer.length() == 0) text.substring(from, end)
else {
val line = buffer.append(text, from, to).toString()
reset()
line
private def onCharacter(char: Char): Unit = {
if (char == '\n') {
flush()
} else {
buffer.append(char)
}
}

private def reset(): Unit = (
buffer = new StringBuilder()
)
case class State(apply: Int => State)

private def toPlainString(buffer: ByteBuffer): String = {
val bytes = new Array[Byte](buffer.remaining())
buffer.get(bytes)
val ansiString = new String(bytes, StandardCharsets.UTF_8)
toPlainString(ansiString)
val S: State = State {
case 27 => I0
case 13 => R // \r
case _ => F
}

private def toPlainString(ansiString: String): String = {
fansi.Str(ansiString, ErrorMode.Sanitize).plainText
val R: State = State {
case 10 => L // drop \r from \r\n
case _ => F
}
val L: State = State(_ => L)

val F: State = State(_ => F)

val T: State = State(_ => T)

val I0: State = State {
case '[' => I1
case _ => F
}

val I1: State = State {
case '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => I2
case _ => F
}

val I2: State = State {
case '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => I2
case ';' => I1
case '@' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' |
'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' |
'X' | 'Y' | 'Z' | '[' | '\\' | ']' | '^' | '_' | '`' | 'a' | 'b' | 'c' |
'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' |
'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | '{' |
'|' | '}' | '~' =>
T // end of ANSI escape
case _ => F
}

private var stack = mutable.ListBuffer.empty[Char]
private var state: State = S // Start
def resetState(): Unit = {
stack.clear()
state = S
}

}
2 changes: 1 addition & 1 deletion tests/unit/src/test/scala/tests/LineListenerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object LineListenerSuite extends BaseSuite {
"eol2", { out =>
out.appendString("a\n\n")
},
List("a", "")
List("a")
)

check(
Expand Down

0 comments on commit e246d85

Please sign in to comment.