<commit_before>package com.google.r4a.adapters

import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout

private var registered = false
private val View.layoutBuilder: LayoutBuilder
    get() {
        if (!registered) {
            registerHandlers()
        }
        return getOrAddLayoutBuilderAdapter()
    }

private fun registerHandlers() {
    registerIntLayoutHandler(android.R.attr.layout_width) { width = it }
    registerIntLayoutHandler(android.R.attr.layout_height) { height = it }
    registerFloatLayoutHandler(android.R.attr.layout_weight) {
        when (this) {
            is LinearLayout.LayoutParams -> weight = it
            else -> error("weight not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_gravity) {
        when (this) {
            is LinearLayout.LayoutParams -> gravity = it
            is FrameLayout.LayoutParams -> gravity = it
            else -> error("gravity not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_margin) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> setMargins(it, it, it, it)
            else -> error("margin not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_marginTop) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> topMargin = it
            else -> error("marginTop not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_marginLeft) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> leftMargin = it
            else -> error("marginLeft not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_marginBottom) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> bottomMargin = it
            else -> error("marginBottom not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_marginRight) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> rightMargin = it
            else -> error("marginRight not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registered = true
}

private fun View.setPixelLayoutWidth(width: Int) = layoutBuilder.set(android.R.attr.layout_width, width)
private fun View.setPixelLayoutHeight(height: Int) = layoutBuilder.set(android.R.attr.layout_height, height)
private fun View.setPixelMarginTop(pixels: Int) = layoutBuilder.set(android.R.attr.layout_marginTop, pixels)
private fun View.setPixelMarginLeft(pixels: Int) = layoutBuilder.set(android.R.attr.layout_marginLeft, pixels)
private fun View.setPixelMarginBottom(pixels: Int) = layoutBuilder.set(android.R.attr.layout_marginBottom, pixels)
private fun View.setPixelMarginRight(pixels: Int) = layoutBuilder.set(android.R.attr.layout_marginRight, pixels)
private fun View.setPixelMarginHorizontal(pixels: Int) {
    setPixelMarginLeft(pixels)
    setPixelMarginRight(pixels)
}
private fun View.setPixelMarginVertical(pixels: Int) {
    setPixelMarginTop(pixels)
    setPixelMarginBottom(pixels)
}

fun View.setLayoutWidth(dim: Dimension) = setPixelLayoutWidth(dim.toIntPixels(metrics))
fun View.setLayoutWidth(width: Int) {
    if (width == -1 || width == -2) {
        // It is either MATCH_PARENT, FILL_PARENT or WRAP_CONTENT
        setPixelLayoutWidth(width)
    } else {
        // It is a dimension resource ID.
        setPixelLayoutWidth(resources.getDimensionPixelSize(width.assertDimensionRes()))
    }
}

fun View.setLayoutHeight(dim: Dimension) = setPixelLayoutHeight(dim.toIntPixels(metrics))
fun View.setLayoutHeight(height: Int) {
    if (height == -1 || height == -2) {
        setPixelLayoutHeight(height)
    } else {
        setPixelLayoutHeight(resources.getDimensionPixelSize(height.assertDimensionRes()))
    }
}

fun View.setLayoutGravity(gravity: Int) = layoutBuilder.set(android.R.attr.layout_gravity, gravity)

fun View.setMarginTop(resId: Int) = setPixelMarginTop(resources.getDimensionPixelSize(resId.assertDimensionRes()))
fun View.setMarginLeft(resId: Int) = setPixelMarginLeft(resources.getDimensionPixelSize(resId.assertDimensionRes()))
fun View.setMarginBottom(resId: Int) = setPixelMarginBottom(resources.getDimensionPixelSize(resId.assertDimensionRes()))
fun View.setMarginRight(resId: Int) = setPixelMarginRight(resources.getDimensionPixelSize(resId.assertDimensionRes()))

fun View.setMarginTop(dim: Dimension) = setPixelMarginTop(dim.toIntPixels(metrics))
fun View.setMarginLeft(dim: Dimension) = setPixelMarginLeft(dim.toIntPixels(metrics))
fun View.setMarginBottom(dim: Dimension) = setPixelMarginBottom(dim.toIntPixels(metrics))
fun View.setMarginRight(dim: Dimension) = setPixelMarginRight(dim.toIntPixels(metrics))

fun View.setMarginHorizontal(resId: Int) = setPixelMarginHorizontal(resources.getDimensionPixelSize(resId.assertDimensionRes()))
fun View.setMarginVertical(resId: Int) = setPixelMarginVertical(resources.getDimensionPixelSize(resId.assertDimensionRes()))<commit_msg>[R4A] Add catch-all weight adapter (independent change)<commit_after>package com.google.r4a.adapters

import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import kotlin.reflect.KMutableProperty

private var registered = false
private val View.layoutBuilder: LayoutBuilder
    get() {
        if (!registered) {
            registerHandlers()
        }
        return getOrAddLayoutBuilderAdapter()
    }

private fun setOrError(params: Any, value: Any?, name: String) {
    val klass = params::class
    try {
        val prop = klass.members.find { it.name == name }
        when (prop) {
            is KMutableProperty<*> -> {
                prop.setter.call(params, value)
            }
            else -> error("$name not possible to be set on ${klass.java.name}")
        }
    } catch (e: Exception) {
        error("$name not possible to be set on ${klass.java.name}")
    }
}

private fun registerHandlers() {
    registerIntLayoutHandler(android.R.attr.layout_width) { width = it }
    registerIntLayoutHandler(android.R.attr.layout_height) { height = it }
    registerFloatLayoutHandler(android.R.attr.layout_weight) {
        when (this) {
            is LinearLayout.LayoutParams -> weight = it
            else -> setOrError(this, it, "weight")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_gravity) {
        when (this) {
            is LinearLayout.LayoutParams -> gravity = it
            is FrameLayout.LayoutParams -> gravity = it
            else -> setOrError(this, it, "gravity")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_margin) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> setMargins(it, it, it, it)
            else -> error("margin not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_marginTop) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> topMargin = it
            else -> error("marginTop not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_marginLeft) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> leftMargin = it
            else -> error("marginLeft not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_marginBottom) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> bottomMargin = it
            else -> error("marginBottom not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registerIntLayoutHandler(android.R.attr.layout_marginRight) {
        when (this) {
            is ViewGroup.MarginLayoutParams -> rightMargin = it
            else -> error("marginRight not possible to be set on ${this::class.java.simpleName}")
        }
    }
    registered = true
}

private fun View.setPixelLayoutWidth(width: Int) = layoutBuilder.set(android.R.attr.layout_width, width)
private fun View.setPixelLayoutHeight(height: Int) = layoutBuilder.set(android.R.attr.layout_height, height)
private fun View.setPixelMarginTop(pixels: Int) = layoutBuilder.set(android.R.attr.layout_marginTop, pixels)
private fun View.setPixelMarginLeft(pixels: Int) = layoutBuilder.set(android.R.attr.layout_marginLeft, pixels)
private fun View.setPixelMarginBottom(pixels: Int) = layoutBuilder.set(android.R.attr.layout_marginBottom, pixels)
private fun View.setPixelMarginRight(pixels: Int) = layoutBuilder.set(android.R.attr.layout_marginRight, pixels)
private fun View.setPixelMarginHorizontal(pixels: Int) {
    setPixelMarginLeft(pixels)
    setPixelMarginRight(pixels)
}
private fun View.setPixelMarginVertical(pixels: Int) {
    setPixelMarginTop(pixels)
    setPixelMarginBottom(pixels)
}

fun View.setLayoutWidth(dim: Dimension) = setPixelLayoutWidth(dim.toIntPixels(metrics))
fun View.setLayoutWidth(width: Int) {
    if (width == -1 || width == -2) {
        // It is either MATCH_PARENT, FILL_PARENT or WRAP_CONTENT
        setPixelLayoutWidth(width)
    } else {
        // It is a dimension resource ID.
        setPixelLayoutWidth(resources.getDimensionPixelSize(width.assertDimensionRes()))
    }
}

fun View.setLayoutHeight(dim: Dimension) = setPixelLayoutHeight(dim.toIntPixels(metrics))
fun View.setLayoutHeight(height: Int) {
    if (height == -1 || height == -2) {
        setPixelLayoutHeight(height)
    } else {
        setPixelLayoutHeight(resources.getDimensionPixelSize(height.assertDimensionRes()))
    }
}

fun View.setLayoutGravity(gravity: Int) = layoutBuilder.set(android.R.attr.layout_gravity, gravity)
fun View.setLayoutWeight(weight: Float) = layoutBuilder.set(android.R.attr.layout_weight, weight)

fun View.setMarginTop(resId: Int) = setPixelMarginTop(resources.getDimensionPixelSize(resId.assertDimensionRes()))
fun View.setMarginLeft(resId: Int) = setPixelMarginLeft(resources.getDimensionPixelSize(resId.assertDimensionRes()))
fun View.setMarginBottom(resId: Int) = setPixelMarginBottom(resources.getDimensionPixelSize(resId.assertDimensionRes()))
fun View.setMarginRight(resId: Int) = setPixelMarginRight(resources.getDimensionPixelSize(resId.assertDimensionRes()))

fun View.setMarginTop(dim: Dimension) = setPixelMarginTop(dim.toIntPixels(metrics))
fun View.setMarginLeft(dim: Dimension) = setPixelMarginLeft(dim.toIntPixels(metrics))
fun View.setMarginBottom(dim: Dimension) = setPixelMarginBottom(dim.toIntPixels(metrics))
fun View.setMarginRight(dim: Dimension) = setPixelMarginRight(dim.toIntPixels(metrics))

fun View.setMarginHorizontal(resId: Int) = setPixelMarginHorizontal(resources.getDimensionPixelSize(resId.assertDimensionRes()))
fun View.setMarginVertical(resId: Int) = setPixelMarginVertical(resources.getDimensionPixelSize(resId.assertDimensionRes()))
{'fraction_non_alphanumeric': 0.10742397137745975, 'fraction_numerical': 0.0009838998211091235, 'mean_word_length': 3.8804015713662157, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 36, 'https://': 0, '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': '2652541', 'n_tokens_mistral': 3225, 'n_tokens_neox': 3322, 'n_words': 623}