<commit_before>"""IntelliJ plugin target rule.

Creates a plugin jar with the given plugin xml and any
optional plugin xmls.

To provide optional plugin xmls, use the 'optional_plugin_xml'
rule. These will be renamed, put in the META-INF directory,
and the main plugin xml stamped with optional plugin dependencies
that point to the correct META-INF optional plugin xmls.

To associate a plugin.xml fragment with some code, you can add both to an
intellij_plugin_library rule and add that target as a dependency of an
intellij_plugin. The XML will get merged into the main META-INF/plugin.xml
file.

optional_plugin_xml(
  name = "optional_python_xml",
  plugin_xml = "my_optional_python_plugin.xml",
  module = "com.idea.python.module.id",
)

intellij_plugin_library(
  name = "piper_support",
  plugin_xmls = ["META-INF/piper-plugin.xml"],
  deps = [":piper_support_lib"],
)

intellij_plugin(
  name = "my_plugin",
  plugin_xml = ["my_plugin.xml"],
  optional_plugin_xmls = [":optional_python_xml"],
  deps = [
    ":code_deps",
    ":piper_support",
  ],
)

"""

_OptionalPluginXmlInfo = provider(fields = ["optional_plugin_xmls"])

def _optional_plugin_xml_impl(ctx):
    attr = ctx.attr
    optional_plugin_xmls = []
    if ctx.file.plugin_xml:
        optional_plugin_xmls.append(struct(
            plugin_xml = ctx.file.plugin_xml,
            module = attr.module,
        ))
    return [_OptionalPluginXmlInfo(optional_plugin_xmls = optional_plugin_xmls)]

optional_plugin_xml = rule(
    implementation = _optional_plugin_xml_impl,
    attrs = {
        "plugin_xml": attr.label(mandatory = True, allow_single_file = [".xml"]),
        "module": attr.string(mandatory = True),
    },
)

_IntellijPluginLibraryInfo = provider(fields = ["plugin_xmls", "optional_plugin_xmls", "java_info"])

def _intellij_plugin_library_impl(ctx):
    java_info = []

    # IntelliJ plugins depend transitively on targets that do not support mac.
    # To workaround this, we explicitly pull out only the java parts of the
    # dependencies. This means that any native parts (.so files) are excluded
    # from the plugin, which means Bazel does not attempt to build them, so
    # they do not break the build.
    # TODO(b/258450529) Fix this properly so that this workaround is not needed.
    compile = []
    runtime = []
    for dep in ctx.attr.deps:
        # Exclude plugin_api targets, they should not be built in the jar.
        # TODO(mathewi): this is a bit of a hack, find a better way.
        if str(dep.label).startswith("//intellij_platform_sdk:"):
            continue
        compile.append(dep[JavaInfo].full_compile_jars)
        runtime.append(dep[JavaInfo].transitive_runtime_jars)

    # Wrap each runtime jar in a JavaInfo provider.
    transtive_runtime = [
        JavaInfo(output_jar = runtime_jar, compile_jar = runtime_jar)
        for runtime_jar in depset(transitive = runtime).to_list()
    ]

    # Wrap each top-level compile time jar in a JavaInfo provider.
    # Use the runtime JavaInfo providers from above as dependencies to keep the
    # transitive runtime jars.
    full_compile = [
        JavaInfo(
            output_jar = compile_jar,
            compile_jar = compile_jar,
            deps = transtive_runtime,
        )
        for compile_jar in depset(transitive = compile).to_list()
    ]

    # Construct a JavaInfo from the above to be included in the plugin jar.
    java_info = java_common.merge(full_compile)

    plugin_xmls = []
    for target in ctx.attr.plugin_xmls:
        for file in target.files.to_list():
            plugin_xmls.append(file)

    return [
        _IntellijPluginLibraryInfo(
            plugin_xmls = depset(plugin_xmls, order = "preorder"),
            optional_plugin_xmls = [
                dep[_OptionalPluginXmlInfo]
                for dep in ctx.attr.optional_plugin_xmls
            ],
            java_info = java_info,
        ),
    ]

intellij_plugin_library = rule(
    implementation = _intellij_plugin_library_impl,
    attrs = {
        "deps": attr.label_list(providers = [JavaInfo]),
        "plugin_xmls": attr.label_list(allow_files = [".xml"]),
        "optional_plugin_xmls": attr.label_list(providers = [_OptionalPluginXmlInfo]),
    },
)
<commit_msg>Automated rollback of commit <PASSWORD>.<commit_after>"""IntelliJ plugin target rule.

Creates a plugin jar with the given plugin xml and any
optional plugin xmls.

To provide optional plugin xmls, use the 'optional_plugin_xml'
rule. These will be renamed, put in the META-INF directory,
and the main plugin xml stamped with optional plugin dependencies
that point to the correct META-INF optional plugin xmls.

To associate a plugin.xml fragment with some code, you can add both to an
intellij_plugin_library rule and add that target as a dependency of an
intellij_plugin. The XML will get merged into the main META-INF/plugin.xml
file.

optional_plugin_xml(
  name = "optional_python_xml",
  plugin_xml = "my_optional_python_plugin.xml",
  module = "com.idea.python.module.id",
)

intellij_plugin_library(
  name = "piper_support",
  plugin_xmls = ["META-INF/piper-plugin.xml"],
  deps = [":piper_support_lib"],
)

intellij_plugin(
  name = "my_plugin",
  plugin_xml = ["my_plugin.xml"],
  optional_plugin_xmls = [":optional_python_xml"],
  deps = [
    ":code_deps",
    ":piper_support",
  ],
)

"""

_OptionalPluginXmlInfo = provider(fields = ["optional_plugin_xmls"])

def _optional_plugin_xml_impl(ctx):
    attr = ctx.attr
    optional_plugin_xmls = []
    if ctx.file.plugin_xml:
        optional_plugin_xmls.append(struct(
            plugin_xml = ctx.file.plugin_xml,
            module = attr.module,
        ))
    return [_OptionalPluginXmlInfo(optional_plugin_xmls = optional_plugin_xmls)]

optional_plugin_xml = rule(
    implementation = _optional_plugin_xml_impl,
    attrs = {
        "plugin_xml": attr.label(mandatory = True, allow_single_file = [".xml"]),
        "module": attr.string(mandatory = True),
    },
)

_IntellijPluginLibraryInfo = provider(fields = ["plugin_xmls", "optional_plugin_xmls", "java_info"])

def _intellij_plugin_library_impl(ctx):
    java_info = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])

    plugin_xmls = []
    for target in ctx.attr.plugin_xmls:
        for file in target.files.to_list():
            plugin_xmls.append(file)

    return [
        _IntellijPluginLibraryInfo(
            plugin_xmls = depset(plugin_xmls, order = "preorder"),
            optional_plugin_xmls = [
                dep[_OptionalPluginXmlInfo]
                for dep in ctx.attr.optional_plugin_xmls
            ],
            java_info = java_info,
        ),
    ]

intellij_plugin_library = rule(
    implementation = _intellij_plugin_library_impl,
    attrs = {
        "deps": attr.label_list(providers = [JavaInfo]),
        "plugin_xmls": attr.label_list(allow_files = [".xml"]),
        "optional_plugin_xmls": attr.label_list(providers = [_OptionalPluginXmlInfo]),
    },
)

{'fraction_non_alphanumeric': 0.0933028048082427, 'fraction_numerical': 0.0012879221522610188, 'mean_word_length': 3.2693952351863165, 'pattern_counts': {'":': 18, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 86}, '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': '2737189', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2143, 'n_words': 563}