itom
Loading...
Searching...
No Matches
pyDocstringGenerator.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität Stuttgart, Germany
6
7 This file is part of itom.
8
9 itom is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public Licence as published by
11 the Free Software Foundation; either version 2 of the Licence, or (at
12 your option) any later version.
13
14 itom is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17 General Public Licence for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with itom. If not, see <http://www.gnu.org/licenses/>.
21
22 Further hints:
23 ------------------------
24
25 This file belongs to the code editor of itom. The code editor is
26 in major parts a fork / rewritten version of the python-based source
27 code editor PyQode from Colin Duquesnoy and others
28 (see https://github.com/pyQode). PyQode itself is licensed under
29 the MIT License (MIT).
30
31 Some parts of the code editor of itom are also inspired by the
32 source code editor of the Spyder IDE (https://github.com/spyder-ide),
33 also licensed under the MIT License and developed by the Spyder Project
34 Contributors.
35
36*********************************************************************** */
37
38#ifndef PYDOCSTRINGGENERATOR_H
39#define PYDOCSTRINGGENERATOR_H
40
41#include "../utils/utils.h"
42#include "../mode.h"
43#include "../../models/outlineItem.h"
44#include <qevent.h>
45#include <qobject.h>
46#include <qtextcursor.h>
47#include <qstring.h>
48#include <qlist.h>
49#include <qsharedpointer.h>
50#include <qmenu.h>
51#include <qaction.h>
52
53namespace ito {
54
55/*
56Contains a generator for a template of a docstring of the current method or class.
57*/
58
59class PyDocstringGeneratorMode : public QObject, public Mode
60{
61 Q_OBJECT
62public:
63 enum Style
64 {
65 GoogleStyle = 0,
66 NumpyStyle = 1
67 };
68
69 PyDocstringGeneratorMode(const QString &name, const QString &description = "", QObject *parent = nullptr);
71
72 virtual void onStateChanged(bool state);
73
74 void insertDocstring(const QTextCursor &cursor, const QString &quotes = "\"\"\"", bool insertOpeningQuotes = true, int overwriteEndLineIdx = -1) const;
75 QSharedPointer<OutlineItem> getOutlineOfLineIdx(int lineIdx) const;
76
77 Style docstringStyle() const { return m_docstringStyle; }
78 void setDocstringStyle(const Style &style) { m_docstringStyle = style; }
79
80protected:
81
82 struct ArgInfo
83 {
84 ArgInfo(
85 const QString& name = "",
86 const QString& type = "",
87 const QString& defaultValue = "", bool isOptional = false) :
88 m_name(name),
89 m_type(type),
90 m_defaultValue(defaultValue),
91 m_isOptional(isOptional)
92 {
93
94 }
95
96 QString m_name;
97 QString m_type;
98 QString m_defaultValue;
99 bool m_isOptional;
100 };
101
103 {
104 FunctionInfo() :
105 m_hasYield(false)
106 {
107
108 }
109
110 QList<ArgInfo> m_args;
111 QList<QString> m_returnTypes;
112 bool m_hasYield;
113 QList<QString> m_raises;
114 };
115
116 int lastLineIdxOfDefinition(const QSharedPointer<OutlineItem> &item) const;
117 FunctionInfo parseFunctionInfo(const QSharedPointer<OutlineItem> &item, int lastLineIdxOfDefinition) const;
118 void parseArgList(const QSharedPointer<OutlineItem> &item, FunctionInfo &info) const;
119 QString generateGoogleDoc(const QSharedPointer<OutlineItem> &item, const FunctionInfo &info, int &cursorPos) const;
120 QString generateNumpyDoc(const QSharedPointer<OutlineItem> &item, const FunctionInfo &info, int &cursorPos) const;
121
122 QSharedPointer<QMenu> m_popupMenu;
123 Style m_docstringStyle;
124 int m_overwriteEndLineIndex;
125
126private slots:
127 void onKeyPressed(QKeyEvent *e);
128 void mnuInsertDocstring();
129};
130
131} //end namespace ito
132
133#endif
Definition mode.h:70
Definition pyDocstringGenerator.h:60
Definition apiFunctionsGraph.cpp:40
Definition pyDocstringGenerator.h:83
Definition pyDocstringGenerator.h:103