diff --git a/language/codegen/sourcefiletemplate.h b/language/codegen/sourcefiletemplate.h --- a/language/codegen/sourcefiletemplate.h +++ b/language/codegen/sourcefiletemplate.h @@ -172,7 +172,7 @@ /** * The type of this option. * - * Currently supported are Int, String and Bool + * Currently supported are Int, String, Enum and Bool */ QString type; /** @@ -205,6 +205,12 @@ * This is applicable only to integers */ QString minValue; + /** + * The possible values of this entry, as a list of strings + * + * This is applicable only to enums + */ + QStringList values; }; /** diff --git a/language/codegen/sourcefiletemplate.cpp b/language/codegen/sourcefiletemplate.cpp --- a/language/codegen/sourcefiletemplate.cpp +++ b/language/codegen/sourcefiletemplate.cpp @@ -57,6 +57,7 @@ entry.name = element.attribute(QStringLiteral("name")); entry.type = element.attribute(QStringLiteral("type"), QStringLiteral("String")); + bool isDefaultValueSet = false; for (QDomElement e = element.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) { QString tag = e.tagName(); @@ -84,10 +85,38 @@ else if ( tag == QLatin1String("default") ) { entry.value = renderer->render(e.text(), entry.name); + isDefaultValueSet = true; + } + else if (tag == QLatin1String("choices")) { + QStringList values; + QDomNodeList choices = element.elementsByTagName(QStringLiteral("choice")); + for (int j = 0; j < choices.size(); ++j) { + QDomElement choiceElement = choices.at(j).toElement(); + values << choiceElement.attribute(QStringLiteral("name")); + } + Q_ASSERT(!values.isEmpty()); + if (values.isEmpty()) { + qCWarning(LANGUAGE) << "Entry " << entry.name << "has an enum without any choices"; + } + entry.values = values; } } qCDebug(LANGUAGE) << "Read entry" << entry.name << "with default value" << entry.value; + + // preset value for enum if needed + if (!entry.values.isEmpty()) { + if (isDefaultValueSet) { + const bool isSaneDefaultValue = entry.values.contains(entry.value.toString()); + Q_ASSERT(isSaneDefaultValue); + if (!isSaneDefaultValue) { + qCWarning(LANGUAGE) << "Default value" << entry.value << "not in enum" << entry.values; + entry.value = entry.values.at(0); + } + } else { + entry.value = entry.values.at(0); + } + } return entry; } diff --git a/plugins/filetemplates/templateoptionspage.cpp b/plugins/filetemplates/templateoptionspage.cpp --- a/plugins/filetemplates/templateoptionspage.cpp +++ b/plugins/filetemplates/templateoptionspage.cpp @@ -34,6 +34,7 @@ #include #include #include +#include using namespace KDevelop; @@ -53,6 +54,7 @@ d->firstEditWidget = nullptr; d->typeProperties.insert(QStringLiteral("String"), "text"); + d->typeProperties.insert(QStringLiteral("Enum"), "currentText"); d->typeProperties.insert(QStringLiteral("Int"), "value"); d->typeProperties.insert(QStringLiteral("Bool"), "checked"); } @@ -88,6 +90,13 @@ { control = new QLineEdit(entry.value.toString(), box); } + else if (type == QLatin1String("Enum")) + { + auto input = new QComboBox(box); + input->addItems(entry.values); + input->setCurrentText(entry.value.toString()); + control = input; + } else if (type == QLatin1String("Int")) { auto input = new QSpinBox(box);