Browse Source

Improvement to prettier library output parsing

pipelines/353213535
eidheim 4 years ago
parent
commit
257668730c
  1. 48
      src/source.cpp

48
src/source.cpp

@ -819,8 +819,7 @@ void Source::View::setup_format_style(bool is_generic_view) {
}; };
static auto prettier_library = get_prettier_library(); static auto prettier_library = get_prettier_library();
auto buffer = get_buffer()->get_text().raw(); if(!prettier_library.empty()) {
if(!prettier_library.empty() && buffer.size() < 25000) { // Node.js repl seems to be limited to around 28786 bytes
struct Error { struct Error {
std::string message; std::string message;
int line = -1, index = -1; int line = -1, index = -1;
@ -847,15 +846,38 @@ void Source::View::setup_format_style(bool is_generic_view) {
"node -e \"const repl = require('repl');repl.start({prompt: '', ignoreUndefined: true, preview: false});\"", "node -e \"const repl = require('repl');repl.start({prompt: '', ignoreUndefined: true, preview: false});\"",
"", "",
[](const char *bytes, size_t n) { [](const char *bytes, size_t n) {
try { static std::stringstream stdout_buffer;
JSON json(std::string(bytes, n)); static int curly_count = 0;
LockGuard lock(mutex); static bool key_or_value = false;
result = Result{json.string("formatted"), static_cast<int>(json.integer_or("cursorOffset", -1))}; for(size_t i = 0; i < n; ++i) {
if(!key_or_value) {
if(bytes[i] == '{')
++curly_count;
else if(bytes[i] == '}')
--curly_count;
else if(bytes[i] == '"')
key_or_value = true;
}
else {
if(bytes[i] == '\\')
++i;
else if(bytes[i] == '"')
key_or_value = false;
}
} }
catch(const std::exception &e) { stdout_buffer.write(bytes, n);
LockGuard lock(mutex); if(curly_count == 0) {
error = Error{e.what()}; key_or_value = false;
error->message += "\nOutput from prettier: " + std::string(bytes, n); try {
JSON json(stdout_buffer);
LockGuard lock(mutex);
result = Result{json.string("formatted"), static_cast<int>(json.integer_or("cursorOffset", -1))};
}
catch(const std::exception &e) {
LockGuard lock(mutex);
error = Error{std::string(e.what()) + "\nOutput from prettier: " + stdout_buffer.str()};
}
stdout_buffer.clear();
} }
}, },
[](const char *bytes, size_t n) { [](const char *bytes, size_t n) {
@ -908,8 +930,7 @@ void Source::View::setup_format_style(bool is_generic_view) {
else else
options += ", cursorOffset: " + std::to_string(get_buffer()->get_insert()->get_iter().get_offset()); 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("{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(buffer)); prettier_background_process->write(to_hex_string(get_buffer()->get_text().raw()));
buffer.clear();
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"); 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");
while(true) { while(true) {
@ -965,8 +986,7 @@ void Source::View::setup_format_style(bool is_generic_view) {
else else
command += " --cursor-offset " + std::to_string(get_buffer()->get_insert()->get_iter().get_offset()); command += " --cursor-offset " + std::to_string(get_buffer()->get_insert()->get_iter().get_offset());
std::stringstream stdin_stream(buffer), stdout_stream, stderr_stream; std::stringstream stdin_stream(get_buffer()->get_text().raw()), stdout_stream, stderr_stream;
buffer.clear();
auto exit_status = Terminal::get().process(stdin_stream, stdout_stream, command, this->file_path.parent_path(), &stderr_stream); auto exit_status = Terminal::get().process(stdin_stream, stdout_stream, command, this->file_path.parent_path(), &stderr_stream);
if(exit_status == 0) { if(exit_status == 0) {
replace_text(stdout_stream.str()); replace_text(stdout_stream.str());

Loading…
Cancel
Save