better lexer name handling
This commit is contained in:
@@ -25,7 +25,7 @@ int DocumentManager::createNewDocument()
|
|||||||
doc.filePath = QString();
|
doc.filePath = QString();
|
||||||
doc.content = QString();
|
doc.content = QString();
|
||||||
doc.encoding = "UTF-8";
|
doc.encoding = "UTF-8";
|
||||||
doc.language = "Plain Text";
|
doc.language = QString();
|
||||||
doc.modified = false;
|
doc.modified = false;
|
||||||
doc.untitled = true;
|
doc.untitled = true;
|
||||||
|
|
||||||
@@ -171,7 +171,7 @@ QString DocumentManager::getDocumentLanguage(int docId) const
|
|||||||
if (m_documents.contains(docId)) {
|
if (m_documents.contains(docId)) {
|
||||||
return m_documents[docId].language;
|
return m_documents[docId].language;
|
||||||
}
|
}
|
||||||
return "Plain Text";
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DocumentManager::isDocumentModified(int docId) const
|
bool DocumentManager::isDocumentModified(int docId) const
|
||||||
@@ -247,29 +247,51 @@ QString DocumentManager::generateUntitledName()
|
|||||||
QString DocumentManager::detectLanguageFromExtension(const QString &filePath)
|
QString DocumentManager::detectLanguageFromExtension(const QString &filePath)
|
||||||
{
|
{
|
||||||
QFileInfo fileInfo(filePath);
|
QFileInfo fileInfo(filePath);
|
||||||
QString extension = fileInfo.suffix().toLower();
|
const QString fileName = fileInfo.fileName();
|
||||||
|
const QString extension = fileInfo.suffix().toLower();
|
||||||
if (extension == "cpp" || extension == "cxx" || extension == "cc" || extension == "c++") {
|
|
||||||
return "C++";
|
if (fileName.compare(QStringLiteral("CMakeLists.txt"), Qt::CaseInsensitive) == 0) {
|
||||||
} else if (extension == "c") {
|
return QStringLiteral("cmake");
|
||||||
return "C";
|
|
||||||
} else if (extension == "h" || extension == "hpp" || extension == "hxx") {
|
|
||||||
return "C/C++ Header";
|
|
||||||
} else if (extension == "py") {
|
|
||||||
return "Python";
|
|
||||||
} else if (extension == "js") {
|
|
||||||
return "JavaScript";
|
|
||||||
} else if (extension == "html" || extension == "htm") {
|
|
||||||
return "HTML";
|
|
||||||
} else if (extension == "css") {
|
|
||||||
return "CSS";
|
|
||||||
} else if (extension == "xml") {
|
|
||||||
return "XML";
|
|
||||||
} else if (extension == "json") {
|
|
||||||
return "JSON";
|
|
||||||
} else if (extension == "txt") {
|
|
||||||
return "Plain Text";
|
|
||||||
} else {
|
|
||||||
return "Plain Text";
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if (extension == QStringLiteral("cpp") || extension == QStringLiteral("cxx") ||
|
||||||
|
extension == QStringLiteral("cc") || extension == QStringLiteral("c++") ||
|
||||||
|
extension == QStringLiteral("c") || extension == QStringLiteral("h") ||
|
||||||
|
extension == QStringLiteral("hpp") || extension == QStringLiteral("hxx")) {
|
||||||
|
return QStringLiteral("cpp");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == QStringLiteral("py")) {
|
||||||
|
return QStringLiteral("python");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == QStringLiteral("html") || extension == QStringLiteral("htm")) {
|
||||||
|
return QStringLiteral("hypertext");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == QStringLiteral("xml") || extension == QStringLiteral("xsd") || extension == QStringLiteral("xsl")) {
|
||||||
|
return QStringLiteral("xml");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == QStringLiteral("json")) {
|
||||||
|
return QStringLiteral("json");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == QStringLiteral("css")) {
|
||||||
|
return QStringLiteral("css");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == QStringLiteral("cmake")) {
|
||||||
|
return QStringLiteral("cmake");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == QStringLiteral("yaml")) {
|
||||||
|
return QStringLiteral("yaml");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == QStringLiteral("ps1") || extension == QStringLiteral("psm1") || extension == QStringLiteral("psd1")) {
|
||||||
|
return QStringLiteral("powershell");
|
||||||
|
}
|
||||||
|
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,8 +33,12 @@ QString LexerGroupActionMenu::displayName(const QString &lexerName)
|
|||||||
{
|
{
|
||||||
static const QHash<QString, QString> displayNameMap {
|
static const QHash<QString, QString> displayNameMap {
|
||||||
{"cpp", "C++"},
|
{"cpp", "C++"},
|
||||||
|
{"python", "Python"},
|
||||||
{"cmake", "CMake"},
|
{"cmake", "CMake"},
|
||||||
{"hypertext", "HTML"},
|
{"hypertext", "HTML"},
|
||||||
|
{"xml", "XML"},
|
||||||
|
{"json", "JSON"},
|
||||||
|
{"css", "CSS"},
|
||||||
{"powershell", "PowerShell"}
|
{"powershell", "PowerShell"}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -111,6 +111,11 @@ void MainWindow::setupActions()
|
|||||||
connect(lexerNoneAction, &QAction::triggered, this, [this](){
|
connect(lexerNoneAction, &QAction::triggered, this, [this](){
|
||||||
if (SciEdit *editor = m_tabWidget->currentEditor()) {
|
if (SciEdit *editor = m_tabWidget->currentEditor()) {
|
||||||
editor->setLexer(nullptr);
|
editor->setLexer(nullptr);
|
||||||
|
const int docId = m_tabWidget->currentDocumentId();
|
||||||
|
if (docId >= 0) {
|
||||||
|
m_documentManager->setDocumentLanguage(docId, QString());
|
||||||
|
}
|
||||||
|
updateStatusBar();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -219,6 +224,11 @@ void MainWindow::applyLexer(const QString &lexerName)
|
|||||||
if (SciEdit *editor = m_tabWidget->currentEditor()) {
|
if (SciEdit *editor = m_tabWidget->currentEditor()) {
|
||||||
Scintilla::ILexer5 * lexer = CreateLexer(lexerName.toStdString().c_str());
|
Scintilla::ILexer5 * lexer = CreateLexer(lexerName.toStdString().c_str());
|
||||||
editor->setLexer(lexer);
|
editor->setLexer(lexer);
|
||||||
|
const int docId = m_tabWidget->currentDocumentId();
|
||||||
|
if (docId >= 0) {
|
||||||
|
m_documentManager->setDocumentLanguage(docId, lexerName);
|
||||||
|
}
|
||||||
|
updateStatusBar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,7 +336,8 @@ void MainWindow::updateStatusBar()
|
|||||||
int docId = m_tabWidget->currentDocumentId();
|
int docId = m_tabWidget->currentDocumentId();
|
||||||
if (docId >= 0) {
|
if (docId >= 0) {
|
||||||
QString encoding = m_documentManager->getDocumentEncoding(docId);
|
QString encoding = m_documentManager->getDocumentEncoding(docId);
|
||||||
QString language = m_documentManager->getDocumentLanguage(docId);
|
const QString lexerName = m_documentManager->getDocumentLanguage(docId);
|
||||||
|
const QString language = lexerName.isEmpty() ? QStringLiteral("Plain Text") : LexerGroupActionMenu::displayName(lexerName);
|
||||||
|
|
||||||
m_encodingStatusLabel->setText(encoding);
|
m_encodingStatusLabel->setText(encoding);
|
||||||
m_languageStatusLabel->setText(language);
|
m_languageStatusLabel->setText(language);
|
||||||
|
|||||||
@@ -10,32 +10,6 @@
|
|||||||
#include <ILexer.h>
|
#include <ILexer.h>
|
||||||
#include <Lexilla.h>
|
#include <Lexilla.h>
|
||||||
|
|
||||||
static QString lexerNameForDocumentLanguage(const QString &language)
|
|
||||||
{
|
|
||||||
if (language == QStringLiteral("C++") ||
|
|
||||||
language == QStringLiteral("C") ||
|
|
||||||
language == QStringLiteral("C/C++ Header")) {
|
|
||||||
return QStringLiteral("cpp");
|
|
||||||
}
|
|
||||||
if (language == QStringLiteral("Python")) {
|
|
||||||
return QStringLiteral("python");
|
|
||||||
}
|
|
||||||
if (language == QStringLiteral("HTML")) {
|
|
||||||
return QStringLiteral("hypertext");
|
|
||||||
}
|
|
||||||
if (language == QStringLiteral("XML")) {
|
|
||||||
return QStringLiteral("xml");
|
|
||||||
}
|
|
||||||
if (language == QStringLiteral("JSON")) {
|
|
||||||
return QStringLiteral("json");
|
|
||||||
}
|
|
||||||
if (language == QStringLiteral("CSS")) {
|
|
||||||
return QStringLiteral("css");
|
|
||||||
}
|
|
||||||
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
|
|
||||||
TabWidget::TabWidget(DocumentManager *documentManager, QWidget *parent)
|
TabWidget::TabWidget(DocumentManager *documentManager, QWidget *parent)
|
||||||
: QTabWidget(parent)
|
: QTabWidget(parent)
|
||||||
, m_documentManager(documentManager)
|
, m_documentManager(documentManager)
|
||||||
@@ -107,7 +81,7 @@ int TabWidget::openDocument(const QString &filePath)
|
|||||||
editor->setEditorFont(AppSettings::editorFont());
|
editor->setEditorFont(AppSettings::editorFont());
|
||||||
editor->applyTheme(AppSettings::editorDarkTheme());
|
editor->applyTheme(AppSettings::editorDarkTheme());
|
||||||
|
|
||||||
const QString lexerName = lexerNameForDocumentLanguage(m_documentManager->getDocumentLanguage(docId));
|
const QString lexerName = m_documentManager->getDocumentLanguage(docId);
|
||||||
if (lexerName.isEmpty()) {
|
if (lexerName.isEmpty()) {
|
||||||
editor->setLexer(nullptr);
|
editor->setLexer(nullptr);
|
||||||
} else {
|
} else {
|
||||||
@@ -179,7 +153,7 @@ bool TabWidget::saveCurrentDocumentAs()
|
|||||||
|
|
||||||
const bool saved = m_documentManager->saveDocumentAs(docId, fileName);
|
const bool saved = m_documentManager->saveDocumentAs(docId, fileName);
|
||||||
if (saved) {
|
if (saved) {
|
||||||
const QString lexerName = lexerNameForDocumentLanguage(m_documentManager->getDocumentLanguage(docId));
|
const QString lexerName = m_documentManager->getDocumentLanguage(docId);
|
||||||
if (lexerName.isEmpty()) {
|
if (lexerName.isEmpty()) {
|
||||||
editor->setLexer(nullptr);
|
editor->setLexer(nullptr);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user