<commit_before>define [
	"base"
	"ace/ace"
	"ace/ext-searchbox"
	"ace/ext-modelist"
	"ace/keybinding-vim"
	"ide/editor/directives/aceEditor/undo/UndoManager"
	"ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager"
	"ide/editor/directives/aceEditor/spell-check/SpellCheckManager"
	"ide/editor/directives/aceEditor/highlights/HighlightsManager"
	"ide/editor/directives/aceEditor/cursor-position/CursorPositionManager"
	"ide/editor/directives/aceEditor/track-changes/TrackChangesManager"
	"ide/editor/directives/aceEditor/metadata/MetadataManager"
	"ide/metadata/services/metadata"
	"ide/graphics/services/graphics"
	"ide/preamble/services/preamble"
    "ide/files/services/files"
], (App, Ace, SearchBox, Vim, ModeList, UndoManager, AutoCompleteManager, SpellCheckManager, HighlightsManager, CursorPositionManager, TrackChangesManager, MetadataManager) ->
	EditSession = ace.require('ace/edit_session').EditSession
	ModeList = ace.require('ace/ext/modelist')
	Vim = ace.require('ace/keyboard/vim').Vim

	# set the path for ace workers if using a CDN (from editor.pug)
	if window.aceWorkerPath != ""
		syntaxValidationEnabled = true
		ace.config.set('workerPath', "#{window.aceWorkerPath}")
	else
		syntaxValidationEnabled = false

	# By default, don't use workers - enable them per-session as required
	ace.config.setDefaultValue("session", "useWorker", false)

	# Ace loads its script itself, so we need to hook in to be able to clear
	# the cache.
	if !ace.config._moduleUrl?
		ace.config._moduleUrl = ace.config.moduleUrl
		ace.config.moduleUrl = (args...) ->
			url = ace.config._moduleUrl(args...)
			return url

	App.directive "aceEditor", ($timeout, $compile, $rootScope, event_tracking, localStorage, $cacheFactory, metadata, graphics, preamble, files, $http, $q, $window) ->
		monkeyPatchSearch($rootScope, $compile)

		return  {
			scope: {
				theme: "="
				showPrintMargin: "="
				keybindings: "="
				fontSize: "="
				autoComplete: "="
				autoPairDelimiters: "="
				sharejsDoc: "="
				spellCheck: "="
				spellCheckLanguage: "="
				highlights: "="
				text: "="
				readOnly: "="
				annotations: "="
				navigateHighlights: "="
				fileName: "="
				onCtrlEnter: "="   # Compile
				onCtrlJ: "="       # Toggle the review panel
				onCtrlShiftC: "="  # Add a new comment
				onCtrlShiftA: "="  # Toggle track-changes on/off
				onSave: "="        # Cmd/Ctrl-S or :w in Vim
				syntaxValidation: "="
				reviewPanel: "="
				eventsBridge: "="
				trackChanges: "="
				trackChangesEnabled: "="
				docId: "="
				rendererData: "="
			}
			link: (scope, element, attrs) ->
				# Don't freak out if we're already in an apply callback
				scope.$originalApply = scope.$apply
				scope.$apply = (fn = () ->) ->
					phase = @$root.$$phase
					if (phase == '$apply' || phase == '$digest')
						fn()
					else
						@$originalApply(fn);

				editor = ace.edit(element.find(".ace-editor-body")[0])
				editor.$blockScrolling = Infinity

				# auto-insertion of braces, brackets, dollars
				editor.setOption('behavioursEnabled', scope.autoPairDelimiters || false)
				editor.setOption('wrapBehavioursEnabled', false)

				scope.$watch "autoPairDelimiters", (autoPairDelimiters) =>
					if autoPairDelimiters
						editor.setOption('behavioursEnabled', true)
					else
						editor.setOption('behavioursEnabled', false)

				window._debug_editors ||= []
				window._debug_editors.push editor

				scope.name = attrs.aceEditor

				if scope.spellCheck # only enable spellcheck when explicitly required
					spellCheckCache = $cacheFactory.get("spellCheck-#{scope.name}") || $cacheFactory("spellCheck-#{scope.name}", {capacity: 1000})
					spellCheckManager = new SpellCheckManager(scope, editor, element, spellCheckCache, $http, $q)
				undoManager           = new UndoManager(scope, editor, element)
				highlightsManager     = new HighlightsManager(scope, editor, element)
				cursorPositionManager = new CursorPositionManager(scope, editor, element, localStorage)
				trackChangesManager   = new TrackChangesManager(scope, editor, element)
				metadataManager = new MetadataManager(scope, editor, element, metadata)
				autoCompleteManager = new AutoCompleteManager(scope, editor, element, metadataManager, graphics, preamble, files)

				# Prevert Ctrl|Cmd-S from triggering save dialog
				scope.$watch "onSave", (callback) ->
					if callback?
						Vim.defineEx 'write', 'w', callback
						editor.commands.addCommand
							name: "save",
							bindKey: win: "Ctrl-S", mac: "Command-S"
							exec: () ->
								callback()
							readOnly: true
						# Not technically 'save', but Ctrl-. recompiles in OL v1
						# so maintain compatibility
						editor.commands.addCommand
							name: "recompile_v1",
							bindKey: win: "Ctrl-.", mac: "Ctrl-."
							exec: () ->
								callback()
							readOnly: true
				editor.commands.removeCommand "transposeletters"
				editor.commands.removeCommand "showSettingsMenu"
				editor.commands.removeCommand "foldall"


				# For European keyboards, the / is above 7 so needs Shift pressing.
				# This comes through as Command-Shift-/ on OS X, which is mapped to
				# toggleBlockComment.
				# This doesn't do anything for LaTeX, so remap this to togglecomment to
				# work for European keyboards as normal.
				# On Windows, the key combo comes as Ctrl-Shift-7.
				editor.commands.removeCommand "toggleBlockComment"<commit_msg>Clean up comments and callbacks<commit_after>define [
	"base"
	"ace/ace"
	"ace/ext-searchbox"
	"ace/ext-modelist"
	"ace/keybinding-vim"
	"ide/editor/directives/aceEditor/undo/UndoManager"
	"ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager"
	"ide/editor/directives/aceEditor/spell-check/SpellCheckManager"
	"ide/editor/directives/aceEditor/highlights/HighlightsManager"
	"ide/editor/directives/aceEditor/cursor-position/CursorPositionManager"
	"ide/editor/directives/aceEditor/track-changes/TrackChangesManager"
	"ide/editor/directives/aceEditor/metadata/MetadataManager"
	"ide/metadata/services/metadata"
	"ide/graphics/services/graphics"
	"ide/preamble/services/preamble"
    "ide/files/services/files"
], (App, Ace, SearchBox, Vim, ModeList, UndoManager, AutoCompleteManager, SpellCheckManager, HighlightsManager, CursorPositionManager, TrackChangesManager, MetadataManager) ->
	EditSession = ace.require('ace/edit_session').EditSession
	ModeList = ace.require('ace/ext/modelist')
	Vim = ace.require('ace/keyboard/vim').Vim

	# set the path for ace workers if using a CDN (from editor.pug)
	if window.aceWorkerPath != ""
		syntaxValidationEnabled = true
		ace.config.set('workerPath', "#{window.aceWorkerPath}")
	else
		syntaxValidationEnabled = false

	# By default, don't use workers - enable them per-session as required
	ace.config.setDefaultValue("session", "useWorker", false)

	# Ace loads its script itself, so we need to hook in to be able to clear
	# the cache.
	if !ace.config._moduleUrl?
		ace.config._moduleUrl = ace.config.moduleUrl
		ace.config.moduleUrl = (args...) ->
			url = ace.config._moduleUrl(args...)
			return url

	App.directive "aceEditor", ($timeout, $compile, $rootScope, event_tracking, localStorage, $cacheFactory, metadata, graphics, preamble, files, $http, $q, $window) ->
		monkeyPatchSearch($rootScope, $compile)

		return  {
			scope: {
				theme: "="
				showPrintMargin: "="
				keybindings: "="
				fontSize: "="
				autoComplete: "="
				autoPairDelimiters: "="
				sharejsDoc: "="
				spellCheck: "="
				spellCheckLanguage: "="
				highlights: "="
				text: "="
				readOnly: "="
				annotations: "="
				navigateHighlights: "="
				fileName: "="
				onCtrlEnter: "="   # Compile
				onCtrlJ: "="       # Toggle the review panel
				onCtrlShiftC: "="  # Add a new comment
				onCtrlShiftA: "="  # Toggle track-changes on/off
				onSave: "="        # Cmd/Ctrl-S or :w in Vim
				syntaxValidation: "="
				reviewPanel: "="
				eventsBridge: "="
				trackChanges: "="
				trackChangesEnabled: "="
				docId: "="
				rendererData: "="
			}
			link: (scope, element, attrs) ->
				# Don't freak out if we're already in an apply callback
				scope.$originalApply = scope.$apply
				scope.$apply = (fn = () ->) ->
					phase = @$root.$$phase
					if (phase == '$apply' || phase == '$digest')
						fn()
					else
						@$originalApply(fn);

				editor = ace.edit(element.find(".ace-editor-body")[0])
				editor.$blockScrolling = Infinity

				# auto-insertion of braces, brackets, dollars
				editor.setOption('behavioursEnabled', scope.autoPairDelimiters || false)
				editor.setOption('wrapBehavioursEnabled', false)

				scope.$watch "autoPairDelimiters", (autoPairDelimiters) =>
					if autoPairDelimiters
						editor.setOption('behavioursEnabled', true)
					else
						editor.setOption('behavioursEnabled', false)

				window._debug_editors ||= []
				window._debug_editors.push editor

				scope.name = attrs.aceEditor

				if scope.spellCheck # only enable spellcheck when explicitly required
					spellCheckCache = $cacheFactory.get("spellCheck-#{scope.name}") || $cacheFactory("spellCheck-#{scope.name}", {capacity: 1000})
					spellCheckManager = new SpellCheckManager(scope, editor, element, spellCheckCache, $http, $q)
				undoManager           = new UndoManager(scope, editor, element)
				highlightsManager     = new HighlightsManager(scope, editor, element)
				cursorPositionManager = new CursorPositionManager(scope, editor, element, localStorage)
				trackChangesManager   = new TrackChangesManager(scope, editor, element)
				metadataManager = new MetadataManager(scope, editor, element, metadata)
				autoCompleteManager = new AutoCompleteManager(scope, editor, element, metadataManager, graphics, preamble, files)

				scope.$watch "onSave", (callback) ->
					if callback?
						Vim.defineEx 'write', 'w', callback
						editor.commands.addCommand
							name: "save",
							bindKey: win: "Ctrl-S", mac: "Command-S"
							exec: callback
							readOnly: true
						# Not technically 'save', but Ctrl-. recompiles in OL v1
						# so maintain compatibility
						editor.commands.addCommand
							name: "recompile_v1",
							bindKey: win: "Ctrl-.", mac: "Ctrl-."
							exec: callback
							readOnly: true
				editor.commands.removeCommand "transposeletters"
				editor.commands.removeCommand "showSettingsMenu"
				editor.commands.removeCommand "foldall"


				# For European keyboards, the / is above 7 so needs Shift pressing.
				# This comes through as Command-Shift-/ on OS X, which is mapped to
				# toggleBlockComment.
				# This doesn't do anything for LaTeX, so remap this to togglecomment to
				# work for European keyboards as normal.
				# On Windows, the key combo comes as Ctrl-Shift-7.
				editor.commands.removeCommand "toggleBlockComment"
{'fraction_non_alphanumeric': 0.12368543508608655, 'fraction_numerical': 0.0016751977664029782, 'mean_word_length': 3.7028446389496716, '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': '678396', 'n_tokens_mistral': 3967, 'n_tokens_neox': 3265, 'n_words': 763}