Browse Source

Added support for prettier version 3

merge-requests/413/head
eidheim 2 years ago
parent
commit
dd2f762924
  1. 1
      src/compile_commands.cpp
  2. 19
      src/source.cpp

1
src/compile_commands.cpp

@ -3,7 +3,6 @@
#include "config.hpp"
#include "filesystem.hpp"
#include "json.hpp"
#include "terminal.hpp"
#include "utility.hpp"
#include <algorithm>
#include <regex>

19
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());
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) {

Loading…
Cancel
Save