File indexing completed on 2023-05-30 09:03:12
0001 /* 0002 SPDX-License-Identifier: GPL-2.0-or-later 0003 SPDX-FileCopyrightText: 2011 Filipe Saraiva <filipe@kde.org> 0004 */ 0005 0006 #include "pythonkeywords.h" 0007 0008 #include <QFile> 0009 #include <QDebug> 0010 0011 #include <KSyntaxHighlighting/Repository> 0012 #include <KSyntaxHighlighting/Definition> 0013 0014 PythonKeywords::PythonKeywords() 0015 { 0016 qDebug() << "PythonKeywords constructor"; 0017 0018 } 0019 0020 PythonKeywords* PythonKeywords::instance() 0021 { 0022 static PythonKeywords* inst = nullptr; 0023 if(inst == nullptr) 0024 { 0025 inst = new PythonKeywords(); 0026 inst->loadKeywords(); 0027 } 0028 0029 return inst; 0030 } 0031 0032 void PythonKeywords::loadKeywords() 0033 { 0034 KSyntaxHighlighting::Repository m_repository; 0035 KSyntaxHighlighting::Definition definition = m_repository.definitionForName(QLatin1String("Python")); 0036 0037 m_keywords << definition.keywordList(QLatin1String("import")); 0038 m_keywords << definition.keywordList(QLatin1String("defs")); 0039 m_keywords << definition.keywordList(QLatin1String("operators")); 0040 m_keywords << definition.keywordList(QLatin1String("flow")); 0041 0042 m_functions << definition.keywordList(QLatin1String("builtinfuncs")); 0043 m_functions << definition.keywordList(QLatin1String("overloaders")); 0044 0045 m_variables << definition.keywordList(QLatin1String("specialvars")); 0046 0047 // We use qBinarySearch in PythonCompletetionObject for type fetching 0048 std::sort(m_keywords.begin(), m_keywords.end()); 0049 std::sort(m_functions.begin(), m_functions.end()); 0050 std::sort(m_variables.begin(), m_variables.end()); 0051 } 0052 0053 void PythonKeywords::loadFromModule(const QString& module, const QStringList& keywords) 0054 { 0055 qDebug() << "Module imported" << module; 0056 0057 if (module.isEmpty()){ 0058 for(int contKeyword = 0; contKeyword < keywords.size(); contKeyword++){ 0059 m_functions << keywords.at(contKeyword); 0060 } 0061 } else { 0062 m_variables << module; 0063 0064 for(int contKeyword = 0; contKeyword < keywords.size(); contKeyword++){ 0065 m_functions << module + QLatin1String(".") + keywords.at(contKeyword); 0066 } 0067 } 0068 } 0069 0070 const QStringList& PythonKeywords::variables() const 0071 { 0072 return m_variables; 0073 } 0074 0075 const QStringList& PythonKeywords::functions() const 0076 { 0077 return m_functions; 0078 } 0079 0080 const QStringList& PythonKeywords::keywords() const 0081 { 0082 return m_keywords; 0083 }