Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added checking for 4 spaces when pressing backspace/delete #687

Merged
merged 6 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions codewof/static/js/question_types/base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require('skulpt');
var CodeMirror = require('codemirror');
require('codemirror/mode/python/python.js');

function ajax_request(url_name, data, success_function) {
$.ajax({
Expand Down Expand Up @@ -153,10 +155,84 @@ function scroll_to_element(containerId, element) {
}
}

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: {
name: "python",
version: 3,
singleLineStringErrors: false
},
lineNumbers: true,
textWrapping: false,
styleActiveLine: true,
autofocus: true,
indentUnit: 4,
viewportMargin: Infinity,
// 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);
}
}
}
});

exports.ajax_request = ajax_request;
exports.clear_submission_feedback = clear_submission_feedback;
exports.display_submission_feedback = display_submission_feedback;
exports.update_test_case_status = update_test_case_status;
exports.run_test_cases = run_test_cases;
exports.scroll_to_element = scroll_to_element;
exports.editor = editor;
22 changes: 1 addition & 21 deletions codewof/static/js/question_types/debugging.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var base = require('./base.js');
var CodeMirror = require('codemirror');
require('codemirror/mode/python/python.js');
const introJS = require('intro.js');

var test_cases = {};
Expand All @@ -15,25 +13,7 @@ $(document).ready(function () {
mark_lines_as_read_only(editor);
});

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: {
name: "python",
version: 3,
singleLineStringErrors: false
},
lineNumbers: true,
textWrapping: false,
styleActiveLine: true,
autofocus: true,
indentUnit: 4,
viewportMargin: Infinity,
// Replace tabs with 4 spaces. Taken from https://stackoverflow.com/questions/15183494/codemirror-tabs-to-spaces
extraKeys: {
"Tab": function(cm) {
cm.replaceSelection(" ", "end");
}
}
});
var editor = base.editor;

mark_lines_as_read_only(editor);

Expand Down
22 changes: 1 addition & 21 deletions codewof/static/js/question_types/function.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var base = require('./base.js');
var CodeMirror = require('codemirror');
require('codemirror/mode/python/python.js');
const introJS = require('intro.js');

var test_cases = {};
Expand All @@ -10,25 +8,7 @@ $(document).ready(function () {
run_code(editor, true);
});

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: {
name: "python",
version: 3,
singleLineStringErrors: false
},
lineNumbers: true,
textWrapping: false,
styleActiveLine: true,
autofocus: true,
indentUnit: 4,
viewportMargin: Infinity,
// Replace tabs with 4 spaces. Taken from https://stackoverflow.com/questions/15183494/codemirror-tabs-to-spaces
extraKeys: {
"Tab": function(cm) {
cm.replaceSelection(" ", "end");
}
}
});
var editor = base.editor;

for (let i = 0; i < test_cases_list.length; i++) {
data = test_cases_list[i];
Expand Down
22 changes: 1 addition & 21 deletions codewof/static/js/question_types/program.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var base = require('./base.js');
var CodeMirror = require('codemirror');
require('codemirror/mode/python/python.js');
const introJS = require('intro.js');

var test_cases = {};
Expand All @@ -10,25 +8,7 @@ $(document).ready(function () {
run_code(editor, true);
});

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: {
name: "python",
version: 3,
singleLineStringErrors: false
},
lineNumbers: true,
textWrapping: false,
styleActiveLine: true,
autofocus: true,
indentUnit: 4,
viewportMargin: Infinity,
// Replace tabs with 4 spaces. Taken from https://stackoverflow.com/questions/15183494/codemirror-tabs-to-spaces
extraKeys: {
"Tab": function(cm) {
cm.replaceSelection(" ", "end");
}
}
});
var editor = base.editor;

for (let i = 0; i < test_cases_list.length; i++) {
data = test_cases_list[i];
Expand Down