# Path: sublime_jedi/console_logging.py
# def get_plugin_debug_level(default='error'):
#     def __init__(self, name):
#     def level(self):
#     def _print(self, msg):
#     def log(self, level, msg, **kwargs):
#     def _log(self, level, msg, **kwargs):
#     def debug(self, msg):
#     def info(self, msg):
#     def error(self, msg, exc_info=False):
#     def exception(self, msg):
#     def warning(self, msg):
# class Logger:
# 
# Path: sublime_jedi/daemon.py
# def ask_daemon(view, callback, ask_type, ask_kwargs=None, location=None):
#     """Jedi async request shortcut.
# 
#     :type view: sublime.View
#     :type callback: callable
#     :type ask_type: str
#     :type ask_kwargs: dict or None
#     :type location: type of (int, int) or None
#     """
#     window_id = view.window().id()
# 
#     def _async_summon():
#         answer = ask_daemon_sync(view, ask_type, ask_kwargs, location)
#         _run_in_active_view(window_id)(callback)(answer)
# 
#     if callback:
#         sublime.set_timeout_async(_async_summon, 0)
# 
# Path: sublime_jedi/utils.py
# def get_settings(view):
#     """Get plugin settings.
# 
#     :type view: sublime.View
#     :rtype: dict
#     """
#     python_virtualenv = get_settings_param(view, 'python_virtualenv', None)
#     if python_virtualenv:
#         python_virtualenv = expand_path(view, python_virtualenv)
# 
#     python_interpreter = get_settings_param(view, 'python_interpreter', None)
#     if python_interpreter:
#         python_interpreter = expand_path(view, python_interpreter)
# 
#     extra_packages = get_settings_param(view, 'python_package_paths', [])
#     extra_packages = [expand_path(view, p) for p in extra_packages]
# 
#     complete_funcargs = get_settings_param(
#         view, 'auto_complete_function_params', 'all')
# 
#     enable_in_sublime_repl = get_settings_param(
#         view, 'enable_in_sublime_repl', False)
# 
#     sublime_completions_visibility = get_settings_param(
#         view, 'sublime_completions_visibility', 'default')
# 
#     only_complete_after_regex = get_settings_param(
#         view, 'only_complete_after_regex', '')
# 
#     first_folder = ''
#     if view.window().folders():
#         first_folder = os.path.split(view.window().folders()[0])[-1]
#     project_name = get_settings_param(view, 'project_name', first_folder)
# 
#     return {
#         'python_interpreter': python_interpreter,
#         'python_virtualenv': python_virtualenv,
#         'extra_packages': extra_packages,
#         'project_name': project_name,
#         'complete_funcargs': complete_funcargs,
#         'enable_in_sublime_repl': enable_in_sublime_repl,
#         'sublime_completions_visibility': sublime_completions_visibility,
#         'follow_imports': get_settings_param(view, 'follow_imports', True),
#         'completion_timeout': get_settings_param(view, 'comletion_timeout', 3),
#         'only_complete_after_regex': only_complete_after_regex,
#     }
# 
# def is_python_scope(view, location):
#     """ (View, Point) -> bool
# 
#     Get if this is a python source scope (not a string and not a comment)
#     """
#     return view.match_selector(location, "source.python - string - comment")
# 
# def is_repl(view):
#     """Check if a view is a REPL."""
#     return view.settings().get("repl", False)


import re
import sublime
import sublime_plugin
from threading import Timer
from .console_logging import getLogger
from .daemon import ask_daemon
from .utils import get_settings, is_python_scope, is_repl

                # IF selection is non-zero
                # OR after cursor no any text and selection size is zero
                # THEN insert closing pair
                if region.size() > 0 or not following_text and region.size() == 0:
                    self.view.insert(edit, region.end() + 1, close_pair)
                    position += (len(open_pair) - 1)
            else:
                self.view.replace(edit, region, open_pair)
                position = region.begin() + len(open_pair)

            self.view.sel().add(sublime.Region(position, position))

    def show_template(self, view, template):
        view.run_command('insert_snippet', {"contents": template})


class Autocomplete(sublime_plugin.ViewEventListener):
    """Sublime Text autocompletion integration."""

    _completions = []
    _previous_completions = []
    _last_location = None

    def __enabled(self):
        settings = get_settings(self.view)

        if sublime.active_window().active_view().id() != self.view.id():
            return False


# Path: sublime_jedi/console_logging.py
# def get_plugin_debug_level(default='error'):
#     def __init__(self, name):
#     def level(self):
#     def _print(self, msg):
#     def log(self, level, msg, **kwargs):
#     def _log(self, level, msg, **kwargs):
#     def debug(self, msg):
#     def info(self, msg):
#     def error(self, msg, exc_info=False):
#     def exception(self, msg):
#     def warning(self, msg):
# class Logger:
# 
# Path: sublime_jedi/daemon.py
# def ask_daemon(view, callback, ask_type, ask_kwargs=None, location=None):
#     """Jedi async request shortcut.
# 
#     :type view: sublime.View
#     :type callback: callable
#     :type ask_type: str
#     :type ask_kwargs: dict or None
#     :type location: type of (int, int) or None
#     """
#     window_id = view.window().id()
# 
#     def _async_summon():
#         answer = ask_daemon_sync(view, ask_type, ask_kwargs, location)
#         _run_in_active_view(window_id)(callback)(answer)
# 
#     if callback:
#         sublime.set_timeout_async(_async_summon, 0)
# 
# Path: sublime_jedi/utils.py
# def get_settings(view):
#     """Get plugin settings.
# 
#     :type view: sublime.View
#     :rtype: dict
#     """
#     python_virtualenv = get_settings_param(view, 'python_virtualenv', None)
#     if python_virtualenv:
#         python_virtualenv = expand_path(view, python_virtualenv)
# 
#     python_interpreter = get_settings_param(view, 'python_interpreter', None)
#     if python_interpreter:
#         python_interpreter = expand_path(view, python_interpreter)
# 
#     extra_packages = get_settings_param(view, 'python_package_paths', [])
#     extra_packages = [expand_path(view, p) for p in extra_packages]
# 
#     complete_funcargs = get_settings_param(
#         view, 'auto_complete_function_params', 'all')
# 
#     enable_in_sublime_repl = get_settings_param(
#         view, 'enable_in_sublime_repl', False)
# 
#     sublime_completions_visibility = get_settings_param(
#         view, 'sublime_completions_visibility', 'default')
# 
#     only_complete_after_regex = get_settings_param(
#         view, 'only_complete_after_regex', '')
# 
#     first_folder = ''
#     if view.window().folders():
#         first_folder = os.path.split(view.window().folders()[0])[-1]
#     project_name = get_settings_param(view, 'project_name', first_folder)
# 
#     return {
#         'python_interpreter': python_interpreter,
#         'python_virtualenv': python_virtualenv,
#         'extra_packages': extra_packages,
#         'project_name': project_name,
#         'complete_funcargs': complete_funcargs,
#         'enable_in_sublime_repl': enable_in_sublime_repl,
#         'sublime_completions_visibility': sublime_completions_visibility,
#         'follow_imports': get_settings_param(view, 'follow_imports', True),
#         'completion_timeout': get_settings_param(view, 'comletion_timeout', 3),
#         'only_complete_after_regex': only_complete_after_regex,
#     }
# 
# def is_python_scope(view, location):
#     """ (View, Point) -> bool
# 
#     Get if this is a python source scope (not a string and not a comment)
#     """
#     return view.match_selector(location, "source.python - string - comment")
# 
# def is_repl(view):
#     """Check if a view is a REPL."""
#     return view.settings().get("repl", False)


# Path: sublime_jedi/completion.py
import re
import sublime
import sublime_plugin
from threading import Timer
from .console_logging import getLogger
from .daemon import ask_daemon
from .utils import get_settings, is_python_scope, is_repl


                # IF selection is non-zero
                # OR after cursor no any text and selection size is zero
                # THEN insert closing pair
                if region.size() > 0 or not following_text and region.size() == 0:
                    self.view.insert(edit, region.end() + 1, close_pair)
                    position += (len(open_pair) - 1)
            else:
                self.view.replace(edit, region, open_pair)
                position = region.begin() + len(open_pair)

            self.view.sel().add(sublime.Region(position, position))

    def show_template(self, view, template):
        view.run_command('insert_snippet', {"contents": template})


class Autocomplete(sublime_plugin.ViewEventListener):
    """Sublime Text autocompletion integration."""

    _completions = []
    _previous_completions = []
    _last_location = None

    def __enabled(self):
        settings = get_settings(self.view)

        if sublime.active_window().active_view().id() != self.view.id():
            return False


        if is_repl(self.view) and not settings['enable_in_sublime_repl']: