Skip to content

Commit 4dd2db8

Browse files
committed
Wrap error messages; account for GDScriptAnalyzer by forcing class_name
1 parent fd6c1a2 commit 4dd2db8

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

course/lesson-7-member-variables/draw_rectangle_at_position/TestsDrawingRectangles.gd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ func test_rectangle_size_is_200_by_120() -> String:
2828
if points != expected_rect:
2929
return tr("The drawn shapes don't have the expected length and height. Did you forget to use the length and height parameter?")
3030
return ""
31-

script_checking/OfflineScriptVerifier.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ func test() -> void:
2929
if wrap.has_errors():
3030
errors = []
3131
for i in wrap.get_error_count():
32-
var error_line: int = wrap.get_error_line(i) - 1
33-
var line_text := _new_script_text.split("\n")[error_line]
32+
var lines := _new_script_text.split("\n")
33+
var error_line: int = clampi(wrap.get_error_line(i) - 1, 0, lines.size()-1)
34+
var line_text := lines[error_line]
3435
var error_data := make_error_from_data(
3536
1,
3637
wrap.get_error(i),

ui/UIPractice.gd

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ const PracticeDonePopup := preload("components/popups/PracticeDonePopup.gd")
2020
const PracticeLeaveUnfinishedPopup := preload("components/popups/PracticeLeaveUnfinishedPopup.gd")
2121
const documentation_resource: Documentation = preload("res://course/Documentation.tres")
2222

23-
var REGEX_DIVSION_BY_ZERO := RegEx.new()
23+
static var REGEX_DIVSION_BY_ZERO := RegEx.create_from_string((r"[/%] *0"))
24+
static var REGEX_CLASS_NAME := RegEx.create_from_string(r"\s*class_name\s")
2425

2526
@export var lesson_test_practice: String
2627
@export_range(0, 1, 1, "or_greater") var test_practice: int
2728
@export var _layout_container: Control
28-
@export var _output_container: Control
2929
@export var _game_container: Container
3030
@export var _game_view: GameView
3131
@export var _output_console: OutputConsole
@@ -331,15 +331,30 @@ func _validate_and_run_student_code() -> void:
331331
_code_editor.unlock_editor()
332332
return
333333

334-
var verifier := OfflineScriptVerifier.new(script_text)
334+
var verifier_script := script_text
335+
var script_is_desynced_by_one_line := false
336+
337+
if not _has_class_name(verifier_script):
338+
# required by GDScriptAnalyzer, since otherwise it has no external parser to cache
339+
verifier_script = "class_name TEMP_UserScript\n" + verifier_script
340+
script_is_desynced_by_one_line = true
341+
342+
var verifier := OfflineScriptVerifier.new(verifier_script)
335343
verifier.test()
336344
var errors := verifier.errors
337345

338346
if not errors.is_empty():
347+
if script_is_desynced_by_one_line:
348+
for error: ScriptError in errors:
349+
error.error_range.start.line -= 1
350+
error.error_range.end.line -= 1
339351
_code_editor.slice_editor.errors = errors
340352

341353
for index in errors.size():
342354
var error: ScriptError = errors[index]
355+
# GDScriptAnalyzer benign issue; script didn't exist until now
356+
if error.message == 'Error while getting cache for script "".':
357+
continue
343358
MessageBus.print_script_error(error, script_file_name)
344359

345360
var is_missing_parser_error: bool = (
@@ -399,7 +414,7 @@ func _validate_and_run_student_code() -> void:
399414
if script_is_valid != OK:
400415
var error := ScriptError.new()
401416
error.message = tr(
402-
"Oh no! The script has an error, but the Script Verifier did not catch it",
417+
"Oh no! The script has an error (code %s), but the Script Verifier did not catch it" % script_is_valid,
403418
)
404419
error.severity = 1
405420
error.code = GDQuestCodes.ErrorCode.INVALID_NO_CATCH
@@ -690,6 +705,10 @@ func _on_init_set_javascript() -> void:
690705
GDQUEST.events.onError.connect(_on_js_error_feedback_ref)
691706

692707

708+
func _has_class_name(source: String) -> bool:
709+
return REGEX_CLASS_NAME.search(source) != null
710+
711+
693712
# Checks if the script text has mixed tabs and spaces in indentation.
694713
# Returns the line number of the error or -1 if there's no error
695714
func _check_for_mixed_indentation(text: String) -> int:

ui/components/OutputConsoleErrorMessage.tscn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[gd_scene format=3 uid="uid://ukja5lpk5jw5"]
22

33
[ext_resource type="Script" uid="uid://vmi2orvhk0yw" path="res://ui/components/OutputConsoleErrorMessage.gd" id="2"]
4-
[ext_resource type="StyleBox" path="res://ui/theme/panel_error_log.tres" id="4"]
4+
[ext_resource type="StyleBox" uid="uid://d0pcjb7n8kfxq" path="res://ui/theme/panel_error_log.tres" id="4"]
55

66
[node name="OutputConsoleErrorMessage" type="PanelContainer" unique_id=1485937269 node_paths=PackedStringArray("_severity_label", "_message_label", "_location_row", "_file_name_label", "_location_label", "_external_label", "_message_explain_button")]
77
self_modulate = Color(1, 1, 1, 0.25)
@@ -37,10 +37,12 @@ theme_override_colors/font_color = Color(0.572549, 0.560784, 0.721569, 1)
3737
text = "INFO"
3838

3939
[node name="MessageValue" type="Label" parent="Layout/Content/MessageRow" unique_id=389845082]
40+
custom_minimum_size = Vector2(0, 20)
4041
layout_mode = 2
4142
size_flags_horizontal = 3
4243
size_flags_vertical = 0
4344
theme_override_colors/font_color = Color(0.572549, 0.560784, 0.721569, 1)
45+
autowrap_mode = 3
4446

4547
[node name="LocationRow" type="HBoxContainer" parent="Layout/Content" unique_id=129616357]
4648
layout_mode = 2

ui/components/OutputConsolePrintMessage.tscn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[gd_scene format=3 uid="uid://cdrh84x70t6mh"]
22

33
[ext_resource type="Script" uid="uid://duuccx1urqa0r" path="res://ui/components/OutputConsolePrintMessage.gd" id="2"]
4-
[ext_resource type="StyleBox" path="res://ui/theme/panel_error_log.tres" id="3"]
4+
[ext_resource type="StyleBox" uid="uid://d0pcjb7n8kfxq" path="res://ui/theme/panel_error_log.tres" id="3"]
55

66
[node name="OutputConsolePrintMessage" type="PanelContainer" unique_id=502726398 node_paths=PackedStringArray("_label")]
77
anchors_preset = 15
@@ -12,6 +12,8 @@ script = ExtResource("2")
1212
_label = NodePath("Label")
1313

1414
[node name="Label" type="Label" parent="." unique_id=807380464]
15+
custom_minimum_size = Vector2(0, 20)
1516
layout_mode = 2
1617
size_flags_vertical = 0
1718
text = "Value"
19+
autowrap_mode = 3

0 commit comments

Comments
 (0)