<commit_before># Copyright 2016 The Bazel Authors. All rights reserved.
#
# 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.

"""Implementation of IntelliJ-specific information collecting aspect."""

# Compile-time dependency attributes, grouped by type.
DEPS = struct(
    label = [
        "_cc_toolchain",  # From C rules
        "_java_toolchain",  # From java rules
    ],
    label_list = [
        "deps",
        "exports",
        "_robolectric",  # From android_robolectric_test
    ],
)

# Run-time dependency attributes, grouped by type.
RUNTIME_DEPS = struct(
    label = [],
    label_list = [
        "runtime_deps",
    ],
)

PREREQUISITE_DEPS = struct(
    label = [],
    label_list = [],
)

##### Helpers

def struct_omit_none(**kwargs):
  """A replacement for standard `struct` function that omits the fields with None value."""
  d = {name: kwargs[name] for name in kwargs if kwargs[name] != None}
  return struct(**d)

def artifact_location(f):
  """Creates an ArtifactLocation proto from a File."""
  if f == None:
    return None

  return struct_omit_none(
      relative_path = get_relative_path(f),
      is_source = f.is_source,
      is_external = is_external(f.owner),
      root_execution_path_fragment = f.root.path if not f.is_source else None,
  )

def is_external(label):
  """Determines whether a label corresponds to an external artifact."""
  return label.workspace_root.startswith("external")

def get_relative_path(artifact):
  """A temporary workaround to find the root-relative path from an artifact.

  This is required because 'short_path' is incorrect for external source artifacts.

  Args:
    artifact: the input artifact
  Returns:
    string: the root-relative path for this artifact.
  """
  # TODO(bazel-team): remove this workaround when Artifact::short_path is fixed.
  if artifact.is_source and is_external(artifact.owner) and artifact.short_path.startswith(".."):
    # short_path is '../repo_name/path', we want 'external/repo_name/path'
    return "external" + artifact.short_path[2:]
  return artifact.short_path

def source_directory_tuple(resource_file):
  """Creates a tuple of (source directory, is_source, root execution path)."""<commit_msg>Skylark IntelliJ aspect: fix relative path for external workspace artifacts.<commit_after># Copyright 2016 The Bazel Authors. All rights reserved.
#
# 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.

"""Implementation of IntelliJ-specific information collecting aspect."""

# Compile-time dependency attributes, grouped by type.
DEPS = struct(
    label = [
        "_cc_toolchain",  # From C rules
        "_java_toolchain",  # From java rules
    ],
    label_list = [
        "deps",
        "exports",
        "_robolectric",  # From android_robolectric_test
    ],
)

# Run-time dependency attributes, grouped by type.
RUNTIME_DEPS = struct(
    label = [],
    label_list = [
        "runtime_deps",
    ],
)

PREREQUISITE_DEPS = struct(
    label = [],
    label_list = [],
)

##### Helpers

def struct_omit_none(**kwargs):
  """A replacement for standard `struct` function that omits the fields with None value."""
  d = {name: kwargs[name] for name in kwargs if kwargs[name] != None}
  return struct(**d)

def artifact_location(f):
  """Creates an ArtifactLocation proto from a File."""
  if f == None:
    return None

  return struct_omit_none(
      relative_path = get_relative_path(f),
      is_source = f.is_source,
      is_external = is_external(f.owner),
      root_execution_path_fragment = f.root.path if not f.is_source else None,
  )

def is_external(label):
  """Determines whether a label corresponds to an external artifact."""
  return label.workspace_root.startswith("external")

def get_relative_path(artifact):
  """A temporary workaround to find the root-relative path from an artifact.

  This is required because 'short_path' is incorrect for external source artifacts.

  Args:
    artifact: the input artifact
  Returns:
    string: the root-relative path for this artifact.
  """
  # TODO(bazel-team): remove this workaround when Artifact::short_path is fixed.
  if is_external(artifact.owner) and artifact.short_path.startswith(".."):
    # short_path is '../repo_name/path', we want 'external/repo_name/path'
    return "external" + artifact.short_path[2:]
  return artifact.short_path

def source_directory_tuple(resource_file):
  """Creates a tuple of (source directory, is_source, root execution path)."""
{'fraction_non_alphanumeric': 0.09878587196467992, 'fraction_numerical': 0.0033112582781456954, 'mean_word_length': 3.8200354609929077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4638333', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1540, 'n_words': 556}