Skip to content

Commit

Permalink
Added checking for 4 spaces when pressing backspace/delete
Browse files Browse the repository at this point in the history
Because this is overwriting the function of the keys, I have re-implemented
the standard functions of both to maintain usability.
Fix for uccser#377
  • Loading branch information
octoscorp committed Mar 29, 2023
1 parent 3b86c97 commit 7d7df59
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion codewof/static/js/question_types/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,63 @@ $(document).ready(function () {
autofocus: true,
indentUnit: 4,
viewportMargin: Infinity,
// Replace tabs with 4 spaces. Taken from https://stackoverflow.com/questions/15183494/codemirror-tabs-to-spaces
// Replace tabs with 4 spaces, and backspace all 4 when possible.
// Taken from https://stackoverflow.com/questions/32622128/codemirror-how-to-read-editor-text-before-or-after-cursor-position
extraKeys: {
"Tab": function(cm) {
cm.replaceSelection(" ", "end");
},
"Backspace": function(cm) {
doc = cm.getDoc();
line = doc.getCursor().line; // Cursor line
ch = doc.getCursor().ch; // Cursor character

if (doc.somethingSelected()) { // Remove user-selected characters
doc.replaceSelection("");
} else { // Determine the ends of the selection to delete
from = {line, ch};
to = {line, ch};
stringToTest = doc.getLine(line).substr(Math.max(ch - 4,0), Math.min(ch, 4));

if (stringToTest === " ") { // Remove 4 spaces (dedent)
from = {line, ch: ch - 4};
} else if (ch == 0) { // Remove last character of previous line
if (line > 0) {
from = {line: line - 1, ch: doc.getLine(line - 1).length};
}
} else { // Remove preceding character
from = {line, ch: ch - 1};
}

// Delete the selection
doc.replaceRange("", from, to);
}
},
"Delete" : function(cm) {
doc = cm.getDoc();
line = doc.getCursor().line; // Cursor line
ch = doc.getCursor().ch; // Cursor character

if (doc.somethingSelected()) { // Remove user-selected characters
doc.replaceSelection("");
} else { // Determine the ends of the selection to delete
from = {line, ch};
to = {line, ch};
stringToTest = doc.getLine(line).substr(ch, 4);

if (stringToTest === " ") { // Remove 4 spaces (dedent)
to = {line, ch: ch + 4};
} else if (ch == doc.getLine(line).length) { // Remove first character of next line
if (line < doc.size - 1) {
to = {line: line + 1, ch: 0};
}
} else { // Remove following character
to = {line, ch: ch + 1};
}

// Delete the selection
doc.replaceRange("", from, to);
}
}
}
});
Expand Down

0 comments on commit 7d7df59

Please sign in to comment.