<commit_before>/*
 * Minecraft Dev for IntelliJ
 *
 * https://minecraftdev.org
 *
 * Copyright (c) 2017 minecraft-dev
 *
 * MIT License
 */

@file:JvmName("UtilKt")
package com.demonwav.mcdev.util

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import org.jetbrains.annotations.Contract

// Kotlin functions
inline fun <T> runInlineReadAction(func: () -> T): T {
    return ApplicationManager.getApplication().acquireReadActionLock().use { func() }
}

inline fun runWriteTask(crossinline func: () -> Unit) {
    invokeAndWait { ApplicationManager.getApplication().runWriteAction { func() } }
}

inline fun runWriteTaskLater(crossinline func: () -> Unit) {
    invokeLater { ApplicationManager.getApplication().runWriteAction { func() } }
}

inline fun invokeAndWait(crossinline func: () -> Unit) {
    ApplicationManager.getApplication().invokeAndWait({ func() }, ModalityState.any())
}

inline fun invokeLater(crossinline func: () -> Unit) {
    ApplicationManager.getApplication().invokeLater({ func() }, ModalityState.any())
}

/**
 * Returns an untyped array for the specified [Collection].
 */
@Contract(pure = true)
fun Collection<*>.toArray(): Array<Any?> {
    @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
    return (this as java.util.Collection<*>).toArray()
}

@Contract(pure = true)
inline fun <T, R> Iterable<T>.mapFirstNotNull(transform: (T) -> R?): R? {
    forEach { transform(it)?.let { return it } }
    return null
}

@Contract(pure = true)
inline fun <T, R> Array<T>.mapFirstNotNull(transform: (T) -> R?): R? {
    return asIterable().mapFirstNotNull(transform)
}

inline fun <T, reified R> List<T>.mapToArray(transform: (T) -> R): Array<R> {
    return Array(size) { i -> transform(this[i]) }
}

object Util {
    // Java static methods
    // Can't call inlined stuff from java
    @JvmStatic
    fun runWriteTask(func: Runnable) {
        runWriteTask { func.run() }
    }

    @JvmStatic
    fun runWriteTaskLater(func: Runnable) {
        runWriteTaskLater { func.run() }
    }

    @JvmStatic
    fun invokeAndWait(func: Runnable) {
        invokeAndWait { func.run() }
    }

    @JvmStatic<commit_msg>Don't use Iterable for Arrays<commit_after>/*
 * Minecraft Dev for IntelliJ
 *
 * https://minecraftdev.org
 *
 * Copyright (c) 2017 minecraft-dev
 *
 * MIT License
 */

@file:JvmName("UtilKt")
package com.demonwav.mcdev.util

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import org.jetbrains.annotations.Contract

// Kotlin functions
inline fun <T> runInlineReadAction(func: () -> T): T {
    return ApplicationManager.getApplication().acquireReadActionLock().use { func() }
}

inline fun runWriteTask(crossinline func: () -> Unit) {
    invokeAndWait { ApplicationManager.getApplication().runWriteAction { func() } }
}

inline fun runWriteTaskLater(crossinline func: () -> Unit) {
    invokeLater { ApplicationManager.getApplication().runWriteAction { func() } }
}

inline fun invokeAndWait(crossinline func: () -> Unit) {
    ApplicationManager.getApplication().invokeAndWait({ func() }, ModalityState.any())
}

inline fun invokeLater(crossinline func: () -> Unit) {
    ApplicationManager.getApplication().invokeLater({ func() }, ModalityState.any())
}

/**
 * Returns an untyped array for the specified [Collection].
 */
@Contract(pure = true)
fun Collection<*>.toArray(): Array<Any?> {
    @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
    return (this as java.util.Collection<*>).toArray()
}

@Contract(pure = true)
inline fun <T, R> Iterable<T>.mapFirstNotNull(transform: (T) -> R?): R? {
    forEach { transform(it)?.let { return it } }
    return null
}

@Contract(pure = true)
inline fun <T, R> Array<T>.mapFirstNotNull(transform: (T) -> R?): R? {
    forEach { transform(it)?.let { return it } }
    return null
}

inline fun <T, reified R> List<T>.mapToArray(transform: (T) -> R): Array<R> {
    return Array(size) { i -> transform(this[i]) }
}

object Util {
    // Java static methods
    // Can't call inlined stuff from java
    @JvmStatic
    fun runWriteTask(func: Runnable) {
        runWriteTask { func.run() }
    }

    @JvmStatic
    fun runWriteTaskLater(func: Runnable) {
        runWriteTaskLater { func.run() }
    }

    @JvmStatic
    fun invokeAndWait(func: Runnable) {
        invokeAndWait { func.run() }
    }

    @JvmStatic
{'fraction_non_alphanumeric': 0.1434389140271493, 'fraction_numerical': 0.0018099547511312218, 'mean_word_length': 3.9730033745781776, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6880950', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1401, 'n_words': 313}