<commit_before>'use strict'

utils = require 'utils'
log = require 'log'

log = log.scope 'Styles'

module.exports = (File) ->

	File::revert = do (_super = File::revert) -> ->
		r = _super.apply @, arguments

		# off listeners
		@updateStylesParent null

		r

	File::updateStylesParent = do ->
		findStyleByNode = (styles, node) ->
			for style in styles
				if style.node is node
					return style

			for style in styles
				if r = findStyleByNode style.children, node
					return r

			null

		(elem) ->
			@_watchedFile?.onReplacedByUse.disconnect @updateStylesParent
			@_watchedFile = null

			return unless elem

			keepLooking = =>
				if elem.self.parentUse
					@updateStylesParent elem.self.parentUse
				else
					@_watchedFile = elem.self
					elem.self.onReplacedByUse @updateStylesParent

			parentStyles = elem.self.styles

			# keep looking if no styles found
			unless parentStyles.length
				keepLooking()
				return

			# find styles parent walking by the tree
			parentNode = @node._parent
			while parentNode
				parentStyle = findStyleByNode parentStyles, parentNode

				if parentStyle
					for style in @styles
						style.render parentStyle

					return

				parentNode = parentNode._parent

			keepLooking()
			return

	File::clone = do (_super = File::clone) -> ->
		clone = _super.call @

		# check whether listening is necessary
		proceed = false
		for style in clone.styles
			if style.isAutoParent
				proceed = true
				break

		if proceed
			desc = utils.ENUMERABLE | utils.WRITABLE
			utils.defineProperty clone, '_watchedFile', desc, null

			clone.updateStylesParent = @updateStylesParent.bind clone
			clone.onReplacedByUse clone.updateStylesParent

		clone<commit_msg>Code optimizations<commit_after>'use strict'

utils = require 'utils'
log = require 'log'

log = log.scope 'Styles'

module.exports = (File) ->

	File::revert = do (_super = File::revert) -> ->
		r = _super.apply @, arguments

		# off listeners
		@updateStylesParent null

		r

	File::updateStylesParent = do ->
		findStyleByNode = (styles, node) ->
			for style in styles
				if style.node is node
					return style

			for style in styles
				if r = findStyleByNode style.children, node
					return r

			null

		keepLooking = (self, elem) ->
			if elem.self.parentUse
				self.updateStylesParent elem.self.parentUse
			else
				self._watchedFile = elem.self
				elem.self.onReplacedByUse self.updateStylesParent, self

		(elem) ->
			@_watchedFile?.onReplacedByUse.disconnect @updateStylesParent, @
			@_watchedFile = null

			return unless elem

			parentStyles = elem.self.styles

			# keep looking if no styles found
			unless parentStyles.length
				keepLooking @, elem
				return

			# find styles parent walking by the tree
			parentNode = @node._parent
			while parentNode
				parentStyle = findStyleByNode parentStyles, parentNode

				if parentStyle
					for style in @styles
						style.render parentStyle

					return

				parentNode = parentNode._parent

			keepLooking @, elem
			return

	File::clone = do (_super = File::clone) -> ->
		clone = _super.call @

		# check whether listening is necessary
		proceed = false
		for style in clone.styles
			if style.isAutoParent
				proceed = true
				break

		if proceed
			desc = utils.ENUMERABLE | utils.WRITABLE
			utils.defineProperty clone, '_watchedFile', desc, null

			clone.onReplacedByUse clone.updateStylesParent, clone

		clone
{'fraction_non_alphanumeric': 0.07954211916642208, 'fraction_numerical': 0.0, 'mean_word_length': 3.0475059382422804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '6392865', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1024, 'n_words': 299}