Commit bcbeaf26 authored by Igor Kushnir's avatar Igor Kushnir
Browse files

Reorder languages in Configure Source Formatter UI

ClangSupport::name() returns "clang" rather than "C" or "C++". So the
languages KDevelop supports best are never found in active or loaded
languages. As a result, the items of the Language combobox are always
sorted alphabetically. When kdev-python plugin is installed, "Python"
appears at the top of the combobox list, while "Python 3" is placed at
the bottom, because Python::LanguageSupport::name() returns "Python".
The resulting Language list order is therefore unsatisfactory:
unsupported "C#" is above best-supported "C++"; "Python" is always at
the top; "Python 3" is very far from "Python" and can be easily missed.

It is possible to fix this issue by analyzing MIME types supported by
active and loaded language plugins. But runtime language order changes
can confuse the user and make understanding this configuration UI even
more difficult (see BUG 358798). Stable and predictable language list
order makes more sense to me. So this commit drops the dependency on
ILanguageController and ILanguageSupport, keeps the alphabetical order,
but moves unsupported "C#" and "Java" to the bottom of the list.
parent 893a4db3
Pipeline #96009 passed with stage
in 33 minutes and 18 seconds
......@@ -15,8 +15,6 @@
#include "plugincontroller.h"
#include <util/scopeddialog.h>
#include <language/interfaces/ilanguagesupport.h>
#include <interfaces/ilanguagecontroller.h>// TODO: remove later
#include <KMessageBox>
#include <KTextEditor/Editor>
......@@ -28,6 +26,8 @@
#include <QMimeDatabase>
#include <algorithm>
#include <array>
#define STYLE_ROLE (Qt::UserRole+1)
......@@ -62,8 +62,42 @@ public:
FormatterMap formatters;
KTextEditor::Document* document;
KTextEditor::View* view;
/**
* Add the keys of @a languages to @a ui.cbLanguages.
*/
void fillLanguageCombobox();
};
void SourceFormatterSelectionEditPrivate::fillLanguageCombobox()
{
// Move the languages not supported by KDevelop to the bottom of the combobox.
// Use std::array to avoid extra memory allocations.
constexpr std::array unsupportedLanguages{
QLatin1String("C#", 2),
QLatin1String("Java", 4),
};
Q_ASSERT(std::is_sorted(unsupportedLanguages.cbegin(), unsupportedLanguages.cend()));
std::array<QString, unsupportedLanguages.size()> skippedLanguages{};
for (auto langIt = languages.keyBegin(); langIt != languages.keyEnd(); ++langIt) {
const auto& name = *langIt;
const auto unsupportedIt = std::find(unsupportedLanguages.cbegin(), unsupportedLanguages.cend(), name);
if (unsupportedIt == unsupportedLanguages.cend()) {
ui.cbLanguages->addItem(name);
} else {
skippedLanguages[unsupportedIt - unsupportedLanguages.cbegin()] = name;
}
}
for (const auto& name : skippedLanguages) {
if (!name.isEmpty()) {
ui.cbLanguages->addItem(name);
}
}
}
SourceFormatterSelectionEdit::SourceFormatterSelectionEdit(QWidget* parent)
: QWidget(parent)
, d_ptr(new SourceFormatterSelectionEditPrivate)
......@@ -251,26 +285,6 @@ void SourceFormatterSelectionEdit::resetUi()
qCDebug(SHELL) << "Resetting UI";
// Create a sorted list of the languages, preferring firstly active, then loaded languages, then others
QList<QString> sortedLanguages;
for (const auto& languages : {ICore::self()->languageController()->activeLanguages(),
ICore::self()->languageController()->loadedLanguages()}) {
for (const auto* language : languages) {
const auto languageName = language->name();
if (d->languages.contains(languageName) && !sortedLanguages.contains(languageName)) {
sortedLanguages.append(languageName);
}
}
}
for (auto it = d->languages.constBegin(); it != d->languages.constEnd(); ++it) {
const auto& languageName = it.key();
if (!sortedLanguages.contains(languageName)) {
sortedLanguages.append(languageName);
}
}
bool b = blockSignals( true );
d->ui.cbLanguages->blockSignals(!b);
d->ui.cbFormatters->blockSignals(!b);
......@@ -278,9 +292,8 @@ void SourceFormatterSelectionEdit::resetUi()
d->ui.cbLanguages->clear();
d->ui.cbFormatters->clear();
d->ui.styleList->clear();
for (const auto& language : sortedLanguages) {
d->ui.cbLanguages->addItem(language);
}
d->fillLanguageCombobox();
if (d->ui.cbLanguages->count() == 0) {
d->ui.cbLanguages->setEnabled(false);
selectLanguage( -1 );
......
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