Commit fa4e839b authored by Nikolai Kosjar's avatar Nikolai Kosjar Committed by Erik Verbruggen

CppEditor: Move two functions to cpptoolsreuse.{h,cpp}

They do not really depend on CppEditorWidget and will be re-used in a
follow-up change.

    CppEditorWidget::identifierUnderCursor()
    CppEditorWidget::findCanonicalMacro()

Change-Id: I553bb68694fb4b5f1baa331debf33ae234e3dedf
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent a87fa1e2
......@@ -296,26 +296,6 @@ void CppEditorWidget::onDocumentUpdated()
d->m_cppEditorOutline->update();
}
const Macro *CppEditorWidget::findCanonicalMacro(const QTextCursor &cursor, Document::Ptr doc) const
{
if (!doc)
return 0;
int line, col;
convertPosition(cursor.position(), &line, &col);
if (const Macro *macro = doc->findMacroDefinitionAt(line)) {
QTextCursor macroCursor = cursor;
const QByteArray name = identifierUnderCursor(&macroCursor).toUtf8();
if (macro->name() == name)
return macro;
} else if (const Document::MacroUse *use = doc->findMacroUseAt(cursor.position())) {
return &use->macro();
}
return 0;
}
void CppEditorWidget::findUsages()
{
if (!d->m_modelManager)
......@@ -325,7 +305,7 @@ void CppEditorWidget::findUsages()
info.snapshot = CppModelManagerInterface::instance()->snapshot();
info.snapshot.insert(info.doc);
if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
if (const Macro *macro = CppTools::findCanonicalMacro(textCursor(), info.doc)) {
d->m_modelManager->findMacroUsages(*macro);
} else {
CanonicalSymbol cs(this, info.doc, info.snapshot);
......@@ -344,7 +324,7 @@ void CppEditorWidget::renameUsages(const QString &replacement)
info.snapshot = CppModelManagerInterface::instance()->snapshot();
info.snapshot.insert(info.doc);
if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
if (const Macro *macro = CppTools::findCanonicalMacro(textCursor(), info.doc)) {
d->m_modelManager->renameMacroUsages(*macro, replacement);
} else {
CanonicalSymbol cs(this, info.doc, info.snapshot);
......@@ -411,7 +391,7 @@ void CppEditorWidget::markSymbols(const QTextCursor &tc, const SemanticInfo &inf
if (!info.doc)
return;
const QTextCharFormat &occurrencesFormat = textCharFormat(TextEditor::C_OCCURRENCES);
if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
if (const Macro *macro = CppTools::findCanonicalMacro(textCursor(), info.doc)) {
QList<QTextEdit::ExtraSelection> selections;
//Macro definition
......@@ -640,13 +620,6 @@ void CppEditorWidget::switchDeclarationDefinition(bool inNextSplit)
openCppEditorAt(symbolLink, inNextSplit != alwaysOpenLinksInNextSplit());
}
QString CppEditorWidget::identifierUnderCursor(QTextCursor *macroCursor)
{
macroCursor->movePosition(QTextCursor::StartOfWord);
macroCursor->movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
return macroCursor->selectedText();
}
CppEditorWidget::Link CppEditorWidget::findLinkAt(const QTextCursor &cursor, bool resolveTarget,
bool inNextSplit)
{
......
......@@ -73,7 +73,6 @@ class CppEditorWidget : public TextEditor::BaseTextEditorWidget
public:
static Link linkToSymbol(CPlusPlus::Symbol *symbol);
static QString identifierUnderCursor(QTextCursor *macroCursor);
public:
CppEditorWidget(TextEditor::BaseTextDocumentPtr doc);
......@@ -155,9 +154,6 @@ private:
unsigned editorRevision() const;
bool isOutdated() const;
const CPlusPlus::Macro *findCanonicalMacro(const QTextCursor &cursor,
CPlusPlus::Document::Ptr doc) const;
QTextCharFormat textCharFormat(TextEditor::TextStyle category);
void markSymbols(const QTextCursor &tc, const CppTools::SemanticInfo &info);
......
......@@ -592,7 +592,7 @@ BaseTextEditorWidget::Link FollowSymbolUnderCursor::findLink(const QTextCursor &
const Macro *macro = doc->findMacroDefinitionAt(line);
if (macro) {
QTextCursor macroCursor = cursor;
const QByteArray name = CppEditorWidget::identifierUnderCursor(&macroCursor).toUtf8();
const QByteArray name = CppTools::identifierUnderCursor(&macroCursor).toUtf8();
if (macro->name() == name)
return link; //already on definition!
} else if (const Document::MacroUse *use = doc->findMacroUseAt(endOfToken - 1)) {
......@@ -723,7 +723,7 @@ BaseTextEditorWidget::Link FollowSymbolUnderCursor::findLink(const QTextCursor &
// Handle macro uses
QTextCursor macroCursor = cursor;
const QByteArray name = CppEditorWidget::identifierUnderCursor(&macroCursor).toUtf8();
const QByteArray name = CppTools::identifierUnderCursor(&macroCursor).toUtf8();
link = findMacroLink(name, documentFromSemanticInfo);
if (link.hasValidTarget()) {
link.linkTextStart = macroCursor.selectionStart();
......
......@@ -30,16 +30,16 @@
#include "cpptoolsreuse.h"
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/convenience.h>
#include <cplusplus/Overview.h>
#include <cplusplus/LookupContext.h>
#include <utils/qtcassert.h>
#include <QSet>
#include <QTextDocument>
#include <QTextCursor>
#include <QStringRef>
#include <QTextCursor>
#include <QTextDocument>
using namespace CPlusPlus;
......@@ -200,4 +200,30 @@ void switchHeaderSource()
Core::EditorManager::openEditor(otherFile);
}
QString identifierUnderCursor(QTextCursor *cursor)
{
cursor->movePosition(QTextCursor::StartOfWord);
cursor->movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
return cursor->selectedText();
}
const Macro *findCanonicalMacro(const QTextCursor &cursor, Document::Ptr document)
{
QTC_ASSERT(document, return 0);
int line, column;
TextEditor::Convenience::convertPosition(cursor.document(), cursor.position(), &line, &column);
if (const Macro *macro = document->findMacroDefinitionAt(line)) {
QTextCursor macroCursor = cursor;
const QByteArray name = CppTools::identifierUnderCursor(&macroCursor).toUtf8();
if (macro->name() == name)
return macro;
} else if (const Document::MacroUse *use = document->findMacroUseAt(cursor.position())) {
return &use->macro();
}
return 0;
}
} // CppTools
......@@ -32,11 +32,16 @@
#include "cpptools_global.h"
QT_FORWARD_DECLARE_CLASS(QChar)
QT_FORWARD_DECLARE_CLASS(QTextCursor)
QT_FORWARD_DECLARE_CLASS(QStringRef)
#include <cplusplus/CppDocument.h>
QT_BEGIN_NAMESPACE
class QChar;
class QStringRef;
class QTextCursor;
QT_END_NAMESPACE
namespace CPlusPlus {
class Macro;
class Symbol;
class LookupContext;
} // namespace CPlusPlus
......@@ -46,15 +51,20 @@ namespace CppTools {
void CPPTOOLS_EXPORT moveCursorToEndOfIdentifier(QTextCursor *tc);
void CPPTOOLS_EXPORT moveCursorToStartOfIdentifier(QTextCursor *tc);
bool CPPTOOLS_EXPORT isOwnershipRAIIType(CPlusPlus::Symbol *symbol,
const CPlusPlus::LookupContext &context);
bool CPPTOOLS_EXPORT isQtKeyword(const QStringRef &text);
bool CPPTOOLS_EXPORT isValidAsciiIdentifierChar(const QChar &ch);
bool CPPTOOLS_EXPORT isValidFirstIdentifierChar(const QChar &ch);
bool CPPTOOLS_EXPORT isValidIdentifierChar(const QChar &ch);
bool CPPTOOLS_EXPORT isValidIdentifier(const QString &s);
bool CPPTOOLS_EXPORT isQtKeyword(const QStringRef &text);
QString CPPTOOLS_EXPORT identifierUnderCursor(QTextCursor *cursor);
bool CPPTOOLS_EXPORT isOwnershipRAIIType(CPlusPlus::Symbol *symbol,
const CPlusPlus::LookupContext &context);
const CPlusPlus::Macro CPPTOOLS_EXPORT *findCanonicalMacro(const QTextCursor &cursor,
CPlusPlus::Document::Ptr document);
QString CPPTOOLS_EXPORT correspondingHeaderOrSource(const QString &fileName, bool *wasHeader = 0);
void CPPTOOLS_EXPORT switchHeaderSource();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment