<commit_before>import errno
import os
import re
import sublime
import shlex

from ..anf_util import *
from ..platform.windows_platform import WindowsPlatform
from ..platform.nix_platform import NixPlatform
from ..completions.nix_completion import NixCompletion
from ..completions.windows_completion import WindowsCompletion

VIEW_NAME = "AdvancedNewFileCreation"


class AdvancedNewFileBase(object):

    def __init__(self, window):
        super(AdvancedNewFileBase, self).__init__(window)

        if PLATFORM == "windows":
            self.platform = WindowsPlatform(window.active_view())
        else:
            self.platform = NixPlatform()

    def __generate_default_root(self):
        root_setting = self.settings.get(DEFAULT_ROOT_SETTING)
        path, folder_index = self.__parse_path_setting(
            root_setting, DEFAULT_FOLDER_INDEX_SETTING)
        if path is None and folder_index is None:
            return os.path.expanduser(self.settings.get(DEFAULT_PATH_SETTING))
        elif path is None:
            return self.__project_folder_from_index(folder_index)
        return path

    def __generate_alias_root(self):
        path, folder_index = self.__parse_path_setting(
            self.settings.get(ALIAS_ROOT_SETTING), ALIAS_FOLDER_INDEX_SETTING)
        if path is None and folder_index is None:
            return os.path.expanduser(self.settings.get(ALIAS_PATH_SETTING))
        elif path is None:
            if folder_index >= 0:
                return self.window.folders()[folder_index]
            else:
                return os.path.expanduser("~/")
        return path

    def generate_initial_path(self, initial_path=None):
        # Search for initial string
        if initial_path is not None:
            path = initial_path
        else:
            if self.settings.get(USE_CURSOR_TEXT_SETTING, False):
                cursor_text = self.get_cursor_path()
                if cursor_text != "":
                    path = cursor_text
            else:
                path = self.settings.get(DEFAULT_INITIAL_SETTING)

        return path

    def run_setup(self):
        self.view = self.window.active_view()
        self.settings = get_settings(self.view)
        self.root = None
        self.alias_root = None
        self.aliases = self.__get_aliases()

        self.root = self.__generate_default_root()
        self.alias_root = self.__generate_alias_root()

        # Need to fix this
        debug = self.settings.get(DEBUG_SETTING) or False

        completion_type = self.settings.get(COMPLETION_TYPE_SETTING)
        if completion_type == "windows":
            self.completion = WindowsCompletion(self)
        else:
            self.completion = NixCompletion(self)

    def __get_aliases(self):
        aliases = self.settings.get(ALIAS_SETTING)
        all_os_aliases = self.settings.get(OS_SPECIFIC_ALIAS_SETTING)
        for key in all_os_aliases:
            if PLATFORM in all_os_aliases.get(key):
                aliases[key] = all_os_aliases.get(key).get(PLATFORM)

        return aliases

    def __parse_path_setting(self, setting, index_setting):
        root = None
        folder_index = None
        if setting == "home":
            root = os.path.expanduser("~/")
        elif setting == "current":
            if self.view is not None:
                filename = self.view.file_name()
                if filename is not None:
                    root = os.path.dirname(filename)
            if root is None:
                root = os.path.expanduser("~/")
        elif setting == "project_folder":
            folder_index = self.settings.get(index_setting)
            folder_index = self.__validate_folder_index(folder_index)
        elif setting == "top_folder":
            folder_index = self.__validate_folder_index(0)
        elif setting == "path":
            pass
        else:
            print("Invalid root specifier")

        return (root, folder_index)

    def __validate_folder_index(self, folder_index):
        num_folders = len(self.window.folders())
        if num_folders == 0:
            folder_index = -1
        elif num_folders < folder_index:
            folder_index = 0
        return folder_index

    def split_path(self, path=""):
        HOME_REGEX = r"^~[/\\]"
        root = None
        try:
            root, path = self.platform.split(path)
            if self.settings.get(SHELL_INPUT_SETTING, False) and len(path) > 0:
                split_path = shlex.split(str(path))
                path = " ".join(split_path)
            # Parse if alias
            if TOP_LEVEL_SPLIT_CHAR in path and root is None:
                parts = path.rsplit(TOP_LEVEL_SPLIT_CHAR, 1)
                root, path = self.__translate_alias(parts[0])
                path_list = []
                if path != "":
                    path_list.append(path)
                if parts[1] != "":
                    path_list.append(parts[1])
                path = TOP_LEVEL_SPLIT_CHAR.join(path_list)
            elif re.match(r"^/", path):
                root, path_offset = self.platform.parse_nix_path(root, path)
                path = path[path_offset:]
            # Parse if tilde used
            elif re.match(HOME_REGEX, path) and root is None:
                root = os.path.expanduser("~")
                path = path[2:]
            elif (re.match(r"^\.{1,2}[/\\]", path) and
                  self.settings.get(RELATIVE_FROM_CURRENT_SETTING, False)):
                path_index = 2
                if self.view.file_name() is not None:
                    root = os.path.dirname(self.view.file_name())
                else:
                    folder_index = self.settings.get(RELATIVE_FALLBACK_INDEX_SETTING, 0)
                    folder_index = self.__validate_folder_index(folder_index)
                    root = self.__project_folder_from_index(folder_index)
                if re.match(r"^\.{2}[/\\]", path):<commit_msg>Fix bug where cursor text is searched for, but contains no content<commit_after>import errno
import os
import re
import sublime
import shlex

from ..anf_util import *
from ..platform.windows_platform import WindowsPlatform
from ..platform.nix_platform import NixPlatform
from ..completions.nix_completion import NixCompletion
from ..completions.windows_completion import WindowsCompletion

VIEW_NAME = "AdvancedNewFileCreation"


class AdvancedNewFileBase(object):

    def __init__(self, window):
        super(AdvancedNewFileBase, self).__init__(window)

        if PLATFORM == "windows":
            self.platform = WindowsPlatform(window.active_view())
        else:
            self.platform = NixPlatform()

    def __generate_default_root(self):
        root_setting = self.settings.get(DEFAULT_ROOT_SETTING)
        path, folder_index = self.__parse_path_setting(
            root_setting, DEFAULT_FOLDER_INDEX_SETTING)
        if path is None and folder_index is None:
            return os.path.expanduser(self.settings.get(DEFAULT_PATH_SETTING))
        elif path is None:
            return self.__project_folder_from_index(folder_index)
        return path

    def __generate_alias_root(self):
        path, folder_index = self.__parse_path_setting(
            self.settings.get(ALIAS_ROOT_SETTING), ALIAS_FOLDER_INDEX_SETTING)
        if path is None and folder_index is None:
            return os.path.expanduser(self.settings.get(ALIAS_PATH_SETTING))
        elif path is None:
            if folder_index >= 0:
                return self.window.folders()[folder_index]
            else:
                return os.path.expanduser("~/")
        return path

    def generate_initial_path(self, initial_path=None):
        path = None
        # Search for initial string
        if initial_path is not None:
            path = initial_path
        else:
            if self.settings.get(USE_CURSOR_TEXT_SETTING, False):
                cursor_text = self.get_cursor_path()
                if cursor_text != "":
                    path = cursor_text

            if path is None:
                path = self.settings.get(DEFAULT_INITIAL_SETTING)

        return path

    def run_setup(self):
        self.view = self.window.active_view()
        self.settings = get_settings(self.view)
        self.root = None
        self.alias_root = None
        self.aliases = self.__get_aliases()

        self.root = self.__generate_default_root()
        self.alias_root = self.__generate_alias_root()

        # Need to fix this
        debug = self.settings.get(DEBUG_SETTING) or False

        completion_type = self.settings.get(COMPLETION_TYPE_SETTING)
        if completion_type == "windows":
            self.completion = WindowsCompletion(self)
        else:
            self.completion = NixCompletion(self)

    def __get_aliases(self):
        aliases = self.settings.get(ALIAS_SETTING)
        all_os_aliases = self.settings.get(OS_SPECIFIC_ALIAS_SETTING)
        for key in all_os_aliases:
            if PLATFORM in all_os_aliases.get(key):
                aliases[key] = all_os_aliases.get(key).get(PLATFORM)

        return aliases

    def __parse_path_setting(self, setting, index_setting):
        root = None
        folder_index = None
        if setting == "home":
            root = os.path.expanduser("~/")
        elif setting == "current":
            if self.view is not None:
                filename = self.view.file_name()
                if filename is not None:
                    root = os.path.dirname(filename)
            if root is None:
                root = os.path.expanduser("~/")
        elif setting == "project_folder":
            folder_index = self.settings.get(index_setting)
            folder_index = self.__validate_folder_index(folder_index)
        elif setting == "top_folder":
            folder_index = self.__validate_folder_index(0)
        elif setting == "path":
            pass
        else:
            print("Invalid root specifier")

        return (root, folder_index)

    def __validate_folder_index(self, folder_index):
        num_folders = len(self.window.folders())
        if num_folders == 0:
            folder_index = -1
        elif num_folders < folder_index:
            folder_index = 0
        return folder_index

    def split_path(self, path=""):
        HOME_REGEX = r"^~[/\\]"
        root = None
        try:
            root, path = self.platform.split(path)
            if self.settings.get(SHELL_INPUT_SETTING, False) and len(path) > 0:
                split_path = shlex.split(path)
                path = " ".join(split_path)
            # Parse if alias
            if TOP_LEVEL_SPLIT_CHAR in path and root is None:
                parts = path.rsplit(TOP_LEVEL_SPLIT_CHAR, 1)
                root, path = self.__translate_alias(parts[0])
                path_list = []
                if path != "":
                    path_list.append(path)
                if parts[1] != "":
                    path_list.append(parts[1])
                path = TOP_LEVEL_SPLIT_CHAR.join(path_list)
            elif re.match(r"^/", path):
                root, path_offset = self.platform.parse_nix_path(root, path)
                path = path[path_offset:]
            # Parse if tilde used
            elif re.match(HOME_REGEX, path) and root is None:
                root = os.path.expanduser("~")
                path = path[2:]
            elif (re.match(r"^\.{1,2}[/\\]", path) and
                  self.settings.get(RELATIVE_FROM_CURRENT_SETTING, False)):
                path_index = 2
                if self.view.file_name() is not None:
                    root = os.path.dirname(self.view.file_name())
                else:
                    folder_index = self.settings.get(RELATIVE_FALLBACK_INDEX_SETTING, 0)
                    folder_index = self.__validate_folder_index(folder_index)
                    root = self.__project_folder_from_index(folder_index)
                if re.match(r"^\.{2}[/\\]", path):
{'fraction_non_alphanumeric': 0.08963210702341137, 'fraction_numerical': 0.0026755852842809363, 'mean_word_length': 1.9984958636249686, 'pattern_counts': {'":': 20, '<': 5, '<?xml version=': 0, '>': 7, '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': '4727999', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3334, 'n_words': 731}