Commit ceb8a53c authored by twanvl's avatar twanvl

The keyword reminder text box now tries to run the script to see if it contains errors

parent 981fbedf
...@@ -52,8 +52,10 @@ void AddKeywordAction::perform(bool to_undo) { ...@@ -52,8 +52,10 @@ void AddKeywordAction::perform(bool to_undo) {
// ----------------------------------------------------------------------------- : Changing keywords // ----------------------------------------------------------------------------- : Changing keywords
KeywordReminderTextValue::KeywordReminderTextValue(const TextFieldP& field, Keyword* keyword, bool editable) KeywordReminderTextValue::KeywordReminderTextValue(Set& set, const TextFieldP& field, Keyword* keyword, bool editable)
: KeywordTextValue(field, keyword, &keyword->reminder.getUnparsed(), editable) : KeywordTextValue(field, keyword, &keyword->reminder.getUnparsed(), editable)
, set(set)
, keyword(*keyword)
{} {}
void KeywordReminderTextValue::store() { void KeywordReminderTextValue::store() {
...@@ -67,10 +69,12 @@ void KeywordReminderTextValue::store() { ...@@ -67,10 +69,12 @@ void KeywordReminderTextValue::store() {
vector<ScriptParseError> parse_errors; vector<ScriptParseError> parse_errors;
ScriptP new_script = parse(new_value, nullptr, true, parse_errors); ScriptP new_script = parse(new_value, nullptr, true, parse_errors);
if (parse_errors.empty()) { if (parse_errors.empty()) {
// parsed okay, assign // parsed okay
errors.clear(); if (checkScript(new_script)) {
keyword.reminder.getScriptP() = new_script; // also runs okay, assign
keyword.reminder.getUnparsed() = new_value; keyword.reminder.getScriptP() = new_script;
keyword.reminder.getUnparsed() = new_value;
}
} else { } else {
// parse errors, report // parse errors, report
errors = ScriptParseErrors(parse_errors).what(); errors = ScriptParseErrors(parse_errors).what();
...@@ -172,6 +176,23 @@ void KeywordReminderTextValue::highlight(const String& code, const vector<Script ...@@ -172,6 +176,23 @@ void KeywordReminderTextValue::highlight(const String& code, const vector<Script
value = new_value; value = new_value;
} }
bool KeywordReminderTextValue::checkScript(const ScriptP& script) {
Context& ctx = set.cards.empty() ? set.getContext() : set.getContext(set.cards.front());
size_t scope = ctx.openScope();
try {
for (size_t i = 0 ; i < keyword.parameters.size() ; ++i) {
String param = String(_("param")) << (int)(i+1);
ctx.setVariable(param, to_script(param));
}
script->eval(ctx);
errors.clear();
} catch (const Error& e) {
errors = e.what();
}
ctx.closeScope(scope);
return errors.empty();
}
// ----------------------------------------------------------------------------- : Changing keywords : mode // ----------------------------------------------------------------------------- : Changing keywords : mode
ChangeKeywordModeAction::ChangeKeywordModeAction(Keyword& keyword, const String& new_mode) ChangeKeywordModeAction::ChangeKeywordModeAction(Keyword& keyword, const String& new_mode)
......
...@@ -70,9 +70,11 @@ class KeywordTextValue : public FakeTextValue { ...@@ -70,9 +70,11 @@ class KeywordTextValue : public FakeTextValue {
/// A FakeTextValue that is used to edit reminder text scripts /// A FakeTextValue that is used to edit reminder text scripts
class KeywordReminderTextValue : public KeywordTextValue { class KeywordReminderTextValue : public KeywordTextValue {
public: public:
KeywordReminderTextValue(const TextFieldP& field, Keyword* keyword, bool editable); KeywordReminderTextValue(Set& set, const TextFieldP& field, Keyword* keyword, bool editable);
String errors; ///< Errors in the script String errors; ///< Errors in the script
Set& set; ///< Set this keyword is in (for script checking)
Keyword& keyword; ///< The keyword we are the reminder text of
/// Try to compile the script /// Try to compile the script
virtual void store(); virtual void store();
...@@ -81,6 +83,9 @@ class KeywordReminderTextValue : public KeywordTextValue { ...@@ -81,6 +83,9 @@ class KeywordReminderTextValue : public KeywordTextValue {
/// Syntax highlight, and store in value /// Syntax highlight, and store in value
void highlight(const String& code, const vector<ScriptParseError>& errors); void highlight(const String& code, const vector<ScriptParseError>& errors);
/// Check the script for errors
bool checkScript(const ScriptP& script);
}; };
/// Changing the mode of a keyword /// Changing the mode of a keyword
......
...@@ -313,7 +313,7 @@ void KeywordsPanel::onKeywordSelect(KeywordSelectEvent& ev) { ...@@ -313,7 +313,7 @@ void KeywordsPanel::onKeywordSelect(KeywordSelectEvent& ev) {
keyword ->setValue(new_intrusive5<KeywordTextValue> (keyword->getFieldP(), &kw, &kw.keyword, !kw.fixed, true)); keyword ->setValue(new_intrusive5<KeywordTextValue> (keyword->getFieldP(), &kw, &kw.keyword, !kw.fixed, true));
match ->setValue(new_intrusive4<KeywordTextValue> (match->getFieldP(), &kw, &kw.match, !kw.fixed)); match ->setValue(new_intrusive4<KeywordTextValue> (match->getFieldP(), &kw, &kw.match, !kw.fixed));
rules ->setValue(new_intrusive4<KeywordTextValue> (rules->getFieldP(), &kw, &kw.rules, !kw.fixed)); rules ->setValue(new_intrusive4<KeywordTextValue> (rules->getFieldP(), &kw, &kw.rules, !kw.fixed));
intrusive_ptr<KeywordReminderTextValue> reminder_value(new KeywordReminderTextValue(reminder->getFieldP(), &kw, !kw.fixed)); intrusive_ptr<KeywordReminderTextValue> reminder_value(new KeywordReminderTextValue(*set, reminder->getFieldP(), &kw, !kw.fixed));
reminder->setValue(reminder_value); reminder->setValue(reminder_value);
errors->SetLabel(reminder_value->errors); errors->SetLabel(reminder_value->errors);
add_param->Enable(!kw.fixed && !set->game->keyword_parameter_types.empty()); add_param->Enable(!kw.fixed && !set->game->keyword_parameter_types.empty());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment