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 "formatter.h"
30 #include "indenter.h"
31 
32 #include <coreplugin/textdocument.h>
33 
34 #include <utils/id.h>
35 #include <utils/link.h>
36 
37 #include <QList>
38 #include <QMap>
39 #include <QSharedPointer>
40 
41 #include <functional>
42 
43 QT_BEGIN_NAMESPACE
44 class QAction;
45 class QTextCursor;
46 class QTextDocument;
47 QT_END_NAMESPACE
48 
49 namespace TextEditor {
50 
51 class CompletionAssistProvider;
52 class ExtraEncodingSettings;
53 class FontSettings;
54 class IAssistProvider;
55 class StorageSettings;
56 class SyntaxHighlighter;
57 class TabSettings;
58 class TextDocumentPrivate;
59 class TextMark;
60 class TypingSettings;
61 
62 using TextMarks = QList<TextMark *>;
63 
64 class TEXTEDITOR_EXPORT TextDocument : public Core::BaseTextDocument
65 {
66     Q_OBJECT
67 
68 public:
69     explicit TextDocument(Utils::Id id = Utils::Id());
70     ~TextDocument() override;
71 
72     static QMap<QString, QString> openedTextDocumentContents();
73     static QMap<QString, QTextCodec *> openedTextDocumentEncodings();
74     static TextDocument *currentTextDocument();
75     static TextDocument *textDocumentForFilePath(const Utils::FilePath &filePath);
76 
77     virtual QString plainText() const;
78     virtual QString textAt(int pos, int length) const;
79     virtual QChar characterAt(int pos) const;
80 
81     void setTypingSettings(const TypingSettings &typingSettings);
82     void setStorageSettings(const StorageSettings &storageSettings);
83     void setExtraEncodingSettings(const ExtraEncodingSettings &extraEncodingSettings);
84 
85     const TypingSettings &typingSettings() const;
86     const StorageSettings &storageSettings() const;
87     virtual TabSettings tabSettings() const;
88     const ExtraEncodingSettings &extraEncodingSettings() const;
89     const FontSettings &fontSettings() const;
90 
91     void setIndenter(Indenter *indenter);
92     Indenter *indenter() const;
93     void autoIndent(const QTextCursor &cursor,
94                     QChar typedChar = QChar::Null,
95                     int currentCursorPosition = -1);
96     void autoReindent(const QTextCursor &cursor, int currentCursorPosition = -1);
97     void autoFormatOrIndent(const QTextCursor &cursor);
98     QTextCursor indent(const QTextCursor &cursor, bool blockSelection, int column, int *offset);
99     QTextCursor unindent(const QTextCursor &cursor, bool blockSelection = false, int column = 0,
100                          int *offset = nullptr);
101 
102     void setFormatter(Formatter *indenter); // transfers ownership
103     void autoFormat(const QTextCursor &cursor);
104     bool applyChangeSet(const Utils::ChangeSet &changeSet);
105 
106     TextMarks marks() const;
107     bool addMark(TextMark *mark);
108     TextMarks marksAt(int line) const;
109     void removeMark(TextMark *mark);
110     void updateMark(TextMark *mark);
111     void moveMark(TextMark *mark, int previousLine);
112     void removeMarkFromMarksCache(TextMark *mark);
113 
114     // IDocument implementation.
115     bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;
116     QByteArray contents() const override;
117     bool setContents(const QByteArray &contents) override;
118     bool shouldAutoSave() const override;
119     bool isModified() const override;
120     bool isSaveAsAllowed() const override;
121     bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override;
122     void setFilePath(const Utils::FilePath &newName) override;
123 
124     QString fallbackSaveAsPath() const override;
125     QString fallbackSaveAsFileName() const override;
126 
127     void setFallbackSaveAsPath(const QString &fallbackSaveAsPath);
128     void setFallbackSaveAsFileName(const QString &fallbackSaveAsFileName);
129 
130     OpenResult open(QString *errorString, const Utils::FilePath &filePath,
131                     const Utils::FilePath &realFilePath) override;
132     virtual bool reload(QString *errorString);
133     bool reload(QString *errorString, const Utils::FilePath &realFilePath);
134 
135     bool setPlainText(const QString &text);
136     QTextDocument *document() const;
137     void setSyntaxHighlighter(SyntaxHighlighter *highlighter);
138     SyntaxHighlighter *syntaxHighlighter() const;
139 
140     bool reload(QString *errorString, QTextCodec *codec);
141     void cleanWhitespace(const QTextCursor &cursor);
142 
143     virtual void triggerPendingUpdates();
144 
145     void setCompletionAssistProvider(CompletionAssistProvider *provider);
146     virtual CompletionAssistProvider *completionAssistProvider() const;
147     void setFunctionHintAssistProvider(CompletionAssistProvider *provider);
148     virtual CompletionAssistProvider *functionHintAssistProvider() const;
149     void setQuickFixAssistProvider(IAssistProvider *provider) const;
150     virtual IAssistProvider *quickFixAssistProvider() const;
151 
152     void setTabSettings(const TextEditor::TabSettings &tabSettings);
153     void setFontSettings(const TextEditor::FontSettings &fontSettings);
154 
155     static QAction *createDiffAgainstCurrentFileAction(QObject *parent,
156         const std::function<Utils::FilePath()> &filePath);
157 
158 signals:
159     void aboutToOpen(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath);
160     void openFinishedSuccessfully();
161     void contentsChangedWithPosition(int position, int charsRemoved, int charsAdded);
162     void tabSettingsChanged();
163     void fontSettingsChanged();
164     void markRemoved(TextMark *mark);
165 
166 protected:
167     virtual void applyFontSettings();
168 
169 private:
170     OpenResult openImpl(QString *errorString,
171                         const Utils::FilePath &filePath,
172                         const Utils::FilePath &realFileName,
173                         bool reload);
174     void cleanWhitespace(QTextCursor &cursor, bool inEntireDocument, bool cleanIndentation);
175     void ensureFinalNewLine(QTextCursor &cursor);
176     void modificationChanged(bool modified);
177     void updateLayout() const;
178 
179     TextDocumentPrivate *d;
180 };
181 
182 using TextDocumentPtr = QSharedPointer<TextDocument>;
183 
184 } // namespace TextEditor
185