コード例 #1
0
    def token_value_resolving_variables(self, token):
        from robotframework_ls.impl import ast_utils

        if isinstance(token, str):
            token = ast_utils.create_token(token)

        try:
            tokenized_vars = ast_utils.tokenize_variables(token)
        except:
            return token.value  # Unable to tokenize
        parts = []
        for v in tokenized_vars:
            if v.type == v.NAME:
                parts.append(str(v))

            elif v.type == v.VARIABLE:
                # Resolve variable from config
                initial_v = v = str(v)
                if v.startswith("${") and v.endswith("}"):
                    v = v[2:-1]
                    parts.append(self.convert_robot_variable(v, initial_v))
                else:
                    log.info("Cannot resolve variable: %s", v)
                    parts.append(v)  # Leave unresolved.

        joined_parts = "".join(parts)
        return joined_parts
コード例 #2
0
    def iter_imports_docs(self):
        from robotframework_ls import uris
        import os.path
        from robotframework_ls.impl import ast_utils

        ws = self._workspace

        # Get keywords from resources
        resource_imports = self.get_resource_imports()
        for resource_import in resource_imports:
            for token in resource_import.tokens:
                if token.type == token.NAME:
                    parts = []
                    for v in ast_utils.tokenize_variables(token):
                        if v.type == v.NAME:
                            parts.append(str(v))

                        elif v.type == v.VARIABLE:
                            # Resolve variable from config
                            v = str(v)
                            if v.startswith("${") and v.endswith("}"):
                                v = v[2:-1]
                                parts.append(self.convert_robot_variable(v))
                            else:
                                log.info("Cannot resolve variable: %s", v)

                    resource_path = "".join(parts)
                    if not os.path.isabs(resource_path):
                        # It's a relative resource, resolve its location based on the
                        # current file.
                        resource_path = os.path.join(
                            os.path.dirname(self.doc.path), resource_path)

                    if not os.path.isfile(resource_path):
                        log.info("Resource not found: %s", resource_path)
                        continue

                    doc_uri = uris.from_fs_path(resource_path)
                    resource_doc = ws.get_document(doc_uri, create=False)
                    if resource_doc is None:
                        resource_doc = ws.create_untracked_document(doc_uri)

                    yield resource_doc
コード例 #3
0
def matches_robot_keyword(keyword_name_call_text, keyword_name, _re_cache={}):
    """
    Checks if a given text matches a given keyword. 
    
    Note: both should be already normalized.
    Note: should NOT be called if keyword does not have '{' in it.
    
    :param keyword_name_call_text:
        The call that has resolved variables.
        
    :param keyword_name:
        The keyword (which has variables -- i.e.: '{').
    """

    try:
        compiled = _re_cache[keyword_name]
    except KeyError:
        from robotframework_ls.impl import ast_utils
        from robot.api import Token
        import re

        regexp = []
        for t in ast_utils.tokenize_variables(
                Token(Token.KEYWORD_NAME, keyword_name)):
            if t.type == t.VARIABLE:
                regexp.append("(.*)")
            else:
                regexp.append(re.escape(t.value))

        regexp.append("$")

        regexp = "".join(regexp)
        _re_cache[keyword_name] = re.compile(regexp)
        compiled = _re_cache[keyword_name]

    return compiled.match(keyword_name_call_text)
コード例 #4
0
    def get_resource_imports_as_docs(self):
        from robocode_ls_core import uris
        import os.path
        from robotframework_ls.impl import ast_utils
        from robotframework_ls.impl.robot_lsp_constants import OPTION_ROBOT_PYTHONPATH

        ws = self._workspace
        ret = []

        # Get keywords from resources
        resource_imports = self.get_resource_imports()
        for resource_import in resource_imports:
            for token in resource_import.tokens:
                if token.type == token.NAME:
                    parts = []
                    for v in ast_utils.tokenize_variables(token):
                        if v.type == v.NAME:
                            parts.append(str(v))

                        elif v.type == v.VARIABLE:
                            # Resolve variable from config
                            v = str(v)
                            if v.startswith("${") and v.endswith("}"):
                                v = v[2:-1]
                                parts.append(self.convert_robot_variable(v))
                            else:
                                log.info("Cannot resolve variable: %s", v)

                    joined_parts = "".join(parts)
                    if not os.path.isabs(joined_parts):
                        # It's a relative resource, resolve its location based on the
                        # current file.
                        check_paths = [
                            os.path.join(os.path.dirname(self.doc.path),
                                         joined_parts)
                        ]
                        config = self.config
                        if config is not None:
                            for additional_pythonpath_entry in config.get_setting(
                                    OPTION_ROBOT_PYTHONPATH, list, []):
                                check_paths.append(
                                    os.path.join(additional_pythonpath_entry,
                                                 joined_parts))

                    else:
                        check_paths = [joined_parts]

                    for resource_path in check_paths:
                        if not os.path.isfile(resource_path):
                            log.info("Resource not found: %s", resource_path)
                            continue

                        doc_uri = uris.from_fs_path(resource_path)
                        resource_doc = ws.get_document(doc_uri, create=False)
                        if resource_doc is None:
                            resource_doc = ws.create_untracked_document(
                                doc_uri)
                        ret.append(resource_doc)
                        break

        return tuple(ret)