<commit_before>/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.ui.tooling.preview

import android.content.Context
import android.graphics.Canvas
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.widget.FrameLayout
import androidx.annotation.VisibleForTesting
import androidx.compose.Composable
import androidx.compose.Composition
import androidx.compose.Providers
import androidx.compose.currentComposer
import androidx.compose.disposeComposition
import androidx.ui.core.FontLoaderAmbient
import androidx.ui.core.setContent
import androidx.ui.core.toFrameworkRect
import androidx.ui.graphics.Color
import androidx.ui.graphics.toArgb
import androidx.ui.tooling.Group
import androidx.ui.tooling.Inspectable
import androidx.ui.tooling.asTree
import androidx.ui.tooling.tables
import androidx.ui.unit.IntPx
import androidx.ui.unit.IntPxBounds
import androidx.ui.unit.PxBounds
import androidx.ui.unit.toPx
import androidx.ui.unit.toRect

const val TOOLS_NS_URI = "http://schemas.android.com/tools"

/**
 * Class containing the minimum information needed by the Preview to map components to the
 * source code and render boundaries.
 *
 * @hide
 */
data class ViewInfo(
    val fileName: String,
    val lineNumber: Int,
    val methodName: String,
    val bounds: IntPxBounds,
    val children: List<ViewInfo>
) {
    fun hasBounds(): Boolean = bounds.bottom != IntPx.Zero && bounds.right != IntPx.Zero

    fun allChildren(): List<ViewInfo> =
        children + children.flatMap { it.allChildren() }

    override fun toString(): String =
        """($fileName:$lineNumber,
            |bounds=(top=${bounds.top.value}, left=${bounds.left.value},
            |bottom=${bounds.bottom.value}, right=${bounds.right.value}),
            |childrenCount=${children.size})""".trimMargin()
}

/**
 * Regular expression that matches and extracts the key information as serialized in
 * [KeySourceInfo#recordSourceKeyInfo]. The expression supports two formats for backwards
 * compatibility:
 *
 *  - fileName:lineNumber
 *  - methodName (fileName:lineNumber)
 */
private val KEY_INFO_REGEX =
    """(?<method>[\w\\.$]*?)\s?\(?(?<fileName>[\w.]+):(?<lineNumber>\d+)\)?""".toRegex()

/**<commit_msg>Removing named groups from regex<commit_after>/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.ui.tooling.preview

import android.content.Context
import android.graphics.Canvas
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.widget.FrameLayout
import androidx.annotation.VisibleForTesting
import androidx.compose.Composable
import androidx.compose.Composition
import androidx.compose.Providers
import androidx.compose.currentComposer
import androidx.compose.disposeComposition
import androidx.ui.core.FontLoaderAmbient
import androidx.ui.core.setContent
import androidx.ui.core.toFrameworkRect
import androidx.ui.graphics.Color
import androidx.ui.graphics.toArgb
import androidx.ui.tooling.Group
import androidx.ui.tooling.Inspectable
import androidx.ui.tooling.asTree
import androidx.ui.tooling.tables
import androidx.ui.unit.IntPx
import androidx.ui.unit.IntPxBounds
import androidx.ui.unit.PxBounds
import androidx.ui.unit.toPx
import androidx.ui.unit.toRect

const val TOOLS_NS_URI = "http://schemas.android.com/tools"

/**
 * Class containing the minimum information needed by the Preview to map components to the
 * source code and render boundaries.
 *
 * @hide
 */
data class ViewInfo(
    val fileName: String,
    val lineNumber: Int,
    val methodName: String,
    val bounds: IntPxBounds,
    val children: List<ViewInfo>
) {
    fun hasBounds(): Boolean = bounds.bottom != IntPx.Zero && bounds.right != IntPx.Zero

    fun allChildren(): List<ViewInfo> =
        children + children.flatMap { it.allChildren() }

    override fun toString(): String =
        """($fileName:$lineNumber,
            |bounds=(top=${bounds.top.value}, left=${bounds.left.value},
            |bottom=${bounds.bottom.value}, right=${bounds.right.value}),
            |childrenCount=${children.size})""".trimMargin()
}

/**
 * Regular expression that matches and extracts the key information as serialized in
 * [KeySourceInfo#recordSourceKeyInfo]. The expression supports two formats for backwards
 * compatibility:
 *
 *  - fileName:lineNumber
 *  - methodName (fileName:lineNumber)
 *
 *  API <=21 does not support named regex but for documentation purposes, the named version
 *  of the regex would be:
 *  `(?<method>[\w\\.$]*?)\s?\(?(?<fileName>[\w.]+):(?<lineNumber>\d+)\)?`
 */
private val KEY_INFO_REGEX =
    """([\w\\.$]*?)\s?\(?([\w.]+):(\d+)\)?""".toRegex()

/**
{'fraction_non_alphanumeric': 0.11193903471634209, 'fraction_numerical': 0.003048264182895851, 'mean_word_length': 4.301615798922801, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3636343', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1686, 'n_words': 494}