Substring long scratch output with new lines

^KT-24016 Fixed
This commit is contained in:
Natalia Selezneva
2018-05-16 14:58:54 +03:00
parent 94bb19c765
commit 5a7b58c615
7 changed files with 52 additions and 23 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.scratch.ScratchExpression
import org.jetbrains.kotlin.idea.scratch.ScratchFile
object InlayScratchOutputHandler : ScratchOutputHandler {
private const val maxLineLength = 120
private const val maxInsertOffset = 60
private const val minSpaceCount = 4
@@ -32,12 +33,9 @@ object InlayScratchOutputHandler : ScratchOutputHandler {
}
override fun handle(file: ScratchFile, expression: ScratchExpression, output: ScratchOutput) {
val inlayText = StringUtil.shortenTextWithEllipsis(output.text.substringBefore("\n"), 50, 0)
if (inlayText != output.text && output.type != ScratchOutputType.ERROR) {
ToolWindowScratchOutputHandler.handle(file, expression, output)
}
if (output.text.isBlank()) return
createInlay(file, expression.lineStart, inlayText, output.type)
createInlay(file, expression, output)
if (output.type == ScratchOutputType.ERROR) {
ToolWindowScratchOutputHandler.handle(file, expression, output)
@@ -57,32 +55,45 @@ object InlayScratchOutputHandler : ScratchOutputHandler {
ToolWindowScratchOutputHandler.clear(file)
}
private fun createInlay(file: ScratchFile, line: Int, inlayText: String, outputType: ScratchOutputType) {
private fun createInlay(file: ScratchFile, expression: ScratchExpression, output: ScratchOutput) {
UIUtil.invokeLaterIfNeeded {
val editor = file.editor.editor
val line = expression.lineStart
val lineStartOffset = editor.document.getLineStartOffset(line)
val lineEndOffset = editor.document.getLineEndOffset(line)
val lineLength = lineEndOffset - lineStartOffset
var spaceCount = maxLineLength(file) - lineLength + minSpaceCount
while(spaceCount + lineLength > maxInsertOffset && spaceCount > minSpaceCount) spaceCount--
while (spaceCount + lineLength > maxInsertOffset && spaceCount > minSpaceCount) spaceCount--
fun addInlay(text: String) {
val textBeforeNewLine = if (StringUtil.containsLineBreak(text)) text.substringBefore("\n") + "..." else text
val shortText = StringUtil.shortenTextWithEllipsis(textBeforeNewLine, maxLineLength - spaceCount - lineLength, 0)
if (shortText != text) {
printToToolWindow(file, expression, output)
}
editor.inlayModel.addInlineElement(lineEndOffset, InlayScratchFileRenderer(" ".repeat(spaceCount) + shortText, output.type))
}
val existing = editor.inlayModel
.getInlineElementsInRange(lineEndOffset, lineEndOffset)
.singleOrNull { it.renderer is InlayScratchFileRenderer }
if (existing != null) {
existing.dispose()
editor.inlayModel.addInlineElement(
lineEndOffset,
InlayScratchFileRenderer((existing.renderer as InlayScratchFileRenderer).text + "; " + inlayText, outputType)
)
addInlay(((existing.renderer as InlayScratchFileRenderer).text + "; " + output.text).drop(spaceCount))
} else {
editor.inlayModel.addInlineElement(lineEndOffset, InlayScratchFileRenderer(" ".repeat(spaceCount) + inlayText, outputType))
addInlay(output.text)
}
}
}
private fun printToToolWindow(file: ScratchFile, expression: ScratchExpression, output: ScratchOutput) {
if (output.type != ScratchOutputType.ERROR) {
ToolWindowScratchOutputHandler.handle(file, expression, output)
}
}
private fun maxLineLength(file: ScratchFile): Int {
val doc = file.editor.editor.document
return (0 until doc.lineCount)
@@ -99,5 +110,4 @@ object InlayScratchOutputHandler : ScratchOutputHandler {
.forEach { Disposer.dispose(it) }
}
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
foo() // ERROR: error: unresolved reference: foo
foo() // ERROR: error: unresolved reference: foo...
/** unresolved.kts:1 error: unresolved reference: foo
foo()
+1 -1
View File
@@ -1,4 +1,4 @@
foo.forEach { // ERROR: Unresolved reference: foo; Cannot choose among the following candidates wi...
foo.forEach { // ERROR: Unresolved reference: foo; Cannot choose among the following candidates without completing ty...
1 + 1
}
+2 -2
View File
@@ -1,8 +1,8 @@
foo.forEach { // ERROR: error: unresolved reference: foo
foo.forEach { // ERROR: error: unresolved reference: foo...
1 + 1
}
fun goo(a: String) { // ERROR: error: unresolved reference: goo
fun goo(a: String) { // ERROR: error: unresolved reference: goo...
super.goo(a)
}
/** unresolvedMultiline.kts:1 error: unresolved reference: foo
+8 -3
View File
@@ -1,3 +1,8 @@
val a = "a".repeat(100) // RESULT: val a: String
a // RESULT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
/** veryLongOutput.kts:2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */
val a = "a".repeat(100) // RESULT: val a: String
a // RESULT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
val b = "0123456789\n".repeat(10) // RESULT: val b: String
b // RESULT: 0123456789; 0123456789; 0123456789; 0123456789; 0123456789; 0123456789; 01234567...
/** veryLongOutput.kts:2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */
/** veryLongOutput.kts:4 0123456789 */
/** veryLongOutput.kts:4 0123456789 */
/** veryLongOutput.kts:4 0123456789 */
+3 -1
View File
@@ -1,2 +1,4 @@
val a = "a".repeat(100)
a
a
val b = "0123456789\n".repeat(10)
b
+14 -2
View File
@@ -1,3 +1,15 @@
val a = "a".repeat(100)
a // RESULT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
/** veryLongOutput.kts:2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */
a // RESULT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
val b = "0123456789\n".repeat(10)
b // RESULT: 0123456789...
/** veryLongOutput.kts:2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */
/** veryLongOutput.kts:4 0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789 */