Browse Source

Cleanup of bracket language indentation

merge-requests/389/head
eidheim 8 years ago
parent
commit
32875d5c0d
  1. 51
      src/source.cc
  2. 2
      src/source.h

51
src/source.cc

@ -180,10 +180,38 @@ Source::View::View(const boost::filesystem::path &file_path, const Glib::RefPtr<
if(language) { if(language) {
auto language_id = language->get_id(); auto language_id = language->get_id();
if(language_id == "chdr" || language_id == "cpphdr" || language_id == "c" || language_id == "cpp") if(language_id == "chdr" || language_id == "cpphdr" || language_id == "c" || language_id == "cpp") {
is_cpp = true; is_cpp = true;
else if(language_id == "js" || language_id == "json" || language_id == "rust") use_fixed_continuation_indenting = false;
is_js_or_rust = true; // TODO 2019: check if clang-format has improved...
// boost::filesystem::path clang_format_file;
// auto search_path=file_path.parent_path();
// boost::system::error_code ec;
// while(true) {
// clang_format_file=search_path/".clang-format";
// if(boost::filesystem::exists(clang_format_file, ec))
// break;
// clang_format_file=search_path/"_clang-format";
// if(boost::filesystem::exists(clang_format_file, ec))
// break;
// clang_format_file.clear();
// if(search_path==search_path.root_directory())
// break;
// search_path=search_path.parent_path();
// }
// if(!clang_format_file.empty()) {
// auto lines=filesystem::read_lines(clang_format_file);
// for(auto &line: lines) {
// std::cout << "1" << std::endl;
// if(!line.empty() && line.compare(0, 23, "ContinuationIndentWidth")==0) {
// std::cout << "2" << std::endl;
// use_continuation_indenting=true;
// break;
// }
// }
// }
}
if(language_id == "chdr" || language_id == "cpphdr" || language_id == "c" || if(language_id == "chdr" || language_id == "cpphdr" || language_id == "c" ||
language_id == "cpp" || language_id == "objc" || language_id == "java" || language_id == "cpp" || language_id == "objc" || language_id == "java" ||
@ -701,8 +729,9 @@ void Source::View::setup_format_style(bool is_generic_view) {
bool use_style_file=false; bool use_style_file=false;
auto style_file_search_path=this->file_path.parent_path(); auto style_file_search_path=this->file_path.parent_path();
boost::system::error_code ec;
while(true) { while(true) {
if(boost::filesystem::exists(style_file_search_path/".clang-format") || boost::filesystem::exists(style_file_search_path/"_clang-format")) { if(boost::filesystem::exists(style_file_search_path/".clang-format", ec) || boost::filesystem::exists(style_file_search_path/"_clang-format", ec)) {
use_style_file=true; use_style_file=true;
break; break;
} }
@ -1975,8 +2004,8 @@ bool Source::View::on_key_press_event_bracket_language(GdkEventKey* key) {
} }
} }
// Special indentation of [ and ( for JavaScript and JSON // Special indentation of {, [ and ( for for instance JavaScript, JSON, Rust
if(is_js_or_rust) { if(use_fixed_continuation_indenting) {
unsigned int open_symbol=0, close_symbol=0; unsigned int open_symbol=0, close_symbol=0;
if(*condition_iter=='[') { if(*condition_iter=='[') {
open_symbol='['; open_symbol='[';
@ -2060,7 +2089,7 @@ bool Source::View::on_key_press_event_bracket_language(GdkEventKey* key) {
if(found_tabs_end_iter.get_line_offset()==tabs_end_iter.get_line_offset()) { if(found_tabs_end_iter.get_line_offset()==tabs_end_iter.get_line_offset()) {
has_right_curly_bracket=true; has_right_curly_bracket=true;
// Special case for functions and classes with no indentation after: namespace { // Special case for functions and classes with no indentation after: namespace {
if(tabs_end_iter.starts_line()) { if(is_cpp && tabs_end_iter.starts_line()) {
auto iter=condition_iter; auto iter=condition_iter;
Gtk::TextIter open_iter; Gtk::TextIter open_iter;
if(iter.backward_char() && find_open_curly_bracket_backward(iter, open_iter)) { if(iter.backward_char() && find_open_curly_bracket_backward(iter, open_iter)) {
@ -2236,15 +2265,15 @@ bool Source::View::on_key_press_event_bracket_language(GdkEventKey* key) {
} }
} }
} }
if(is_js_or_rust) if(use_fixed_continuation_indenting)
return false; // Use basic indentation since JavaScript code can contain JSX: return false; // Use basic indentation since for instance JavaScript code can contain JSX
get_buffer()->insert_at_cursor('\n'+tabs); get_buffer()->insert_at_cursor('\n'+tabs);
scroll_to(get_buffer()->get_insert()); scroll_to(get_buffer()->get_insert());
return true; return true;
} }
// Indent left when writing }, ) or ] on a new line // Indent left when writing }, ) or ] on a new line
else if(key->keyval==GDK_KEY_braceright || else if(key->keyval==GDK_KEY_braceright ||
(is_js_or_rust && (key->keyval==GDK_KEY_bracketright || key->keyval==GDK_KEY_parenright))) { (use_fixed_continuation_indenting && (key->keyval==GDK_KEY_bracketright || key->keyval==GDK_KEY_parenright))) {
std::string bracket; std::string bracket;
if(key->keyval==GDK_KEY_braceright) if(key->keyval==GDK_KEY_braceright)
bracket="}"; bracket="}";
@ -2562,7 +2591,7 @@ bool Source::View::on_key_press_event_smart_inserts(GdkEventKey *key) {
if(found_tabs_end_iter.get_line_offset()==tabs_end_iter.get_line_offset()) { if(found_tabs_end_iter.get_line_offset()==tabs_end_iter.get_line_offset()) {
has_right_curly_bracket=true; has_right_curly_bracket=true;
// Special case for functions and classes with no indentation after: namespace {: // Special case for functions and classes with no indentation after: namespace {:
if(tabs_end_iter.starts_line()) { if(is_cpp && tabs_end_iter.starts_line()) {
Gtk::TextIter open_iter; Gtk::TextIter open_iter;
if(find_open_curly_bracket_backward(iter, open_iter)) { if(find_open_curly_bracket_backward(iter, open_iter)) {
if(open_iter.starts_line()) // in case of: namespace test\n{ if(open_iter.starts_line()) // in case of: namespace test\n{

2
src/source.h

@ -155,8 +155,8 @@ namespace Source {
sigc::connection renderer_activate_connection; sigc::connection renderer_activate_connection;
bool is_bracket_language=false; bool is_bracket_language=false;
bool use_fixed_continuation_indenting=true;
bool is_cpp=false; bool is_cpp=false;
bool is_js_or_rust=false;
guint previous_non_modifier_keyval=0; guint previous_non_modifier_keyval=0;
bool multiple_cursors_signals_set=false; bool multiple_cursors_signals_set=false;

Loading…
Cancel
Save