Register
4coder»Forums»Vim style comments
3 posts
Vim style comments
6 months, 2 weeks ago Edited by thorduragust on April 6, 2019, 4:46 p.m.
I made a command that preserves indentation of the line you came from when you input a newline. When you are inside a single line comment,
the command also preserves the "//" as well as the whitespace after it, just like vim does it.
Thought it might be useful to someone. There are some additional custom commands for added context:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#include "4coder_default_include.cpp"

bool extentionIsC(String ext) {
	return (match(ext, "cpp") || match(ext, "h") || match(ext, "c") || match(ext, "hpp") || match(ext, "cc"));
}

bool extentionIsCLike(String ext) {
	return (extentionIsC(ext) || match(ext, "java") || match(ext, "cs") || match(ext, "rs"));
}

void customNewline(Application_Links *app, bool below) {
    int access = AccessOpen;
    View_Summary view = get_active_view(app, access);
    Buffer_Summary buffer = get_buffer(app, view.buffer_id, access);
    
    int start = buffer_get_line_start(app, &buffer, view.cursor.line);
    int hard_start = get_start_of_line_at_cursor(app, &view, &buffer);

    String name = make_string(buffer.file_name, buffer.file_name_len);
	String ext = file_extension(name);

    if(extentionIsCLike(ext) && c_line_comment_starts_at_position(app, &buffer, hard_start)) {
        Hard_Start_Result after_comment = buffer_find_hard_start(app, &buffer, hard_start+2, DEF_TAB_WIDTH);
        hard_start = after_comment.char_pos;
    }

    Partition *scratch = &global_part;
    Temp_Memory temp = begin_temp_memory(scratch);
    
    int size = hard_start - start;
    char *str = push_array(scratch, char, size);

    if(str != 0) {
        buffer_read_range(app, &buffer, start, hard_start, str);
        
        String prev_line_continuation = make_string(str, hard_start - start);
        
        if(below) write_string(app, make_lit_string("\n"));
        write_string(app, prev_line_continuation);
        if(!below) write_string(app, make_lit_string("\n"));
    }
    
    end_temp_memory(temp);
}

CUSTOM_COMMAND_SIG(custom_newline_below_cursor) {
    exec_command(app, seek_end_of_line);
    customNewline(app, true);
}

CUSTOM_COMMAND_SIG(custom_newline_above_cursor) {
    exec_command(app, seek_beginning_of_line);
    customNewline(app, false);
    exec_command(app, move_up);
    exec_command(app, seek_end_of_line);
}

CUSTOM_COMMAND_SIG(custom_newline) {
    customNewline(app, true);
}

extern "C" int32_t get_bindings(void *data, int32_t size) {
    Bind_Helper contextValue = begin_bind_helper(data, size);
    Bind_Helper *context = &contextValue;
	
    set_all_default_hooks(context);
    
    begin_map(context, mapid_file);
    {
        bind_vanilla_keys(context, write_character);
	bind(context, '\n', MDFR_NONE, custom_newline);
        bind(context, 'o', MDFR_CTRL, custom_newline_below_cursor);
        bind(context, 'O', MDFR_CTRL, custom_newline_above_cursor);
    }
    end_map(context);
    
    begin_map(context, default_code_map);
    {
        inherit_map(context, mapid_file);
    }
    end_map(context);
	
	begin_map(context, default_lister_ui_map);
	{
	    bind_vanilla_keys(context, lister__write_character);
	    bind(context, key_esc, MDFR_NONE, lister__quit);
	    bind(context, '\n', MDFR_NONE, lister__activate);
	    bind(context, '\t', MDFR_NONE, lister__activate);
	    bind(context, key_back, MDFR_NONE, lister__backspace_text_field);
	    bind(context, key_up, MDFR_NONE, lister__move_up);
	    bind(context, key_page_up, MDFR_NONE, lister__move_up);
	    bind(context, key_down, MDFR_NONE, lister__move_down);
	    bind(context, key_page_down, MDFR_NONE, lister__move_down);
	    bind(context, key_mouse_wheel, MDFR_NONE, lister__wheel_scroll);
	    bind(context, key_mouse_left, MDFR_NONE, lister__mouse_press);
	    bind(context, key_mouse_left_release, MDFR_NONE, lister__mouse_release);
	    bind(context, key_mouse_move, MDFR_NONE, lister__repaint);
	    bind(context, key_animate, MDFR_NONE, lister__repaint);
	}
	end_map(context);
    
    return end_bind_helper(context);
}


Sander Boer
3 posts
Vim style comments
6 months, 1 week ago
YOINK!

thanks, works nicely.

S
Jackson
6 posts
Vim style comments
6 months, 1 week ago
Thanks for sharing!