<commit_before>define [
	"models/Doc"
	"models/File"
	"models/Folder"
	"file-tree/FileTreeView"
	"utils/Effects"
	"utils/Modal"
	"libs/backbone"
	"libs/jquery.storage"
], (Doc, File, Folder, FileTreeView, Effects, Modal) ->
	class FileTreeManager
		constructor: (@ide) ->
			_.extend(@, Backbone.Events)
			@views = {}
			@multiSelectedEntities = []
			@ide.on "afterJoinProject", (@project) =>
				@populateFileTree()
				@project_id = @project.id
				if @ide.editor?.current_doc_id?
					@openDoc(@ide.editor.current_doc_id)
				else if location.hash.length > 1
					fileName = location.hash.slice(1)
					@openDocByPath(fileName)
				else if openDoc_id = $.localStorage("doc.open_id.#{@project_id}") and @getEntity(openDoc_id)
					@openDoc(openDoc_id)
				else if @project.get("rootDoc_id")?
					@openDoc(project.get("rootDoc_id"))
				else
					$('#settings').click()
			@view = new FileTreeView(@)
			@ide.sideBarView.addLink
				identifier: "file-tree"
				element:    @view.$el
				prepend:    true
			@view.render()
			@listenForUpdates()

		populateFileTree: () ->
			@view.bindToRootFolder(@project.get("rootFolder"))

		listenForUpdates: () ->
			@ide.socket.on 'reciveNewDoc', (folder_id, doc) =>
				@addEntityToFolder(
					new Doc(id: doc._id, name: doc.name)
					folder_id
				)
		
			@ide.socket.on 'reciveNewFolder', (folder_id, folder) =>
				@addEntityToFolder(
					new Folder(id: folder._id, name: folder.name)
					folder_id
				)
		
			@ide.socket.on 'reciveNewFile', (folder_id, file) =>
				@addEntityToFolder(
					new File(id: file._id, name: file.name)
					folder_id
				)
			
			@ide.socket.on 'removeEntity', (entity_id) =>
				@onDeleteEntity(entity_id)
	
			@ide.socket.on 'reciveEntityRename', (entity_id, newName) =>
				@onRenameEntity(entity_id, newName)

			@ide.socket.on 'reciveEntityMove', (entity_id, folder_id) =>
				@onMoveEntity(entity_id, folder_id)

		registerView: (entity_id, view) ->
			@views[entity_id] = view

		addEntityToFolder: (entity, folder_id) ->
			folder = @views[folder_id].model
			children = folder.get("children")
			children.add(entity)

		openDoc: (doc, line) ->
			doc_id = doc.id or doc
			@trigger "open:doc", doc_id, line: line
			@selectEntity(doc_id)
			$.localStorage "doc.open_id.#{@project_id}", doc_id

		openDocByPath: (path, line) ->
			doc_id = @getDocIdOfPath(path)
			return null if !doc_id?
			@openDoc(doc_id, line)

		openFile: (file) ->
			@trigger "open:file", file
			@selectEntity(file.id)

		openFolder: (folder) ->
			@selectEntity(folder.id)

		selectEntity: (entity_id) ->
			if @views[@selected_entity_id]?
				@views[@selected_entity_id].deselect()
			@selected_entity_id = entity_id
			@ide.sideBarView.deselectAll()
			@views[entity_id]?.select()

		getEntity: (entity_id) ->
			@views[entity_id]?.model<commit_msg>Don't choke when passed null doc to openDoc<commit_after>define [
	"models/Doc"
	"models/File"
	"models/Folder"
	"file-tree/FileTreeView"
	"utils/Effects"
	"utils/Modal"
	"libs/backbone"
	"libs/jquery.storage"
], (Doc, File, Folder, FileTreeView, Effects, Modal) ->
	class FileTreeManager
		constructor: (@ide) ->
			_.extend(@, Backbone.Events)
			@views = {}
			@multiSelectedEntities = []
			@ide.on "afterJoinProject", (@project) =>
				@populateFileTree()
				@project_id = @project.id
				if @ide.editor?.current_doc_id?
					@openDoc(@ide.editor.current_doc_id)
				else if location.hash.length > 1
					fileName = location.hash.slice(1)
					@openDocByPath(fileName)
				else if openDoc_id = $.localStorage("doc.open_id.#{@project_id}") and @getEntity(openDoc_id)
					@openDoc(openDoc_id)
				else if @project.get("rootDoc_id")?
					@openDoc(project.get("rootDoc_id"))
				else
					$('#settings').click()
			@view = new FileTreeView(@)
			@ide.sideBarView.addLink
				identifier: "file-tree"
				element:    @view.$el
				prepend:    true
			@view.render()
			@listenForUpdates()

		populateFileTree: () ->
			@view.bindToRootFolder(@project.get("rootFolder"))

		listenForUpdates: () ->
			@ide.socket.on 'reciveNewDoc', (folder_id, doc) =>
				@addEntityToFolder(
					new Doc(id: doc._id, name: doc.name)
					folder_id
				)
		
			@ide.socket.on 'reciveNewFolder', (folder_id, folder) =>
				@addEntityToFolder(
					new Folder(id: folder._id, name: folder.name)
					folder_id
				)
		
			@ide.socket.on 'reciveNewFile', (folder_id, file) =>
				@addEntityToFolder(
					new File(id: file._id, name: file.name)
					folder_id
				)
			
			@ide.socket.on 'removeEntity', (entity_id) =>
				@onDeleteEntity(entity_id)
	
			@ide.socket.on 'reciveEntityRename', (entity_id, newName) =>
				@onRenameEntity(entity_id, newName)

			@ide.socket.on 'reciveEntityMove', (entity_id, folder_id) =>
				@onMoveEntity(entity_id, folder_id)

		registerView: (entity_id, view) ->
			@views[entity_id] = view

		addEntityToFolder: (entity, folder_id) ->
			folder = @views[folder_id].model
			children = folder.get("children")
			children.add(entity)

		openDoc: (doc, line) ->
			return if !doc?
			doc_id = doc.id or doc
			@trigger "open:doc", doc_id, line: line
			@selectEntity(doc_id)
			$.localStorage "doc.open_id.#{@project_id}", doc_id

		openDocByPath: (path, line) ->
			doc_id = @getDocIdOfPath(path)
			return null if !doc_id?
			@openDoc(doc_id, line)

		openFile: (file) ->
			@trigger "open:file", file
			@selectEntity(file.id)

		openFolder: (folder) ->
			@selectEntity(folder.id)

		selectEntity: (entity_id) ->
			if @views[@selected_entity_id]?
				@views[@selected_entity_id].deselect()
			@selected_entity_id = entity_id
			@ide.sideBarView.deselectAll()
			@views[entity_id]?.select()

		getEntity: (entity_id) ->
			@views[entity_id]?.model
{'fraction_non_alphanumeric': 0.1525065963060686, 'fraction_numerical': 0.0007036059806508355, 'mean_word_length': 3.6991735537190085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '7587739', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2074, 'n_words': 363}