File indexing completed on 2024-04-28 04:36:31
0001 /* 0002 SPDX-FileCopyrightText: 2008 Cédric Pasteur <cedric.pasteur@free.fr> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "isourceformatter.h" 0008 0009 #include <KLocalizedString> 0010 0011 #include <QDebug> 0012 #include <QDebugStateSaver> 0013 #include <QVariant> 0014 #include <QMimeType> 0015 0016 #include <algorithm> 0017 0018 namespace KDevelop 0019 { 0020 0021 SettingsWidget::SettingsWidget(QWidget *parent) 0022 : QWidget(parent) 0023 { 0024 } 0025 0026 SettingsWidget::~SettingsWidget() 0027 { 0028 } 0029 0030 ISourceFormatter::~ISourceFormatter() 0031 { 0032 } 0033 0034 QString ISourceFormatter::usageHint() const 0035 { 0036 return QString(); 0037 } 0038 0039 SourceFormatterStyle::SourceFormatterStyle() 0040 { 0041 } 0042 0043 SourceFormatterStyle::SourceFormatterStyle(const QString &name) 0044 : m_usePreview(false) 0045 , m_name(name) 0046 { 0047 } 0048 0049 void SourceFormatterStyle::setContent(const QString &content) 0050 { 0051 m_content = content; 0052 } 0053 0054 void SourceFormatterStyle::setCaption(const QString &caption) 0055 { 0056 m_caption = caption; 0057 } 0058 0059 QString SourceFormatterStyle::content() const 0060 { 0061 return m_content; 0062 } 0063 0064 QString SourceFormatterStyle::description() const 0065 { 0066 return m_description; 0067 } 0068 0069 void SourceFormatterStyle::setDescription(const QString &desc) 0070 { 0071 m_description = desc; 0072 } 0073 0074 bool SourceFormatterStyle::usePreview() const 0075 { 0076 return m_usePreview; 0077 } 0078 0079 void SourceFormatterStyle::setUsePreview(bool use) 0080 { 0081 m_usePreview = use; 0082 } 0083 0084 void SourceFormatterStyle::setMimeTypes(const SourceFormatterStyle::MimeList& types) 0085 { 0086 m_mimeTypes = types; 0087 } 0088 0089 void SourceFormatterStyle::setMimeTypes(const QStringList& types) 0090 { 0091 for (auto& t : types) { 0092 auto items = t.split(QLatin1Char('|')); 0093 if (items.size() != 2 || items.at(0).isEmpty() || items.at(1).isEmpty()) { 0094 // An empty MIME type name is invalid and not useful. 0095 // Language names are displayed in a combobox. So an empty language name is unacceptable. 0096 qWarning() << "Skipping invalid Mime/Highlight pair in MimeTypes config entry for" 0097 << *this << ':' << t; 0098 continue; 0099 } 0100 m_mimeTypes << MimeHighlightPair{items.at(0), items.at(1)}; 0101 } 0102 } 0103 0104 void SourceFormatterStyle::setOverrideSample(const QString &sample) 0105 { 0106 m_overrideSample = sample; 0107 } 0108 0109 QString SourceFormatterStyle::overrideSample() const 0110 { 0111 return m_overrideSample; 0112 } 0113 0114 SourceFormatterStyle::MimeList SourceFormatterStyle::mimeTypes() const 0115 { 0116 return m_mimeTypes; 0117 } 0118 0119 QVariant SourceFormatterStyle::mimeTypesVariant() const 0120 { 0121 QStringList result; 0122 result.reserve(m_mimeTypes.size()); 0123 for ( const auto& item: m_mimeTypes ) { 0124 result << item.mimeType + QLatin1Char('|') + item.highlightMode; 0125 } 0126 return QVariant::fromValue(result); 0127 } 0128 0129 bool SourceFormatterStyle::supportsLanguage(const QString &language) const 0130 { 0131 for ( const auto& item: m_mimeTypes ) { 0132 if ( item.highlightMode == language ) { 0133 return true; 0134 } 0135 } 0136 return false; 0137 } 0138 0139 QString SourceFormatterStyle::modeForMimetype(const QMimeType& mime) const 0140 { 0141 const auto mimeTypes = this->mimeTypes(); 0142 for (const auto& item : mimeTypes) { 0143 if (mime.inherits(item.mimeType)) { 0144 return item.highlightMode; 0145 } 0146 } 0147 return QString(); 0148 } 0149 0150 void SourceFormatterStyle::copyDataFrom(const SourceFormatterStyle& other) 0151 { 0152 m_usePreview = other.m_usePreview; 0153 m_content = other.m_content; 0154 m_mimeTypes = other.m_mimeTypes; 0155 m_overrideSample = other.m_overrideSample; 0156 } 0157 0158 QDebug operator<<(QDebug dbg, const SourceFormatterStyle& style) 0159 { 0160 QDebugStateSaver saver(dbg); 0161 // For a given formatter, a style is uniquely identified by its name. But style names never appear in 0162 // the UI, only in config files. Style captions feature prominently in the UI. However, multiple styles 0163 // can have equal captions. Print both the name and the caption to allow quick and easy style 0164 // recognition by caption and reliable identification by name. 0165 dbg.nospace() << "SourceFormatterStyle{name=" << style.name() << ", caption=" << style.caption() << '}'; 0166 return dbg; 0167 } 0168 0169 SourceFormatterStyle ISourceFormatter::predefinedStyle(const QString& name) const 0170 { 0171 const auto styles = predefinedStyles(); 0172 const auto it = std::find_if(styles.cbegin(), styles.cend(), [&name](const SourceFormatterStyle& style) { 0173 return style.name() == name; 0174 }); 0175 return it == styles.cend() ? SourceFormatterStyle{name} : *it; 0176 } 0177 0178 QString ISourceFormatter::optionMapToString(const QMap<QString, QVariant> &map) 0179 { 0180 QString options; 0181 QMap<QString, QVariant>::const_iterator it = map.constBegin(); 0182 for (; it != map.constEnd(); ++it) { 0183 options += it.key() + QLatin1Char('=') + it.value().toString() + QLatin1Char(','); 0184 } 0185 return options; 0186 } 0187 0188 QMap<QString, QVariant> ISourceFormatter::stringToOptionMap(const QString &options) 0189 { 0190 QMap<QString, QVariant> map; 0191 const auto pairs = options.splitRef(QLatin1Char(','), Qt::SkipEmptyParts); 0192 for (auto& pair : pairs) { 0193 const int pos = pair.indexOf(QLatin1Char('=')); 0194 map.insert(pair.left(pos).toString(), pair.mid(pos+1).toString()); 0195 } 0196 return map; 0197 } 0198 0199 SourceFormatterStyle::MimeList ISourceFormatter::mimeTypesSupportedByBuiltInStyles() 0200 { 0201 static const SourceFormatterStyle::MimeList list = { 0202 {QStringLiteral("text/x-c++src"), QStringLiteral("C++")}, 0203 {QStringLiteral("text/x-csrc"), QStringLiteral("C")}, 0204 {QStringLiteral("text/x-chdr"), QStringLiteral("C")}, 0205 {QStringLiteral("text/x-c++hdr"), QStringLiteral("C++")}, 0206 {QStringLiteral("text/x-java"), QStringLiteral("Java")}, 0207 {QStringLiteral("text/x-csharp"), QStringLiteral("C#")}, 0208 {QStringLiteral("text/x-objcsrc"), QStringLiteral("Objective-C")}, 0209 {QStringLiteral("text/x-objc++src"), QStringLiteral("Objective-C++")}, 0210 }; 0211 return list; 0212 } 0213 0214 QString ISourceFormatter::missingExecutableMessage(const QString &name) 0215 { 0216 return i18n("The executable %1 cannot be found. Please make sure" 0217 " it is installed and can be executed. <br />" 0218 "The plugin will not work until you fix this problem.", QLatin1String("<b>") + name + QLatin1String("</b>")); 0219 } 0220 0221 } 0222 0223 #include "moc_isourceformatter.cpp"