File indexing completed on 2024-04-28 15:53:09

0001 /*
0002     SPDX-FileCopyrightText: 2013 Sven Brauch <svenbrauch@googlemail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef CORRECTIONFILEGENERATOR_H
0008 #define CORRECTIONFILEGENERATOR_H
0009 
0010 #include <QCache>
0011 #include <QStringList>
0012 
0013 #include <interfaces/context.h>
0014 #include <interfaces/contextmenuextension.h>
0015 #include <language/duchain/types/abstracttype.h>
0016 #include <language/duchain/declaration.h>
0017 #include <language/duchain/duchain.h>
0018 
0019 #include "parser/codehelpers.h"
0020 
0021 #include "ui_correctionwidget.h"
0022 
0023 namespace Python {
0024 
0025 class CorrectionFileGenerator;
0026 
0027 class TypeCorrection : public QObject
0028 {
0029     Q_OBJECT
0030 public:
0031     static TypeCorrection& self();
0032     void doContextMenu(KDevelop::ContextMenuExtension& extension, KDevelop::Context* context);
0033 
0034 public slots:
0035     void executeSpecifyTypeAction();
0036     void accepted();
0037 
0038 private:
0039     TypeCorrection();
0040 
0041     QScopedPointer<Ui::CorrectionWidget> m_ui;
0042 };
0043 
0044 class CorrectionFileGenerator
0045 {
0046 public:
0047     CorrectionFileGenerator(const QString& filePath);
0048 
0049     enum HintType {
0050         FunctionReturnHint,
0051         LocalVariableHint
0052     };
0053 
0054     enum StructureType {
0055         ClassType,
0056         FunctionType,
0057         MemberFunctionType
0058     };
0059 
0060     void addHint(const QString& typeCode, const QStringList &modules, KDevelop::Declaration* forDeclaration, HintType hintType);
0061 
0062 private:
0063     /// Find the given structure as far as it exists, and return the line number
0064     /// where more stuff can be added.
0065     /// You can pass an empty string for class or function, or both.
0066     int findStructureFor(const QString& klass, const QString& function);
0067 
0068     /// Create an empty structure part, such as
0069     /// class class_identifierSuffix:\n    pass
0070     QString createStructurePart(const QString& identifierSuffix, StructureType type);
0071 
0072     /// Checks if the syntax of the created file is still valid.
0073     bool checkForValidSyntax();
0074 
0075 private:
0076     /// The file which is being modified by this object
0077     QFile m_file;
0078     QString m_filePath;
0079     /// The last known-valid contents of the document
0080     QStringList m_oldContents;
0081     /// The current contents of the document to be written to disk later
0082     QStringList m_code;
0083 
0084     QScopedPointer<FileIndentInformation> m_fileIndents;
0085 
0086     static const int DEFAULT_INDENT_LEVEL = 4;
0087 };
0088 
0089 class CorrectionAssistant : public QDialog
0090 {
0091     Q_OBJECT
0092 public:
0093     CorrectionAssistant(KDevelop::IndexedDeclaration declaration, CorrectionFileGenerator::HintType hintType,
0094                         QWidget *parent = nullptr);
0095 
0096     KDevelop::IndexedDeclaration declaration() const;
0097     CorrectionFileGenerator::HintType hintType() const;
0098 
0099 private:
0100     KDevelop::IndexedDeclaration m_declaration;
0101     CorrectionFileGenerator::HintType m_hintType;
0102 };
0103 
0104 } // namespace python
0105 
0106 #endif // CORRECTIONFILEGENERATOR_H