forked from qt-creator/qt-creator
TextEditor: Shuffle some convenience functions
Remove rarely used ones, add a currentTextEditorWidget() Change-Id: I27b97c17927c71e07dc3b489785b7f2f76eb801e Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
This commit is contained in:
@@ -63,18 +63,18 @@ static void moveCursorToEndOfName(QTextCursor *tc)
|
|||||||
// We cannot depend on this since CppEditor plugin code is internal and requires building the implementation files ourselves
|
// We cannot depend on this since CppEditor plugin code is internal and requires building the implementation files ourselves
|
||||||
CPlusPlus::Symbol *AnalyzerUtils::findSymbolUnderCursor()
|
CPlusPlus::Symbol *AnalyzerUtils::findSymbolUnderCursor()
|
||||||
{
|
{
|
||||||
TextEditor::BaseTextEditor *editor = TextEditor::BaseTextEditor::currentTextEditor();
|
TextEditor::TextEditorWidget *widget = TextEditor::TextEditorWidget::currentTextEditorWidget();
|
||||||
if (!editor)
|
if (!widget)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
QTextCursor tc = editor->textCursor();
|
QTextCursor tc = widget->textCursor();
|
||||||
int line = 0;
|
int line = 0;
|
||||||
int column = 0;
|
int column = 0;
|
||||||
const int pos = tc.position();
|
const int pos = tc.position();
|
||||||
editor->convertPosition(pos, &line, &column);
|
widget->convertPosition(pos, &line, &column);
|
||||||
|
|
||||||
const CPlusPlus::Snapshot &snapshot = CppTools::CppModelManager::instance()->snapshot();
|
const CPlusPlus::Snapshot &snapshot = CppTools::CppModelManager::instance()->snapshot();
|
||||||
CPlusPlus::Document::Ptr doc = snapshot.document(editor->document()->filePath());
|
CPlusPlus::Document::Ptr doc = snapshot.document(widget->textDocument()->filePath());
|
||||||
QTC_ASSERT(doc, return 0);
|
QTC_ASSERT(doc, return 0);
|
||||||
|
|
||||||
// fetch the expression's code
|
// fetch the expression's code
|
||||||
|
|||||||
@@ -220,25 +220,22 @@ QString BeautifierPlugin::format(const QString &text, const Command &command,
|
|||||||
|
|
||||||
void BeautifierPlugin::formatCurrentFile(const Command &command)
|
void BeautifierPlugin::formatCurrentFile(const Command &command)
|
||||||
{
|
{
|
||||||
QPlainTextEdit *textEditor = 0;
|
TextEditorWidget *widget = TextEditorWidget::currentTextEditorWidget();
|
||||||
QString filePath;
|
if (!widget)
|
||||||
if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor()) {
|
|
||||||
textEditor = editor->editorWidget();
|
|
||||||
filePath = editor->document()->filePath();
|
|
||||||
}
|
|
||||||
if (!textEditor)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const QString sourceData = textEditor->toPlainText();
|
const QString sourceData = widget->toPlainText();
|
||||||
if (sourceData.isEmpty())
|
if (sourceData.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QFutureWatcher<FormatTask> *watcher = new QFutureWatcher<FormatTask>;
|
QFutureWatcher<FormatTask> *watcher = new QFutureWatcher<FormatTask>;
|
||||||
connect(textEditor->document(), SIGNAL(contentsChanged()), watcher, SLOT(cancel()));
|
connect(widget->textDocument(), &TextDocument::contentsChanged,
|
||||||
|
watcher, &QFutureWatcher<FormatTask>::cancel);
|
||||||
connect(watcher, SIGNAL(finished()), m_asyncFormatMapper, SLOT(map()));
|
connect(watcher, SIGNAL(finished()), m_asyncFormatMapper, SLOT(map()));
|
||||||
m_asyncFormatMapper->setMapping(watcher, watcher);
|
m_asyncFormatMapper->setMapping(watcher, watcher);
|
||||||
|
const QString filePath = widget->textDocument()->filePath();
|
||||||
watcher->setFuture(QtConcurrent::run(&BeautifierPlugin::formatAsync, this,
|
watcher->setFuture(QtConcurrent::run(&BeautifierPlugin::formatAsync, this,
|
||||||
FormatTask(textEditor, filePath, sourceData, command)));
|
FormatTask(widget, filePath, sourceData, command)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BeautifierPlugin::formatAsync(QFutureInterface<FormatTask> &future, FormatTask task)
|
void BeautifierPlugin::formatAsync(QFutureInterface<FormatTask> &future, FormatTask task)
|
||||||
|
|||||||
@@ -114,11 +114,11 @@ void ClangFormat::formatFile()
|
|||||||
|
|
||||||
void ClangFormat::formatSelectedText()
|
void ClangFormat::formatSelectedText()
|
||||||
{
|
{
|
||||||
TextEditor::BaseTextEditor *editor = TextEditor::BaseTextEditor::currentTextEditor();
|
TextEditor::TextEditorWidget *widget = TextEditor::TextEditorWidget::currentTextEditorWidget();
|
||||||
if (!editor)
|
if (!widget)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QTextCursor tc = editor->textCursor();
|
QTextCursor tc = widget->textCursor();
|
||||||
if (tc.hasSelection()) {
|
if (tc.hasSelection()) {
|
||||||
const int offset = tc.selectionStart();
|
const int offset = tc.selectionStart();
|
||||||
const int length = tc.selectionEnd() - offset;
|
const int length = tc.selectionEnd() - offset;
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ public:
|
|||||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, cursorPosition);
|
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, cursorPosition);
|
||||||
editor->setTextCursor(cursor);
|
editor->setTextCursor(cursor);
|
||||||
|
|
||||||
QTextDocument *qtextDocument = editor->qdocument();
|
QTextDocument *qtextDocument = editor->textDocument()->document();
|
||||||
CppRefactoringFilePtr cppRefactoringFile
|
CppRefactoringFilePtr cppRefactoringFile
|
||||||
= CppRefactoringChanges::file(editor->editorWidget(), document);
|
= CppRefactoringChanges::file(editor->editorWidget(), document);
|
||||||
|
|
||||||
|
|||||||
@@ -44,12 +44,12 @@
|
|||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
using namespace Core;
|
using namespace Core;
|
||||||
|
using namespace TextEditor;
|
||||||
|
|
||||||
namespace Debugger {
|
namespace Debugger {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
@@ -61,9 +61,9 @@ public:
|
|||||||
~SourceAgentPrivate();
|
~SourceAgentPrivate();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
QPointer<TextEditor::BaseTextEditor> editor;
|
QPointer<BaseTextEditor> editor;
|
||||||
QPointer<DebuggerEngine> engine;
|
QPointer<DebuggerEngine> engine;
|
||||||
TextEditor::TextMark *locationMark;
|
TextMark *locationMark;
|
||||||
QString path;
|
QString path;
|
||||||
QString producer;
|
QString producer;
|
||||||
};
|
};
|
||||||
@@ -73,7 +73,6 @@ SourceAgentPrivate::SourceAgentPrivate()
|
|||||||
, locationMark(0)
|
, locationMark(0)
|
||||||
, producer(QLatin1String("remote"))
|
, producer(QLatin1String("remote"))
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SourceAgentPrivate::~SourceAgentPrivate()
|
SourceAgentPrivate::~SourceAgentPrivate()
|
||||||
@@ -104,9 +103,6 @@ void SourceAgent::setSourceProducerName(const QString &name)
|
|||||||
void SourceAgent::setContent(const QString &filePath, const QString &content)
|
void SourceAgent::setContent(const QString &filePath, const QString &content)
|
||||||
{
|
{
|
||||||
QTC_ASSERT(d, return);
|
QTC_ASSERT(d, return);
|
||||||
using namespace Core;
|
|
||||||
using namespace TextEditor;
|
|
||||||
|
|
||||||
d->path = filePath;
|
d->path = filePath;
|
||||||
|
|
||||||
if (!d->editor) {
|
if (!d->editor) {
|
||||||
@@ -143,9 +139,11 @@ void SourceAgent::updateLocationMarker()
|
|||||||
d->locationMark = 0;
|
d->locationMark = 0;
|
||||||
if (d->engine->stackHandler()->currentFrame().file == d->path) {
|
if (d->engine->stackHandler()->currentFrame().file == d->path) {
|
||||||
int lineNumber = d->engine->stackHandler()->currentFrame().line;
|
int lineNumber = d->engine->stackHandler()->currentFrame().line;
|
||||||
d->locationMark = new TextEditor::TextMark(QString(), lineNumber);
|
|
||||||
d->locationMark->setIcon(Internal::locationMarkIcon());
|
d->locationMark = new TextMark(QString(), lineNumber);
|
||||||
d->locationMark->setPriority(TextEditor::TextMark::HighPriority);
|
d->locationMark->setIcon(locationMarkIcon());
|
||||||
|
d->locationMark->setPriority(TextMark::HighPriority);
|
||||||
|
|
||||||
d->editor->textDocument()->addMark(d->locationMark);
|
d->editor->textDocument()->addMark(d->locationMark);
|
||||||
QTextCursor tc = d->editor->textCursor();
|
QTextCursor tc = d->editor->textCursor();
|
||||||
QTextBlock block = tc.document()->findBlockByNumber(lineNumber - 1);
|
QTextBlock block = tc.document()->findBlockByNumber(lineNumber - 1);
|
||||||
|
|||||||
@@ -1018,6 +1018,12 @@ TextDocumentPtr TextEditorWidget::textDocumentPtr() const
|
|||||||
return d->m_document;
|
return d->m_document;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextEditorWidget *TextEditorWidget::currentTextEditorWidget()
|
||||||
|
{
|
||||||
|
BaseTextEditor *editor = qobject_cast<BaseTextEditor *>(EditorManager::currentEditor());
|
||||||
|
return editor ? editor->editorWidget() : 0;
|
||||||
|
}
|
||||||
|
|
||||||
void TextEditorWidgetPrivate::editorContentsChange(int position, int charsRemoved, int charsAdded)
|
void TextEditorWidgetPrivate::editorContentsChange(int position, int charsRemoved, int charsAdded)
|
||||||
{
|
{
|
||||||
if (m_animator)
|
if (m_animator)
|
||||||
@@ -6702,11 +6708,6 @@ void BaseTextEditor::convertPosition(int pos, int *line, int *column) const
|
|||||||
editorWidget()->convertPosition(pos, line, column);
|
editorWidget()->convertPosition(pos, line, column);
|
||||||
}
|
}
|
||||||
|
|
||||||
QRect BaseTextEditor::cursorRect(int pos) const
|
|
||||||
{
|
|
||||||
return editorWidget()->cursorRect(pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
QString BaseTextEditor::selectedText() const
|
QString BaseTextEditor::selectedText() const
|
||||||
{
|
{
|
||||||
return editorWidget()->selectedText();
|
return editorWidget()->selectedText();
|
||||||
@@ -7124,11 +7125,6 @@ TextEditorWidget *BaseTextEditor::editorWidget() const
|
|||||||
return static_cast<TextEditorWidget *>(m_widget.data());
|
return static_cast<TextEditorWidget *>(m_widget.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextDocument *BaseTextEditor::qdocument() const
|
|
||||||
{
|
|
||||||
return textDocument()->document();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseTextEditor::setTextCursor(const QTextCursor &cursor)
|
void BaseTextEditor::setTextCursor(const QTextCursor &cursor)
|
||||||
{
|
{
|
||||||
editorWidget()->setTextCursor(cursor);
|
editorWidget()->setTextCursor(cursor);
|
||||||
|
|||||||
@@ -147,7 +147,6 @@ public:
|
|||||||
TextDocument *textDocument() const;
|
TextDocument *textDocument() const;
|
||||||
|
|
||||||
// Some convenience text access
|
// Some convenience text access
|
||||||
QTextDocument *qdocument() const;
|
|
||||||
void setTextCursor(const QTextCursor &cursor);
|
void setTextCursor(const QTextCursor &cursor);
|
||||||
QTextCursor textCursor() const;
|
QTextCursor textCursor() const;
|
||||||
QChar characterAt(int pos) const;
|
QChar characterAt(int pos) const;
|
||||||
@@ -184,9 +183,6 @@ public:
|
|||||||
/*! 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 */
|
||||||
virtual void convertPosition(int pos, int *line, int *column) const;
|
virtual void convertPosition(int pos, int *line, int *column) const;
|
||||||
|
|
||||||
/*! Returns the cursor rectangle in pixels at \a pos, or current position if \a pos = -1 */
|
|
||||||
virtual QRect cursorRect(int pos = -1) const;
|
|
||||||
|
|
||||||
virtual QString selectedText() const;
|
virtual QString selectedText() const;
|
||||||
|
|
||||||
/*! Removes \a length characters to the right of the cursor. */
|
/*! Removes \a length characters to the right of the cursor. */
|
||||||
@@ -574,6 +570,8 @@ public:
|
|||||||
QString contextHelpId();
|
QString contextHelpId();
|
||||||
void setContextHelpId(const QString &id);
|
void setContextHelpId(const QString &id);
|
||||||
|
|
||||||
|
static TextEditorWidget *currentTextEditorWidget();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/*!
|
/*!
|
||||||
Reimplement this function to enable code navigation.
|
Reimplement this function to enable code navigation.
|
||||||
|
|||||||
Reference in New Issue
Block a user