# Path: lib/tsconfiglint/TsconfigLinter.py
# @catch_CancelCommand
# def check_tsconfig(view):
#     """ Tests view for being a tsconfig.json.
#         Reads the tsconfig.json and checks for errors.
#         Show Errors using Regions.
#         Set a error map [<(a,b), msg>] into views settings. """
# 
#     if not _is_valid_and_enabled(view):
#         return
# 
#     Debug('tsconfig', 'tsconfig modified, check')
# 
#     linter = TsconfigLinter(view)
# 
#     return linter
# 
# def show_lint_in_status(view):
#     """ Uses the error map stored in view.settings() to display
#         status messages if the cursor is above an error """
# 
#     if not _is_valid_and_enabled(view):
#         return
# 
#     if view.settings().has('tsconfig-lints'):
#         error_locations = view.settings().get('tsconfig-lints', [])
# 
#         current_errors = []
#         current_selection = view.sel()[0]
# 
#         for pos, error in error_locations:
#             error_region = sublime.Region(pos[0], pos[1])
#             if current_selection.intersects(error_region):
#                 current_errors.append(error)
# 
#         if len(current_errors):
#             view.set_status('tsconfig-errors', ' | '.join(current_errors))
#         else:
#             view.erase_status('tsconfig-errors')
# 
# Path: lib/tsconfiglint/tsconfigglobexpand.py
# def expand_filesglob(linter):
#     """
#         This mimics the filesGlob behaviour of atom-typescript [2]
#         If tsconfig.json has no HardErrors, it executes bin/expandglob.js
#         and reloads linter.view from disk.
#         This operates on the file contents, so the file should have been saved
#         before.
#         Returns immediately if not linted or linter is None
#         Returns True if the filesGlob has been expanded
#         Returns False if there was a linter error, so no expansion has been done
#     """
# 
#     # Expanding?
# 
#     if is_tsglobexpansion_disabled():
#         return False
# 
#     if linter is None or not linter or not linter.linted:
#         return False
# 
#     if len(linter.harderrors) > 0:
#         return False
# 
#     if len(linter.softerrors) != linter.numerrors:
#         return False
# 
#     if linter.content == "":
#         return False
# 
#     if "filesGlob" not in linter.tsconfig:
#         return True # Should reopen project, so return True here
# 
#     # Expand!
#     project_dir = os.path.dirname(linter.view.file_name())
#     file_list = _expand_globs_with_javascript(project_dir, linter)
#     Debug("tsconfig.json", "fileGlobs expaned")
# 
#     # reload file
#     linter.view.run_command("revert")
# 
#     # lint again, so the soft errors are still displayed
#     check_tsconfig(linter.view)
# 
#     return True
# 
# Path: lib/utils/CancelCommand.py
# def catch_CancelCommand(func):
#     """ Decorate every command with this one. It will check for
#         the plugin disabled flag and catch CancelCommand exceptins. """
# 
#     def catcher(*kargs, **kwargs):
#         if is_plugin_temporarily_disabled():
#             return # do not execute command
#         try:
#             return func(*kargs, **kwargs)
#         except CancelCommand:
#             Debug('command', "A COMMAND WAS CANCELED")
#             return False
#     return catcher
# 
# class CancelCommand(Exception):
#     """ Throw this exception in a command. The decorator
#         catch_CancelCommand will catch it and cancel silently """
#     pass
# 
# Path: lib/system/Project.py
# def opened_project_by_tsconfig(tsconfigfile):
#     if not tsconfigfile:
#         return None
#     tsconfigfile = os.path.normcase(tsconfigfile)
#     for p in OPENED_PROJECTS.values():
#         if os.path.normcase(p.tsconfigfile) == tsconfigfile:
#             return p
#     return None


import sublime
import sublime_plugin
import os
from .TsconfigLinter import check_tsconfig, show_lint_in_status
from .tsconfigglobexpand import expand_filesglob
from ..utils.CancelCommand import catch_CancelCommand, CancelCommand
from ..system.Project import opened_project_by_tsconfig
# coding=utf8




class TsconfigEventListener(sublime_plugin.EventListener):
    """ Listen to file events -> Activate TsconfigLinter.
        check_tsconfig immediately returns if file is no tsconfig.json """

    def on_activated_async(self, view):
        check_tsconfig(view)


    def on_load_async(self, view):

# Path: lib/tsconfiglint/TsconfigLinter.py
# @catch_CancelCommand
# def check_tsconfig(view):
#     """ Tests view for being a tsconfig.json.
#         Reads the tsconfig.json and checks for errors.
#         Show Errors using Regions.
#         Set a error map [<(a,b), msg>] into views settings. """
# 
#     if not _is_valid_and_enabled(view):
#         return
# 
#     Debug('tsconfig', 'tsconfig modified, check')
# 
#     linter = TsconfigLinter(view)
# 
#     return linter
# 
# def show_lint_in_status(view):
#     """ Uses the error map stored in view.settings() to display
#         status messages if the cursor is above an error """
# 
#     if not _is_valid_and_enabled(view):
#         return
# 
#     if view.settings().has('tsconfig-lints'):
#         error_locations = view.settings().get('tsconfig-lints', [])
# 
#         current_errors = []
#         current_selection = view.sel()[0]
# 
#         for pos, error in error_locations:
#             error_region = sublime.Region(pos[0], pos[1])
#             if current_selection.intersects(error_region):
#                 current_errors.append(error)
# 
#         if len(current_errors):
#             view.set_status('tsconfig-errors', ' | '.join(current_errors))
#         else:
#             view.erase_status('tsconfig-errors')
# 
# Path: lib/tsconfiglint/tsconfigglobexpand.py
# def expand_filesglob(linter):
#     """
#         This mimics the filesGlob behaviour of atom-typescript [2]
#         If tsconfig.json has no HardErrors, it executes bin/expandglob.js
#         and reloads linter.view from disk.
#         This operates on the file contents, so the file should have been saved
#         before.
#         Returns immediately if not linted or linter is None
#         Returns True if the filesGlob has been expanded
#         Returns False if there was a linter error, so no expansion has been done
#     """
# 
#     # Expanding?
# 
#     if is_tsglobexpansion_disabled():
#         return False
# 
#     if linter is None or not linter or not linter.linted:
#         return False
# 
#     if len(linter.harderrors) > 0:
#         return False
# 
#     if len(linter.softerrors) != linter.numerrors:
#         return False
# 
#     if linter.content == "":
#         return False
# 
#     if "filesGlob" not in linter.tsconfig:
#         return True # Should reopen project, so return True here
# 
#     # Expand!
#     project_dir = os.path.dirname(linter.view.file_name())
#     file_list = _expand_globs_with_javascript(project_dir, linter)
#     Debug("tsconfig.json", "fileGlobs expaned")
# 
#     # reload file
#     linter.view.run_command("revert")
# 
#     # lint again, so the soft errors are still displayed
#     check_tsconfig(linter.view)
# 
#     return True
# 
# Path: lib/utils/CancelCommand.py
# def catch_CancelCommand(func):
#     """ Decorate every command with this one. It will check for
#         the plugin disabled flag and catch CancelCommand exceptins. """
# 
#     def catcher(*kargs, **kwargs):
#         if is_plugin_temporarily_disabled():
#             return # do not execute command
#         try:
#             return func(*kargs, **kwargs)
#         except CancelCommand:
#             Debug('command', "A COMMAND WAS CANCELED")
#             return False
#     return catcher
# 
# class CancelCommand(Exception):
#     """ Throw this exception in a command. The decorator
#         catch_CancelCommand will catch it and cancel silently """
#     pass
# 
# Path: lib/system/Project.py
# def opened_project_by_tsconfig(tsconfigfile):
#     if not tsconfigfile:
#         return None
#     tsconfigfile = os.path.normcase(tsconfigfile)
#     for p in OPENED_PROJECTS.values():
#         if os.path.normcase(p.tsconfigfile) == tsconfigfile:
#             return p
#     return None


# Path: lib/tsconfiglint/TsconfigListener.py
import sublime
import sublime_plugin
import os
from .TsconfigLinter import check_tsconfig, show_lint_in_status
from .tsconfigglobexpand import expand_filesglob
from ..utils.CancelCommand import catch_CancelCommand, CancelCommand
from ..system.Project import opened_project_by_tsconfig

# coding=utf8




class TsconfigEventListener(sublime_plugin.EventListener):
    """ Listen to file events -> Activate TsconfigLinter.
        check_tsconfig immediately returns if file is no tsconfig.json """

    def on_activated_async(self, view):
        check_tsconfig(view)


    def on_load_async(self, view):

        check_tsconfig(view)