Ejemplo n.º 1
0
    def _do_create_libspec_on_get(self, libname, current_doc_uri):
        from robocode_ls_core import uris

        additional_path = None
        abspath = None
        cwd = None
        if current_doc_uri is not None:
            cwd = os.path.dirname(uris.to_fs_path(current_doc_uri))
            if not cwd or not os.path.isdir(cwd):
                cwd = None

        if os.path.isabs(libname):
            abspath = libname

        elif current_doc_uri is not None:
            # relative path: let's make it absolute
            fs_path = os.path.dirname(uris.to_fs_path(current_doc_uri))
            abspath = os.path.abspath(os.path.join(fs_path, libname))

        if abspath:
            additional_path = os.path.dirname(abspath)
            libname = os.path.basename(libname)
            if libname.lower().endswith((".py", ".class", ".java")):
                libname = os.path.splitext(libname)[0]

        if self._create_libspec(libname,
                                additional_path=additional_path,
                                cwd=cwd):
            self.synchronize_internal_libspec_folders()
            return True
        return False
Ejemplo n.º 2
0
    def __init__(self, root_uri, init_opts, process_id, capabilities):
        self._root_path = uris.to_fs_path(root_uri)
        self._root_uri = root_uri
        self._init_opts = init_opts
        self._process_id = process_id
        self._capabilities = capabilities

        self._settings = {}

        self._config_sources = {}
Ejemplo n.º 3
0
    def __init__(self, uri, source=None, version=None):
        self.uri = uri
        self.version = version
        self.path = uris.to_fs_path(uri)  # Note: may be None.

        self._source = source
        self.__line_start_offsets = None

        # Only set when the source is read from disk.
        self._source_mtime = -1
Ejemplo n.º 4
0
    def __init__(self, root_uri, workspace_folders=None) -> None:
        from robocode_ls_core.lsp import WorkspaceFolder

        self._root_uri = root_uri
        self._root_uri_scheme = uri_scheme(self._root_uri)
        self._root_path = to_fs_path(self._root_uri)
        self._folders: Dict[str, WorkspaceFolder] = {}

        # Contains the docs with files considered open.
        self._docs: Dict[str, IDocument] = {}

        # Contains the docs pointing to the filesystem.
        self._filesystem_docs: Dict[str, IDocument] = {}

        if workspace_folders is not None:
            for folder in workspace_folders:
                self.add_folder(folder)

        if root_uri and root_uri not in self.folders:
            as_fs_path = uris.to_fs_path(root_uri)
            name = os.path.basename(as_fs_path)
            self.add_folder(WorkspaceFolder(root_uri, name))
Ejemplo n.º 5
0
    def __init__(self, resource_doc):
        """
        :param RobotDocument resource_doc:
        """
        from robocode_ls_core import uris

        self.keyword_name = ""
        self.resource_doc = resource_doc
        self.source = uris.to_fs_path(resource_doc.uri)
        self.lineno = 1
        self.end_lineno = 1
        self.col_offset = 1
        self.end_col_offset = 1
Ejemplo n.º 6
0
    def add_workspace_folder(self, folder_uri):
        from robocode_ls_core import uris

        if folder_uri not in self._workspace_folder_uri_to_folder_info:
            log.debug("Added workspace folder: %s", folder_uri)
            cp = self._workspace_folder_uri_to_folder_info.copy()
            folder_info = cp[folder_uri] = _FolderInfo(
                uris.to_fs_path(folder_uri), recursive=True)
            self._workspace_folder_uri_to_folder_info = cp
            folder_info.start_watch(self._observer,
                                    self._spec_changes_notifier)
            folder_info.synchronize()
        else:
            log.debug("Workspace folder already added: %s", folder_uri)
Ejemplo n.º 7
0
    def get_interpreter_info_for_doc_uri(self, doc_uri) -> Optional[IInterpreterInfo]:
        try:
            pm = self._pm()
            if pm is None:
                log.critical("Did not expect PluginManager to be None at this point.")
                return None

            from robotframework_ls.ep_providers import (
                EPConfigurationProvider,
                EPEndPointProvider,
            )

            # Check that our requirements are there.
            pm[EPConfigurationProvider]
            pm[EPEndPointProvider]

            fs_path = Path(uris.to_fs_path(doc_uri))
            for path in fs_path.parents:
                package_yaml: Path = path / "package.yaml"
                if package_yaml.exists():
                    break
            else:
                # i.e.: Could not find any package.yaml in the structure.
                log.debug("Could not find package yaml for: %s", fs_path)
                return None

            # Ok, we have the package_yaml, so, we should be able to run RCC with it.
            package_yaml_file_info = _CacheInfo.get_file_info(package_yaml)
            yaml_contents = package_yaml_file_info.yaml_contents
            if not isinstance(yaml_contents, dict):
                raise AssertionError(f"Expected dict as root in: {package_yaml}")

            conda_config = yaml_contents.get("condaConfig")
            conda_config_file_info = None

            if conda_config:
                parent: Path = package_yaml.parent
                conda_config_path = parent.joinpath(conda_config)
                if conda_config_path.exists():
                    conda_config_file_info = _CacheInfo.get_file_info(conda_config_path)

            return _CacheInfo.get_interpreter_info(
                package_yaml_file_info, conda_config_file_info, pm
            )

        except:
            log.exception(f"Error getting interpreter info for: {doc_uri}")
        return None
Ejemplo n.º 8
0
def _add_completions_from_dir(
    completion_context,
    directory,
    matcher,
    ret,
    sel,
    token,
    qualifier,
    extensions,
    skip_current,
):
    from robocode_ls_core import uris

    def normfile(path):
        return os.path.normpath(os.path.normcase(path))

    curr_file = normfile(uris.to_fs_path(completion_context.doc.uri))

    try:
        # This is ok if the directory doesn't exist.
        contents = sorted(os.listdir(directory))
    except:
        return

    for filename in contents:
        use_path = None
        if filename.endswith(extensions):
            # If that'd be a match for the current .robot file, don't show it.
            if skip_current and curr_file == normfile(
                    os.path.join(directory, filename)):
                continue

            use_path = filename

        elif filename not in (u"__pycache__", u".git") and os.path.isdir(
                os.path.join(directory, filename)):
            use_path = filename + u"/"
        else:
            continue

        if matcher.accepts(use_path):

            ret.append(
                _create_completion_item(use_path,
                                        sel,
                                        token,
                                        start_col_offset=sel.col -
                                        len(qualifier)))
Ejemplo n.º 9
0
    def get_interpreter_info_for_doc_uri(self, doc_uri) -> Optional[IInterpreterInfo]:
        as_path = Path(uris.to_fs_path(doc_uri))

        environ = dict(os.environ)

        if as_path.parent.name == "env1":
            return DefaultInterpreterInfo(
                "env1", sys.executable, environ, [str(as_path.parent / "lib1")]
            )

        elif as_path.parent.name == "env2":
            return DefaultInterpreterInfo(
                "env2", sys.executable, environ, [str(as_path.parent / "lib2")]
            )

        return None
Ejemplo n.º 10
0
def test_keyword_completions_user_library(data_regression, workspace, tmpdir,
                                          cases, libspec_manager,
                                          library_import):
    import os.path
    from robotframework_ls.impl import keyword_completions
    from robotframework_ls.impl.completion_context import CompletionContext
    from robotframework_ls_tests.fixtures import LIBSPEC_1
    from robocode_ls_core import uris

    workspace_dir = str(tmpdir.join("workspace"))
    cases.copy_to("case1", workspace_dir)

    workspace.set_root(workspace_dir, libspec_manager=libspec_manager)
    doc = workspace.get_doc("case1.robot")

    if library_import == "__FULL_PATH__":
        case1_robot_path = uris.to_fs_path(doc.uri)
        case1_py_path = os.path.join(os.path.dirname(case1_robot_path),
                                     "case1_library.py")
        library_import = case1_py_path

    doc.source = doc.source.replace("case1_library", library_import)
    doc.source = doc.source + "\n    verify"

    completions = keyword_completions.complete(
        CompletionContext(doc, workspace=workspace.ws))
    data_regression.check(completions, basename="keyword_completions_1")

    # Now, let's put a .libspec file in the workspace and check whether
    # it has priority over the auto-generated spec file.
    with open(os.path.join(workspace_dir, "new_spec.libspec"), "w") as stream:
        stream.write(LIBSPEC_1)
    libspec_manager.synchronize_workspace_folders()

    completions = keyword_completions.complete(
        CompletionContext(doc, workspace=workspace.ws))
    data_regression.check(completions, basename="keyword_completions_2_new")
Ejemplo n.º 11
0
def test_win_to_fs_path(uri, path):
    assert uris.to_fs_path(uri) == path
Ejemplo n.º 12
0
    def source(self):
        from robocode_ls_core import uris

        return uris.to_fs_path(self.completion_context.doc.uri)
Ejemplo n.º 13
0
 def get_folder_paths(self) -> List[str]:
     folders = self._folders
     return [uris.to_fs_path(ws_folder.uri) for ws_folder in folders.values()]
Ejemplo n.º 14
0
    def resource_name(self):
        from robocode_ls_core import uris

        uri = self.completion_context.doc.uri
        return os.path.splitext(os.path.basename(uris.to_fs_path(uri)))[0]
Ejemplo n.º 15
0
def _get_completions(completion_context, token, match_libs, extensions,
                     skip_current):
    from robotframework_ls.impl.string_matcher import RobotStringMatcher
    from robocode_ls_core import uris
    from robotframework_ls.impl.robot_constants import BUILTIN_LIB

    ret = []

    sel = completion_context.sel
    value_to_cursor = token.value
    if token.end_col_offset > sel.col:
        value_to_cursor = value_to_cursor[:-(token.end_col_offset - sel.col)]
    if u"{" in value_to_cursor:
        value_to_cursor = completion_context.token_value_resolving_variables(
            value_to_cursor)

    value_to_cursor_split = os.path.split(value_to_cursor)

    if os.path.isabs(value_to_cursor):
        _add_completions_from_dir(
            completion_context,
            value_to_cursor_split[0],
            RobotStringMatcher(value_to_cursor_split[1]),
            ret,
            sel,
            token,
            value_to_cursor_split[1],
            extensions,
            skip_current=skip_current,
        )

    else:
        if match_libs:
            matcher = RobotStringMatcher(value_to_cursor)
            libspec_manager = completion_context.workspace.libspec_manager
            library_names = set(libspec_manager.get_library_names())
            library_names.discard(BUILTIN_LIB)

            for library_name in library_names:
                if matcher.accepts(library_name):
                    ret.append(
                        _create_completion_item(library_name, sel, token))

        # After checking the existing library names in memory (because we
        # loaded them at least once), check libraries in the filesystem.
        uri = completion_context.doc.uri
        path = uris.to_fs_path(uri)
        dirname = os.path.dirname(path)

        matcher = RobotStringMatcher(value_to_cursor_split[1])
        directory = os.path.join(dirname, value_to_cursor_split[0])
        _add_completions_from_dir(
            completion_context,
            directory,
            matcher,
            ret,
            sel,
            token,
            value_to_cursor_split[1],
            extensions,
            skip_current=skip_current,
        )
    return ret
Ejemplo n.º 16
0
 def find_parents(self, path, names):
     root_path = uris.to_fs_path(self._root_uri)
     return basic.find_parents(root_path, path, names)