<commit_before>/*
 *  Copyright (c) 2018-present, Facebook, Inc.
 *
 *  This source code is licensed under the MIT license found in the LICENSE
 *  file in the root directory of this source tree.
 *
 */
#if FB_SONARKIT_ENABLED

#import "SKComponentLayoutWrapper.h"

#import <ComponentKit/CKAnalyticsListenerHelpers.h>
#import <ComponentKit/CKComponent.h>
#import <ComponentKit/CKComponentInternal.h>
#import <ComponentKit/CKComponentRootView.h>
#import <ComponentKit/CKComponentAttachController.h>
#import <ComponentKit/CKComponentAttachControllerInternal.h>
#import <ComponentKit/CKInspectableView.h>

#import "CKComponent+Sonar.h"

static char const kLayoutWrapperKey = ' ';

static CKFlexboxComponentChild findFlexboxLayoutParams(CKComponent *parent, CKComponent *child) {
  if ([parent isKindOfClass:[CKFlexboxComponent class]]) {
    static Ivar ivar = class_getInstanceVariable([CKFlexboxComponent class], "_children");
    static ptrdiff_t offset = ivar_getOffset(ivar);

    unsigned char *pComponent = (unsigned char*)(__bridge void*)parent;
    auto children = (std::vector<CKFlexboxComponentChild> *)(pComponent + offset);

    if (children) {
      for (auto it = children->begin(); it != children->end(); it++) {
        if (it->component == child) {
          return *it;
        }
      }
    }
  }

  return {};
}

@implementation SKComponentLayoutWrapper

+ (instancetype)newFromRoot:(id<CKInspectableView>)root {
  const CKComponentLayout layout = [root mountedLayout];
  // Check if there is a cached wrapper.
  if (layout.component) {
    SKComponentLayoutWrapper *cachedWrapper = objc_getAssociatedObject(layout.component, &kLayoutWrapperKey);
    if (cachedWrapper) {
      return cachedWrapper;
    }
  }
  CKComponentReuseWrapper *reuseWrapper = CKAnalyticsListenerHelpers::GetReusedNodes(layout.component);
  // Create a new layout wrapper.
  SKComponentLayoutWrapper *const wrapper =
  [[SKComponentLayoutWrapper alloc] initWithLayout:layout
                                          position:CGPointMake(0, 0)
                                         parentKey:[NSString stringWithFormat: @"%p.", layout.component]
                                      reuseWrapper:reuseWrapper];
  // Cache the result.
  if (layout.component) {
    objc_setAssociatedObject(layout.component, &kLayoutWrapperKey, wrapper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  }
  return wrapper;
}

- (instancetype)initWithLayout:(const CKComponentLayout &)layout
                      position:(CGPoint)position
                     parentKey:(NSString *)parentKey
                  reuseWrapper:(CKComponentReuseWrapper *)reuseWrapper
{
  if (self = [super init]) {
    _component = layout.component;
    _size = layout.size;
    _position = position;
    _identifier = [parentKey stringByAppendingString:layout.component ? NSStringFromClass([layout.component class]) : @"(null)"];

    if (_component && reuseWrapper) {
      auto const canBeReusedCounter = [reuseWrapper canBeReusedCounter:_component.treeNode.nodeIdentifier];
      if (canBeReusedCounter > 0) {
        _component.flipper_canBeReusedCounter = canBeReusedCounter;
      }
    }

    if (layout.children != nullptr) {
      int index = 0;
      for (const auto &child : *layout.children) {
        if (child.layout.component == nil) {
          continue; // nil children are allowed, ignore them
        }
        SKComponentLayoutWrapper *childWrapper = [[SKComponentLayoutWrapper alloc] initWithLayout:child.layout
                                                                                         position:child.position
                                                                                        parentKey:[_identifier stringByAppendingFormat:@"[%d].", index++]
                                                                                     reuseWrapper:reuseWrapper];
        childWrapper->_isFlexboxChild = [_component isKindOfClass:[CKFlexboxComponent class]];
        childWrapper->_flexboxChild = findFlexboxLayoutParams(_component, child.layout.component);
        _children.push_back(childWrapper);
      }
    }
  }

  return self;
}

@end

#endif<commit_msg>Use the tree node idenitifer as the component identifier<commit_after>/*
 *  Copyright (c) 2018-present, Facebook, Inc.
 *
 *  This source code is licensed under the MIT license found in the LICENSE
 *  file in the root directory of this source tree.
 *
 */
#if FB_SONARKIT_ENABLED

#import "SKComponentLayoutWrapper.h"

#import <ComponentKit/CKAnalyticsListenerHelpers.h>
#import <ComponentKit/CKComponent.h>
#import <ComponentKit/CKComponentInternal.h>
#import <ComponentKit/CKComponentRootView.h>
#import <ComponentKit/CKComponentAttachController.h>
#import <ComponentKit/CKComponentAttachControllerInternal.h>
#import <ComponentKit/CKInspectableView.h>

#import "CKComponent+Sonar.h"

static char const kLayoutWrapperKey = ' ';

static CKFlexboxComponentChild findFlexboxLayoutParams(CKComponent *parent, CKComponent *child) {
  if ([parent isKindOfClass:[CKFlexboxComponent class]]) {
    static Ivar ivar = class_getInstanceVariable([CKFlexboxComponent class], "_children");
    static ptrdiff_t offset = ivar_getOffset(ivar);

    unsigned char *pComponent = (unsigned char*)(__bridge void*)parent;
    auto children = (std::vector<CKFlexboxComponentChild> *)(pComponent + offset);

    if (children) {
      for (auto it = children->begin(); it != children->end(); it++) {
        if (it->component == child) {
          return *it;
        }
      }
    }
  }

  return {};
}

@implementation SKComponentLayoutWrapper

+ (instancetype)newFromRoot:(id<CKInspectableView>)root {
  const CKComponentLayout layout = [root mountedLayout];
  // Check if there is a cached wrapper.
  if (layout.component) {
    SKComponentLayoutWrapper *cachedWrapper = objc_getAssociatedObject(layout.component, &kLayoutWrapperKey);
    if (cachedWrapper) {
      return cachedWrapper;
    }
  }
  CKComponentReuseWrapper *reuseWrapper = CKAnalyticsListenerHelpers::GetReusedNodes(layout.component);
  // Create a new layout wrapper.
  SKComponentLayoutWrapper *const wrapper =
  [[SKComponentLayoutWrapper alloc] initWithLayout:layout
                                          position:CGPointMake(0, 0)
                                      reuseWrapper:reuseWrapper];
  // Cache the result.
  if (layout.component) {
    objc_setAssociatedObject(layout.component, &kLayoutWrapperKey, wrapper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  }
  return wrapper;
}

- (instancetype)initWithLayout:(const CKComponentLayout &)layout
                      position:(CGPoint)position
                  reuseWrapper:(CKComponentReuseWrapper *)reuseWrapper
{
  if (self = [super init]) {
    _component = layout.component;
    _size = layout.size;
    _position = position;
    _identifier = _component ? [NSString stringWithFormat:@"%d", _component.treeNode.nodeIdentifier] : @"(null)";

    if (_component && reuseWrapper) {
      auto const canBeReusedCounter = [reuseWrapper canBeReusedCounter:_component.treeNode.nodeIdentifier];
      if (canBeReusedCounter > 0) {
        _component.flipper_canBeReusedCounter = canBeReusedCounter;
      }
    }

    if (layout.children != nullptr) {
      for (const auto &child : *layout.children) {
        if (child.layout.component == nil) {
          continue; // nil children are allowed, ignore them
        }
        SKComponentLayoutWrapper *childWrapper = [[SKComponentLayoutWrapper alloc] initWithLayout:child.layout
                                                                                         position:child.position
                                                                                     reuseWrapper:reuseWrapper];
        childWrapper->_isFlexboxChild = [_component isKindOfClass:[CKFlexboxComponent class]];
        childWrapper->_flexboxChild = findFlexboxLayoutParams(_component, child.layout.component);
        _children.push_back(childWrapper);
      }
    }
  }

  return self;
}

@end

#endif
{'fraction_non_alphanumeric': 0.09508360369353631, 'fraction_numerical': 0.0018717244821562266, 'mean_word_length': 2.946331856228459, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3146318', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2096, 'n_words': 465}