Browse Source

Auto-indent now uses stdin instead of temporary file.

merge-requests/365/head
eidheim 10 years ago
parent
commit
61722b063b
  1. 13
      src/source_clang.cc
  2. 17
      src/terminal.cc
  3. 3
      src/terminal.h
  4. 18
      src/terminal_win.cc

13
src/source_clang.cc

@ -930,18 +930,15 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
});
auto_indent=[this]() {
boost::filesystem::path temp_file = "jucipp_auto_indent_tmp_file";
auto temp_directory=boost::filesystem::temp_directory_path();
boost::filesystem::path temp_file_path=temp_directory.string()+'/'+temp_file.string();
juci::filesystem::write(temp_file_path, get_buffer());
std::string command="clang-format \""+temp_file_path.string()+"\"";
std::string command="clang-format";
command+=" -style=\"{IndentWidth: "+std::to_string(tab_size);
if(Singleton::Config::source()->clang_format_style!="")
command+=", "+Singleton::Config::source()->clang_format_style;
command+="}\"";
std::stringstream stdout_stream;
auto exit_code=Singleton::terminal()->execute(stdout_stream, command);
std::stringstream stdin_stream(get_buffer()->get_text()), stdout_stream;
auto exit_code=Singleton::terminal()->execute(stdin_stream, stdout_stream, command);
if(exit_code==0) {
get_source_buffer()->begin_user_action();
auto iter=get_buffer()->get_insert()->get_iter();
@ -967,8 +964,6 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
get_source_buffer()->end_user_action();
set_tab_char_and_size(' ', tab_size); //clang-format only does basic indentation with spaces as I understand it
}
boost::filesystem::remove(temp_file_path);
};
get_token=[this]() -> Token {

17
src/terminal.cc

@ -8,7 +8,7 @@
#include <iostream> //TODO: remove
using namespace std; //TODO: remove
const size_t buffer_size=131072;
const ssize_t buffer_size=131072;
//A working implementation of popen3, with all pipes getting closed properly.
//TODO: Eidheim is going to publish this one on his github, along with example uses
@ -190,7 +190,7 @@ int Terminal::execute(const std::string &command, const boost::filesystem::path
}
}
int Terminal::execute(std::iostream &stdout_stream, const std::string &command, const boost::filesystem::path &path) {
int Terminal::execute(std::istream &stdin_stream, std::ostream &stdout_stream, const std::string &command, const boost::filesystem::path &path) {
int stdin_fd, stdout_fd, stderr_fd;
auto pid=popen3(command, path.string(), &stdin_fd, &stdout_fd, &stderr_fd);
@ -230,9 +230,20 @@ int Terminal::execute(std::iostream &stdout_stream, const std::string &command,
});
stdout_thread.detach();
char buffer[buffer_size];
for(;;) {
stdin_stream.readsome(buffer, buffer_size);
auto read_n=stdin_stream.gcount();
if(read_n==0)
break;
auto write_n=write(stdin_fd, buffer, read_n);
if(write_n==0)
break;
}
close(stdin_fd);
int exit_code;
waitpid(pid, &exit_code, 0);
close(stdin_fd);
close(stdout_fd);
close(stderr_fd);

3
src/terminal.h

@ -8,6 +8,7 @@
#include <thread>
#include <atomic>
#include <list>
#include <iostream>
class Terminal : public Gtk::TextView {
public:
@ -34,7 +35,7 @@ public:
Terminal();
int execute(const std::string &command, const boost::filesystem::path &path="");
int execute(std::iostream &stdout_stream, const std::string &command, const boost::filesystem::path &path="");
int execute(std::istream &stdin_stream, std::ostream &stdout_stream, const std::string &command, const boost::filesystem::path &path="");
void async_execute(const std::string &command, const boost::filesystem::path &path="", std::function<void(int exit_code)> callback=nullptr);
void kill_last_async_execute(bool force=false);
void kill_async_executes(bool force=false);

18
src/terminal_win.cc

@ -235,7 +235,7 @@ int Terminal::execute(const std::string &command, const boost::filesystem::path
return exit_code;
}
int Terminal::execute(std::iostream &stdout_stream, const std::string &command, const boost::filesystem::path &path) {
int Terminal::execute(std::istream &stdin_stream, std::ostream &stdout_stream, const std::string &command, const boost::filesystem::path &path) {
HANDLE stdin_h, stdout_h, stderr_h;
auto process=popen3(command, path.string(), &stdin_h, &stdout_h, &stderr_h);
@ -281,13 +281,25 @@ int Terminal::execute(std::iostream &stdout_stream, const std::string &command,
}
});
stdout_thread.detach();
CHAR buffer[buffer_size];
for(;;) {
stdin_stream.readsome(buffer, buffer_size);
auto read_n=stdin_stream.gcount();
if(read_n==0)
break;
DWORD write_n;
BOOL bSuccess = WriteFile(stdin_h, buffer, static_cast<DWORD>(read_n), &write_n, NULL);
if(!bSuccess || write_n==0)
break;
}
CloseHandle(stdin_h);
unsigned long exit_code;
WaitForSingleObject(process, INFINITE);
GetExitCodeProcess(process, &exit_code);
CloseHandle(process);
CloseHandle(stdin_h);
CloseHandle(stdout_h);
CloseHandle(stderr_h);
return exit_code;

Loading…
Cancel
Save