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 a8843f5
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 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,64 @@ $(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 remove all 4 when deleting if possible.
// Taken from https://stackoverflow.com/questions/15183494/codemirror-tabs-to-spaces and
// 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 a8843f5

Please sign in to comment.