itom  4.1.0
pyDocstringGenerator.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet 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 
53 namespace ito {
54 
55 /*
56 Contains a generator for a template of a docstring of the current method or class.
57 */
58 
59 class PyDocstringGeneratorMode : public QObject, public Mode
60 {
61  Q_OBJECT
62 public:
63  enum Style
64  {
65  GoogleStyle = 0,
66  NumpyStyle = 1
67  };
68 
69  PyDocstringGeneratorMode(const QString &name, const QString &description = "", QObject *parent = nullptr);
70  virtual ~PyDocstringGeneratorMode();
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 
80 protected:
81 
82  struct ArgInfo
83  {
84  ArgInfo(const QString &name = "", const QString &type = "", bool isOptional = false) :
85  m_name(name),
86  m_type(type),
87  m_isOptional(isOptional)
88  {
89 
90  }
91 
92  QString m_name;
93  QString m_type;
94  bool m_isOptional;
95  };
96 
97  struct FunctionInfo
98  {
99  FunctionInfo() :
100  m_hasYield(false)
101  {
102 
103  }
104 
105  QList<ArgInfo> m_args;
106  QList<QString> m_returnTypes;
107  bool m_hasYield;
108  QList<QString> m_raises;
109  };
110 
111  int lastLineIdxOfDefinition(const QSharedPointer<OutlineItem> &item) const;
112  FunctionInfo parseFunctionInfo(const QSharedPointer<OutlineItem> &item, int lastLineIdxOfDefinition) const;
113  void parseArgList(const QSharedPointer<OutlineItem> &item, FunctionInfo &info) const;
114  QString generateGoogleDoc(const QSharedPointer<OutlineItem> &item, const FunctionInfo &info, int &cursorPos) const;
115  QString generateNumpyDoc(const QSharedPointer<OutlineItem> &item, const FunctionInfo &info, int &cursorPos) const;
116 
117  QSharedPointer<QMenu> m_popupMenu;
118  Style m_docstringStyle;
119  int m_overwriteEndLineIndex;
120 
121 private slots:
122  void onKeyPressed(QKeyEvent *e);
123  void mnuInsertDocstring();
124 };
125 
126 } //end namespace ito
127 
128 #endif
Definition: pyDocstringGenerator.h:97
Definition: pyDocstringGenerator.h:59
Definition: apiFunctionsGraph.cpp:39
Definition: pyDocstringGenerator.h:82
Definition: mode.h:69