import sublime
import sublime_plugin
import os
import time

from . import context
from . import util

class SFDCEventListener(sublime_plugin.EventListener):
    def on_new_async(self, view):
        """
        1. Eveytime when you open a new view, default syntax is Apex
        2. Set Status with current default project
        """
        view.set_syntax_file("Packages/SublimeApex/syntaxes/Apex.tmLanguage")
        context.display_active_project(view)

    def on_load_async(self, view):
        """
        1. Set Status with current default project
        """

        context.display_active_project(view)

    def on_modified_async(self, view):
        """
        Every time when you modified the context, just hide the console, 
        you can close it in sublime settings
        """
        toolingapi_settings = context.get_toolingapi_settings() 

        # If it is not SFDC Component, just return
        if view.file_name() == None: return
        name, extension = util.get_file_attr(view.file_name())
        if extension not in toolingapi_settings["component_extensions"]: return

        # If functionality is close, just return
        if not toolingapi_settings["hidden_console_on_modify"]: return

        # Hidden Console
        sublime.active_window().run_command("hide_panel", 
            {"panel": "console", "toggle": False})

    def on_post_save_async(self, view):
        """
        Every time when you save your ApexPage, ApexTrigger, ApexClass, ApexComponent, 
        this class will make a copy with the time_stamp in the history path of current project
        """
        settings = context.get_toolingapi_settings()

        # Check whether need to keep history
        if not settings["keep_local_change_history"]: return

        # Get current file name and Read file content
        file_name = view.file_name()
        try:
            body = open(file_name, encoding="utf-8").read()
        except:
            body = open(file_name, "rb").read()

        # Get component_name amd component_type
        name, extension = util.get_file_attr(file_name)

        # If this file is not ApexTrigger, ApexComponent, 
        # ApexPage or ApexClass, just return
        if extension not in settings["component_extensions"]: return

        # Get Workspace, if not exist, make it
        component_type = settings[extension]
        workspace = toolingapi_settings["workspace"] + "/history/" + component_type
        if not os.path.exists(workspace):
            os.makedirs(workspace)

        # Backup current file
        time_stamp = time.strftime("%Y-%m-%d-%H-%M", time.localtime())
        fp = open(workspace + "/" + component_name + "-" +\
            time_stamp + extension, "w")

        fp.write(body)
        fp.close()

@@ -1608,16 +1608,27 @@
         
+toolingapi_
 settings
@@ -1725,16 +1725,27 @@
  if not 
+toolingapi_
 settings
@@ -2249,16 +2249,27 @@
  not in 
+toolingapi_
 settings
@@ -2361,25 +2361,48 @@
     
-component_type = 
+folder = toolingapi_settings%5Btoolingapi_
 sett
@@ -2416,16 +2416,27 @@
 tension%5D
+%5D%5B%22folder%22%5D
 %0A       
@@ -2497,30 +2497,22 @@
 ory/%22 + 
-component_type
+folder
 %0A       
@@ -2719,26 +2719,16 @@
 + %22/%22 + 
-component_
 name + %22
@@ -2735,21 +2735,8 @@
 -%22 +
-%5C%0A           
  tim

fix bug caused by release v1.1