<commit_before>////
////  UIView+GetConstraints.swift
////  Created by <NAME> on 7/31/15.
////  Copyright (c) 2015 <NAME>. All rights reserved.
////
///*
//
//The idea is that you can call view.ip_topConstraint or view.ip_heightConstraint on any view and you'll be given the editable constraint that deals with that attribute.
//Optionally you can use the base function ip_constraintForAttribute() and pass in the toItem and itemAttribute. It is less convienient but more accurate.
//
/*

The idea is that you can call view.ip_topConstraint or view.ip_heightConstraint on any view and you'll be given the editable constraint that deals with that attribute.
Optionally you can use the base function ip_constraintForAttribute() and pass in the toItem and itemAttribute. It is less convienient but more accurate.

*/
import Foundation
import UIKit

public extension UIView {
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_widthConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Width)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_heightConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Height)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_topConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Top)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_bottomConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Bottom)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_leadingConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Leading)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_trailingConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Trailing)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_centerXConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.CenterX)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_centerYConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.CenterY)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_aspectRatioConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Height, toView: self, viewAttribute: .Width)
    }
    
    /**
     More precise way of getting constraints for a view. The constraint does not have to belong to the reciever -- this method searches the superview for the matching constraint as well.
     
     :param: attribute     Attribute to look for belonging to the receiver
     :param: view1        Defaults to the receiver, but could be any subview as well.
     :param: view2        Probably a UIView, that view1 is related to.
     :param: viewAttribute Attribute that should be used with the toItem
     
     :returns: The first constraint that matches. May return unexpected constraint if receiver contains multiple constraints with this item and itemAttribute.
     */
    public func ip_constraintForAttribute(attribute: NSLayoutAttribute, var onView view1: UIView? = nil, toView view2: UIView? = nil, viewAttribute: NSLayoutAttribute? = nil) -> NSLayoutConstraint? {
        
        if view1 == nil {
            view1 = self
        }
        
        if let toAttribute = viewAttribute, let toItem = view2 {
            
            return constraints.filter { constraint in
                return constraint.ip_relatesView(view: view1!, viaAttribute: attribute, toView: toItem, andItsAttribute: toAttribute)
                }
                .first
            
        } else if let toItem = view2 {
            
            return constraints.filter { constraint in
                return constraint.ip_relatesView(view: view1!, viaAttribute: attribute, toView: toItem)
                }
                .first
            
        } else {
            
            if attribute == .Height || attribute == .Width {
                //For size constraints
                return constraints.filter { constraint in
                    return constraint.ip_isIntrinsicConstraintWithView(view: view1!, andAttribute: attribute)
                    }
                    .first
                
            } else {
                // For simple positioning constraints
                return constraints.filter { constraint in
                    return constraint.ip_relatesView(view: view1!, viaAttribute: attribute)
                    }
                    .first
                
            }
        }
        
    }
}

extension NSLayoutConstraint {
    
    public func ip_relatesView(view view1: UIView,
        viaAttribute attribute1: NSLayoutAttribute,
        toView view2: UIView,
        andItsAttribute attribute2: NSLayoutAttribute) -> Bool {
            
            let possibility1 = (firstItem as? UIView == view1 && firstAttribute == attribute1 && secondItem as? UIView == view2 && secondAttribute == attribute2)
            
            let possibility2 = (secondItem as? UIView == view1 && secondAttribute == attribute1 && firstItem as? UIView == view2 && firstAttribute == attribute2)
            
            return possibility1 || possibility2
    }
    
    public func ip_relatesView(view view1: UIView,
        viaAttribute attribute1: NSLayoutAttribute,
        toView view2: UIView) -> Bool {
            
            let possibility1 = (firstItem as? UIView == view1 && firstAttribute == attribute1 && secondItem as? UIView == view2)<commit_msg>same external and internal variable names. better assignment of onView.<commit_after>////
////  UIView+GetConstraints.swift
////  Created by <NAME> on 7/31/15.
////  Copyright (c) 2015 <NAME>. All rights reserved.
////
///*
//
//The idea is that you can call view.ip_topConstraint or view.ip_heightConstraint on any view and you'll be given the editable constraint that deals with that attribute.
//Optionally you can use the base function ip_constraintForAttribute() and pass in the toItem and itemAttribute. It is less convienient but more accurate.
//
/*

The idea is that you can call view.ip_topConstraint or view.ip_heightConstraint on any view and you'll be given the editable constraint that deals with that attribute.
Optionally you can use the base function ip_constraintForAttribute() and pass in the toItem and itemAttribute. It is less convienient but more accurate.

*/
import Foundation
import UIKit

public extension UIView {
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_widthConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Width)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_heightConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Height)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_topConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Top)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_bottomConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Bottom)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_leadingConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Leading)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_trailingConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Trailing)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_centerXConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.CenterX)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_centerYConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.CenterY)
    }
    
    /// This computed property result may be unexpected when inspecting views with multiple constraints on an attribute.
    public var ip_aspectRatioConstraint: NSLayoutConstraint? {
        return ip_constraintForAttribute(.Height, toView: self, viewAttribute: .Width)
    }
    
    /**
     More precise way of getting constraints for a view. The constraint does not have to belong to the reciever -- this method searches the superview for the matching constraint as well.
     
     :param: attribute     Attribute to look for belonging to the receiver
     :param: view1        Defaults to the receiver, but could be any subview as well.
     :param: view2        Probably a UIView, that view1 is related to.
     :param: viewAttribute Attribute that should be used with the toItem
     
     :returns: The first constraint that matches. May return unexpected constraint if receiver contains multiple constraints with this item and itemAttribute.
     */
    public func ip_constraintForAttribute(attribute: NSLayoutAttribute, onView: UIView? = nil, toView: UIView? = nil, viewAttribute: NSLayoutAttribute? = nil) -> NSLayoutConstraint? {
        
        let onView = onView ?? self
        
        if let toAttribute = viewAttribute, let toItem = toView {
            
            return constraints.filter { constraint in
                return constraint.ip_relatesView(view: onView, viaAttribute: attribute, toView: toItem, andItsAttribute: toAttribute)
                }
                .first
            
        } else if let toItem = toView {
            
            return constraints.filter { constraint in
                return constraint.ip_relatesView(view: onView, viaAttribute: attribute, toView: toItem)
                }
                .first
            
        } else {
            
            if attribute == .Height || attribute == .Width {
                //For size constraints
                return constraints.filter { constraint in
                    return constraint.ip_isIntrinsicConstraintWithView(view: onView, andAttribute: attribute)
                    }
                    .first
                
            } else {
                // For simple positioning constraints
                return constraints.filter { constraint in
                    return constraint.ip_relatesView(view: onView, viaAttribute: attribute)
                    }
                    .first
                
            }
        }
        
    }
}

extension NSLayoutConstraint {
    
    public func ip_relatesView(view view1: UIView,
        viaAttribute attribute1: NSLayoutAttribute,
        toView view2: UIView,
        andItsAttribute attribute2: NSLayoutAttribute) -> Bool {
            
            let possibility1 = (firstItem as? UIView == view1 && firstAttribute == attribute1 && secondItem as? UIView == view2 && secondAttribute == attribute2)
            
            let possibility2 = (secondItem as? UIView == view1 && secondAttribute == attribute1 && firstItem as? UIView == view2 && firstAttribute == attribute2)
            
            return possibility1 || possibility2
    }
    
    public func ip_relatesView(view view1: UIView,
        viaAttribute attribute1: NSLayoutAttribute,
        toView view2: UIView) -> Bool {
            
            let possibility1 = (firstItem as? UIView == view1 && firstAttribute == attribute1 && secondItem as? UIView == view2)
{'fraction_non_alphanumeric': 0.06006532303035131, 'fraction_numerical': 0.0063729785708595554, 'mean_word_length': 2.494988864142539, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4668730', 'n_tokens_mistral': 2946, 'n_tokens_neox': 2854, 'n_words': 1156}