4coder/4coder_default_building.cpp

500 lines
15 KiB
C++
Raw Normal View History

2016-06-28 22:58:50 +00:00
#ifndef FCODER_DEFAULT_BUILDING
#define FCODER_DEFAULT_BUILDING
#include "4coder_custom.h"
2016-07-06 19:18:10 +00:00
#define FSTRING_IMPLEMENTATION
2016-06-28 22:58:50 +00:00
#include "4coder_string.h"
#include "4coder_helper.h"
2016-07-02 14:56:50 +00:00
2016-06-28 22:58:50 +00:00
//
// Basic Build Behavior
//
struct Prev_Jump{
int buffer_id;
int line;
};
2016-07-06 19:18:10 +00:00
static Prev_Jump null_location = {0};
static Prev_Jump prev_location = {0};
2016-06-28 22:58:50 +00:00
CUSTOM_COMMAND_SIG(build_in_build_panel){
Buffer_Summary buffer =
app->get_buffer_by_name(app, literal("*compilation*"), AccessAll);
View_Summary build_view = {0};
2016-07-02 14:56:50 +00:00
2016-06-28 22:58:50 +00:00
View_Summary original_view = app->get_active_view(app, AccessAll);
2016-07-02 14:56:50 +00:00
Buffer_Summary original_buffer =
app->get_buffer(app, original_view.buffer_id, AccessAll);
2016-06-28 22:58:50 +00:00
if (buffer.exists){
build_view = get_first_view_with_buffer(app, buffer.buffer_id);
}
if (!build_view.exists){
2016-07-05 01:36:30 +00:00
exec_command(app, open_panel_hsplit);
exec_command(app, hide_scrollbar);
2016-06-28 22:58:50 +00:00
build_view = app->get_active_view(app, AccessAll);
app->view_set_split_proportion(app, &build_view, .2f);
2016-07-02 14:56:50 +00:00
app->set_active_view(app, &original_view);
2016-06-28 22:58:50 +00:00
}
2016-07-02 14:56:50 +00:00
execute_standard_build(app, &build_view, &original_buffer);
2016-07-06 19:18:10 +00:00
prev_location = null_location;
2016-06-28 22:58:50 +00:00
}
// TODO(allen): This is a bit nasty. I want a system for picking
// the most advanced and correct version of a command to bind to a
// name based on which files are included.
#ifndef BUILD_SEARCH
# define BUILD_SEARCH 2
#elif BUILD_SEARCH <= 2
# undef BUILD_SEARCH
# define BUILD_SEARCH 2
#endif
#if BUILD_SEARCH <= 2
# ifdef build_search
# undef build_search
# endif
# define build_search build_in_build_panel
#endif
2016-07-02 14:15:15 +00:00
#define GET_COMP_BUFFER() app->get_buffer_by_name(app, literal("*compilation*"), AccessAll);
2016-06-28 22:58:50 +00:00
CUSTOM_COMMAND_SIG(close_build_panel){
2016-07-02 14:15:15 +00:00
Buffer_Summary buffer = GET_COMP_BUFFER();
2016-07-05 01:36:30 +00:00
View_Summary build_view = get_first_view_with_buffer(app, buffer.buffer_id);
2016-06-28 22:58:50 +00:00
2016-07-05 01:36:30 +00:00
if (build_view.exists){
2016-06-28 22:58:50 +00:00
View_Summary original_view = app->get_active_view(app, AccessAll);
app->set_active_view(app, &build_view);
2016-07-05 01:36:30 +00:00
exec_command(app, close_panel);
2016-06-28 22:58:50 +00:00
app->set_active_view(app, &original_view);
}
}
2016-07-02 14:15:15 +00:00
CUSTOM_COMMAND_SIG(change_to_build_panel){
Buffer_Summary buffer = GET_COMP_BUFFER();
if (buffer.exists){
View_Summary build_view = get_first_view_with_buffer(app, buffer.buffer_id);
app->set_active_view(app, &build_view);
}
}
2016-06-28 22:58:50 +00:00
CUSTOM_COMMAND_SIG(change_active_panel_build){
2016-07-02 14:15:15 +00:00
Buffer_Summary buffer = GET_COMP_BUFFER();
2016-06-28 22:58:50 +00:00
if (buffer.exists){
View_Summary build_view = get_first_view_with_buffer(app, buffer.buffer_id);
View_Summary view = app->get_active_view(app, AccessAll);
int prev_view_id = view.view_id;
2016-07-05 01:36:30 +00:00
exec_command(app, change_active_panel_regular);
2016-06-28 22:58:50 +00:00
view = app->get_active_view(app, AccessAll);
for (;(view.view_id != prev_view_id &&
build_view.view_id == view.view_id);){
prev_view_id = view.view_id;
2016-07-05 01:36:30 +00:00
exec_command(app, change_active_panel_regular);
2016-06-28 22:58:50 +00:00
view = app->get_active_view(app, AccessAll);
}
}
2016-06-29 17:45:26 +00:00
else{
2016-07-05 01:36:30 +00:00
exec_command(app, change_active_panel_regular);
2016-06-29 17:45:26 +00:00
}
2016-06-28 22:58:50 +00:00
}
// TODO(allen): This is a bit nasty. I want a system for picking
// the most advanced and correct version of a command to bind to a
// name based on which files are included.
#ifndef CHANGE_ACTIVE_PANEL
# define CHANGE_ACTIVE_PANEL 2
#elif CHANGE_ACTIVE_PANEL <= 2
# undef CHANGE_ACTIVE_PANEL
# define CHANGE_ACTIVE_PANEL 2
#endif
#if CHANGE_ACTIVE_PANEL <= 2
# ifdef change_active_panel
# undef change_active_panel
# endif
# define change_active_panel change_active_panel_build
#endif
CUSTOM_COMMAND_SIG(open_file_in_quotes_build){
char file_name_[256];
String file_name = make_fixed_width_string(file_name_);
if (file_name_in_quotes(app, &file_name)){
exec_command(app, change_active_panel_build);
View_Summary view = app->get_active_view(app, AccessAll);
view_open_file(app, &view, expand_str(file_name), false);
}
}
// TODO(allen): This is a bit nasty. I want a system for picking
// the most advanced and correct version of a command to bind to a
// name based on which files are included.
#ifndef OPEN_FILE_IN_QUOTES
# define OPEN_FILE_IN_QUOTES 2
#elif OPEN_FILE_IN_QUOTES <= 2
# undef OPEN_FILE_IN_QUOTES
# define OPEN_FILE_IN_QUOTES 2
#endif
#if OPEN_FILE_IN_QUOTES <= 2
# ifdef open_file_in_quotes
# undef open_file_in_quotes
# endif
# define open_file_in_quotes open_file_in_quotes_build
#endif
CUSTOM_COMMAND_SIG(open_in_other_build){
exec_command(app, change_active_panel_build);
exec_command(app, cmdid_interactive_open);
}
// TODO(allen): This is a bit nasty. I want a system for picking
// the most advanced and correct version of a command to bind to a
// name based on which files are included.
#ifndef OPEN_IN_OTHER
# define OPEN_IN_OTHER 1
#elif OPEN_IN_OTHER <= 1
# undef OPEN_IN_OTHER
# define OPEN_IN_OTHER 1
#endif
#if OPEN_IN_OTHER <= 1
# ifdef open_in_other
# undef open_in_other
# endif
# define open_in_other open_in_other_build
#endif
//
// Jump to Error
//
struct Jump_Location{
String file;
int line;
2016-07-06 22:57:55 +00:00
int column;
2016-06-28 22:58:50 +00:00
};
static void
jump_to_location(Application_Links *app, View_Summary *view, Jump_Location *l){
view_open_file(app, view, l->file.str, l->file.size, false);
2016-07-06 22:57:55 +00:00
app->view_set_cursor(app, view, seek_line_char(l->line, l->column), true);
2016-06-28 22:58:50 +00:00
}
static int
2016-07-06 22:57:55 +00:00
gcc_style_verify(String line, int colon_pos){
int result = false;
if (colon_pos < line.size){
result = true;
}
2016-07-06 22:57:55 +00:00
return(result);
}
static int
ms_style_verify(String line, int paren_pos){
int result = false;
String line_part = substr(line, paren_pos);
if (match_part(line_part, ") : error")){
result = true;
}
else if (match_part(line_part, ") : warning")){
result = true;
}
else if (match_part(line_part, ") : error")){
result = true;
}
else if (match_part(line_part, ") : warning")){
result = true;
}
return(result);
}
static int
parse_error(String line, Jump_Location *location,
int skip_sub_errors, int *colon_char){
2016-06-28 22:58:50 +00:00
int result = false;
int colon_pos = find(line, 0, ')');
2016-07-06 22:57:55 +00:00
if (ms_style_verify(line, colon_pos)){
colon_pos = find(line, colon_pos, ':');
if (colon_pos < line.size){
String location_str = substr(line, 0, colon_pos);
2016-06-28 22:58:50 +00:00
2016-07-06 22:57:55 +00:00
if (!(skip_sub_errors && location_str.str[0] == ' ')){
location_str = skip_chop_whitespace(location_str);
2016-06-28 22:58:50 +00:00
2016-07-06 22:57:55 +00:00
int paren_pos = find(location_str, 0, '(');
if (paren_pos < location_str.size){
String file = substr(location_str, 0, paren_pos);
file = skip_chop_whitespace(file);
2016-07-06 22:57:55 +00:00
int close_pos = find(location_str, 0, ')') + 1;
if (close_pos == location_str.size && file.size > 0){
String line_number = substr(location_str,
paren_pos+1,
close_pos-paren_pos-2);
line_number = skip_chop_whitespace(line_number);
if (line_number.size > 0){
//copy(&location->file, file);
location->file = file;
2016-07-06 22:57:55 +00:00
int comma_pos = find(line_number, 0, ',');
if (comma_pos < line_number.size){
int start = comma_pos+1;
String column_number = substr(line_number, start, line_number.size-start);
line_number = substr(line_number, 0, comma_pos);
location->line = str_to_int(line_number);
location->column = str_to_int(column_number);
}
else{
location->line = str_to_int(line_number);
location->column = 1;
}
*colon_char = colon_pos;
result = true;
}
}
2016-06-28 22:58:50 +00:00
}
}
}
}
2016-07-06 22:57:55 +00:00
else{
int colon_pos1 = find(line, 0, ':');
if (colon_pos1 == 1){
if (line.size > colon_pos1+1){
if (char_is_slash(line.str[colon_pos1+1])){
colon_pos1 = find(line, colon_pos1+1, ':');
}
}
}
2016-07-06 22:57:55 +00:00
int colon_pos2 = find(line, colon_pos1+1, ':');
int colon_pos3 = find(line, colon_pos2+1, ':');
2016-07-06 22:57:55 +00:00
if (gcc_style_verify(line, colon_pos3)){
String filename = substr(line, 0, colon_pos1);
String line_number = substr(line, colon_pos1+1, colon_pos2 - colon_pos1 - 1);
String column_number = substr(line, colon_pos2+1, colon_pos3 - colon_pos2 - 1);
if (filename.size > 0 &&
line_number.size > 0 &&
column_number.size > 0){
//copy(&location->file, filename);
location->file = filename;
2016-07-06 22:57:55 +00:00
location->line = str_to_int(line_number);
location->column = str_to_int(column_number);
*colon_char = colon_pos3;
result = true;
}
}
else{
int colon_pos1 = find(line, 0, ':');
int colon_pos2 = find(line, colon_pos1+1, ':');
String filename = substr(line, 0, colon_pos1);
String line_number = substr(line, colon_pos1+1, colon_pos2 - colon_pos1 - 1);
if (filename.size > 0 && line_number.size > 0){
//copy(&location->file, filename);
location->file = filename;
location->line = str_to_int(line_number);
location->column = 0;
*colon_char = colon_pos2;
result = true;
}
}
2016-07-06 22:57:55 +00:00
}
2016-06-28 22:58:50 +00:00
return(result);
}
static int
2016-07-06 22:57:55 +00:00
next_error(Application_Links *app,
Partition *part,
View_Summary *comp_out, int *start_line,
Jump_Location *location,
int direction,
int skip_sub_errors,
int *colon_char){
2016-06-28 22:58:50 +00:00
int result = false;
int line = *start_line + direction;
String line_str = {0};
Buffer_Summary buffer = app->get_buffer(app, comp_out->buffer_id, AccessAll);
2016-06-28 22:58:50 +00:00
for (;;){
if (read_line(app, part, &buffer, line, &line_str)){
2016-07-06 22:57:55 +00:00
if (parse_error(line_str, location, skip_sub_errors, colon_char)){
2016-06-28 22:58:50 +00:00
result = true;
break;
}
line += direction;
}
else{
break;
}
}
if (line < 0){
line = 0;
}
*start_line = line;
return(result);
}
static int
goto_error(Application_Links *app,
Partition *part,
View_Summary *view, int line,
Jump_Location *location,
int skip_sub_errors){
int result = false;
String line_str = {0};
Buffer_Summary buffer = app->get_buffer(app, view->buffer_id, AccessAll);
if (read_line(app, part, &buffer, line, &line_str)){
int colon_char = 0;
if (parse_error(line_str, location, skip_sub_errors, &colon_char)){
result = true;
}
}
return(result);
}
static int
seek_error(Application_Links *app, Partition *part, int direction, int skip_sub_errors, Jump_Location *loc){
int result = false;
2016-06-28 22:58:50 +00:00
View_Summary active_view = app->get_active_view(app, AccessAll);
Jump_Location location = {0};
2016-06-28 22:58:50 +00:00
Buffer_Summary buffer = app->get_buffer_by_name(app, literal("*compilation*"), AccessAll);
if (buffer.exists){
View_Summary view = get_first_view_with_buffer(app, buffer.buffer_id);
int line = view.cursor.line;
int colon_char = 0;
if (next_error(app, part, &view, &line, &location,
2016-07-06 22:57:55 +00:00
skip_sub_errors, direction, &colon_char)){
2016-06-28 22:58:50 +00:00
jump_to_location(app, &active_view, &location);
app->view_set_cursor(app, &view, seek_line_char(line, colon_char+1), true);
result = true;
if (loc){
*loc = location;
}
2016-06-28 22:58:50 +00:00
}
}
return(result);
}
static Prev_Jump
jump_location_store(Application_Links *app, Jump_Location loc){
Prev_Jump result = {0};
Buffer_Summary buffer =
app->get_buffer_by_name(app, loc.file.str, loc.file.size, AccessAll);
if (buffer.exists){
result.buffer_id = buffer.buffer_id;
result.line = loc.line;
}
return(result);
}
static int
skip_this_jump(Prev_Jump prev, Prev_Jump jump){
int result = false;
if (prev.buffer_id != 0 && prev.buffer_id == jump.buffer_id &&
prev.line == jump.line){
result = true;
}
return(result);
2016-06-28 22:58:50 +00:00
}
2016-07-06 22:57:55 +00:00
CUSTOM_COMMAND_SIG(goto_next_error){
Temp_Memory temp = begin_temp_memory(&global_part);
Jump_Location location = {0};
Prev_Jump jump = {0};
do{
if (seek_error(app, &global_part, true, 1, &location)){
jump = jump_location_store(app, location);
}
else{
jump.buffer_id = 0;
}
}while(skip_this_jump(prev_location, jump));
prev_location = jump;
end_temp_memory(temp);
2016-06-28 22:58:50 +00:00
}
2016-07-06 22:57:55 +00:00
CUSTOM_COMMAND_SIG(goto_prev_error){
Temp_Memory temp = begin_temp_memory(&global_part);
Jump_Location location = {0};
Prev_Jump jump = {0};
do{
if (seek_error(app, &global_part, true, -1, &location)){
jump = jump_location_store(app, location);
}
else{
jump.buffer_id = 0;
}
}while(skip_this_jump(prev_location, jump));
prev_location = jump;
end_temp_memory(temp);
2016-06-28 22:58:50 +00:00
}
2016-07-06 22:57:55 +00:00
CUSTOM_COMMAND_SIG(goto_first_error){
Temp_Memory temp = begin_temp_memory(&global_part);
2016-06-28 22:58:50 +00:00
View_Summary active_view = app->get_active_view(app, AccessAll);
app->view_set_cursor(app, &active_view, seek_pos(0), true);
Jump_Location location = {0};
seek_error(app, &global_part, true, 1, &location);
prev_location = jump_location_store(app, location);
end_temp_memory(temp);
2016-06-28 22:58:50 +00:00
}
CUSTOM_COMMAND_SIG(goto_postion_at_cursor){
Temp_Memory temp = begin_temp_memory(&global_part);
View_Summary view = app->get_active_view(app, AccessProtected);
Jump_Location location = {0};
goto_error(app, &global_part,
&view, view.cursor.line,
&location, false);
exec_command(app, change_active_panel);
view = app->get_active_view(app, AccessAll);
jump_to_location(app, &view, &location);
end_temp_memory(temp);
}
2016-06-28 22:58:50 +00:00
#endif