1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "texteditor_global.h"
29 #include "tabsettings.h"
30 
31 #include <QString>
32 
33 QT_BEGIN_NAMESPACE
34 class QTextBlock;
35 class QTextCursor;
36 QT_END_NAMESPACE
37 
38 namespace TextEditor {
39 
40 class TEXTEDITOR_EXPORT AutoCompleter
41 {
42 public:
43     AutoCompleter();
44     virtual ~AutoCompleter();
45 
setAutoInsertBracketsEnabled(bool b)46     void setAutoInsertBracketsEnabled(bool b) { m_autoInsertBrackets = b; }
isAutoInsertBracketsEnabled()47     bool isAutoInsertBracketsEnabled() const { return m_autoInsertBrackets; }
setSurroundWithBracketsEnabled(bool b)48     void setSurroundWithBracketsEnabled(bool b) { m_surroundWithBrackets = b; }
isSurroundWithBracketsEnabled()49     bool isSurroundWithBracketsEnabled() const { return m_surroundWithBrackets; }
50 
setAutoInsertQuotesEnabled(bool b)51     void setAutoInsertQuotesEnabled(bool b) { m_autoInsertQuotes = b; }
isAutoInsertQuotesEnabled()52     bool isAutoInsertQuotesEnabled() const { return m_autoInsertQuotes; }
setSurroundWithQuotesEnabled(bool b)53     void setSurroundWithQuotesEnabled(bool b) { m_surroundWithQuotes = b; }
isSurroundWithQuotesEnabled()54     bool isSurroundWithQuotesEnabled() const { return m_surroundWithQuotes; }
55 
setOverwriteClosingCharsEnabled(bool b)56     void setOverwriteClosingCharsEnabled(bool b) { m_overwriteClosingChars = b; }
isOverwriteClosingCharsEnabled()57     bool isOverwriteClosingCharsEnabled() const { return m_overwriteClosingChars; }
58 
setTabSettings(const TabSettings & tabSettings)59     void setTabSettings(const TabSettings &tabSettings) { m_tabSettings = tabSettings; }
tabSettings()60     const TabSettings &tabSettings() const { return m_tabSettings; }
61 
62     // Returns the text to complete at the cursor position, or an empty string
63     virtual QString autoComplete(QTextCursor &cursor, const QString &text, bool skipChars) const;
64 
65     // Handles backspace. When returning true, backspace processing is stopped
66     virtual bool autoBackspace(QTextCursor &cursor);
67 
68     // Hook to insert special characters on enter. Returns the number of extra blocks inserted.
69     virtual int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor);
70 
71     virtual bool contextAllowsAutoBrackets(const QTextCursor &cursor,
72                                               const QString &textToInsert = QString()) const;
73     virtual bool contextAllowsAutoQuotes(const QTextCursor &cursor,
74                                          const QString &textToInsert = QString()) const;
75     virtual bool contextAllowsElectricCharacters(const QTextCursor &cursor) const;
76 
77     // Returns true if the cursor is inside a comment.
78     virtual bool isInComment(const QTextCursor &cursor) const;
79 
80     // Returns true if the cursor is inside a string.
81     virtual bool isInString(const QTextCursor &cursor) const;
82 
83     virtual QString insertMatchingBrace(const QTextCursor &cursor, const
84                                         QString &text,
85                                         QChar lookAhead, bool skipChars,
86                                         int *skippedChars) const;
87 
88     virtual QString insertMatchingQuote(const QTextCursor &cursor, const
89                                         QString &text,
90                                         QChar lookAhead, bool skipChars,
91                                         int *skippedChars) const;
92 
93     // Returns the text that needs to be inserted
94     virtual QString insertParagraphSeparator(const QTextCursor &cursor) const;
95 
96     static bool isQuote(const QString &text);
97     bool isNextBlockIndented(const QTextBlock &currentBlock) const;
98 
99 private:
100     QString replaceSelection(QTextCursor &cursor, const QString &textToInsert) const;
101 
102 private:
103     TabSettings m_tabSettings;
104     mutable bool m_allowSkippingOfBlockEnd;
105     bool m_autoInsertBrackets;
106     bool m_surroundWithBrackets;
107     bool m_autoInsertQuotes;
108     bool m_surroundWithQuotes;
109     bool m_overwriteClosingChars;
110 };
111 
112 } // TextEditor
113