On
http://4coder.net/ At the bottom of the page there is a link that reads "Handmade Network discord" which is actually the invite to the 4coder discord.
The problem with the python indentation (as I understand it, and I will say it again: I know next to nothing about Python) is that there is no rules to when the indentation should decrease a level. For example:
| if test:
do_something()
do_semithing_2()
|
There is no rule saying that do_something_2 should not be indented, and if you change the indentation you change the flow of the program.
If I remember correctly the auto indentation in 4coder will run on the whole file when you save the file and some other actions, and that would not work with python. That's why I think you should treat python files as plain text and write some indentation function to help you, but that wouldn't be an auto indenter.
Here is an example of indenting a line after pressing enter. It will not work if treat_as_code is set as that would cause the C++ auto indenter to reindent (I think), and it assumes virtual whitespace is off. I believe that having another function bound to <shift + enter> that would create a new line with a decreased indentation level would be useful to "close a scope". It's not properly tested.
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 | CUSTOM_COMMAND_SIG( python_new_line_indent ) {
View_ID view = get_active_view( app, 0 );
Buffer_ID buffer = view_get_buffer( app, view, 0 );
Scratch_Block scratch( app );
i64 line = get_line_number_from_pos( app, buffer, view_get_cursor_pos( app, view ) );
String_Const_u8 string = push_buffer_line( app, scratch, buffer, line );
i64 tab_count = 0;
i64 space_count = 0;
for ( umm i = 0; i < string.size; i++ ) {
if ( string.str[ i ] == ' ' ) {
space_count++;
} else if ( string.str[ i ] == '\t' ) {
tab_count++;
} else {
break;
}
}
i64 indent_level = tab_count;
indent_level += space_count / global_config.indent_width;
indent_level += ( space_count % global_config.indent_width ) ? 1 : 0;
i64 cursor = view_get_cursor_pos( app, view );
if ( cursor > 1 ) {
u8 c = buffer_get_char( app, buffer, cursor - 1 );
if ( c == ':' ) {
indent_level += 1;
}
}
String_u8 insert = { 0 };
if ( global_config.indent_with_tabs ) {
i64 size = 1 + indent_level;
insert = string_u8_push( scratch, size );
string_append_character( &insert, '\n' );
for ( i64 i = 0; i < indent_level; i++ ) {
string_append_character( &insert, '\t' );
}
} else {
i64 size = 1 + indent_level * global_config.indent_width;
insert = string_u8_push( scratch, size );
string_append_character( &insert, '\n' );
for ( i64 i = 1; i < size; i++ ) {
string_append_character( &insert, ' ' );
}
}
write_text( app, SCu8( insert.str, insert.size ) );
}
|