Editing of keywords

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@255 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2007-04-13 23:59:42 +00:00
parent 1be1304c94
commit 7b45f6f69e
15 changed files with 326 additions and 79 deletions
+101
View File
@@ -10,6 +10,9 @@
#include <data/keyword.hpp>
#include <data/set.hpp>
#include <data/game.hpp>
#include <script/parser.hpp>
#include <util/tagged_string.hpp>
#include <util/error.hpp>
DECLARE_TYPEOF_COLLECTION(KeywordModeP);
@@ -69,3 +72,101 @@ void RemoveKeywordAction::perform(bool to_undo) {
set.keywords.insert(set.keywords.begin() + keyword_id, keyword);
}
}
// ----------------------------------------------------------------------------- : Changing keywords
KeywordReminderTextValue::KeywordReminderTextValue(const TextFieldP& field, Keyword* keyword, bool editable)
: KeywordTextValue(field, keyword, &keyword->reminder.getUnparsed(), editable)
{}
void KeywordReminderTextValue::store() {
if (!editable) {
retrieve();
return;
}
// Re-highlight
String new_value = untag(value);
highlight(new_value);
// Try to parse the script
try {
ScriptP new_script = parse(new_value, true);
// parsed okay, assign
errors.clear();
keyword.reminder.getScriptP() = new_script;
keyword.reminder.getUnparsed() = new_value;
} catch (const Error& e) {
// parse errors, report
errors = e.what(); // TODO
}
}
void KeywordReminderTextValue::retrieve() {
highlight(*underlying);
}
void KeywordReminderTextValue::highlight(const String& code) {
// Add tags to indicate code / syntax highlight
// i.e. bla {if code "x" } bla
// becomes:
// bla <code>{<code-kw>if</code-kw> code "<code-string>x</code-string>" } bla
String new_value;
int in_brace = 0;
bool in_string = true;
for (size_t pos = 0 ; pos < code.size() ; ) {
Char c = code.GetChar(pos);
if (c == _('<')) {
new_value += _('\1'); // escape
++pos;
} else if (c == _('{')) {
in_brace++;
if (in_brace == 1) new_value += _("<code>");
if (in_string) in_string = false;
new_value += c;
++pos;
} else if (c == _('}') && !in_string) {
new_value += c;
in_brace--;
if (in_brace == 0) {
new_value += _("</code>");
in_string = true;
}
++pos;
} else if (c == _('"')) {
if (in_string) {
in_string = false;
new_value += _("\"<code-str>");
} else {
in_string = true;
new_value += _("<code-str>\"");
}
++pos;
} else if (c == _('\\') && in_string && pos + 1 < code.size()) {
new_value += c + code.GetChar(pos + 1); // escape code
pos += 2;
} else if (is_substr(code, pos, _("if ")) && !in_string) {
new_value += _("<code-kw>if</code-kw> ");
pos += 3;
} else if (is_substr(code, pos, _("then ")) && !in_string) {
new_value += _("<code-kw>then</code-kw> ");
pos += 5;
} else if (is_substr(code, pos, _("else ")) && !in_string) {
new_value += _("<code-kw>else</code-kw> ");
pos += 5;
} else if (is_substr(code, pos, _("for ")) && !in_string) {
new_value += _("<code-kw>for</code-kw> ");
pos += 4;
} else if (is_substr(code, pos, _("param")) && !in_string) {
// parameter reference
size_t end = code.find_first_not_of(_("0123456789"), pos + 5);
if (end == String::npos) end = code.size();
String param = code.substr(pos, end-pos);
new_value += _("<") + param + _(">") + param + _("</") + param + _(">");
pos = end;
} else {
new_value += c;
++pos;
}
}
// set
value = new_value;
}
+34
View File
@@ -16,6 +16,7 @@
#include <util/prec.hpp>
#include <util/action_stack.hpp>
#include <data/field/text.hpp>
class Set;
DECLARE_POINTER_TYPE(Keyword);
@@ -62,5 +63,38 @@ class RemoveKeywordAction : public KeywordListAction {
// ----------------------------------------------------------------------------- : Changing keywords
/// A FakeTextValue that is used to edit an aspect of a keyword
/** These values can be seen in ValueActions.
* Can edit one of:
* - the keyword name
* - the match string
* - reminder text
*/
class KeywordTextValue : public FakeTextValue {
public:
KeywordTextValue(const TextFieldP& field, Keyword* keyword, String* underlying, bool editable, bool untagged = false)
: FakeTextValue(field, underlying, editable, untagged)
, keyword(*keyword)
{}
Keyword& keyword; ///< The keyword that is being edited
};
/// A FakeTextValue that is used to edit reminder text scripts
class KeywordReminderTextValue : public KeywordTextValue {
public:
KeywordReminderTextValue(const TextFieldP& field, Keyword* keyword, bool editable);
String errors; ///< Errors in the script
/// Try to compile the script
virtual void store();
/// Add some tags, so the script looks nice
virtual void retrieve();
/// Syntax highlight, and store in value
void highlight(const String& code);
};
// ----------------------------------------------------------------------------- : EOF
#endif