Exemple #1
0
def resolve_path_type(needle, trigger):
    """ ^
        |  Force
        | --------
        |  Needle
        | --------
        | Trigger?
    """
    type_of_path = False  # OR RELATIVE BY DEFAULT?
    # test if forced by command
    if override.get("filepath_type"):
        type_of_path = override.get("filepath_type")
    # test path to trigger auto-completion by needle
    elif not by_command() and trigger.get("auto") is False and settings.get(
            "auto_trigger") and Path.is_absolute(needle):
        type_of_path = "absolute"
    elif Path.is_absolute(needle):
        type_of_path = "absolute"
    elif Path.is_relative(needle):
        type_of_path = "relative"
    elif trigger.get("relative") is True:
        type_of_path = "relative"
    elif trigger.get("relative") is False:
        type_of_path = "absolute"

    return type_of_path
Exemple #2
0
def build(needle, trigger, current_folder):
    """
        updates state object for given trigger. The state object is then used to query the file cache:

        @see completion
        ProjectManager.search_completions(
                query.get_needle(),
                current_file.get_project_directory(),
                query.get_extensions(),
                query.get_base_path()
            )
    """
    needle = Path.sanitize(needle)
    needle_is_absolute = Path.is_absolute(needle)
    needle_is_path = needle_is_absolute or Path.is_relative(needle)

    if not trigger or not (
            by_command() or
        (settings.get("auto_trigger") if needle_is_path else trigger.get(
            "auto", settings.get("auto_trigger")))):
        return False
    """ Adjust current folder by specified base folder:

        BASE-FOLDER ->  CURRENT_FOLDER
        ------------------------------------------------------------
        True            use settings base_directory
        String          use string as base_directory
        False           use current file's directory (parameter)
    """
    base_path = resolve_value("base_directory", trigger, False)
    if base_path is True:
        current_folder = settings.get("base_directory")
    elif base_path:
        current_folder = Path.sanitize_base_directory(base_path)

    state["post_remove_path"] = current_folder if (
        base_path and needle_is_absolute) else False
    state["base_directory"] = current_folder if resolve_path_type(
        needle, trigger) == "relative" else False
    state["replace_on_insert"] = resolve_value("replace_on_insert", trigger,
                                               [])
    state["extensions"] = resolve_value("extensions", trigger, ["*"])
    state["needle"] = sanitize_needle(needle, current_folder)

    return True
Exemple #3
0
    def run(self, edit):
        current_directory = os.path.dirname(self.view.file_name())
        context = Context.get_context(self.view)
        if context.get("valid") is False:
            return log(ID, "abort, no valid path given:", context.get("needle"))

        path = context.get("needle")
        project_folder = state.get_project_directory()

        if not (path and project_folder):
            return log(ID, "path or project invalid", path, project_folder)

        is_relative = Path.is_relative(path)
        if is_relative:
            path = Path.get_absolute_path(current_directory, path)
            path = re.sub(project_folder, "", path)


        path = re.sub("^[\\\\/\.]", "", path)
        realpath = path
        # cleanup string, in case there are special characters for a path
        # e.g. webpack uses ~, which is not part of path
        path = re.sub("[^A-Za-z0-9 \-_\\\\/.%?#]*", "", path)
        files = state.find_file(path)

        if len(files) == 0:
            # it may be an uncached file, try to open it by using the real path
            if self.filepath_exists(os.path.join(project_folder, realpath)):
                return self.open_file(project_folder, realpath)
            return log(ID, "failed finding file", path, "it is not within the cache and not a realpath")

        if len(files) == 1:
            self.open_file(project_folder, files[0])
        else:
            # if javascript, search for index.js
            current_scope = self.view.scope_name(Selection.get_position(self.view))
            if re.search("\.js ", current_scope):
                for file in files:
                    if "index.js" in file:
                        return self.open_file(project_folder, file)

            self.files = files
            self.project_folder = project_folder
            self.view.show_popup_menu(files, self.select_file)
Exemple #4
0
    def run(self, edit):
        current_directory = os.path.dirname(self.view.file_name())
        context = Context.get_context(self.view)
        if context.get("valid") is False:
            return log(ID, "abort, no valid path given:",
                       context.get("needle"))

        path = context.get("needle")
        project_folder = state.get_project_directory()

        if not (path and project_folder):
            return log(ID, "path or project invalid", path, project_folder)

        is_relative = Path.is_relative(path)
        if is_relative:
            path = Path.get_absolute_path(current_directory, path)
            path = re.sub(project_folder, "", path)

        path = re.sub("^[\\\\/\.]", "", path)
        files = state.find_file(path)

        if len(files) == 0:
            return log(ID, "failed finding file", path)

        if len(files) == 1:
            self.open_file(project_folder, files[0])
        else:
            # if javascript, search for index.js
            current_scope = self.view.scope_name(
                Selection.get_position(self.view))
            if re.search("\.js ", current_scope):
                for file in files:
                    if "index.js" in file:
                        return self.open_file(project_folder, file)

            self.files = files
            self.project_folder = project_folder
            self.view.show_popup_menu(files, self.select_file)
    def run(self, edit):
        current_directory = os.path.dirname(self.view.file_name())

        context = Context.get_context(self.view)
        if context.get("valid") is False:
            return log(ID, "abort, no valid path given:", context.get("needle"))

        path = context.get("needle")
        project = ProjectManager.get_current_project()

        if not (path and project):
            return log(ID, "path or project invalid", path, project)

        is_relative = Path.is_relative(path)
        if is_relative:
            path = Path.get_absolute_path(current_directory, path)
            path = re.sub(project.get_directory(), "", path)

        path = re.sub("^[\\\\/\.]", "", path)
        files = project.find_file(path)

        if len(files) == 0:
            return log(ID, "failed finding file", path)

        if len(files) == 1:
            self.open_file(project.get_directory(), files[0])
        else:
            # if javascript, search for index.js
            current_scope = self.view.scope_name(Selection.get_position(self.view))
            if re.search("\.js ", current_scope):
                for file in files:
                    if "index.js" in file:
                        return self.open_file(project.get_directory(), file)

            self.files = files
            self.project_folder = project.get_directory()
            self.view.show_popup_menu(files, self.select_file)
    def build(needle, trigger, current_folder, project_folder):

        query = {}

        force_type = Query.get("filepath_type", False)
        triggered = Query.by_command()
        filepath_type = "relative"
        needle = Path.sanitize(needle)
        needle_is_absolute = Path.is_absolute(needle)
        needle_is_relative = Path.is_relative(needle)
        needle_is_path = needle_is_absolute or needle_is_relative
        # abort if autocomplete is not available
        if not triggered and trigger.get("auto", False) is False and needle_is_path is False:
            # verbose("FFP no autocomplete")
            return False
        # test path to trigger auto-completion by needle
        if not triggered and trigger["auto"] is False and config["AUTO_TRIGGER"] and needle_is_absolute:
            force_type = "absolute"
        # base_directory: override - trigger - False
        base_directory = trigger.get("base_directory", False)
        base_directory = Query.get("base_directory", base_directory)
        #
        # set current directory by force, else by trigger:
        #
        # trigger       |
        # --------------|--------------------
        # False         | use current file's directory
        # True          | use settings: base_directory
        # String        | use string as base_directory
        # change base folder to base directory
        #
        if base_directory is True:
            current_folder = config["BASE_DIRECTORY"]
        elif base_directory:
            current_folder = Path.sanitize_base_directory(base_directory)
        # notify completion to replace path
        if base_directory and needle_is_absolute:
            Completion.base_directory = current_folder
        #
        # filepath_type
        #
        # needle    | trigger rel   | force     | RESULT
        # ----------|---------------|-----------|---------
        # ?         | relative      | False     | relative
        # ?         | absolute      | False     | absolute
        # absolute  | *             | False     | absolute
        # relative  | *             | False     | relative
        # *         | *             | relative  | relative
        # *         | *             | absolute  | absolute
        #
        if force_type:
            filepath_type = force_type
        elif needle_is_absolute:
            filepath_type = "absolute"
        elif needle_is_relative:
            filepath_type = "relative"
        elif trigger.get("relative") is True:
            filepath_type = "relative"
        elif trigger.get("relative") is False:
            filepath_type = "absolute"

        Query.base_path = current_folder if filepath_type == "relative" else False

        # replacements: override - trigger - None
        Query.replace_on_insert = trigger.get("replace_on_insert", [])
        Query.replace_on_insert = Query.get("replace_on_insert", Query.replace_on_insert)
        # extensions: override - trigger - "js"
        extensions = trigger.get("extensions", ["*"])
        extensions = Query.get("extensions", extensions)
        Query.extensions = extensions
        Query.needle = Query.build_needle_query(needle, current_folder)
        # strip any starting dots or slashes
        Query.needle = re.sub("^[\.\/]*", "", Query.needle)
        # --------------------------------------------------------------------
        # tests throw error if results are set to class
        # Require refactoring of static classes with dynamic properties?
        # --------------------------------------------------------------------
        query["extensions"] = extensions
        query["base_path"] = current_folder if filepath_type == "relative" else False
        query["needle"] = Query.build_needle_query(needle, current_folder)

        if triggered or (config["AUTO_TRIGGER"] if needle_is_path else trigger.get("auto", config["AUTO_TRIGGER"])):
            return query

        return False
    def build(needle, trigger, current_folder, project_folder):

        query = {}

        force_type = Query.get("filepath_type", False)
        triggered = Query.by_command()
        filepath_type = "relative"
        needle = Path.sanitize(needle)
        needle_is_absolute = Path.is_absolute(needle)
        needle_is_relative = Path.is_relative(needle)
        needle_is_path = needle_is_absolute or needle_is_relative
        # abort if autocomplete is not available
        if not triggered and trigger.get(
                "auto", False) is False and needle_is_path is False:
            # verbose("FFP no autocomplete")
            return False
        # test path to trigger auto-completion by needle
        if not triggered and trigger["auto"] is False and config[
                "AUTO_TRIGGER"] and needle_is_absolute:
            force_type = "absolute"
        # base_directory: override - trigger - False
        base_directory = trigger.get("base_directory", False)
        base_directory = Query.get("base_directory", base_directory)
        #
        # set current directory by force, else by trigger:
        #
        # trigger       |
        # --------------|--------------------
        # False         | use current file's directory
        # True          | use settings: base_directory
        # String        | use string as base_directory
        # change base folder to base directory
        #
        if base_directory is True:
            current_folder = config["BASE_DIRECTORY"]
        elif base_directory:
            current_folder = Path.sanitize_base_directory(base_directory)
        # notify completion to replace path
        if base_directory and needle_is_absolute:
            Completion.base_directory = current_folder
        #
        # filepath_type
        #
        # needle    | trigger rel   | force     | RESULT
        # ----------|---------------|-----------|---------
        # ?         | relative      | False     | relative
        # ?         | absolute      | False     | absolute
        # absolute  | *             | False     | absolute
        # relative  | *             | False     | relative
        # *         | *             | relative  | relative
        # *         | *             | absolute  | absolute
        #
        if force_type:
            filepath_type = force_type
        elif needle_is_absolute:
            filepath_type = "absolute"
        elif needle_is_relative:
            filepath_type = "relative"
        elif trigger.get("relative") is True:
            filepath_type = "relative"
        elif trigger.get("relative") is False:
            filepath_type = "absolute"

        Query.base_path = current_folder if filepath_type == "relative" else False

        # replacements: override - trigger - None
        Query.replace_on_insert = trigger.get("replace_on_insert", [])
        Query.replace_on_insert = Query.get("replace_on_insert",
                                            Query.replace_on_insert)
        # extensions: override - trigger - "js"
        extensions = trigger.get("extensions", ["*"])
        extensions = Query.get("extensions", extensions)
        Query.extensions = extensions
        Query.needle = Query.build_needle_query(needle, current_folder)
        # strip any starting dots or slashes
        Query.needle = re.sub("^[\.\/]*", "", Query.needle)
        # --------------------------------------------------------------------
        # tests throw error if results are set to class
        # Require refactoring of static classes with dynamic properties?
        # --------------------------------------------------------------------
        query["extensions"] = extensions
        query[
            "base_path"] = current_folder if filepath_type == "relative" else False
        query["needle"] = Query.build_needle_query(needle, current_folder)

        if triggered or (config["AUTO_TRIGGER"] if needle_is_path else
                         trigger.get("auto", config["AUTO_TRIGGER"])):
            return query

        return False