Example #1
0
    def __init__(self, port):
        super().__init__("localhost", port)
        self.hsdev_process = None

        hsdev_ver = hsdev_version()
        if hsdev_ver is None:
            Common.output_error_async(
                sublime.active_window(),
                "\n".join(HsDevLocalAgent.hsdev_not_found))
        elif not check_version(hsdev_ver, HSDEV_MIN_VER):
            Common.output_error_async(
                sublime.active_window(),
                "\n".join(HsDevLocalAgent.hsdev_wrong_version(hsdev_ver)))
        else:
            hsdev_log_settings = Settings.PLUGIN.hsdev_log_config
            if patch_simple_log(hsdev_ver):
                hsdev_log_settings = Settings.PLUGIN.hsdev_log_level

            self.hsdev_process = HsDevProcess(
                port,
                hsdev_ver,
                cache=os.path.join(Common.sublime_haskell_cache_path(),
                                   'hsdev'),
                log_file=os.path.join(Common.sublime_haskell_cache_path(),
                                      'hsdev', 'hsdev.log'),
                log_config=hsdev_log_settings)
Example #2
0
    def do_start_hsdev(self):
        hsdev_ver = hsdev_version()
        if hsdev_ver is None:
            Common.output_error_async(sublime.active_window(), "\n".join(HsDevLocalAgent.hsdev_not_found))
            return False
        elif not check_version(hsdev_ver, HSDEV_MIN_VER, HSDEV_MAX_VER):
            Common.output_error_async(sublime.active_window(), "\n".join(HsDevLocalAgent.hsdev_wrong_version(hsdev_ver)))
            return False
        else:
            def start_():
                Logging.log('hsdev process started', Logging.LOG_TRACE)
                self.connect_clients()

            def exit_():
                Logging.log('hsdev process exited', Logging.LOG_TRACE)
                self.disconnect_clients()

            def connected_():
                Logging.log('hsdev agent: primary connection to hsdev established', Logging.LOG_TRACE)
                self.client.link()

            self.client.set_on_connected(connected_)
            self.hsdev_process.on_start = start_
            self.hsdev_process.on_exit = exit_

            self.hsdev_process.start()
            self.hsdev_process.create()
            return True
Example #3
0
    def invoke_tool(command, tool_name, inp='', on_result=None, filename=None, on_line=None, check_enabled=True,
                    **popen_kwargs):
        if check_enabled and not Settings.PLUGIN.__getattribute__(Utils.tool_enabled(tool_name)):
            return None

        source_dir = get_source_dir(filename)

        def mk_result(result):
            return on_result(result) if on_result else result

        try:
            with ProcHelper(command, cwd=source_dir, **popen_kwargs) as proc:
                exit_code, stdout, stderr = proc.wait(inp)
                if exit_code != 0:
                    raise Exception('{0} exited with exit code {1} and stderr: {2}'.format(tool_name, exit_code, stderr))

                if on_line:
                    for line in io.StringIO(stdout):
                        on_line(mk_result(line))
                else:
                    return mk_result(stdout)

        except OSError as os_exc:
            if os_exc.errno == errno.ENOENT:
                errmsg = "SublimeHaskell: {0} was not found!\n'{1}' is set to False".format(tool_name,
                                                                                            Utils.tool_enabled(tool_name))
                Common.output_error_async(sublime.active_window(), errmsg)
                Settings.PLUGIN.__setattr__(Utils.tool_enabled(tool_name), False)
            else:
                Logging.log('{0} fails with {1}, command: {2}'.format(tool_name, os_exc, command), Logging.LOG_ERROR)

            return None

        return None
Example #4
0
def call_ghcmod_and_wait(arg_list, filename=None, cabal=None):
    """
    Calls ghc-mod with the given arguments.
    Shows a sublime error message if ghc-mod is not available.
    """

    ghc_opts_args = get_ghc_opts_args(filename, add_package_db=False, cabal=cabal)

    try:
        command = ['ghc-mod'] + ghc_opts_args + arg_list

        # Logging.log('running ghc-mod: {0}'.format(command))

        # Set cwd to user directory
        # Otherwise ghc-mod will fail with 'cannot satisfy package...'
        # Seems, that user directory works well
        # Current source directory is set with -i argument in get_ghc_opts_args
        #
        # When cabal project is available current directory is set to the project root
        # to avoid troubles with possible template haskell openFile calls
        ghc_mod_current_dir = ProcHelper.get_source_dir(filename)
        if filename:
            cabal_project_dir = Common.get_cabal_project_dir_of_file(filename)
            if cabal_project_dir:
                ghc_mod_current_dir = cabal_project_dir
        exit_code, out, err = ProcHelper.ProcHelper.run_process(command, cwd=ghc_mod_current_dir)

        if exit_code != 0:
            raise Exception("%s exited with status %d and stderr: %s" % (' '.join(command), exit_code, err))

        # return crlf2lf(out)
        return out

    except OSError as os_exc:
        if os_exc.errno == errno.ENOENT:
            Common.output_error_async(sublime.active_window(),
                                      "SublimeHaskell: ghc-mod was not found!\n"
                                      "It is used for LANGUAGE and import autocompletions and type inference.\n"
                                      "Try adjusting the 'add_to_PATH' setting.\n"
                                      "You can also turn this off using the 'enable_ghc_mod' setting.")
        # Re-raise so that calling code doesn't try to work on the `None` return value
        raise os_exc