2011-09-06 09:51:22 +02:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01: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"
|
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
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <QTextCodec>
|
|
|
|
|
#include <QFutureInterface>
|
|
|
|
|
#include <QMainWindow>
|
|
|
|
|
#include <QSyntaxHighlighter>
|
|
|
|
|
#include <QApplication>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-07-13 15:00:20 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
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>
|
2011-03-30 13:45:16 +02:00
|
|
|
#include <utils/fileutils.h>
|
2010-02-04 10:33:12 +01:00
|
|
|
#include <utils/reloadpromptutils.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
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_fileName;
|
|
|
|
|
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-20 11:27:07 +02:00
|
|
|
bool m_hasHighlightWarning;
|
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-20 11:27:07 +02:00
|
|
|
m_hasHighlightWarning(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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
d->m_mimeType = mt;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
QString BaseTextDocument::fileName() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_fileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
QTC_ASSERT(documentLayout, return 0)
|
|
|
|
|
return documentLayout->markableInterface();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-05-10 20:43:03 +02:00
|
|
|
bool BaseTextDocument::save(QString *errorString, const QString &fileName, 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;
|
2011-06-20 18:32:06 +02:00
|
|
|
int undos = d->m_document->availableUndoSteps();
|
|
|
|
|
|
2010-07-07 13:17:58 +02:00
|
|
|
// When saving the current editor, make sure to maintain the cursor position for undo
|
|
|
|
|
Core::IEditor *currentEditor = Core::EditorManager::instance()->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();
|
|
|
|
|
cursor.setPosition(cur.position());
|
|
|
|
|
}
|
2010-07-07 13:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
cursor.beginEditBlock();
|
2010-07-07 13:17:58 +02:00
|
|
|
cursor.movePosition(QTextCursor::Start);
|
|
|
|
|
|
2010-10-29 15:20:10 +02: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)
|
2008-12-02 12:01:29 +01:00
|
|
|
ensureFinalNewLine(cursor);
|
|
|
|
|
cursor.endEditBlock();
|
|
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
QString fName = d->m_fileName;
|
2008-12-02 12:01:29 +01:00
|
|
|
if (!fileName.isEmpty())
|
|
|
|
|
fName = fileName;
|
|
|
|
|
|
2011-08-25 12:54:20 +02:00
|
|
|
Utils::TextFileFormat saveFormat = format();
|
|
|
|
|
if (saveFormat.codec->name() == "UTF-8") {
|
|
|
|
|
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
|
|
|
}
|
2011-08-25 12:54:20 +02:00
|
|
|
} // "UTF-8"
|
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
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
const QFileInfo fi(fName);
|
2012-03-12 16:56:25 +01:00
|
|
|
const QString oldFileName = d->m_fileName;
|
2010-10-29 15:20:10 +02:00
|
|
|
d->m_fileName = QDir::cleanPath(fi.absoluteFilePath());
|
|
|
|
|
d->m_document->setModified(false);
|
2012-03-12 16:56:25 +01:00
|
|
|
emit fileNameChanged(oldFileName, d->m_fileName);
|
2008-12-02 12:01:29 +01:00
|
|
|
emit titleChanged(fi.fileName());
|
|
|
|
|
emit changed();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-10 20:43:03 +02:00
|
|
|
bool BaseTextDocument::shouldAutoSave() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_autoSaveRevision != d->m_document->revision();
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-11 14:13:38 +02:00
|
|
|
void BaseTextDocument::rename(const QString &newName)
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(newName);
|
2012-03-12 16:56:25 +01:00
|
|
|
const QString oldFileName = d->m_fileName;
|
2010-10-29 15:20:10 +02:00
|
|
|
d->m_fileName = QDir::cleanPath(fi.absoluteFilePath());
|
2012-03-12 16:56:25 +01:00
|
|
|
emit fileNameChanged(oldFileName, d->m_fileName);
|
2010-05-11 14:13:38 +02:00
|
|
|
emit titleChanged(fi.fileName());
|
|
|
|
|
emit changed();
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
bool BaseTextDocument::isFileReadOnly() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-10-29 15:20:10 +02:00
|
|
|
if (d->m_fileName.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;
|
|
|
|
|
if (!d->m_fileName.isEmpty()) {
|
|
|
|
|
const QFileInfo fi(d->m_fileName);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
QString title = tr("untitled");
|
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();
|
|
|
|
|
d->m_fileName = QDir::cleanPath(fi.absoluteFilePath());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
title = fi.fileName();
|
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);
|
2012-01-24 15:36:40 +01:00
|
|
|
Core::ICore::progressManager()->addTask(
|
2012-01-05 11:05:28 +01:00
|
|
|
interface.future(), tr("Opening file"), QLatin1String(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);
|
2008-12-02 12:01:29 +01:00
|
|
|
emit titleChanged(title);
|
|
|
|
|
emit changed();
|
|
|
|
|
}
|
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
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
bool success = open(errorString, d->m_fileName, d->m_fileName);
|
|
|
|
|
|
|
|
|
|
if (documentLayout)
|
|
|
|
|
documentLayout->documentReloaded(marks); // readds text marks
|
|
|
|
|
if (success)
|
|
|
|
|
emit reloaded();
|
|
|
|
|
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();
|
2010-10-29 15:20:10 +02:00
|
|
|
if (int trailing = d->m_tabSettings.trailingWhitespaces(blockText)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
cursor.setPosition(block.position() + block.length() - 1);
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, trailing);
|
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
|
}
|
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
|
|
|
|
2011-05-20 11:27:07 +02:00
|
|
|
bool BaseTextDocument::hasHighlightWarning() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_hasHighlightWarning;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocument::setHighlightWarning(bool has)
|
|
|
|
|
{
|
|
|
|
|
d->m_hasHighlightWarning = has;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 15:20:10 +02:00
|
|
|
} // namespace TextEditor
|