From dd2f76292451fca4c7efd90f6971eae0f11b03b3 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 1 Aug 2023 21:11:39 +0200 Subject: [PATCH] Added support for prettier version 3 --- src/compile_commands.cpp | 1 - src/source.cpp | 25 ++++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/compile_commands.cpp b/src/compile_commands.cpp index fbefc41..b50223a 100644 --- a/src/compile_commands.cpp +++ b/src/compile_commands.cpp @@ -3,7 +3,6 @@ #include "config.hpp" #include "filesystem.hpp" #include "json.hpp" -#include "terminal.hpp" #include "utility.hpp" #include #include diff --git a/src/source.cpp b/src/source.cpp index 5379582..573f135 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -873,6 +873,18 @@ void Source::View::setup_format_style(bool is_generic_view) { static auto prettier_library = get_prettier_library(); if(!prettier_library.empty()) { + static bool is_prettier_version_3_or_larger = []() -> bool { + std::stringstream stdin_stream, stdout_stream; + auto exit_status = Terminal::get().process(stdin_stream, stdout_stream, prettier.string() + " --version"); + if(exit_status == 0) { + auto str = stdout_stream.str(); + if(!str.empty()) + str.pop_back(); + return version_compare(str, "3.0.0") >= 0; + } + return false; + }(); + struct Error { std::string message; int line = -1, index = -1; @@ -989,9 +1001,16 @@ void Source::View::setup_format_style(bool is_generic_view) { } else options += ", cursorOffset: " + std::to_string(get_buffer()->get_insert()->get_iter().get_offset()); - prettier_background_process->write("{prettier.clearConfigCache();let _ = prettier.resolveConfig(\"" + escape(file_path.string(), {'"'}) + "\").then(options => {try{let _ = process.stdout.write(JSON.stringify(prettier.formatWithCursor(Buffer.from('"); - prettier_background_process->write(to_hex_string(get_buffer()->get_text().raw())); - prettier_background_process->write("', 'hex').toString(), {...options, " + options + "})));}catch(error){let _ = process.stderr.write('ParseError: ' + error.message);}}).catch(error => {let _ = process.stderr.write('ConfigError: ' + error.message);});}\n"); + if(is_prettier_version_3_or_larger) { + prettier_background_process->write("{let _ = prettier.clearConfigCache().then(() => {let _ = prettier.resolveConfig(\"" + escape(file_path.string(), {'"'}) + "\").then(options => {let _ = prettier.formatWithCursor(Buffer.from('"); + prettier_background_process->write(to_hex_string(get_buffer()->get_text().raw())); + prettier_background_process->write("', 'hex').toString(), {...options, " + options + "}).then(result => {let _ = process.stdout.write(JSON.stringify(result));}).catch(error => {let _ = process.stderr.write('ParseError: ' + error.message);});}).catch(error => {let _ = process.stderr.write('ConfigError: ' + error.message);});}).catch((error) => {let _ = process.stderr.write('ConfigError: ' + error.message);});}\n"); + } + else { + prettier_background_process->write("{prettier.clearConfigCache();let _ = prettier.resolveConfig(\"" + escape(file_path.string(), {'"'}) + "\").then(options => {try{let _ = process.stdout.write(JSON.stringify(prettier.formatWithCursor(Buffer.from('"); + prettier_background_process->write(to_hex_string(get_buffer()->get_text().raw())); + prettier_background_process->write("', 'hex').toString(), {...options, " + options + "})));}catch(error){let _ = process.stderr.write('ParseError: ' + error.message);}}).catch(error => {let _ = process.stderr.write('ConfigError: ' + error.message);});}\n"); + } int exit_status = -1; while(true) {