qsciapis.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // This module defines interface to the QsciAPIs class.
  2. //
  3. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of QScintilla.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. #ifndef QSCIAPIS_H
  20. #define QSCIAPIS_H
  21. #include <QList>
  22. #include <QObject>
  23. #include <QPair>
  24. #include <QStringList>
  25. #include <Qsci/qsciabstractapis.h>
  26. #include <Qsci/qsciglobal.h>
  27. #include <Qsci/qsciscintilla.h>
  28. class QsciAPIsPrepared;
  29. class QsciAPIsWorker;
  30. class QsciLexer;
  31. //! \brief The QsciAPIs class provies an implementation of the textual API
  32. //! information used in call tips and for auto-completion.
  33. //!
  34. //! Raw API information is read from one or more files. Each API function is
  35. //! described by a single line of text comprising the function's name, followed
  36. //! by the function's optional comma separated parameters enclosed in
  37. //! parenthesis, and finally followed by optional explanatory text.
  38. //!
  39. //! A function name may be followed by a `?' and a number. The number is used
  40. //! by auto-completion to display a registered QPixmap with the function name.
  41. //!
  42. //! All function names are used by auto-completion, but only those that include
  43. //! function parameters are used in call tips.
  44. //!
  45. //! QScintilla only deals with prepared API information and not the raw
  46. //! information described above. This is done so that large APIs can be
  47. //! handled while still being responsive to user input. The conversion of raw
  48. //! information to prepared information is time consuming (think tens of
  49. //! seconds) and implemented in a separate thread. Prepared information can
  50. //! be quickly saved to and loaded from files. Such files are portable between
  51. //! different architectures.
  52. //!
  53. //! QScintilla based applications that want to support large APIs would
  54. //! normally provide the user with the ability to specify a set of, possibly
  55. //! project specific, raw API files and convert them to prepared files that are
  56. //! loaded quickly when the application is invoked.
  57. class QSCINTILLA_EXPORT QsciAPIs : public QsciAbstractAPIs
  58. {
  59. Q_OBJECT
  60. public:
  61. //! Constructs a QsciAPIs instance attached to lexer \a lexer. \a lexer
  62. //! becomes the instance's parent object although the instance can also be
  63. //! subsequently attached to other lexers.
  64. QsciAPIs(QsciLexer *lexer);
  65. //! Destroy the QsciAPIs instance.
  66. virtual ~QsciAPIs();
  67. //! Add the single raw API entry \a entry to the current set.
  68. //!
  69. //! \sa clear(), load(), remove()
  70. void add(const QString &entry);
  71. //! Deletes all raw API information.
  72. //!
  73. //! \sa add(), load(), remove()
  74. void clear();
  75. //! Load the API information from the file named \a filename, adding it to
  76. //! the current set. Returns true if successful, otherwise false.
  77. bool load(const QString &filename);
  78. //! Remove the single raw API entry \a entry from the current set.
  79. //!
  80. //! \sa add(), clear(), load()
  81. void remove(const QString &entry);
  82. //! Convert the current raw API information to prepared API information.
  83. //! This is implemented by a separate thread.
  84. //!
  85. //! \sa cancelPreparation()
  86. void prepare();
  87. //! Cancel the conversion of the current raw API information to prepared
  88. //! API information.
  89. //!
  90. //! \sa prepare()
  91. void cancelPreparation();
  92. //! Return the default name of the prepared API information file. It is
  93. //! based on the name of the associated lexer and in the directory defined
  94. //! by the QSCIDIR environment variable. If the environment variable isn't
  95. //! set then $HOME/.qsci is used.
  96. QString defaultPreparedName() const;
  97. //! Check to see is a prepared API information file named \a filename
  98. //! exists. If \a filename is empty then the value returned by
  99. //! defaultPreparedName() is used. Returns true if successful, otherwise
  100. //! false.
  101. //!
  102. //! \sa defaultPreparedName()
  103. bool isPrepared(const QString &filename = QString()) const;
  104. //! Load the prepared API information from the file named \a filename. If
  105. //! \a filename is empty then a name is constructed based on the name of
  106. //! the associated lexer and saved in the directory defined by the QSCIDIR
  107. //! environment variable. If the environment variable isn't set then
  108. //! $HOME/.qsci is used. Returns true if successful, otherwise false.
  109. bool loadPrepared(const QString &filename = QString());
  110. //! Save the prepared API information to the file named \a filename. If
  111. //! \a filename is empty then a name is constructed based on the name of
  112. //! the associated lexer and saved in the directory defined by the QSCIDIR
  113. //! environment variable. If the environment variable isn't set then
  114. //! $HOME/.qsci is used. Returns true if successful, otherwise false.
  115. bool savePrepared(const QString &filename = QString()) const;
  116. //! \reimp
  117. virtual void updateAutoCompletionList(const QStringList &context,
  118. QStringList &list);
  119. //! \reimp
  120. virtual void autoCompletionSelected(const QString &sel);
  121. //! \reimp
  122. virtual QStringList callTips(const QStringList &context, int commas,
  123. QsciScintilla::CallTipsStyle style, QList<int> &shifts);
  124. //! \internal Reimplemented to receive termination events from the worker
  125. //! thread.
  126. virtual bool event(QEvent *e);
  127. //! Return a list of the installed raw API file names for the associated
  128. //! lexer.
  129. QStringList installedAPIFiles() const;
  130. signals:
  131. //! This signal is emitted when the conversion of raw API information to
  132. //! prepared API information has been cancelled.
  133. //!
  134. //! \sa apiPreparationFinished(), apiPreparationStarted()
  135. void apiPreparationCancelled();
  136. //! This signal is emitted when the conversion of raw API information to
  137. //! prepared API information starts and can be used to give some visual
  138. //! feedback to the user.
  139. //!
  140. //! \sa apiPreparationCancelled(), apiPreparationFinished()
  141. void apiPreparationStarted();
  142. //! This signal is emitted when the conversion of raw API information to
  143. //! prepared API information has finished.
  144. //!
  145. //! \sa apiPreparationCancelled(), apiPreparationStarted()
  146. void apiPreparationFinished();
  147. private:
  148. friend class QsciAPIsPrepared;
  149. friend class QsciAPIsWorker;
  150. // This indexes a word in a set of raw APIs. The first part indexes the
  151. // entry in the set, the second part indexes the word within the entry.
  152. typedef QPair<quint32, quint32> WordIndex;
  153. // This is a list of word indexes.
  154. typedef QList<WordIndex> WordIndexList;
  155. QsciAPIsWorker *worker;
  156. QStringList old_context;
  157. QStringList::const_iterator origin;
  158. int origin_len;
  159. QString unambiguous_context;
  160. QStringList apis;
  161. QsciAPIsPrepared *prep;
  162. static bool enoughCommas(const QString &s, int commas);
  163. QStringList positionOrigin(const QStringList &context, QString &path);
  164. bool originStartsWith(const QString &path, const QString &wsep);
  165. const WordIndexList *wordIndexOf(const QString &word) const;
  166. void lastCompleteWord(const QString &word, QStringList &with_context,
  167. bool &unambig);
  168. void lastPartialWord(const QString &word, QStringList &with_context,
  169. bool &unambig);
  170. void addAPIEntries(const WordIndexList &wl, bool complete,
  171. QStringList &with_context, bool &unambig);
  172. QString prepName(const QString &filename, bool mkpath = false) const;
  173. void deleteWorker();
  174. QsciAPIs(const QsciAPIs &);
  175. QsciAPIs &operator=(const QsciAPIs &);
  176. };
  177. #endif