Ejemplo n.º 1
0
    def format_code(self, source, node_path, prettier_cli_path, prettier_options, view, provide_cursor=False):
        self._error_message = None

        cursor = None
        if provide_cursor:
            cursor = view.sel()[0].a
            prettier_options += ['--cursor-offset', str(cursor)]

        if is_str_none_or_empty(node_path):
            cmd = [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options
        else:
            cmd = [node_path] \
                + [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options

        try:
            format_debug_message('Prettier CLI Command', list_to_str(cmd), debug_enabled(view))

            proc = Popen(
                cmd, stdin=PIPE,
                stderr=PIPE,
                stdout=PIPE,
                env=get_proc_env(),
                shell=is_windows())

            stdout, stderr = proc.communicate(input=source.encode('utf-8'))
            if proc.returncode != 0:
                error_output = stderr.decode('utf-8')
                self.error_message = format_error_message(error_output, str(proc.returncode))

                # detect and scroll to 'Syntax Errors':
                _, _, error_line, error_col = self.has_syntax_error(error_output)
                if error_line != -1 and error_col != -1:
                    scroll_view_to(view, error_line, error_col)

                return None

            new_cursor = None
            if stderr:
                stderr_output = stderr.decode('utf-8')
                if provide_cursor:
                    stderr_lines = stderr_output.splitlines()
                    stderr_output, new_cursor = '\n'.join(stderr_lines[:-1]), stderr_lines[-1]

                # allow warnings to pass-through
                if stderr_output:
                    print(format_error_message(stderr_output, str(proc.returncode)))

            if provide_cursor:
                if not new_cursor and cursor is not None:
                    new_cursor = cursor
                return stdout.decode('utf-8'), int(new_cursor)

            return stdout.decode('utf-8')
        except OSError as ex:
            sublime.error_message('{0} - {1}'.format(PLUGIN_NAME, ex))
            raise
Ejemplo n.º 2
0
    def format_code(self, source, node_path, prettier_cli_path,
                    prettier_options, view):
        self._error_message = None

        if is_str_none_or_empty(node_path):
            cmd = [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options
        else:
            cmd = [node_path] \
                + [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options

        try:
            format_debug_message('Prettier CLI Command', list_to_str(cmd),
                                 debug_enabled(view))

            proc = Popen(cmd,
                         stdin=PIPE,
                         stderr=PIPE,
                         stdout=PIPE,
                         env=get_proc_env(),
                         shell=is_windows())

            stdout, stderr = proc.communicate(input=source.encode('utf-8'))
            if proc.returncode != 0:
                error_output = stderr.decode('utf-8')
                self.error_message = format_error_message(
                    error_output, str(proc.returncode))

                # detect and scroll to 'Syntax Errors':
                _, _, error_line, error_col = self.has_syntax_error(
                    error_output)
                if error_line != -1 and error_col != -1:
                    scroll_view_to(view, error_line, error_col)

                return None
            if stderr:
                # allow warnings to pass-through
                print(
                    format_error_message(stderr.decode('utf-8'),
                                         str(proc.returncode)))
            return stdout.decode('utf-8')
        except OSError as ex:
            sublime.error_message('{0} - {1}'.format(PLUGIN_NAME, ex))
            raise
Ejemplo n.º 3
0
    def format_code(self, source, node_path, prettier_cli_path, prettier_options, view):
        self._error_message = None

        if is_str_none_or_empty(node_path):
            cmd = [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options
        else:
            cmd = [node_path] \
                + [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options

        try:
            format_debug_message('Prettier CLI Command', list_to_str(cmd), debug_enabled(view))

            proc = Popen(
                cmd, stdin=PIPE,
                stderr=PIPE,
                stdout=PIPE,
                env=get_proc_env(),
                shell=is_windows())

            stdout, stderr = proc.communicate(input=source.encode('utf-8'))
            if proc.returncode != 0:
                error_output = stderr.decode('utf-8')
                self.error_message = format_error_message(error_output, str(proc.returncode))

                # detect and scroll to 'Syntax Errors':
                _, _, error_line, error_col = self.has_syntax_error(error_output)
                if error_line != -1 and error_col != -1:
                    scroll_view_to(view, error_line, error_col)

                return None
            if stderr:
                # allow warnings to pass-through
                print(format_error_message(stderr.decode('utf-8'), str(proc.returncode)))
            return stdout.decode('utf-8')
        except OSError as ex:
            sublime.error_message('{0} - {1}'.format(PLUGIN_NAME, ex))
            raise
Ejemplo n.º 4
0
    def format_code(self,
                    source,
                    node_path,
                    prettier_cli_path,
                    prettier_options,
                    view,
                    provide_cursor=False,
                    is_selection=False):

        self._error_message = None

        cursor = None
        if provide_cursor:
            cursor = view.sel()[0].a
            prettier_options += ['--cursor-offset', str(cursor)]

        if is_windows() and is_str_none_or_empty(
                node_path) and prettier_cli_path.endswith(".js"):
            # on windows, when a custom 'node_path' is not specified and 'prettier_cli_path' is
            # presumably a .js script (e.g: 'bin-prettier.js')...
            # automatically prepend the environment detected node[.exe|.cmd] path to
            # the generated command (see #146 --no-bin-links).
            cmd = [resolve_node_path(view.file_name())] \
                + [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options
        elif is_str_none_or_empty(node_path):
            cmd = [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options
        else:
            cmd = [node_path] \
                + [prettier_cli_path] \
                + ['--stdin'] \
                + prettier_options

        try:
            format_debug_message('Prettier CLI Command', list_to_str(cmd),
                                 debug_enabled(view))

            proc = Popen(cmd,
                         stdin=PIPE,
                         stderr=PIPE,
                         stdout=PIPE,
                         env=get_proc_env(),
                         shell=is_windows())

            stdout, stderr = proc.communicate(input=source.encode('utf-8'))
            if proc.returncode != 0:
                error_output = stderr.decode('utf-8')
                self.error_message = format_error_message(
                    error_output, str(proc.returncode))

                # detect and scroll to 'Syntax Errors' (if not formatting a selection):
                if not is_selection:
                    _, _, error_line, error_col = self.has_syntax_error(
                        error_output)
                    if error_line != -1 and error_col != -1:
                        scroll_view_to(view, error_line, error_col)

                return None

            new_cursor = None
            if stderr:
                stderr_output = stderr.decode('utf-8')
                if provide_cursor:
                    stderr_lines = stderr_output.splitlines()
                    stderr_output, new_cursor = '\n'.join(
                        stderr_lines[:-1]), stderr_lines[-1]

                # allow warnings to pass-through
                if stderr_output:
                    print(
                        format_error_message(stderr_output,
                                             str(proc.returncode)))

            if provide_cursor:
                if not new_cursor and cursor is not None:
                    new_cursor = cursor
                try:
                    new_cursor = int(new_cursor)
                except ValueError:
                    log_warn(
                        view,
                        'Adjusted cursor position could not be parsed (int).')
                    return stdout.decode('utf-8'), None
                return stdout.decode('utf-8'), new_cursor

            return stdout.decode('utf-8')
        except OSError as ex:
            sublime.error_message('{0} - {1}'.format(PLUGIN_NAME, ex))
            raise
Ejemplo n.º 5
0
    def format_code(
        self,
        source,
        node_path,
        prettier_cli_path,
        prettier_options,
        vid,
        region=None,
        provide_cursor=False,
        save=False,
    ):
        view = sublime.View(vid)
        if not view.is_valid():
            return

        self._error_message = None

        cursor = None
        if provide_cursor:
            cursor = view.sel()[0].a
            prettier_options += ["--cursor-offset", str(cursor)]

        if is_str_none_or_empty(node_path):
            cmd = [prettier_cli_path] + ["--stdin"] + prettier_options
        else:
            cmd = [node_path] + [prettier_cli_path] + ["--stdin"
                                                       ] + prettier_options

        try:
            format_debug_message("Prettier CLI Command", list_to_str(cmd),
                                 debug_enabled(view))

            proc = Popen(
                cmd,
                stdin=PIPE,
                stderr=PIPE,
                stdout=PIPE,
                env=get_proc_env(),
                shell=is_windows(),
            )

            stdout, stderr = proc.communicate(input=source.encode("utf-8"))
            if proc.returncode != 0:
                error_output = stderr.decode("utf-8")
                self.error_message = format_error_message(
                    error_output, str(proc.returncode))

                # detect and scroll to 'Syntax Errors':
                _, _, error_line, error_col = self.has_syntax_error(
                    error_output)
                if error_line != -1 and error_col != -1:
                    scroll_view_to(view, error_line, error_col)

                format_console_error(self.error_message)
                return show_status_bar_error()

            new_cursor = None
            if stderr:
                stderr_output = stderr.decode("utf-8")
                if provide_cursor:
                    stderr_lines = stderr_output.splitlines()
                    stderr_output, new_cursor = (
                        "\n".join(stderr_lines[:-1]),
                        stderr_lines[-1],
                    )

                # allow warnings to pass-through
                if stderr_output:
                    print(
                        format_error_message(stderr_output,
                                             str(proc.returncode)))

            if provide_cursor:
                if not new_cursor and cursor is not None:
                    new_cursor = cursor
                view.run_command(
                    "js_prettier_replace",
                    {
                        "code": stdout.decode("utf-8"),
                        "cursor": int(new_cursor),
                        "region": region,
                        "save": save,
                    },
                )
            else:
                view.run_command(
                    "js_prettier_replace",
                    {
                        "code": stdout.decode("utf-8"),
                        "region": region,
                        "save": save
                    },
                )
        except OSError as ex:
            sublime.error_message("{0} - {1}".format(PLUGIN_NAME, ex))
            raise