<commit_before>/*
 * Copyright (C) 2019-2020 <NAME>
 *
 * 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.
 */
/**
 * IntelliJ defines a DSL for creating UIs, but currently has several disadvantages:
 * 1.  It is not supported on IntelliJ 2019.1 or earlier;
 * 2.  It is classified as being in active development, with potential breaking changes between releases;
 * 3.  It is not necessarily comparable to the swing API in terms of available functionality and flexibility.
 *
 * As such, a swing compatible DSL is defined in this file.
 */
package uk.co.reecedunn.intellij.plugin.core.ui.layout

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogBuilder
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.JBTabbedPane
import com.intellij.ui.scale.JBUIScale
import com.intellij.uiDesigner.core.GridConstraints
import com.intellij.uiDesigner.core.Spacer
import com.intellij.util.ui.JBInsets
import com.intellij.util.ui.JBUI
import uk.co.reecedunn.intellij.plugin.core.execution.ui.TextConsoleView
import uk.co.reecedunn.intellij.plugin.core.ui.Borders
import java.awt.*
import javax.swing.JCheckBox
import javax.swing.JPanel
import javax.swing.JTabbedPane

// Layouts

// region panel

fun panel(layout: LayoutManager, init: JPanel.() -> Unit): JPanel {
    val panel = JPanel(layout)
    panel.init()
    return panel
}

fun panel(init: JPanel.() -> Unit): JPanel = panel(GridBagLayout(), init)

fun Container.horizontalPanel(constraints: Any?, init: JPanel.() -> Unit): JPanel {
    if (constraints is GridBagConstraints) {
        constraints.fill = GridBagConstraints.HORIZONTAL
        constraints.weightx = 1.0
    }

    val panel = JPanel(GridBagLayout())
    panel.init()
    add(panel, constraints)
    return panel
}

// endregion
// region grid

fun grid(x: Int, y: Int): GridBagConstraints = GridBagConstraints(
    x, y, 1, 1, 0.0, 0.0,
    GridBagConstraints.CENTER,
    GridBagConstraints.NONE,
    Insets(0, 0, 0, 0),
    0, 0
)

fun GridBagConstraints.size(dx: Int, dy: Int): GridBagConstraints {
    gridwidth = dx
    gridheight = dy
    return this
}

fun GridBagConstraints.padding(x: Int, y: Int): GridBagConstraints {
    insets = JBInsets(y, x, y, x)
    return this
}

fun GridBagConstraints.vgap(y: Int): GridBagConstraints {
    insets = JBInsets(0, 0, y, 0)
    return this
}

// endregion
// region scrollable

fun Container.scrollable(view: Component?, constraints: Any?, init: JBScrollPane.() -> Unit): JBScrollPane {
    if (constraints is GridBagConstraints) {
        constraints.gridwidth = GridBagConstraints.REMAINDER
        constraints.fill = GridBagConstraints.BOTH
        constraints.weightx = 1.0
        constraints.weighty = 1.0
    }

    val pane = JBScrollPane(view)
    pane.init()
    add(pane, constraints)
    return pane
}

fun Container.scrollable(constraints: Any?, init: JBScrollPane.() -> Unit): JBScrollPane {
    return scrollable(null, constraints, init)
}
<commit_msg>Set the inset values without creating a new object.<commit_after>/*
 * Copyright (C) 2019-2020 <NAME>
 *
 * 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.
 */
/**
 * IntelliJ defines a DSL for creating UIs, but currently has several disadvantages:
 * 1.  It is not supported on IntelliJ 2019.1 or earlier;
 * 2.  It is classified as being in active development, with potential breaking changes between releases;
 * 3.  It is not necessarily comparable to the swing API in terms of available functionality and flexibility.
 *
 * As such, a swing compatible DSL is defined in this file.
 */
package uk.co.reecedunn.intellij.plugin.core.ui.layout

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogBuilder
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.JBTabbedPane
import com.intellij.ui.scale.JBUIScale
import com.intellij.uiDesigner.core.GridConstraints
import com.intellij.uiDesigner.core.Spacer
import com.intellij.util.ui.JBInsets
import com.intellij.util.ui.JBUI
import uk.co.reecedunn.intellij.plugin.core.execution.ui.TextConsoleView
import uk.co.reecedunn.intellij.plugin.core.ui.Borders
import java.awt.*
import javax.swing.JCheckBox
import javax.swing.JPanel
import javax.swing.JTabbedPane

// Layouts

// region panel

fun panel(layout: LayoutManager, init: JPanel.() -> Unit): JPanel {
    val panel = JPanel(layout)
    panel.init()
    return panel
}

fun panel(init: JPanel.() -> Unit): JPanel = panel(GridBagLayout(), init)

fun Container.horizontalPanel(constraints: Any?, init: JPanel.() -> Unit): JPanel {
    if (constraints is GridBagConstraints) {
        constraints.fill = GridBagConstraints.HORIZONTAL
        constraints.weightx = 1.0
    }

    val panel = JPanel(GridBagLayout())
    panel.init()
    add(panel, constraints)
    return panel
}

// endregion
// region grid

fun grid(x: Int, y: Int): GridBagConstraints = GridBagConstraints(
    x, y, 1, 1, 0.0, 0.0,
    GridBagConstraints.CENTER,
    GridBagConstraints.NONE,
    Insets(0, 0, 0, 0),
    0, 0
)

fun GridBagConstraints.size(dx: Int, dy: Int): GridBagConstraints {
    gridwidth = dx
    gridheight = dy
    return this
}

fun GridBagConstraints.padding(x: Int, y: Int): GridBagConstraints {
    val sx = JBUIScale.scale(x)
    val sy = JBUIScale.scale(y)
    insets.set(sy, sx, sy, sx)
    return this
}

fun GridBagConstraints.vgap(y: Int): GridBagConstraints {
    val sy = JBUIScale.scale(y)
    insets.set(0, 0, sy, 0)
    return this
}

// endregion
// region scrollable

fun Container.scrollable(view: Component?, constraints: Any?, init: JBScrollPane.() -> Unit): JBScrollPane {
    if (constraints is GridBagConstraints) {
        constraints.gridwidth = GridBagConstraints.REMAINDER
        constraints.fill = GridBagConstraints.BOTH
        constraints.weightx = 1.0
        constraints.weighty = 1.0
    }

    val pane = JBScrollPane(view)
    pane.init()
    add(pane, constraints)
    return pane
}

fun Container.scrollable(constraints: Any?, init: JBScrollPane.() -> Unit): JBScrollPane {
    return scrollable(null, constraints, init)
}

{'fraction_non_alphanumeric': 0.09971891313077232, 'fraction_numerical': 0.010975772988890376, 'mean_word_length': 3.829993535875889, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '1922305', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2319, 'n_words': 689}