# Path: commands/scan_and_index.py
# def index_popen_arg_parser(mode):
#     arg_list = []
#     arg_list.append(get_setting(SettingObject.python_binary))
#     arg_list.append(get_setting(SettingObject.index_runner))
#     arg_list.append(mode)
#     arg_list.append('--db_path')
#     arg_list.append(get_setting(SettingObject.table_dir))
#     arg_list.append('--index_path')
#     arg_list.append(get_setting(SettingObject.index_dir))
#     arg_list.append('--path_to_lib_in_xml')
#     arg_list.append(get_setting(SettingObject.lib_in_xml))
#     arg_list.append('--module_search_path')
#     for module in get_setting(SettingObject.module_search_path):
#         arg_list.append(module)
#     return arg_list
# 
# Path: commands/scan_and_index.py
# def add_builtin_vars(db_path):
#     builtin = 'BuiltIn'
#     table_name = '{0}-{1}.json'.format(
#         builtin, md5(builtin.encode('utf-8')).hexdigest())
#     table_path = path.join(db_path, table_name)
#     f_table = open(table_path, 'r')
#     data = json.load(f_table)
#     f_table.close()
#     builtin_variables = get_setting(SettingObject.builtin_variables)
#     data[DBJsonSetting.variables] = builtin_variables
#     f_table = open(table_path, 'w')
#     json.dump(data, f_table, indent=4)
#     f_table.close()


import sublime_plugin
import sublime
import subprocess
from platform import system
from os import path, makedirs
from ..setting.setting import get_setting
from ..setting.setting import SettingObject
from ..dataparser.parser_utils.file_formatter import rf_table_name
from ..dataparser.parser_utils.util import normalise_path
from .scan_and_index import index_popen_arg_parser
from .scan_and_index import add_builtin_vars
        """
        log_file = get_setting(SettingObject.log_file)
        makedirs(path.dirname(log_file), exist_ok=True)
        open_tab = self.view.file_name()
        if not open_tab:
            message = 'Not able to index because no tabs are active'
            sublime.status_message(message)
            return
        db_table_name = rf_table_name(normalise_path(open_tab))
        db_dir = get_setting(SettingObject.table_dir)
        sublime.set_timeout_async(add_builtin_vars(db_dir))
        if db_table_name:
            file_ = open(log_file, 'a')
            sublime.set_timeout_async(
                self.run_single_index(
                    db_table_name,
                    file_
                ),
                0
            )
            file_.close()
        else:
            message = 'Not able to index file: {0}'.format(open_tab)
            sublime.status_message(message)

    def run_single_index(self, db_table_name, log_file):
        startupinfo = None
        if system() == 'Windows':
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

# Path: commands/scan_and_index.py
# def index_popen_arg_parser(mode):
#     arg_list = []
#     arg_list.append(get_setting(SettingObject.python_binary))
#     arg_list.append(get_setting(SettingObject.index_runner))
#     arg_list.append(mode)
#     arg_list.append('--db_path')
#     arg_list.append(get_setting(SettingObject.table_dir))
#     arg_list.append('--index_path')
#     arg_list.append(get_setting(SettingObject.index_dir))
#     arg_list.append('--path_to_lib_in_xml')
#     arg_list.append(get_setting(SettingObject.lib_in_xml))
#     arg_list.append('--module_search_path')
#     for module in get_setting(SettingObject.module_search_path):
#         arg_list.append(module)
#     return arg_list
# 
# Path: commands/scan_and_index.py
# def add_builtin_vars(db_path):
#     builtin = 'BuiltIn'
#     table_name = '{0}-{1}.json'.format(
#         builtin, md5(builtin.encode('utf-8')).hexdigest())
#     table_path = path.join(db_path, table_name)
#     f_table = open(table_path, 'r')
#     data = json.load(f_table)
#     f_table.close()
#     builtin_variables = get_setting(SettingObject.builtin_variables)
#     data[DBJsonSetting.variables] = builtin_variables
#     f_table = open(table_path, 'w')
#     json.dump(data, f_table, indent=4)
#     f_table.close()


# Path: commands/index_open_tab.py
import sublime_plugin
import sublime
import subprocess
from platform import system
from os import path, makedirs
from ..setting.setting import get_setting
from ..setting.setting import SettingObject
from ..dataparser.parser_utils.file_formatter import rf_table_name
from ..dataparser.parser_utils.util import normalise_path
from .scan_and_index import index_popen_arg_parser
from .scan_and_index import add_builtin_vars

        """
        log_file = get_setting(SettingObject.log_file)
        makedirs(path.dirname(log_file), exist_ok=True)
        open_tab = self.view.file_name()
        if not open_tab:
            message = 'Not able to index because no tabs are active'
            sublime.status_message(message)
            return
        db_table_name = rf_table_name(normalise_path(open_tab))
        db_dir = get_setting(SettingObject.table_dir)
        sublime.set_timeout_async(add_builtin_vars(db_dir))
        if db_table_name:
            file_ = open(log_file, 'a')
            sublime.set_timeout_async(
                self.run_single_index(
                    db_table_name,
                    file_
                ),
                0
            )
            file_.close()
        else:
            message = 'Not able to index file: {0}'.format(open_tab)
            sublime.status_message(message)

    def run_single_index(self, db_table_name, log_file):
        startupinfo = None
        if system() == 'Windows':
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        p_args = index_popen_arg_parser('single')