Alexey Tsvetkov
3 years ago
committed by
GitHub
14 changed files with 303 additions and 42 deletions
@ -0,0 +1,21 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.desktop.ui.tooling.preview.rpc |
||||||
|
|
||||||
|
interface PreviewErrorReporter { |
||||||
|
fun report(e: Throwable, details: String? = null) |
||||||
|
fun report(e: String, details: String? = null) |
||||||
|
} |
||||||
|
|
||||||
|
object StderrPreviewErrorReporter : PreviewErrorReporter { |
||||||
|
override fun report(e: Throwable, details: String?) { |
||||||
|
report(e.stackTraceString) |
||||||
|
} |
||||||
|
|
||||||
|
override fun report(e: String, details: String?) { |
||||||
|
System.err.println(e) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.desktop.ui.tooling.preview.rpc |
||||||
|
|
||||||
|
class PreviewException(message: String) : RuntimeException(message) |
@ -0,0 +1,51 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.desktop.ui.tooling.preview.rpc.utils |
||||||
|
|
||||||
|
internal class RingBuffer<T : Any>(internal val maxSize: Int) : Iterable<T> { |
||||||
|
private var start = 0 |
||||||
|
private var size = 0 |
||||||
|
private val values = arrayOfNulls<Any?>(maxSize) |
||||||
|
|
||||||
|
init { |
||||||
|
check(maxSize > 0) { "Max size should be a positive number: $maxSize" } |
||||||
|
} |
||||||
|
|
||||||
|
fun add(element: T) { |
||||||
|
if (size < maxSize) { |
||||||
|
size++ |
||||||
|
} else { |
||||||
|
start = (start + 1) % maxSize |
||||||
|
} |
||||||
|
values[(start + size - 1) % maxSize] = element |
||||||
|
} |
||||||
|
|
||||||
|
fun addAll(elements: Iterable<T>) { |
||||||
|
elements.forEach { add(it) } |
||||||
|
} |
||||||
|
|
||||||
|
fun clear() { |
||||||
|
start = 0 |
||||||
|
size = 0 |
||||||
|
for (i in values.indices) { |
||||||
|
values[i] = null |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun iterator(): Iterator<T> = |
||||||
|
object : Iterator<T> { |
||||||
|
private var i = 0 |
||||||
|
|
||||||
|
override fun hasNext(): Boolean = i < size |
||||||
|
|
||||||
|
override fun next(): T { |
||||||
|
if (!hasNext()) throw NoSuchElementException() |
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST") |
||||||
|
return values[(start + i++) % maxSize] as T |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,60 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.desktop.ui.tooling.preview.rpc.utils |
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test |
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions.* |
||||||
|
|
||||||
|
internal class RingBufferTest { |
||||||
|
private fun numbers(size: Int): IntArray = IntArray(size) { it } |
||||||
|
private fun testBuffer() = RingBuffer<Int>(4) |
||||||
|
|
||||||
|
@Test |
||||||
|
fun empty() { |
||||||
|
val it = testBuffer().iterator() |
||||||
|
assertFalse(it.hasNext()) |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun addSmall() { |
||||||
|
val buf = testBuffer() |
||||||
|
val expected = numbers(buf.maxSize / 2) |
||||||
|
buf.addAll(expected.asIterable()) |
||||||
|
assertArrayEquals(expected, buf.toList().toIntArray()) |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun addMedium() { |
||||||
|
val buf = testBuffer() |
||||||
|
val expected = numbers(buf.maxSize + buf.maxSize / 2) |
||||||
|
buf.addAll(expected.asIterable()) |
||||||
|
checkAllEquals(expected.takeLast(buf.maxSize), buf.toList()) |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun addBig() { |
||||||
|
val buf = testBuffer() |
||||||
|
val expected = numbers(buf.maxSize * 3) |
||||||
|
buf.addAll(expected.asIterable()) |
||||||
|
checkAllEquals(expected.takeLast(buf.maxSize), buf.toList()) |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun testClear() { |
||||||
|
val buf = testBuffer() |
||||||
|
val expected = numbers(buf.maxSize * 3) |
||||||
|
buf.addAll(expected.asIterable()) |
||||||
|
buf.clear() |
||||||
|
checkAllEquals(emptyList(), buf.toList()) |
||||||
|
} |
||||||
|
|
||||||
|
private fun <T> checkAllEquals(expected: Collection<T>, actual: Collection<T>) { |
||||||
|
val expectedString = expected.joinToString(", ", prefix = "[", postfix = "]") |
||||||
|
val actualString = actual.joinToString(", ", prefix = "[", postfix = "]") |
||||||
|
assertEquals(expectedString, actualString) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.desktop.ide.preview |
||||||
|
|
||||||
|
import com.intellij.openapi.diagnostic.Logger |
||||||
|
import org.jetbrains.compose.desktop.ui.tooling.preview.rpc.PreviewErrorReporter |
||||||
|
|
||||||
|
internal class IdePreviewErrorReporter( |
||||||
|
private val logger: Logger, |
||||||
|
private val previewStateService: PreviewStateService |
||||||
|
) : PreviewErrorReporter { |
||||||
|
override fun report(e: Throwable, details: String?) { |
||||||
|
report(e.stackTraceToString(), details) |
||||||
|
} |
||||||
|
|
||||||
|
override fun report(e: String, details: String?) { |
||||||
|
if (details != null) { |
||||||
|
logger.error(e, details) |
||||||
|
} else { |
||||||
|
logger.error(e) |
||||||
|
} |
||||||
|
previewStateService.clearPreviewOnError() |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue