2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "basetextdocument.h"
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
#include "basetextdocumentlayout.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "basetexteditor.h"
|
2013-04-18 18:21:17 +02:00
|
|
|
#include "convenience.h"
|
2011-08-16 10:45:23 +02:00
|
|
|
#include "typingsettings.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "storagesettings.h"
|
2010-10-29 15:20:10 +02:00
|
|
|
#include "tabsettings.h"
|
2011-02-01 14:13:54 +01:00
|
|
|
#include "extraencodingsettings.h"
|
2010-07-09 14:47:18 +02:00
|
|
|
#include "syntaxhighlighter.h"
|
2011-02-04 14:55:50 +01:00
|
|
|
#include "texteditorconstants.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-01-29 13:55:55 +01:00
|
|
|
#include <QApplication>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QFutureInterface>
|
2013-01-29 13:55:55 +01:00
|
|
|
#include <QScrollBar>
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QTextCodec>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-02-04 10:33:12 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2011-02-04 14:55:50 +01:00
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
2008-12-09 15:25:01 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-08-30 09:22:42 +02:00
|
|
|
using namespace Core;
|
|
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
namespace TextEditor {
|
2010-10-29 15:36:49 +02:00
|
|
|
class BaseTextDocumentPrivate
|
|
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
public:
|
|
|
|
|
explicit BaseTextDocumentPrivate(BaseTextDocument *q);
|
|
|
|
|
|
|
|
|
|
QString m_defaultPath;
|
|
|
|
|
QString m_suggestedFileName;
|
|
|
|
|
QString m_mimeType;
|
2011-08-16 10:45:23 +02:00
|
|
|
TypingSettings m_typingSettings;
|
2010-10-29 15:20:10 +02:00
|
|
|
StorageSettings m_storageSettings;
|
|
|
|
|
TabSettings m_tabSettings;
|
2011-02-01 14:13:54 +01:00
|
|
|
ExtraEncodingSettings m_extraEncodingSettings;
|
2010-10-29 15:20:10 +02:00
|
|
|
QTextDocument *m_document;
|
|
|
|
|
SyntaxHighlighter *m_highlighter;
|
|
|
|
|
|
|
|
|
|
bool m_fileIsReadOnly;
|
2011-05-10 20:43:03 +02:00
|
|
|
int m_autoSaveRevision;
|
2010-10-29 15:20:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BaseTextDocumentPrivate::BaseTextDocumentPrivate(BaseTextDocument *q) :
|
|
|
|
|
m_document(new QTextDocument(q)),
|
|
|
|
|
m_highlighter(0),
|
|
|
|
|
m_fileIsReadOnly(false),
|
2011-05-10 20:43:03 +02:00
|
|
|
m_autoSaveRevision(-1)
|
2010-10-29 15:20:10 +02:00
|
|
|
{
|
|
|
|
|
}
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
BaseTextDocument::BaseTextDocument() : d(new BaseTextDocumentPrivate(this))
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseTextDocument::~BaseTextDocument()
|
|
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
delete d->m_document;
|
|
|
|
|
d->m_document = 0;
|
|
|
|
|
delete d;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-04-18 18:21:17 +02:00
|
|
|
QString BaseTextDocument::contents() const
|
|
|
|
|
{
|
|
|
|
|
return document()->toPlainText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BaseTextDocument::textAt(int pos, int length) const
|
|
|
|
|
{
|
|
|
|
|
return Convenience::textAt(QTextCursor(document()), pos, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QChar BaseTextDocument::characterAt(int pos) const
|
|
|
|
|
{
|
|
|
|
|
return document()->characterAt(pos);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QString BaseTextDocument::mimeType() const
|
|
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
return d->m_mimeType;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocument::setMimeType(const QString &mt)
|
|
|
|
|
{
|
2013-05-02 11:42:33 +02:00
|
|
|
if (d->m_mimeType != mt) {
|
|
|
|
|
d->m_mimeType = mt;
|
|
|
|
|
emit mimeTypeChanged();
|
|
|
|
|
}
|
2010-10-29 15:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
void BaseTextDocument::setTypingSettings(const TypingSettings &typingSettings)
|
|
|
|
|
{
|
|
|
|
|
d->m_typingSettings = typingSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
void BaseTextDocument::setStorageSettings(const StorageSettings &storageSettings)
|
|
|
|
|
{
|
|
|
|
|
d->m_storageSettings = storageSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
const TypingSettings &BaseTextDocument::typingSettings() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_typingSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
const StorageSettings &BaseTextDocument::storageSettings() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_storageSettings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocument::setTabSettings(const TabSettings &tabSettings)
|
|
|
|
|
{
|
|
|
|
|
d->m_tabSettings = tabSettings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TabSettings &BaseTextDocument::tabSettings() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_tabSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-01 14:13:54 +01:00
|
|
|
void BaseTextDocument::setExtraEncodingSettings(const ExtraEncodingSettings &extraEncodingSettings)
|
|
|
|
|
{
|
|
|
|
|
d->m_extraEncodingSettings = extraEncodingSettings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ExtraEncodingSettings &BaseTextDocument::extraEncodingSettings() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_extraEncodingSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
bool BaseTextDocument::isSaveAsAllowed() const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BaseTextDocument::defaultPath() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_defaultPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BaseTextDocument::suggestedFileName() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_suggestedFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocument::setDefaultPath(const QString &defaultPath)
|
|
|
|
|
{
|
|
|
|
|
d->m_defaultPath = defaultPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocument::setSuggestedFileName(const QString &suggestedFileName)
|
|
|
|
|
{
|
|
|
|
|
d->m_suggestedFileName = suggestedFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextDocument *BaseTextDocument::document() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_document;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SyntaxHighlighter *BaseTextDocument::syntaxHighlighter() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_highlighter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ITextMarkable *BaseTextDocument::documentMarker() const
|
|
|
|
|
{
|
2012-02-15 13:06:16 +01:00
|
|
|
BaseTextDocumentLayout *documentLayout =
|
|
|
|
|
qobject_cast<BaseTextDocumentLayout *>(d->m_document->documentLayout());
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(documentLayout, return 0);
|
2012-02-15 13:06:16 +01:00
|
|
|
return documentLayout->markableInterface();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-24 08:49:17 +01:00
|
|
|
/*!
|
|
|
|
|
* \brief Saves the document to the specified file.
|
|
|
|
|
* \param errorString output parameter, contains error reason.
|
|
|
|
|
* \param autoSave signalise that this function was called by the automatic save routine.
|
2013-02-13 12:14:09 +01:00
|
|
|
* If autosave is true, the cursor will be restored and some signals suppressed
|
|
|
|
|
* and we do not clean up the text file (cleanWhitespace(), ensureFinalNewLine()).
|
2013-01-24 08:49:17 +01:00
|
|
|
*/
|
2013-07-04 11:35:56 +02:00
|
|
|
bool BaseTextDocument::save(QString *errorString, const QString &saveFileName, bool autoSave)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
QTextCursor cursor(d->m_document);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-06-20 18:32:06 +02:00
|
|
|
// When autosaving, we don't want to modify the document/location under the user's fingers.
|
|
|
|
|
BaseTextEditorWidget *editorWidget = 0;
|
2011-07-21 09:12:10 +02:00
|
|
|
int savedPosition = 0;
|
|
|
|
|
int savedAnchor = 0;
|
2013-01-29 13:55:55 +01:00
|
|
|
int savedVScrollBarValue = 0;
|
|
|
|
|
int savedHScrollBarValue = 0;
|
2011-06-20 18:32:06 +02:00
|
|
|
int undos = d->m_document->availableUndoSteps();
|
|
|
|
|
|
2013-01-29 13:55:55 +01:00
|
|
|
// When saving the current editor, make sure to maintain the cursor and scroll bar
|
|
|
|
|
// positions for undo
|
2013-08-30 09:22:42 +02:00
|
|
|
IEditor *currentEditor = EditorManager::currentEditor();
|
2011-02-21 16:02:26 +01:00
|
|
|
if (BaseTextEditor *editable = qobject_cast<BaseTextEditor*>(currentEditor)) {
|
2012-02-14 16:43:51 +01:00
|
|
|
if (editable->document() == this) {
|
2011-06-20 18:32:06 +02:00
|
|
|
editorWidget = editable->editorWidget();
|
|
|
|
|
QTextCursor cur = editorWidget->textCursor();
|
|
|
|
|
savedPosition = cur.position();
|
|
|
|
|
savedAnchor = cur.anchor();
|
2013-01-29 13:55:55 +01:00
|
|
|
savedVScrollBarValue = editorWidget->verticalScrollBar()->value();
|
|
|
|
|
savedHScrollBarValue = editorWidget->horizontalScrollBar()->value();
|
2011-06-20 18:32:06 +02:00
|
|
|
cursor.setPosition(cur.position());
|
|
|
|
|
}
|
2010-07-07 13:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
2013-02-13 12:14:09 +01:00
|
|
|
if (!autoSave) {
|
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
|
cursor.movePosition(QTextCursor::Start);
|
2010-07-07 13:17:58 +02:00
|
|
|
|
2013-02-13 12:14:09 +01:00
|
|
|
if (d->m_storageSettings.m_cleanWhitespace)
|
|
|
|
|
cleanWhitespace(cursor, d->m_storageSettings.m_cleanIndentation, d->m_storageSettings.m_inEntireDocument);
|
|
|
|
|
if (d->m_storageSettings.m_addFinalNewLine)
|
|
|
|
|
ensureFinalNewLine(cursor);
|
|
|
|
|
cursor.endEditBlock();
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-07-04 13:30:26 +02:00
|
|
|
QString fName = filePath();
|
2013-07-04 11:35:56 +02:00
|
|
|
if (!saveFileName.isEmpty())
|
|
|
|
|
fName = saveFileName;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-01-24 08:47:31 +01:00
|
|
|
// check if UTF8-BOM has to be added or removed
|
2011-08-25 12:54:20 +02:00
|
|
|
Utils::TextFileFormat saveFormat = format();
|
2013-01-24 08:47:31 +01:00
|
|
|
if (saveFormat.codec->name() == "UTF-8" && supportsUtf8Bom()) {
|
2011-08-25 12:54:20 +02:00
|
|
|
switch (d->m_extraEncodingSettings.m_utf8BomSetting) {
|
|
|
|
|
case TextEditor::ExtraEncodingSettings::AlwaysAdd:
|
|
|
|
|
saveFormat.hasUtf8Bom = true;
|
|
|
|
|
break;
|
|
|
|
|
case TextEditor::ExtraEncodingSettings::OnlyKeep:
|
|
|
|
|
break;
|
|
|
|
|
case TextEditor::ExtraEncodingSettings::AlwaysDelete:
|
|
|
|
|
saveFormat.hasUtf8Bom = false;
|
|
|
|
|
break;
|
2011-03-30 13:45:16 +02:00
|
|
|
}
|
2013-01-24 08:47:31 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-08-25 12:54:20 +02:00
|
|
|
const bool ok = write(fName, saveFormat, d->m_document->toPlainText(), errorString);
|
2011-06-20 18:32:06 +02:00
|
|
|
|
2013-01-29 13:55:55 +01:00
|
|
|
// restore text cursor and scroll bar positions
|
2011-06-20 18:32:06 +02:00
|
|
|
if (autoSave && undos < d->m_document->availableUndoSteps()) {
|
|
|
|
|
d->m_document->undo();
|
|
|
|
|
if (editorWidget) {
|
|
|
|
|
QTextCursor cur = editorWidget->textCursor();
|
|
|
|
|
cur.setPosition(savedAnchor);
|
|
|
|
|
cur.setPosition(savedPosition, QTextCursor::KeepAnchor);
|
2013-01-29 13:55:55 +01:00
|
|
|
editorWidget->verticalScrollBar()->setValue(savedVScrollBarValue);
|
|
|
|
|
editorWidget->horizontalScrollBar()->setValue(savedHScrollBarValue);
|
2011-06-20 18:32:06 +02:00
|
|
|
editorWidget->setTextCursor(cur);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-25 12:54:20 +02:00
|
|
|
if (!ok)
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
2011-05-10 20:43:03 +02:00
|
|
|
d->m_autoSaveRevision = d->m_document->revision();
|
|
|
|
|
if (autoSave)
|
|
|
|
|
return true;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-01-24 08:49:17 +01:00
|
|
|
// inform about the new filename
|
2008-12-02 12:01:29 +01:00
|
|
|
const QFileInfo fi(fName);
|
2010-10-29 15:20:10 +02:00
|
|
|
d->m_document->setModified(false);
|
2013-07-04 13:30:26 +02:00
|
|
|
setFilePath(QDir::cleanPath(fi.absoluteFilePath()));
|
2008-12-02 12:01:29 +01:00
|
|
|
emit changed();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-15 15:14:10 +02:00
|
|
|
bool BaseTextDocument::setContents(const QByteArray &contents)
|
|
|
|
|
{
|
2013-08-30 09:22:42 +02:00
|
|
|
if (contents.size() > EditorManager::maxTextFileSize()) {
|
2013-07-15 15:14:10 +02:00
|
|
|
document()->setPlainText(BaseTextEditorWidget::msgTextTooLarge(contents.size()));
|
|
|
|
|
document()->setModified(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
document()->setPlainText(QString::fromUtf8(contents));
|
|
|
|
|
document()->setModified(false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-10 20:43:03 +02:00
|
|
|
bool BaseTextDocument::shouldAutoSave() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_autoSaveRevision != d->m_document->revision();
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 13:30:26 +02:00
|
|
|
void BaseTextDocument::setFilePath(const QString &newName)
|
2010-05-11 14:13:38 +02:00
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
if (newName == filePath())
|
2013-07-04 11:35:56 +02:00
|
|
|
return;
|
2010-05-11 14:13:38 +02:00
|
|
|
const QFileInfo fi(newName);
|
2013-07-04 13:30:26 +02:00
|
|
|
IDocument::setFilePath(QDir::cleanPath(fi.absoluteFilePath()));
|
2010-05-11 14:13:38 +02:00
|
|
|
emit titleChanged(fi.fileName());
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
bool BaseTextDocument::isFileReadOnly() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
if (filePath().isEmpty()) //have no corresponding file, so editing is ok
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
2010-10-29 15:20:10 +02:00
|
|
|
return d->m_fileIsReadOnly;
|
2009-05-08 15:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BaseTextDocument::isModified() const
|
|
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
return d->m_document->isModified();
|
2009-05-08 15:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocument::checkPermissions()
|
|
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
bool previousReadOnly = d->m_fileIsReadOnly;
|
2013-07-04 13:30:26 +02:00
|
|
|
if (!filePath().isEmpty()) {
|
|
|
|
|
const QFileInfo fi(filePath());
|
2010-10-29 15:20:10 +02:00
|
|
|
d->m_fileIsReadOnly = !fi.isWritable();
|
2009-05-08 15:48:00 +02:00
|
|
|
} else {
|
2010-10-29 15:20:10 +02:00
|
|
|
d->m_fileIsReadOnly = false;
|
2009-05-08 15:48:00 +02:00
|
|
|
}
|
2010-10-29 15:20:10 +02:00
|
|
|
if (previousReadOnly != d->m_fileIsReadOnly)
|
2010-04-08 16:07:23 +02:00
|
|
|
emit changed();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-05-10 20:43:03 +02:00
|
|
|
bool BaseTextDocument::open(QString *errorString, const QString &fileName, const QString &realFileName)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-08-25 12:54:20 +02:00
|
|
|
QStringList content;
|
|
|
|
|
|
|
|
|
|
ReadResult readResult = Utils::TextFileFormat::ReadIOError;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
|
const QFileInfo fi(fileName);
|
2010-10-29 15:20:10 +02:00
|
|
|
d->m_fileIsReadOnly = !fi.isWritable();
|
2011-08-25 12:54:20 +02:00
|
|
|
readResult = read(realFileName, &content, errorString);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
d->m_document->setModified(false);
|
2011-02-04 15:00:43 +01:00
|
|
|
const int chunks = content.size();
|
2011-08-25 12:54:20 +02:00
|
|
|
if (chunks == 0) {
|
|
|
|
|
d->m_document->setPlainText(QString());
|
|
|
|
|
} else if (chunks == 1) {
|
2011-02-04 15:00:43 +01:00
|
|
|
d->m_document->setPlainText(content.at(0));
|
2011-02-04 13:12:58 +01:00
|
|
|
} else {
|
2011-02-04 15:00:43 +01:00
|
|
|
QFutureInterface<void> interface;
|
|
|
|
|
interface.setProgressRange(0, chunks);
|
2013-09-03 15:18:37 +02:00
|
|
|
ProgressManager::addTask(interface.future(), tr("Opening file"), Constants::TASK_OPEN_FILE);
|
2011-02-04 15:00:43 +01:00
|
|
|
interface.reportStarted();
|
2011-02-08 14:00:10 +01:00
|
|
|
d->m_document->setUndoRedoEnabled(false);
|
2011-02-04 15:00:43 +01:00
|
|
|
QTextCursor c(d->m_document);
|
2011-02-10 15:03:38 +01:00
|
|
|
c.beginEditBlock();
|
|
|
|
|
d->m_document->clear();
|
2011-02-04 15:00:43 +01:00
|
|
|
for (int i = 0; i < chunks; ++i) {
|
|
|
|
|
c.insertText(content.at(i));
|
|
|
|
|
interface.setProgressValue(i + 1);
|
|
|
|
|
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
|
2011-02-04 13:12:58 +01:00
|
|
|
}
|
2011-02-10 15:03:38 +01:00
|
|
|
c.endEditBlock();
|
2011-02-08 14:00:10 +01:00
|
|
|
d->m_document->setUndoRedoEnabled(true);
|
2011-02-04 15:00:43 +01:00
|
|
|
interface.reportFinished();
|
2011-02-04 13:12:58 +01:00
|
|
|
}
|
|
|
|
|
BaseTextDocumentLayout *documentLayout =
|
|
|
|
|
qobject_cast<BaseTextDocumentLayout*>(d->m_document->documentLayout());
|
2008-12-09 15:25:01 +01:00
|
|
|
QTC_ASSERT(documentLayout, return true);
|
2011-05-10 20:43:03 +02:00
|
|
|
documentLayout->lastSaveRevision = d->m_autoSaveRevision = d->m_document->revision();
|
|
|
|
|
d->m_document->setModified(fileName != realFileName);
|
2013-07-04 13:30:26 +02:00
|
|
|
setFilePath(QDir::cleanPath(fi.absoluteFilePath()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2011-10-25 14:57:30 +02:00
|
|
|
return readResult == Utils::TextFileFormat::ReadSuccess
|
|
|
|
|
|| readResult == Utils::TextFileFormat::ReadEncodingError;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-04-04 15:24:13 +02:00
|
|
|
bool BaseTextDocument::reload(QString *errorString, QTextCodec *codec)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-04-04 15:24:13 +02:00
|
|
|
QTC_ASSERT(codec, return false);
|
2011-08-25 12:54:20 +02:00
|
|
|
setCodec(codec);
|
2011-04-04 15:24:13 +02:00
|
|
|
return reload(errorString);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-04-04 15:24:13 +02:00
|
|
|
bool BaseTextDocument::reload(QString *errorString)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
emit aboutToReload();
|
2012-02-15 13:39:10 +01:00
|
|
|
BaseTextDocumentLayout *documentLayout =
|
|
|
|
|
qobject_cast<BaseTextDocumentLayout*>(d->m_document->documentLayout());
|
2012-03-30 11:40:31 +02:00
|
|
|
TextMarks marks;
|
2012-02-15 13:39:10 +01:00
|
|
|
if (documentLayout)
|
2012-03-30 11:40:31 +02:00
|
|
|
marks = documentLayout->documentClosing(); // removes text marks non-permanently
|
2010-05-28 14:06:57 +02:00
|
|
|
|
2013-07-04 13:30:26 +02:00
|
|
|
bool success = open(errorString, filePath(), filePath());
|
2012-03-30 11:40:31 +02:00
|
|
|
|
|
|
|
|
if (documentLayout)
|
|
|
|
|
documentLayout->documentReloaded(marks); // readds text marks
|
2013-06-20 12:49:19 +02:00
|
|
|
emit reloadFinished(success);
|
2012-03-30 11:40:31 +02:00
|
|
|
return success;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-04-04 15:24:13 +02:00
|
|
|
bool BaseTextDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type)
|
2010-03-19 10:28:05 +01:00
|
|
|
{
|
|
|
|
|
if (flag == FlagIgnore)
|
2011-04-04 15:24:13 +02:00
|
|
|
return true;
|
2010-03-19 10:28:05 +01:00
|
|
|
if (type == TypePermissions) {
|
2010-04-08 16:07:23 +02:00
|
|
|
checkPermissions();
|
2011-04-04 15:24:13 +02:00
|
|
|
return true;
|
2010-03-19 10:28:05 +01:00
|
|
|
} else {
|
2011-04-04 15:24:13 +02:00
|
|
|
return reload(errorString);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-09 14:47:18 +02:00
|
|
|
void BaseTextDocument::setSyntaxHighlighter(SyntaxHighlighter *highlighter)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
if (d->m_highlighter)
|
|
|
|
|
delete d->m_highlighter;
|
|
|
|
|
d->m_highlighter = highlighter;
|
|
|
|
|
d->m_highlighter->setParent(this);
|
|
|
|
|
d->m_highlighter->setDocument(d->m_document);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-09 17:43:31 +01:00
|
|
|
|
|
|
|
|
|
2009-09-09 16:37:09 +02:00
|
|
|
void BaseTextDocument::cleanWhitespace(const QTextCursor &cursor)
|
2008-12-09 17:43:31 +01:00
|
|
|
{
|
2009-09-09 16:37:09 +02:00
|
|
|
bool hasSelection = cursor.hasSelection();
|
|
|
|
|
QTextCursor copyCursor = cursor;
|
2010-09-07 15:55:06 +02:00
|
|
|
copyCursor.setVisualNavigation(false);
|
2009-09-09 16:37:09 +02:00
|
|
|
copyCursor.beginEditBlock();
|
|
|
|
|
cleanWhitespace(copyCursor, true, true);
|
|
|
|
|
if (!hasSelection)
|
|
|
|
|
ensureFinalNewLine(copyCursor);
|
|
|
|
|
copyCursor.endEditBlock();
|
2008-12-09 17:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
2010-05-28 14:06:57 +02:00
|
|
|
void BaseTextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, bool inEntireDocument)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
BaseTextDocumentLayout *documentLayout = qobject_cast<BaseTextDocumentLayout*>(d->m_document->documentLayout());
|
2010-09-07 15:55:06 +02:00
|
|
|
Q_ASSERT(cursor.visualNavigation() == false);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
QTextBlock block = d->m_document->findBlock(cursor.selectionStart());
|
2009-09-09 16:37:09 +02:00
|
|
|
QTextBlock end;
|
|
|
|
|
if (cursor.hasSelection())
|
2010-10-29 15:20:10 +02:00
|
|
|
end = d->m_document->findBlock(cursor.selectionEnd()-1).next();
|
2009-09-09 16:37:09 +02:00
|
|
|
|
|
|
|
|
while (block.isValid() && block != end) {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-12-11 19:49:17 +01:00
|
|
|
if (inEntireDocument || block.revision() != documentLayout->lastSaveRevision) {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
QString blockText = block.text();
|
2013-08-07 10:19:24 +02:00
|
|
|
d->m_tabSettings.removeTrailingWhitespace(cursor, block);
|
2010-10-29 15:20:10 +02:00
|
|
|
if (cleanIndentation && !d->m_tabSettings.isIndentationClean(block)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
cursor.setPosition(block.position());
|
2010-10-29 15:20:10 +02:00
|
|
|
int firstNonSpace = d->m_tabSettings.firstNonSpace(blockText);
|
2008-12-02 12:01:29 +01:00
|
|
|
if (firstNonSpace == blockText.length()) {
|
|
|
|
|
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
|
} else {
|
2010-10-29 15:20:10 +02:00
|
|
|
int column = d->m_tabSettings.columnAt(blockText, firstNonSpace);
|
2008-12-02 12:01:29 +01:00
|
|
|
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, firstNonSpace);
|
2010-10-29 15:20:10 +02:00
|
|
|
QString indentationString = d->m_tabSettings.indentationString(0, column, block);
|
2008-12-02 12:01:29 +01:00
|
|
|
cursor.insertText(indentationString);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
block = block.next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocument::ensureFinalNewLine(QTextCursor& cursor)
|
|
|
|
|
{
|
|
|
|
|
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
|
|
|
|
bool emptyFile = !cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
|
|
if (!emptyFile && cursor.selectedText().at(0) != QChar::ParagraphSeparator)
|
|
|
|
|
{
|
|
|
|
|
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
|
|
|
|
cursor.insertText(QLatin1String("\n"));
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-28 14:06:57 +02:00
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
} // namespace TextEditor
|