Skip to content

Commit e236b9a

Browse files
committed
Fix indentation and Shift+Tab
1 parent e5d6185 commit e236b9a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

code-input.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ var codeInput = {
5151
let input_element = this.querySelector("textarea");
5252
let code = input_element.value;
5353
event.preventDefault(); // stop normal
54-
55-
if(input_element.selectionStart == input_element.selectionEnd) {
56-
54+
55+
if(!event.shiftKey && input_element.selectionStart == input_element.selectionEnd) {
56+
// Shift always means dedent - this places a tab here.
5757
let before_selection = code.slice(0, input_element.selectionStart); // text before tab
5858
let after_selection = code.slice(input_element.selectionEnd, input_element.value.length); // text after tab
5959

@@ -75,10 +75,14 @@ var codeInput = {
7575
let first_line_indents = 0;
7676

7777
for (let i = 0; i < lines.length; i++) {
78-
letter_i += lines[i].length;
79-
if(input_element.selectionStart < letter_i && input_element.selectionEnd > letter_i - lines[i].length) {
78+
letter_i += lines[i].length+1; // newline counted
79+
80+
console.log(lines[i], ": start", input_element.selectionStart, letter_i, "&& end", input_element.selectionEnd , letter_i - lines[i].length)
81+
if(input_element.selectionStart <= letter_i && input_element.selectionEnd >= letter_i - lines[i].length) {
82+
// Starts before or at last char and ends after or at first char
8083
if(event.shiftKey) {
8184
if(lines[i][0] == "\t") {
85+
// Remove first tab
8286
lines[i] = lines[i].slice(1);
8387
if(number_indents == 0) first_line_indents--;
8488
number_indents--;

0 commit comments

Comments
 (0)