diff --git a/src/plugins/texteditor/quickfix.cpp b/src/plugins/texteditor/quickfix.cpp new file mode 100644 index 00000000000..3b41186d882 --- /dev/null +++ b/src/plugins/texteditor/quickfix.cpp @@ -0,0 +1,160 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "quickfix.h" +#include "basetexteditor.h" + +#include +#include + +#include + +using TextEditor::QuickFixOperation; + +QuickFixOperation::QuickFixOperation(TextEditor::BaseTextEditor *editor) + : _editor(editor) +{ +} + +QuickFixOperation::~QuickFixOperation() +{ +} + +TextEditor::BaseTextEditor *QuickFixOperation::editor() const +{ + return _editor; +} + +QTextCursor QuickFixOperation::textCursor() const +{ + return _textCursor; +} + +void QuickFixOperation::setTextCursor(const QTextCursor &cursor) +{ + _textCursor = cursor; +} + +int QuickFixOperation::selectionStart() const +{ + return _textCursor.selectionStart(); +} + +int QuickFixOperation::selectionEnd() const +{ + return _textCursor.selectionEnd(); +} + +const Utils::ChangeSet &QuickFixOperation::changeSet() const +{ + return _changeSet; +} + +void QuickFixOperation::apply() +{ + const Range r = topLevelRange(); + + _textCursor.beginEditBlock(); + _changeSet.apply(&_textCursor); + reindent(r); + _textCursor.endEditBlock(); +} + +QuickFixOperation::Range QuickFixOperation::range(int from, int to) const +{ + QTextDocument *doc = editor()->document(); + + QTextCursor begin(doc); + begin.setPosition(from); + + QTextCursor end(doc); + end.setPosition(to); + + Range range; + range.begin = begin; + range.end = end; + return range; +} + +int QuickFixOperation::position(int line, int column) const +{ + QTextDocument *doc = editor()->document(); + return doc->findBlockByNumber(line - 1).position() + column - 1; +} + +void QuickFixOperation::reindent(const Range &range) +{ + QTextCursor tc = range.begin; + tc.setPosition(range.end.position(), QTextCursor::KeepAnchor); + editor()->indentInsertedText(tc); +} + +void QuickFixOperation::move(int start, int end, int to) +{ + _changeSet.move(start, end-start, to); +} + +void QuickFixOperation::replace(int start, int end, const QString &replacement) +{ + _changeSet.replace(start, end-start, replacement); +} + +void QuickFixOperation::insert(int at, const QString &text) +{ + _changeSet.insert(at, text); +} + +void QuickFixOperation::remove(int start, int end) +{ + _changeSet.remove(start, end-start); +} + +void QuickFixOperation::flip(int start1, int end1, int start2, int end2) +{ + _changeSet.flip(start1, end1-start1, start2, end2-start2); +} + +void QuickFixOperation::copy(int start, int end, int to) +{ + _changeSet.copy(start, end-start, to); +} + +QChar QuickFixOperation::charAt(int offset) const +{ + QTextDocument *doc = _textCursor.document(); + return doc->characterAt(offset); +} + +QString QuickFixOperation::textOf(int start, int end) const +{ + QTextCursor tc = _textCursor; + tc.setPosition(start); + tc.setPosition(end, QTextCursor::KeepAnchor); + return tc.selectedText(); +} diff --git a/src/plugins/texteditor/quickfix.h b/src/plugins/texteditor/quickfix.h new file mode 100644 index 00000000000..f48eeb74730 --- /dev/null +++ b/src/plugins/texteditor/quickfix.h @@ -0,0 +1,113 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef TEXTEDITORQUICKFIX_H +#define TEXTEDITORQUICKFIX_H + +#include "texteditor_global.h" +#include + +#include +#include + +namespace TextEditor { + +class BaseTextEditor; +class QuickFixOperation; + +class TEXTEDITOR_EXPORT QuickFixState +{ + Q_DISABLE_COPY(QuickFixState) + +public: + QuickFixState() {} + virtual ~QuickFixState() {} +}; + +class TEXTEDITOR_EXPORT QuickFixOperation +{ + Q_DISABLE_COPY(QuickFixOperation) + +public: + typedef QSharedPointer Ptr; + + struct Range { + Range() {} + Range(const QTextCursor &tc): begin(tc), end(tc) {} + + QTextCursor begin; + QTextCursor end; + }; + +public: + QuickFixOperation(TextEditor::BaseTextEditor *editor); + virtual ~QuickFixOperation(); + + virtual QString description() const = 0; + virtual void createChangeSet() = 0; + virtual Range topLevelRange() const = 0; + + virtual int match(QuickFixState *state) = 0; + + void apply(); + + TextEditor::BaseTextEditor *editor() const; + + QTextCursor textCursor() const; + void setTextCursor(const QTextCursor &cursor); + + void reindent(const Range &range); + + int selectionStart() const; + int selectionEnd() const; + + int position(int line, int column) const; + Range range(int from, int to) const; + + void move(int start, int end, int to); + void replace(int start, int end, const QString &replacement); + void insert(int at, const QString &text); + void remove(int start, int end); + void flip(int start1, int end1, int start2, int end2); + void copy(int start, int end, int to); + + QChar charAt(int offset) const; + QString textOf(int start, int end) const; + + const Utils::ChangeSet &changeSet() const; + +private: + TextEditor::BaseTextEditor *_editor; + QTextCursor _textCursor; + Utils::ChangeSet _changeSet; +}; + +} // end of namespace TextEditor + +#endif // TEXTEDITORQUICKFIX_H diff --git a/src/plugins/texteditor/texteditor.pro b/src/plugins/texteditor/texteditor.pro index 399d330b4c5..747c5dadc77 100644 --- a/src/plugins/texteditor/texteditor.pro +++ b/src/plugins/texteditor/texteditor.pro @@ -41,6 +41,7 @@ SOURCES += texteditorplugin.cpp \ completionsettings.cpp \ normalindenter.cpp \ indenter.cpp \ + quickfix.cpp \ generichighlighter/itemdata.cpp \ generichighlighter/specificrules.cpp \ generichighlighter/rule.cpp \ @@ -95,6 +96,7 @@ HEADERS += texteditorplugin.h \ completionsettings.h \ normalindenter.h \ indenter.h \ + quickfix.h \ generichighlighter/reuse.h \ generichighlighter/itemdata.h \ generichighlighter/specificrules.h \