Add support to crtl or shift to indent/unindent anywhere in the line

Fixes issue https://github.com/nesbox/TIC-80/issues/192
This commit is contained in:
Guilherme Medeiros 2017-12-28 15:21:38 -03:00
parent 2bb479031e
commit ea9f478076
1 changed files with 33 additions and 18 deletions

View File

@ -617,20 +617,31 @@ static void redo(Code* code)
update(code); update(code);
} }
static void doTab(Code* code, bool shift) static void doTab(Code* code, bool shift, bool crtl)
{ {
char* pos = code->cursor.position; char* cursor_position = code->cursor.position;
char* sel = code->cursor.selection; char* cursor_selection = code->cursor.selection;
if(sel && sel != pos) bool has_selection = cursor_selection && cursor_selection != cursor_position;
bool modifier_key_pressed = shift || crtl;
if(has_selection || modifier_key_pressed)
{ {
char* start = SDL_min(sel, pos); char* start;
char* end = SDL_max(sel, pos); char* end;
bool changed = false; bool changed = false;
if(cursor_selection) {
start = SDL_min(cursor_selection, cursor_position);
end = SDL_max(cursor_selection, cursor_position);
} else {
start = end = cursor_position;
}
char* line = start = getLineByPos(code, start); char* line = start = getLineByPos(code, start);
while(line && line < end) while(line)
{ {
if(shift) if(shift)
{ {
@ -649,18 +660,21 @@ static void doTab(Code* code, bool shift)
changed = true; changed = true;
} }
line = getNextLineByPos(code, line); line = getNextLineByPos(code, line);
if(line >= end) break;
} }
code->cursor.position = start; if(changed) {
code->cursor.selection = end;
if(changed)
history(code); history(code);
parseSyntaxColor(code);
parseSyntaxColor(code);
if(has_selection) {
code->cursor.position = start;
code->cursor.selection = end;
}
else if (start <= end) code->cursor.position = end;
}
} }
else inputSymbolBase(code, '\t'); else inputSymbolBase(code, '\t');
} }
@ -896,6 +910,7 @@ static void processKeydown(Code* code, SDL_Keycode keycode)
{ {
case SDLK_LEFT: leftWord(code); break; case SDLK_LEFT: leftWord(code); break;
case SDLK_RIGHT: rightWord(code); break; case SDLK_RIGHT: rightWord(code); break;
case SDLK_TAB: doTab(code, keymod & KMOD_SHIFT, keymod & KMOD_CTRL); break;
} }
} }
else if(keymod & KMOD_GUI) else if(keymod & KMOD_GUI)
@ -943,7 +958,7 @@ static void processKeydown(Code* code, SDL_Keycode keycode)
case SDLK_DELETE: deleteChar(code); break; case SDLK_DELETE: deleteChar(code); break;
case SDLK_BACKSPACE: backspaceChar(code); break; case SDLK_BACKSPACE: backspaceChar(code); break;
case SDLK_RETURN: newLine(code); break; case SDLK_RETURN: newLine(code); break;
case SDLK_TAB: doTab(code, keymod & KMOD_SHIFT); break; case SDLK_TAB: doTab(code, keymod & KMOD_SHIFT, keymod & KMOD_CTRL); break;
} }
} }