Exemplo n.º 1
0
def get_projects():
    if hsdev.agent_connected():
        folders = sublime.active_window().folders()
        view_files = [
            v.file_name() for v in sublime.active_window().views()
            if (Common.is_haskell_source(v) or Common.is_cabal_source(v))
            and v.file_name()
        ]

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

        def childof(path, prefix):
            return npath(path).startswith(npath(prefix))

        return dict((info['name'], info) for info in filter(
            lambda p: any([childof(p['path'], f) for f in folders]) or any(
                [childof(src, p['path']) for src in view_files]), (
                    hsdev.client.list_projects() or [])))
    else:
        folder_files = [src for f in sublime.active_window().folders() for src in Common.list_files_in_dir_recursively(f) \
                        if os.path.splitext(src)[1] in [".hs", ".cabal"]
                       ]
        view_files = [
            v.file_name() for v in sublime.active_window().views()
            if (Common.is_haskell_source(v) or Common.is_cabal_source(v))
            and v.file_name()
        ]
        src_files = list(
            map(lambda p: os.path.normcase(os.path.normpath(p)),
                folder_files + view_files))
        active_projects = []
        while src_files:
            src = src_files.pop()
            proj_dir, proj_name = Common.get_cabal_project_dir_and_name_of_file(
                src)
            if proj_dir:
                active_projects.append((proj_name, proj_dir))
                src_files = [
                    f for f in src_files if not f.startswith(proj_dir)
                ]

        return dict((name, {
            'name': name,
            'path': path
        }) for name, path in active_projects)
Exemplo n.º 2
0
    def get_projects(self):
        folders = sublime.active_window().folders()
        view_files = [v.file_name() for v in sublime.active_window().views()
                      if (Common.is_haskell_source(v) or Common.is_cabal_source(v)) and v.file_name()]

        def childof(path, prefix):
            return Utils.normalize_path(path).startswith(Utils.normalize_path(prefix))

        def relevant_project(proj):
            return any([childof(proj['path'], f) for f in folders]) or any([childof(src, proj['path']) for src in view_files])

        projects = BackendMgr.active_backend().list_projects() or []
        return dict((info['name'], info) for info in projects if relevant_project(info))
Exemplo n.º 3
0
    def on_query_context(self, view, key, _operator, _operand, _matchall):
        if Settings.COMPONENT_DEBUG.event_viewer:
            print('{0} key = {1}.'.format(
                type(self).__name__ + '.on_query_context', key))
        retval = None
        if key == 'haskell_autofix':
            retval = view.settings().get('autofix')
        elif key == 'auto_completion_popup':
            retval = Settings.PLUGIN.auto_completion_popup
        elif key == 'haskell_source':
            retval = Common.is_haskell_source(view)
        elif key == 'haskell_source_or_repl':
            retval = Common.is_haskell_source(view) or Common.is_haskell_repl(
                view)
        elif key == 'haskell_repl':
            retval = Common.is_haskell_repl(view)
        elif key == 'haskell_symbol_info':
            retval = Common.is_haskell_symbol_info(view)
        elif key == 'cabal_source':
            retval = Common.is_cabal_source(view)
        elif key == 'scanned_source':
            retval = self.is_scanned_source(view)
        elif key == 'in_project':
            retval = self.is_in_project(view)
        elif key == "is_module_completion" or key == "is_import_completion":
            # Completion context is the only branch here where a backend is needed.
            retval = False
            with self.backend_mgr:
                project_dir, project_name = Common.locate_cabal_project_from_view(
                    view)
                region = view.sel()[0]
                if region.a == region.b:
                    word_region = view.word(region)
                    preline = Common.get_line_contents_before_region(
                        view, word_region)
                    preline += self.COMPLETION_CHARS[key]
                    qsymbol = Common.get_qualified_symbol(preline)
                    if qsymbol.module:
                        mod_completions = self.autocompleter.get_current_module_completions(
                            project_name, project_dir)
                        if qsymbol.is_import_list:
                            retval = qsymbol.module in mod_completions
                        else:
                            retval = [
                                m for m in mod_completions
                                if m.startswith(qsymbol.module)
                            ] != []

        return retval