Substring long scratch output with new lines
^KT-24016 Fixed
This commit is contained in:
+23
-13
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.scratch.ScratchExpression
|
|||||||
import org.jetbrains.kotlin.idea.scratch.ScratchFile
|
import org.jetbrains.kotlin.idea.scratch.ScratchFile
|
||||||
|
|
||||||
object InlayScratchOutputHandler : ScratchOutputHandler {
|
object InlayScratchOutputHandler : ScratchOutputHandler {
|
||||||
|
private const val maxLineLength = 120
|
||||||
private const val maxInsertOffset = 60
|
private const val maxInsertOffset = 60
|
||||||
private const val minSpaceCount = 4
|
private const val minSpaceCount = 4
|
||||||
|
|
||||||
@@ -32,12 +33,9 @@ object InlayScratchOutputHandler : ScratchOutputHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun handle(file: ScratchFile, expression: ScratchExpression, output: ScratchOutput) {
|
override fun handle(file: ScratchFile, expression: ScratchExpression, output: ScratchOutput) {
|
||||||
val inlayText = StringUtil.shortenTextWithEllipsis(output.text.substringBefore("\n"), 50, 0)
|
if (output.text.isBlank()) return
|
||||||
if (inlayText != output.text && output.type != ScratchOutputType.ERROR) {
|
|
||||||
ToolWindowScratchOutputHandler.handle(file, expression, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
createInlay(file, expression.lineStart, inlayText, output.type)
|
createInlay(file, expression, output)
|
||||||
|
|
||||||
if (output.type == ScratchOutputType.ERROR) {
|
if (output.type == ScratchOutputType.ERROR) {
|
||||||
ToolWindowScratchOutputHandler.handle(file, expression, output)
|
ToolWindowScratchOutputHandler.handle(file, expression, output)
|
||||||
@@ -57,32 +55,45 @@ object InlayScratchOutputHandler : ScratchOutputHandler {
|
|||||||
ToolWindowScratchOutputHandler.clear(file)
|
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 {
|
UIUtil.invokeLaterIfNeeded {
|
||||||
val editor = file.editor.editor
|
val editor = file.editor.editor
|
||||||
|
val line = expression.lineStart
|
||||||
|
|
||||||
val lineStartOffset = editor.document.getLineStartOffset(line)
|
val lineStartOffset = editor.document.getLineStartOffset(line)
|
||||||
val lineEndOffset = editor.document.getLineEndOffset(line)
|
val lineEndOffset = editor.document.getLineEndOffset(line)
|
||||||
val lineLength = lineEndOffset - lineStartOffset
|
val lineLength = lineEndOffset - lineStartOffset
|
||||||
var spaceCount = maxLineLength(file) - lineLength + minSpaceCount
|
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
|
val existing = editor.inlayModel
|
||||||
.getInlineElementsInRange(lineEndOffset, lineEndOffset)
|
.getInlineElementsInRange(lineEndOffset, lineEndOffset)
|
||||||
.singleOrNull { it.renderer is InlayScratchFileRenderer }
|
.singleOrNull { it.renderer is InlayScratchFileRenderer }
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
existing.dispose()
|
existing.dispose()
|
||||||
editor.inlayModel.addInlineElement(
|
addInlay(((existing.renderer as InlayScratchFileRenderer).text + "; " + output.text).drop(spaceCount))
|
||||||
lineEndOffset,
|
|
||||||
InlayScratchFileRenderer((existing.renderer as InlayScratchFileRenderer).text + "; " + inlayText, outputType)
|
|
||||||
)
|
|
||||||
} else {
|
} 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 {
|
private fun maxLineLength(file: ScratchFile): Int {
|
||||||
val doc = file.editor.editor.document
|
val doc = file.editor.editor.document
|
||||||
return (0 until doc.lineCount)
|
return (0 until doc.lineCount)
|
||||||
@@ -99,5 +110,4 @@ object InlayScratchOutputHandler : ScratchOutputHandler {
|
|||||||
.forEach { Disposer.dispose(it) }
|
.forEach { Disposer.dispose(it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
foo() // ERROR: error: unresolved reference: foo
|
foo() // ERROR: error: unresolved reference: foo...
|
||||||
/** unresolved.kts:1 error: unresolved reference: foo
|
/** unresolved.kts:1 error: unresolved reference: foo
|
||||||
|
|
||||||
foo()
|
foo()
|
||||||
|
|||||||
+1
-1
@@ -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
|
1 + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
foo.forEach { // ERROR: error: unresolved reference: foo
|
foo.forEach { // ERROR: error: unresolved reference: foo...
|
||||||
1 + 1
|
1 + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
fun goo(a: String) { // ERROR: error: unresolved reference: goo
|
fun goo(a: String) { // ERROR: error: unresolved reference: goo...
|
||||||
super.goo(a)
|
super.goo(a)
|
||||||
}
|
}
|
||||||
/** unresolvedMultiline.kts:1 error: unresolved reference: foo
|
/** unresolvedMultiline.kts:1 error: unresolved reference: foo
|
||||||
|
|||||||
+8
-3
@@ -1,3 +1,8 @@
|
|||||||
val a = "a".repeat(100) // RESULT: val a: String
|
val a = "a".repeat(100) // RESULT: val a: String
|
||||||
a // RESULT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
|
a // RESULT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
|
||||||
/** veryLongOutput.kts:2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */
|
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
@@ -1,2 +1,4 @@
|
|||||||
val a = "a".repeat(100)
|
val a = "a".repeat(100)
|
||||||
a
|
a
|
||||||
|
val b = "0123456789\n".repeat(10)
|
||||||
|
b
|
||||||
+14
-2
@@ -1,3 +1,15 @@
|
|||||||
val a = "a".repeat(100)
|
val a = "a".repeat(100)
|
||||||
a // RESULT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
|
a // RESULT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
|
||||||
/** veryLongOutput.kts:2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */
|
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 */
|
||||||
Reference in New Issue
Block a user