TextEditors: Separate away methods that actually work on a document

Introduces ITextEditorDocument. This is part of a more general "use
documents instead of editors whereever possible". It will allow to move
to e.g. ITextEditor::openedTextDocumentContents() instead of
ITextEditor::openedTextEditorsContents().

Change-Id: I5ebceaa257a0d2c3e8ac4ac51b9b08b6faa42487
Reviewed-by: David Schulz <david.schulz@digia.com>
This commit is contained in:
Eike Ziller
2013-04-18 18:21:17 +02:00
parent e31575a493
commit b450b3071e
20 changed files with 96 additions and 79 deletions

View File

@@ -213,7 +213,7 @@ void CodepasterPlugin::postEditor()
if (ITextEditor *textEditor = qobject_cast<ITextEditor *>(editor)) { if (ITextEditor *textEditor = qobject_cast<ITextEditor *>(editor)) {
data = textEditor->selectedText(); data = textEditor->selectedText();
if (data.isEmpty()) if (data.isEmpty())
data = textEditor->contents(); data = textEditor->textDocument()->contents();
mimeType = textEditor->document()->mimeType(); mimeType = textEditor->document()->mimeType();
} }
} }

View File

@@ -1482,10 +1482,10 @@ CPPEditorWidget::Link CPPEditorWidget::findLinkAt(const QTextCursor &cursor,
const Snapshot &snapshot = m_modelManager->snapshot(); const Snapshot &snapshot = m_modelManager->snapshot();
QTextCursor tc = cursor; QTextCursor tc = cursor;
QChar ch = characterAt(tc.position()); QChar ch = document()->characterAt(tc.position());
while (ch.isLetterOrNumber() || ch == QLatin1Char('_')) { while (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
tc.movePosition(QTextCursor::NextCharacter); tc.movePosition(QTextCursor::NextCharacter);
ch = characterAt(tc.position()); ch = document()->characterAt(tc.position());
} }
// Initially try to macth decl/def. For this we need the semantic doc with the AST. // Initially try to macth decl/def. For this we need the semantic doc with the AST.
@@ -1493,9 +1493,9 @@ CPPEditorWidget::Link CPPEditorWidget::findLinkAt(const QTextCursor &cursor,
&& m_lastSemanticInfo.doc->translationUnit() && m_lastSemanticInfo.doc->translationUnit()
&& m_lastSemanticInfo.doc->translationUnit()->ast()) { && m_lastSemanticInfo.doc->translationUnit()->ast()) {
int pos = tc.position(); int pos = tc.position();
while (characterAt(pos).isSpace()) while (document()->characterAt(pos).isSpace())
++pos; ++pos;
if (characterAt(pos) == QLatin1Char('(')) { if (document()->characterAt(pos) == QLatin1Char('(')) {
link = attemptFuncDeclDef(cursor, m_lastSemanticInfo.doc, snapshot); link = attemptFuncDeclDef(cursor, m_lastSemanticInfo.doc, snapshot);
if (link.hasValidLinkText()) if (link.hasValidLinkText())
return link; return link;
@@ -1626,7 +1626,7 @@ CPPEditorWidget::Link CPPEditorWidget::findLinkAt(const QTextCursor &cursor,
QString expression = expressionUnderCursor(tc); QString expression = expressionUnderCursor(tc);
for (int pos = tc.position();; ++pos) { for (int pos = tc.position();; ++pos) {
const QChar ch = characterAt(pos); const QChar ch = document()->characterAt(pos);
if (ch.isSpace()) if (ch.isSpace())
continue; continue;
else { else {
@@ -2488,9 +2488,9 @@ bool CPPEditorWidget::handleDocumentationComment(QKeyEvent *e)
bool CPPEditorWidget::isStartOfDoxygenComment(const QTextCursor &cursor) const bool CPPEditorWidget::isStartOfDoxygenComment(const QTextCursor &cursor) const
{ {
const int pos = cursor.position(); const int pos = cursor.position();
QString comment = QString(characterAt(pos - 3)) QString comment = QString(document()->characterAt(pos - 3))
+ characterAt(pos - 2) + document()->characterAt(pos - 2)
+ characterAt(pos - 1); + document()->characterAt(pos - 1);
if ((comment == QLatin1String("/**")) if ((comment == QLatin1String("/**"))
|| (comment == QLatin1String("/*!")) || (comment == QLatin1String("/*!"))

View File

@@ -242,7 +242,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
// If the function doesn't return anything, automatically place the semicolon, // If the function doesn't return anything, automatically place the semicolon,
// unless we're doing a scope completion (then it might be function definition). // unless we're doing a scope completion (then it might be function definition).
const QChar characterAtCursor = editor->characterAt(editor->position()); const QChar characterAtCursor = editor->textDocument()->characterAt(editor->position());
bool endWithSemicolon = m_typedChar == QLatin1Char(';') bool endWithSemicolon = m_typedChar == QLatin1Char(';')
|| (function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON); || (function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON);
const QChar semicolon = m_typedChar.isNull() ? QLatin1Char(';') : m_typedChar; const QChar semicolon = m_typedChar.isNull() ? QLatin1Char(';') : m_typedChar;
@@ -260,7 +260,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
m_typedChar = QChar(); m_typedChar = QChar();
} }
} else if (autoParenthesesEnabled) { } else if (autoParenthesesEnabled) {
const QChar lookAhead = editor->characterAt(editor->position() + 1); const QChar lookAhead = editor->textDocument()->characterAt(editor->position() + 1);
if (MatchingText::shouldInsertMatchingText(lookAhead)) { if (MatchingText::shouldInsertMatchingText(lookAhead)) {
extraChars += QLatin1Char(')'); extraChars += QLatin1Char(')');
--cursorOffset; --cursorOffset;
@@ -299,7 +299,8 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
// Determine the length of characters that should just be kept on the editor, but do // Determine the length of characters that should just be kept on the editor, but do
// not consider content that ends as an identifier (which could be undesired). // not consider content that ends as an identifier (which could be undesired).
const int lineEnd = editor->position(TextEditor::ITextEditor::EndOfLine); const int lineEnd = editor->position(TextEditor::ITextEditor::EndOfLine);
const QString inEditor = editor->textAt(editor->position(), lineEnd - editor->position()); const QString inEditor = editor->textDocument()->textAt(editor->position(),
lineEnd - editor->position());
int preserveLength = 0; int preserveLength = 0;
if (!inEditor.isEmpty()) { if (!inEditor.isEmpty()) {
preserveLength = toInsert.length() - (editor->position() - basePosition); preserveLength = toInsert.length() - (editor->position() - basePosition);
@@ -317,7 +318,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
for (int i = 0; i < extraChars.length(); ++i) { for (int i = 0; i < extraChars.length(); ++i) {
const QChar a = extraChars.at(i); const QChar a = extraChars.at(i);
const QChar b = editor->characterAt(editor->position() + i + preserveLength); const QChar b = editor->textDocument()->characterAt(editor->position() + i + preserveLength);
if (a == b) if (a == b)
++extraLength; ++extraLength;
else else

View File

@@ -73,7 +73,7 @@ QString CppEditorSupport::contents()
if (! _textEditor) if (! _textEditor)
return QString(); return QString();
else if (! _cachedContents.isEmpty()) else if (! _cachedContents.isEmpty())
_cachedContents = _textEditor->contents(); _cachedContents = _textEditor->textDocument()->contents();
return _cachedContents; return _cachedContents;
} }
@@ -112,7 +112,7 @@ void CppEditorSupport::updateDocumentNow()
_updateDocumentTimer->stop(); _updateDocumentTimer->stop();
QStringList sourceFiles(_textEditor->document()->fileName()); QStringList sourceFiles(_textEditor->document()->fileName());
_cachedContents = _textEditor->contents(); _cachedContents = _textEditor->textDocument()->contents();
_documentParser = _modelManager->updateSourceFiles(sourceFiles); _documentParser = _modelManager->updateSourceFiles(sourceFiles);
} }
} }

View File

@@ -685,7 +685,7 @@ static bool currentTextEditorPosition(ContextData *data)
data->fileName = document->fileName(); data->fileName = document->fileName();
if (textEditor->property("DisassemblerView").toBool()) { if (textEditor->property("DisassemblerView").toBool()) {
int lineNumber = textEditor->currentLine(); int lineNumber = textEditor->currentLine();
QString line = textEditor->contents() QString line = textEditor->textDocument()->contents()
.section(QLatin1Char('\n'), lineNumber - 1, lineNumber - 1); .section(QLatin1Char('\n'), lineNumber - 1, lineNumber - 1);
data->address = DisassemblerLine::addressFromDisassemblyLine(line); data->address = DisassemblerLine::addressFromDisassemblyLine(line);
} else { } else {
@@ -1845,7 +1845,7 @@ void DebuggerPluginPrivate::requestContextMenu(ITextEditor *editor,
const QString fileName = editor->document()->fileName(); const QString fileName = editor->document()->fileName();
if (editor->property("DisassemblerView").toBool()) { if (editor->property("DisassemblerView").toBool()) {
args.fileName = fileName; args.fileName = fileName;
QString line = editor->contents() QString line = editor->textDocument()->contents()
.section(QLatin1Char('\n'), lineNumber - 1, lineNumber - 1); .section(QLatin1Char('\n'), lineNumber - 1, lineNumber - 1);
BreakpointResponse needle; BreakpointResponse needle;
needle.type = BreakpointByAddress; needle.type = BreakpointByAddress;
@@ -1959,7 +1959,7 @@ void DebuggerPluginPrivate::toggleBreakpoint()
QTC_ASSERT(textEditor, return); QTC_ASSERT(textEditor, return);
const int lineNumber = textEditor->currentLine(); const int lineNumber = textEditor->currentLine();
if (textEditor->property("DisassemblerView").toBool()) { if (textEditor->property("DisassemblerView").toBool()) {
QString line = textEditor->contents() QString line = textEditor->textDocument()->contents()
.section(QLatin1Char('\n'), lineNumber - 1, lineNumber - 1); .section(QLatin1Char('\n'), lineNumber - 1, lineNumber - 1);
quint64 address = DisassemblerLine::addressFromDisassemblyLine(line); quint64 address = DisassemblerLine::addressFromDisassemblyLine(line);
toggleBreakpointByAddress(address); toggleBreakpointByAddress(address);
@@ -2016,7 +2016,7 @@ void DebuggerPluginPrivate::requestMark(ITextEditor *editor,
return; return;
if (editor->property("DisassemblerView").toBool()) { if (editor->property("DisassemblerView").toBool()) {
QString line = editor->contents() QString line = editor->textDocument()->contents()
.section(QLatin1Char('\n'), lineNumber - 1, lineNumber - 1); .section(QLatin1Char('\n'), lineNumber - 1, lineNumber - 1);
quint64 address = DisassemblerLine::addressFromDisassemblyLine(line); quint64 address = DisassemblerLine::addressFromDisassemblyLine(line);
toggleBreakpointByAddress(address); toggleBreakpointByAddress(address);

View File

@@ -340,7 +340,7 @@ QString cppExpressionAt(TextEditor::ITextEditor *editor, int pos,
QTextCursor tc(plaintext->document()); QTextCursor tc(plaintext->document());
tc.setPosition(pos); tc.setPosition(pos);
const QChar ch = editor->characterAt(pos); const QChar ch = editor->textDocument()->characterAt(pos);
if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
tc.movePosition(QTextCursor::EndOfWord); tc.movePosition(QTextCursor::EndOfWord);

View File

@@ -340,7 +340,7 @@ static Document::Ptr addDefinition(const Snapshot &docTable,
//! \todo use the InsertionPointLocator to insert at the correct place. //! \todo use the InsertionPointLocator to insert at the correct place.
// (we'll have to extend that class first to do definition insertions) // (we'll have to extend that class first to do definition insertions)
const QString contents = editable->contents(); const QString contents = editable->textDocument()->contents();
int column; int column;
editable->convertPosition(contents.length(), line, &column); editable->convertPosition(contents.length(), line, &column);
editable->gotoLine(*line, column); editable->gotoLine(*line, column);

View File

@@ -248,7 +248,7 @@ void GLSLTextEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
QString GLSLTextEditorWidget::wordUnderCursor() const QString GLSLTextEditorWidget::wordUnderCursor() const
{ {
QTextCursor tc = textCursor(); QTextCursor tc = textCursor();
const QChar ch = characterAt(tc.position() - 1); const QChar ch = document()->characterAt(tc.position() - 1);
// make sure that we're not at the start of the next word. // make sure that we're not at the start of the next word.
if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
tc.movePosition(QTextCursor::Left); tc.movePosition(QTextCursor::Left);

View File

@@ -374,7 +374,7 @@ void QmlJSAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor
int replacedLength = 0; int replacedLength = 0;
for (int i = 0; i < replaceable.length(); ++i) { for (int i = 0; i < replaceable.length(); ++i) {
const QChar a = replaceable.at(i); const QChar a = replaceable.at(i);
const QChar b = editor->characterAt(editor->position() + i); const QChar b = editor->textDocument()->characterAt(editor->position() + i);
if (a == b) if (a == b)
++replacedLength; ++replacedLength;
else else

View File

@@ -1001,7 +1001,7 @@ void QmlJSTextEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
QString QmlJSTextEditorWidget::wordUnderCursor() const QString QmlJSTextEditorWidget::wordUnderCursor() const
{ {
QTextCursor tc = textCursor(); QTextCursor tc = textCursor();
const QChar ch = characterAt(tc.position() - 1); const QChar ch = document()->characterAt(tc.position() - 1);
// make sure that we're not at the start of the next word. // make sure that we're not at the start of the next word.
if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
tc.movePosition(QTextCursor::Left); tc.movePosition(QTextCursor::Left);

View File

@@ -214,7 +214,7 @@ void HoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
i = j = pos; i = j = pos;
QString nameAtt; QString nameAtt;
for (;;) { for (;;) {
QChar c = qmlEditor->characterAt(j); QChar c = qmlEditor->document()->characterAt(j);
if (!c.isLetterOrNumber()) break; if (!c.isLetterOrNumber()) break;
nameAtt.append(c); nameAtt.append(c);
++j; ++j;
@@ -222,7 +222,7 @@ void HoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
QStringList qName; QStringList qName;
while (i>0) { while (i>0) {
--i; --i;
QChar c = qmlEditor->characterAt(i); QChar c = qmlEditor->document()->characterAt(i);
if (c.isLetterOrNumber()) { if (c.isLetterOrNumber()) {
nameAtt.prepend(c); nameAtt.prepend(c);
} else if (c == QLatin1Char('.')) { } else if (c == QLatin1Char('.')) {

View File

@@ -312,7 +312,7 @@ void Qt4Manager::addLibrary(const QString &fileName, ProFileEditorWidget *editor
// add extra \n in case the last line is not empty // add extra \n in case the last line is not empty
int line, column; int line, column;
editable->convertPosition(endOfDoc, &line, &column); editable->convertPosition(endOfDoc, &line, &column);
if (!editable->textAt(endOfDoc - column, column).simplified().isEmpty()) if (!editable->textDocument()->textAt(endOfDoc - column, column).simplified().isEmpty())
snippet = QLatin1Char('\n') + snippet; snippet = QLatin1Char('\n') + snippet;
editable->insert(snippet); editable->insert(snippet);

View File

@@ -31,6 +31,7 @@
#include "basetextdocumentlayout.h" #include "basetextdocumentlayout.h"
#include "basetexteditor.h" #include "basetexteditor.h"
#include "convenience.h"
#include "typingsettings.h" #include "typingsettings.h"
#include "storagesettings.h" #include "storagesettings.h"
#include "tabsettings.h" #include "tabsettings.h"
@@ -90,6 +91,21 @@ BaseTextDocument::~BaseTextDocument()
delete d; delete d;
} }
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);
}
QString BaseTextDocument::mimeType() const QString BaseTextDocument::mimeType() const
{ {
return d->m_mimeType; return d->m_mimeType;

View File

@@ -32,7 +32,7 @@
#include "texteditor_global.h" #include "texteditor_global.h"
#include <coreplugin/textdocument.h> #include "itexteditor.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QTextCursor; class QTextCursor;
@@ -49,7 +49,7 @@ class ExtraEncodingSettings;
class SyntaxHighlighter; class SyntaxHighlighter;
class BaseTextDocumentPrivate; class BaseTextDocumentPrivate;
class TEXTEDITOR_EXPORT BaseTextDocument : public Core::TextDocument class TEXTEDITOR_EXPORT BaseTextDocument : public ITextEditorDocument
{ {
Q_OBJECT Q_OBJECT
@@ -57,6 +57,11 @@ public:
BaseTextDocument(); BaseTextDocument();
virtual ~BaseTextDocument(); virtual ~BaseTextDocument();
// ITextEditorDocument
QString contents() const;
QString textAt(int pos, int length) const;
QChar characterAt(int pos) const;
void setTypingSettings(const TypingSettings &typingSettings); void setTypingSettings(const TypingSettings &typingSettings);
void setStorageSettings(const StorageSettings &storageSettings); void setStorageSettings(const StorageSettings &storageSettings);
void setTabSettings(const TabSettings &tabSettings); void setTabSettings(const TabSettings &tabSettings);

View File

@@ -707,7 +707,7 @@ void BaseTextEditorWidget::editorContentsChange(int position, int charsRemoved,
if (doc->isRedoAvailable()) if (doc->isRedoAvailable())
emit editor()->contentsChangedBecauseOfUndo(); emit editor()->contentsChangedBecauseOfUndo();
if (charsAdded != 0 && characterAt(position + charsAdded - 1).isPrint()) if (charsAdded != 0 && document()->characterAt(position + charsAdded - 1).isPrint())
d->m_assistRelevantContentAdded = true; d->m_assistRelevantContentAdded = true;
} }
@@ -1241,7 +1241,7 @@ bool BaseTextEditorWidget::camelCaseLeft(QTextCursor &cursor, QTextCursor::MoveM
return false; return false;
forever { forever {
QChar c = characterAt(cursor.position()); QChar c = document()->characterAt(cursor.position());
Input input = Input_other; Input input = Input_other;
if (c.isUpper()) if (c.isUpper())
input = Input_U; input = Input_U;
@@ -1348,7 +1348,7 @@ bool BaseTextEditorWidget::camelCaseRight(QTextCursor &cursor, QTextCursor::Move
}; };
forever { forever {
QChar c = characterAt(cursor.position()); QChar c = document()->characterAt(cursor.position());
Input input = Input_other; Input input = Input_other;
if (c.isUpper()) if (c.isUpper())
input = Input_U; input = Input_U;
@@ -2101,7 +2101,7 @@ void BaseTextEditorWidget::gotoLine(int line, int column)
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, column); cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, column);
} else { } else {
int pos = cursor.position(); int pos = cursor.position();
while (characterAt(pos).category() == QChar::Separator_Space) { while (document()->characterAt(pos).category() == QChar::Separator_Space) {
++pos; ++pos;
} }
cursor.setPosition(pos); cursor.setPosition(pos);
@@ -2148,11 +2148,6 @@ void BaseTextEditorWidget::convertPosition(int pos, int *line, int *column) cons
Convenience::convertPosition(document(), pos, line, column); Convenience::convertPosition(document(), pos, line, column);
} }
QChar BaseTextEditorWidget::characterAt(int pos) const
{
return document()->characterAt(pos);
}
bool BaseTextEditorWidget::event(QEvent *e) bool BaseTextEditorWidget::event(QEvent *e)
{ {
#if QT_VERSION >= 0x050000 #if QT_VERSION >= 0x050000
@@ -4752,14 +4747,14 @@ void BaseTextEditorWidget::handleHomeKey(bool anchor)
const int initpos = cursor.position(); const int initpos = cursor.position();
int pos = cursor.block().position(); int pos = cursor.block().position();
QChar character = characterAt(pos); QChar character = document()->characterAt(pos);
const QLatin1Char tab = QLatin1Char('\t'); const QLatin1Char tab = QLatin1Char('\t');
while (character == tab || character.category() == QChar::Separator_Space) { while (character == tab || character.category() == QChar::Separator_Space) {
++pos; ++pos;
if (pos == initpos) if (pos == initpos)
break; break;
character = characterAt(pos); character = document()->characterAt(pos);
} }
// Go to the start of the block when we're already at the start of the text // Go to the start of the block when we're already at the start of the text
@@ -4834,7 +4829,7 @@ void BaseTextEditorWidget::handleBackspaceKey()
} }
} }
} else if (typingSettings.m_smartBackspaceBehavior == TypingSettings::BackspaceUnindents) { } else if (typingSettings.m_smartBackspaceBehavior == TypingSettings::BackspaceUnindents) {
const QChar &c = characterAt(pos - 1); const QChar &c = document()->characterAt(pos - 1);
if (!(c == QLatin1Char(' ') || c == QLatin1Char('\t'))) { if (!(c == QLatin1Char(' ') || c == QLatin1Char('\t'))) {
if (cursorWithinSnippet) if (cursorWithinSnippet)
cursor.beginEditBlock(); cursor.beginEditBlock();
@@ -5241,7 +5236,7 @@ void BaseTextEditorWidget::_q_matchParentheses()
QPalette pal; QPalette pal;
pal.setBrush(QPalette::Text, d->m_matchFormat.foreground()); pal.setBrush(QPalette::Text, d->m_matchFormat.foreground());
pal.setBrush(QPalette::Base, d->m_matchFormat.background()); pal.setBrush(QPalette::Base, d->m_matchFormat.background());
d->m_animator->setData(font(), pal, characterAt(d->m_animator->position())); d->m_animator->setData(font(), pal, document()->characterAt(d->m_animator->position()));
connect(d->m_animator, SIGNAL(updateRequest(int,QPointF,QRectF)), connect(d->m_animator, SIGNAL(updateRequest(int,QPointF,QRectF)),
this, SLOT(_q_animateUpdate(int,QPointF,QRectF))); this, SLOT(_q_animateUpdate(int,QPointF,QRectF)));
} }
@@ -6334,11 +6329,6 @@ void BaseTextEditor::insertExtraToolBarWidget(BaseTextEditor::Side side,
m_toolBar->insertWidget(m_toolBar->actions().first(), widget); m_toolBar->insertWidget(m_toolBar->actions().first(), widget);
} }
int BaseTextEditor::find(const QString &) const
{
return 0;
}
int BaseTextEditor::currentLine() const int BaseTextEditor::currentLine() const
{ {
return e->textCursor().blockNumber() + 1; return e->textCursor().blockNumber() + 1;
@@ -6370,11 +6360,6 @@ QRect BaseTextEditor::cursorRect(int pos) const
return result; return result;
} }
QString BaseTextEditor::contents() const
{
return e->toPlainText();
}
QString BaseTextEditor::selectedText() const QString BaseTextEditor::selectedText() const
{ {
if (e->textCursor().hasSelection()) if (e->textCursor().hasSelection())
@@ -6382,11 +6367,6 @@ QString BaseTextEditor::selectedText() const
return QString(); return QString();
} }
QString BaseTextEditor::textAt(int pos, int length) const
{
return Convenience::textAt(e->textCursor(), pos, length);
}
void BaseTextEditor::remove(int length) void BaseTextEditor::remove(int length)
{ {
QTextCursor tc = e->textCursor(); QTextCursor tc = e->textCursor();

View File

@@ -158,8 +158,6 @@ public:
BaseTextEditor *editor() const; BaseTextEditor *editor() const;
ITextMarkable *markableInterface() const; ITextMarkable *markableInterface() const;
QChar characterAt(int pos) const;
void print(QPrinter *); void print(QPrinter *);
void setSuggestedFileName(const QString &suggestedFileName); void setSuggestedFileName(const QString &suggestedFileName);
@@ -621,8 +619,7 @@ public:
friend class BaseTextEditorWidget; friend class BaseTextEditorWidget;
BaseTextEditorWidget *editorWidget() const { return e; } BaseTextEditorWidget *editorWidget() const { return e; }
// EditorInterface // IEditor
//QWidget *widget() { return e; }
Core::IDocument * document() { return e->editorDocument(); } Core::IDocument * document() { return e->editorDocument(); }
bool createNew(const QString &contents) { return e->createNew(contents); } bool createNew(const QString &contents) { return e->createNew(contents); }
bool open(QString *errorString, const QString &fileName, const QString &realFileName) { return e->open(errorString, fileName, realFileName); } bool open(QString *errorString, const QString &fileName, const QString &realFileName) { return e->open(errorString, fileName, realFileName); }
@@ -637,7 +634,6 @@ public:
void insertExtraToolBarWidget(Side side, QWidget *widget); void insertExtraToolBarWidget(Side side, QWidget *widget);
// ITextEditor // ITextEditor
int find(const QString &string) const;
int currentLine() const; int currentLine() const;
int currentColumn() const; int currentColumn() const;
void gotoLine(int line, int column = 0) { e->gotoLine(line, column); } void gotoLine(int line, int column = 0) { e->gotoLine(line, column); }
@@ -650,10 +646,7 @@ public:
{ e->convertPosition(pos, line, column); } { e->convertPosition(pos, line, column); }
QRect cursorRect(int pos = -1) const; QRect cursorRect(int pos = -1) const;
QString contents() const;
QString selectedText() const; QString selectedText() const;
QString textAt(int pos, int length) const;
inline QChar characterAt(int pos) const { return e->characterAt(pos); }
inline ITextMarkable *markableInterface() { return e->markableInterface(); } inline ITextMarkable *markableInterface() { return e->markableInterface(); }

View File

@@ -195,8 +195,9 @@ void CodeAssistantPrivate::invoke(AssistKind kind, IAssistProvider *provider)
if (isDisplayingProposal() && m_assistKind == kind && !m_proposal->isFragile()) { if (isDisplayingProposal() && m_assistKind == kind && !m_proposal->isFragile()) {
m_proposalWidget->setReason(ExplicitlyInvoked); m_proposalWidget->setReason(ExplicitlyInvoked);
m_proposalWidget->updateProposal( m_proposalWidget->updateProposal(
m_textEditor->textAt(m_proposal->basePosition(), m_textEditor->textDocument()->textAt(
m_textEditor->position() - m_proposal->basePosition())); m_proposal->basePosition(),
m_textEditor->position() - m_proposal->basePosition()));
} else { } else {
destroyContext(); destroyContext();
requestProposal(ExplicitlyInvoked, kind, provider); requestProposal(ExplicitlyInvoked, kind, provider);
@@ -334,7 +335,7 @@ void CodeAssistantPrivate::displayProposal(IAssistProposal *newProposal, AssistR
m_proposalWidget->setIsSynchronized(false); m_proposalWidget->setIsSynchronized(false);
else else
m_proposalWidget->setIsSynchronized(true); m_proposalWidget->setIsSynchronized(true);
m_proposalWidget->showProposal(m_textEditor->textAt( m_proposalWidget->showProposal(m_textEditor->textDocument()->textAt(
m_proposal->basePosition(), m_proposal->basePosition(),
m_textEditor->position() - m_proposal->basePosition())); m_textEditor->position() - m_proposal->basePosition()));
} }
@@ -393,7 +394,7 @@ CompletionAssistProvider *CodeAssistantPrivate::identifyActivationSequence()
const int length = provider->activationCharSequenceLength(); const int length = provider->activationCharSequenceLength();
if (length == 0) if (length == 0)
continue; continue;
QString sequence = m_textEditor->textAt(m_textEditor->position() - length, length); QString sequence = m_textEditor->textDocument()->textAt(m_textEditor->position() - length, length);
// In pretty much all cases the sequence will have the appropriate length. Only in the // In pretty much all cases the sequence will have the appropriate length. Only in the
// case of typing the very first characters in the document for providers that request a // case of typing the very first characters in the document for providers that request a
// length greater than 1 (currently only C++, which specifies 3), the sequence needs to // length greater than 1 (currently only C++, which specifies 3), the sequence needs to
@@ -417,7 +418,7 @@ void CodeAssistantPrivate::notifyChange()
destroyContext(); destroyContext();
} else { } else {
m_proposalWidget->updateProposal( m_proposalWidget->updateProposal(
m_textEditor->textAt(m_proposal->basePosition(), m_textEditor->textDocument()->textAt(m_proposal->basePosition(),
m_textEditor->position() - m_proposal->basePosition())); m_textEditor->position() - m_proposal->basePosition()));
if (m_proposal->isFragile()) if (m_proposal->isFragile())
startAutomaticProposalTimer(); startAutomaticProposalTimer();

View File

@@ -109,10 +109,10 @@ void KeywordsAssistProposalItem::applyContextualContent(TextEditor::BaseTextEdit
int cursorOffset = 0; int cursorOffset = 0;
if (m_keywords.isFunction(toInsert) && settings.m_autoInsertBrackets) { if (m_keywords.isFunction(toInsert) && settings.m_autoInsertBrackets) {
if (settings.m_spaceAfterFunctionName) { if (settings.m_spaceAfterFunctionName) {
if (editor->textAt(editor->position(), 2) == QLatin1String(" (")) { if (editor->textDocument()->textAt(editor->position(), 2) == QLatin1String(" (")) {
cursorOffset = 2; cursorOffset = 2;
} else if (editor->characterAt(editor->position()) == QLatin1Char('(') } else if (editor->textDocument()->characterAt(editor->position()) == QLatin1Char('(')
|| editor->characterAt(editor->position()) == QLatin1Char(' ')) { || editor->textDocument()->characterAt(editor->position()) == QLatin1Char(' ')) {
replaceLength += 1; replaceLength += 1;
toInsert += QLatin1String(" ("); toInsert += QLatin1String(" (");
} else { } else {
@@ -120,7 +120,7 @@ void KeywordsAssistProposalItem::applyContextualContent(TextEditor::BaseTextEdit
cursorOffset = -1; cursorOffset = -1;
} }
} else { } else {
if (editor->characterAt(editor->position()) == QLatin1Char('(')) { if (editor->textDocument()->characterAt(editor->position()) == QLatin1Char('(')) {
cursorOffset = 1; cursorOffset = 1;
} else { } else {
toInsert += QLatin1String("()"); toInsert += QLatin1String("()");

View File

@@ -35,6 +35,11 @@
using namespace TextEditor; using namespace TextEditor;
ITextEditorDocument::ITextEditorDocument(QObject *parent)
: Core::TextDocument(parent)
{
}
QMap<QString, QString> ITextEditor::openedTextEditorsContents() QMap<QString, QString> ITextEditor::openedTextEditorsContents()
{ {
QMap<QString, QString> workingCopy; QMap<QString, QString> workingCopy;
@@ -43,7 +48,7 @@ QMap<QString, QString> ITextEditor::openedTextEditorsContents()
if (!textEditor) if (!textEditor)
continue; continue;
QString fileName = textEditor->document()->fileName(); QString fileName = textEditor->document()->fileName();
workingCopy[fileName] = textEditor->contents(); workingCopy[fileName] = textEditor->textDocument()->contents();
} }
return workingCopy; return workingCopy;
} }
@@ -60,3 +65,9 @@ QMap<QString, QTextCodec *> TextEditor::ITextEditor::openedTextEditorsEncodings(
} }
return workingCopy; return workingCopy;
} }
ITextEditorDocument *ITextEditor::textDocument()
{
return qobject_cast<ITextEditorDocument *>(document());
}

View File

@@ -34,6 +34,7 @@
#include "itextmark.h" #include "itextmark.h"
#include <coreplugin/textdocument.h>
#include <coreplugin/editormanager/ieditor.h> #include <coreplugin/editormanager/ieditor.h>
#include <QMap> #include <QMap>
@@ -54,6 +55,17 @@ namespace Utils {
namespace TextEditor { namespace TextEditor {
class TEXTEDITOR_EXPORT ITextEditorDocument : public Core::TextDocument
{
Q_OBJECT
public:
explicit ITextEditorDocument(QObject *parent = 0);
virtual QString contents() const = 0;
virtual QString textAt(int pos, int length) const = 0;
virtual QChar characterAt(int pos) const = 0;
};
class TEXTEDITOR_EXPORT ITextEditor : public Core::IEditor class TEXTEDITOR_EXPORT ITextEditor : public Core::IEditor
{ {
Q_OBJECT Q_OBJECT
@@ -68,7 +80,8 @@ public:
ITextEditor() {} ITextEditor() {}
virtual int find(const QString &string) const = 0; virtual ITextEditorDocument *textDocument();
/*! Returns the position at \a posOp in characters from the beginning of the document */ /*! Returns the position at \a posOp in characters from the beginning of the document */
virtual int position(PositionOperation posOp = Current, int at = -1) const = 0; virtual int position(PositionOperation posOp = Current, int at = -1) const = 0;
/*! Converts the \a pos in characters from beginning of document to \a line and \a column */ /*! Converts the \a pos in characters from beginning of document to \a line and \a column */
@@ -80,10 +93,7 @@ public:
/*! Returns the amount of visible lines (in characters) in the editor */ /*! Returns the amount of visible lines (in characters) in the editor */
virtual int rowCount() const = 0; virtual int rowCount() const = 0;
virtual QString contents() const = 0;
virtual QString selectedText() const = 0; virtual QString selectedText() const = 0;
virtual QString textAt(int pos, int length) const = 0;
virtual QChar characterAt(int pos) const = 0;
/*! Removes \a length characters to the right of the cursor. */ /*! Removes \a length characters to the right of the cursor. */
virtual void remove(int length) = 0; virtual void remove(int length) = 0;