Commit 97f8850a authored by Roberto Raggi's avatar Roberto Raggi

Introduced SemanticHighlighter

Done with Thorbjørn Lindeijer
parent 206adf2d
This diff is collapsed.
......@@ -31,10 +31,14 @@
#define CPPEDITOR_H
#include "cppeditorenums.h"
#include <ASTfwd.h>
#include <cplusplus/CppDocument.h>
#include <texteditor/basetexteditor.h>
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <QtCore/QWaitCondition>
QT_BEGIN_NAMESPACE
class QComboBox;
class QSortFilterProxyModel;
......@@ -57,6 +61,99 @@ namespace CppEditor {
namespace Internal {
class CPPEditor;
class SemanticHighlighter;
class SemanticInfo
{
public:
struct Use {
CPlusPlus::NameAST *name;
unsigned line;
unsigned column;
unsigned length;
Use() {}
Use(CPlusPlus::NameAST *name, unsigned line, unsigned column, unsigned length)
: name(name), line(line), column(column), length(length) {}
};
typedef QHash<CPlusPlus::Symbol *, QList<Use> > LocalUseMap;
typedef QHashIterator<CPlusPlus::Symbol *, QList<Use> > LocalUseIterator;
typedef QHash<CPlusPlus::Identifier *, QList<Use> > ExternalUseMap;
typedef QHashIterator<CPlusPlus::Identifier *, QList<Use> > ExternalUseIterator;
SemanticInfo()
: revision(0)
{ }
int revision;
CPlusPlus::Document::Ptr doc;
LocalUseMap localUses;
ExternalUseMap externalUses;
};
class SemanticHighlighter: public QThread
{
Q_OBJECT
public:
SemanticHighlighter(QObject *parent = 0);
virtual ~SemanticHighlighter();
void abort();
struct Source
{
CPlusPlus::Snapshot snapshot;
QString fileName;
QString code;
int line;
int column;
int revision;
Source()
: line(0), column(0), revision(0)
{ }
Source(const CPlusPlus::Snapshot &snapshot,
const QString &fileName,
const QString &code,
int line, int column,
int revision)
: snapshot(snapshot), fileName(fileName),
code(code), line(line), column(column),
revision(revision)
{ }
void clear()
{
snapshot.clear();
fileName.clear();
code.clear();
line = 0;
column = 0;
revision = 0;
}
};
SemanticInfo semanticInfo(const Source &source) const;
void rehighlight(const Source &source);
Q_SIGNALS:
void changed(const SemanticInfo &semanticInfo);
protected:
virtual void run();
private:
QMutex m_mutex;
QWaitCondition m_condition;
bool m_done;
Source m_source;
};
class CPPEditorEditable : public TextEditor::BaseTextEditorEditable
{
......@@ -89,7 +186,7 @@ public:
void indentInsertedText(const QTextCursor &tc);
public slots:
public Q_SLOTS:
virtual void setFontSettings(const TextEditor::FontSettings &);
virtual void setDisplaySettings(const TextEditor::DisplaySettings &);
void setSortedMethodOverview(bool sort);
......@@ -116,7 +213,7 @@ protected:
// Rertuns true if key triggers anindent.
virtual bool isElectricCharacter(const QChar &ch) const;
private slots:
private Q_SLOTS:
void updateFileName();
void jumpToMethod(int index);
void updateMethodBoxIndex();
......@@ -129,6 +226,9 @@ private slots:
void renameInPlace();
void onContentsChanged(int position, int charsRemoved, int charsAdded);
void semanticRehighlight();
void updateSemanticInfo(const SemanticInfo &semanticInfo);
private:
bool sortedMethodOverview() const;
CPlusPlus::Symbol *findDefinition(CPlusPlus::Symbol *symbol);
......@@ -141,6 +241,8 @@ private:
QTextCursor moveToPreviousToken(QTextCursor::MoveMode mode) const;
QTextCursor moveToNextToken(QTextCursor::MoveMode mode) const;
SemanticHighlighter::Source currentSource();
void createToolBar(CPPEditorEditable *editable);
enum EditOperation {
......@@ -198,8 +300,11 @@ private:
QList<QTextEdit::ExtraSelection> m_renameSelections;
int m_currentRenameSelection;
bool m_inRename;
SemanticHighlighter *m_semanticHighlighter;
};
} // namespace Internal
} // namespace CppEditor
......
......@@ -131,7 +131,6 @@ class Class;
class Enum;
class ForwardClassDeclaration;
class Use;
class Token;
CPLUSPLUS_END_NAMESPACE
......
......@@ -63,10 +63,7 @@ Scope::Scope(ScopedSymbol *owner)
_allocatedSymbols(0),
_symbolCount(-1),
_hash(0),
_hashSize(0),
_uses(0),
_allocatedUses(0),
_useCount(-1)
_hashSize(0)
{ }
Scope::~Scope()
......@@ -75,8 +72,6 @@ Scope::~Scope()
free(_symbols);
if (_hash)
free(_hash);
if (_uses)
free(_uses);
}
ScopedSymbol *Scope::owner() const
......@@ -300,30 +295,7 @@ Scope::iterator Scope::firstSymbol() const
Scope::iterator Scope::lastSymbol() const
{ return _symbols + _symbolCount + 1; }
unsigned Scope::useCount() const
{ return _useCount + 1; }
Use *Scope::useAt(unsigned index) const
{ return &_uses[index]; }
void Scope::addUse(unsigned sourceOffset, Name *name)
{
#ifdef CPLUSPLUS_WITH_USES
if (++_useCount == _allocatedUses) {
_allocatedUses += 4;
_uses = reinterpret_cast<Use *>(realloc(_uses, _allocatedUses * sizeof(Use)));
}
Symbol *lastVisibleSymbol;
if (_symbolCount == -1)
lastVisibleSymbol = owner();
else
lastVisibleSymbol = _symbols[_symbolCount];
_uses[_useCount].init(sourceOffset, name, lastVisibleSymbol);
#else
(void) sourceOffset;
(void) name;
#endif
}
void Scope::addUse(unsigned, Name *)
{ }
CPLUSPLUS_END_NAMESPACE
......@@ -160,8 +160,6 @@ public:
Symbol *lookat(Identifier *id) const;
Symbol *lookat(int operatorId) const;
unsigned useCount() const;
Use *useAt(unsigned index) const;
void addUse(unsigned sourceOffset, Name *name);
private:
......@@ -182,10 +180,6 @@ private:
Symbol **_hash;
int _hashSize;
Use *_uses;
int _allocatedUses;
int _useCount;
};
CPLUSPLUS_END_NAMESPACE
......
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