diff --git a/autotests/syntaxrepository_test.cpp b/autotests/syntaxrepository_test.cpp --- a/autotests/syntaxrepository_test.cpp +++ b/autotests/syntaxrepository_test.cpp @@ -550,6 +550,28 @@ QCOMPARE(klist.size(), 1); QVERIFY(klist.contains(QLatin1String("f"))); } + + void testKeywordListModification() + { + auto def = m_repo.definitionForName(QLatin1String("Python")); + QVERIFY(def.isValid()); + + const QStringList& lists = def.keywordLists(); + QVERIFY(!lists.isEmpty()); + + const QString& listName = lists.first(); + const QStringList keywords = def.keywordList(listName); + + QStringList modified = keywords; + modified.append(QLatin1String("test")); + + QVERIFY(def.setKeywordList(listName, modified) == true); + QCOMPARE(keywords + QStringList(QLatin1String("test")), def.keywordList(listName)); + + const QString& unexistedName = QLatin1String("unexisted-keyword-name"); + QVERIFY(lists.contains(unexistedName) == false); + QVERIFY(def.setKeywordList(unexistedName, QStringList()) == false); + } }; } diff --git a/src/lib/definition.h b/src/lib/definition.h --- a/src/lib/definition.h +++ b/src/lib/definition.h @@ -333,6 +333,21 @@ */ QStringList keywordList(const QString& name) const; + /** + * Set content of keyword list with name @p name to @p content + * Only existing keywordLists() can be changed. For non-existent keyword lists, false is returned. + * @see keywordList(), keywordLists() + * + * This function can be usefull for dynamic higlighting, when the document rehiglights more, that one time. + * For example, you have a program for editing and running Octave scripts files and + * you are using KSyntaxHighlighting for syntax higlighting. But also, you can request list of + * variables, functions, etc from corresponed Octave process. + * And, used this function, you can provide Octave live higlighting - by passing, for + * example, variables names from Octave to KSyntaxHighlighting variable keyword list via this function. + * @since 5.62 + */ + bool setKeywordList(const QString& name, const QStringList& content); + /** * Returns a list of all Format items used by this definition. * The order of the Format items equals the order of the itemDatas in the xml file. diff --git a/src/lib/definition.cpp b/src/lib/definition.cpp --- a/src/lib/definition.cpp +++ b/src/lib/definition.cpp @@ -234,6 +234,19 @@ return list ? list->keywords() : QStringList(); } +bool Definition::setKeywordList(const QString& name, const QStringList& content) +{ + d->load(DefinitionData::OnlyKeywords(true)); + KeywordList* list = d->keywordList(name); + if (list) + { + list->setKeywordList(content); + return true; + } + else + return false; +} + QVector Definition::formats() const { d->load(); diff --git a/src/lib/keywordlist_p.h b/src/lib/keywordlist_p.h --- a/src/lib/keywordlist_p.h +++ b/src/lib/keywordlist_p.h @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -60,6 +61,14 @@ return m_keywords; } + void setKeywordList(const QStringList& keywords) + { + m_keywords = keywords; + m_keywordsSortedCaseSensitive.clear(); + m_keywordsSortedCaseInsensitive.clear(); + initLookupForCaseSensitivity(m_caseSensitive); + } + /** Checks if @p str is a keyword in this list. */ bool contains(const QStringRef &str) const {