<commit_before>import sublime, sublime_plugin, sys, os, re

if sys.version_info < (3, 0):
	# ST2, Python 2.6
	from libs.cssformatter import format_code
else:
	# ST3, Python 3.3
	from .libs.cssformatter import format_code

global_settings = sublime.load_settings('CSS Format.sublime-settings')


class CssFormatCommand(sublime_plugin.TextCommand):

	def run(self, edit, action='compact'):
		view = self.view

		if view.is_loading():
			sublime.status_message('Waiting for loading.')
			return False

		indentation = view.settings().get('indentation', global_settings.get('indentation', '\t'))

		selection = view.sel()[0]
		if len(selection) > 0:
			self.format_selection(edit, action, indentation)
		else:
			self.format_whole_file(edit, action, indentation)

	def format_selection(self, edit, action, indentation):
		view = self.view
		regions = []

		for sel in view.sel():
			region = sublime.Region(
				view.line(min(sel.a, sel.b)).a,  # line start of first line
				view.line(max(sel.a, sel.b)).b   # line end of last line
			)
			code = view.substr(region)
			code = format_code(code, action, indentation)
			#view.sel().clear()
			view.replace(edit, region, code)

	def format_whole_file(self, edit, action, indentation):
		view = self.view
		region = sublime.Region(0, view.size())
		code = view.substr(region)
		code = format_code(code, action, indentation)
		view.replace(edit, region, code)

	def is_visible(self):
		view = self.view
		file_name = view.file_name()
		syntax_path = view.settings().get('syntax')
		suffix_array = ['css', 'sass', 'scss', 'less', 'html', 'htm']
		suffix = ''
		syntax = ''
		
		if file_name != None: # file exists, pull syntax type from extension
			suffix = os.path.splitext(file_name)[1][1:]
		if syntax_path != None:
			syntax = os.path.splitext(syntax_path)[0].split('/')[-1].lower()
		return suffix in suffix_array or syntax in suffix_array


class FormatOnSave(sublime_plugin.EventListener):

	def on_post_save(self, view):
		should_format = view.settings().get('format_on_save', global_settings.get('format_on_save', True))
		if not should_format:
			return

		file_filter = view.settings().get('format_on_save_filter', global_settings.get('format_on_save_filter', '\.(css|sass|scss|less)$'))
		if not re.search(file_filter, view.file_name()):
			return

		format_action = view.settings().get('format_on_save_action', global_settings.get('format_on_save_action', 'expand'))
		if not format_action:
			return<commit_msg>Fix bug for ST3<commit_after>import sublime, sublime_plugin, sys, os, re

if sys.version_info < (3, 0):
	# ST2, Python 2.6
	from libs.cssformatter import format_code
else:
	# ST3, Python 3.3
	from .libs.cssformatter import format_code


class CssFormatCommand(sublime_plugin.TextCommand):

	def run(self, edit, action='compact'):
		view = self.view

		if view.is_loading():
			sublime.status_message('Waiting for loading.')
			return False

		global_settings = sublime.load_settings('CSS Format.sublime-settings')
		indentation = view.settings().get('indentation', global_settings.get('indentation', '\t'))

		selection = view.sel()[0]
		if len(selection) > 0:
			self.format_selection(edit, action, indentation)
		else:
			self.format_whole_file(edit, action, indentation)

	def format_selection(self, edit, action, indentation):
		view = self.view
		regions = []

		for sel in view.sel():
			region = sublime.Region(
				view.line(min(sel.a, sel.b)).a,  # line start of first line
				view.line(max(sel.a, sel.b)).b   # line end of last line
			)
			code = view.substr(region)
			code = format_code(code, action, indentation)
			#view.sel().clear()
			view.replace(edit, region, code)

	def format_whole_file(self, edit, action, indentation):
		view = self.view
		region = sublime.Region(0, view.size())
		code = view.substr(region)
		code = format_code(code, action, indentation)
		view.replace(edit, region, code)

	def is_visible(self):
		view = self.view
		file_name = view.file_name()
		syntax_path = view.settings().get('syntax')
		suffix_array = ['css', 'sass', 'scss', 'less', 'html', 'htm']
		suffix = ''
		syntax = ''
		
		if file_name != None: # file exists, pull syntax type from extension
			suffix = os.path.splitext(file_name)[1][1:]
		if syntax_path != None:
			syntax = os.path.splitext(syntax_path)[0].split('/')[-1].lower()
		return suffix in suffix_array or syntax in suffix_array


class FormatOnSave(sublime_plugin.EventListener):

	def on_post_save(self, view):
		global_settings = sublime.load_settings('CSS Format.sublime-settings')

		should_format = view.settings().get('format_on_save', global_settings.get('format_on_save', True))
		if not should_format:
			return

		file_filter = view.settings().get('format_on_save_filter', global_settings.get('format_on_save_filter', '\.(css|sass|scss|less)$'))
		if not re.search(file_filter, view.file_name()):
			return

		format_action = view.settings().get('format_on_save_action', global_settings.get('format_on_save_action', 'expand'))
		if not format_action:
			return
{'fraction_non_alphanumeric': 0.13923798124875325, 'fraction_numerical': 0.006183921803311391, 'mean_word_length': 4.67836919592299, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2073494', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1680, 'n_words': 381}