Fixes: Add a combo for diff file browsing, bring diff editors to front, exclude qconfig.pri from files examined by p4.

Task: 234842
This commit is contained in:
Friedemann Kleint
2009-03-10 11:02:12 +01:00
parent f2550c2ca7
commit 6ffdf7cf40
6 changed files with 172 additions and 12 deletions

View File

@@ -106,6 +106,27 @@ static inline QString debugCodec(const QTextCodec *c)
return c ? QString::fromAscii(c->name()) : QString::fromAscii("Null codec"); return c ? QString::fromAscii(c->name()) : QString::fromAscii("Null codec");
} }
// Return the project files relevant for VCS
static const QStringList currentProjectFiles(QString *name)
{
QStringList files = VCSBase::VCSBaseSubmitEditor::currentProjectFiles(true, name);
if (!files.empty()) {
// Filter out mkspecs/qconfig.pri
QString exclusion = QLatin1String("mkspecs");
exclusion += QDir::separator();
exclusion += QLatin1String("qconfig.pri");
for (QStringList::iterator it = files.begin(); it != files.end(); ) {
if (it->endsWith(exclusion)) {
it = files.erase(it);
break;
} else {
++it;
}
}
}
return files;
}
const char * const PerforcePlugin::PERFORCE_MENU = "Perforce.Menu"; const char * const PerforcePlugin::PERFORCE_MENU = "Perforce.Menu";
const char * const PerforcePlugin::EDIT = "Perforce.Edit"; const char * const PerforcePlugin::EDIT = "Perforce.Edit";
const char * const PerforcePlugin::ADD = "Perforce.Add"; const char * const PerforcePlugin::ADD = "Perforce.Add";
@@ -450,7 +471,7 @@ void PerforcePlugin::diffCurrentFile()
void PerforcePlugin::diffCurrentProject() void PerforcePlugin::diffCurrentProject()
{ {
QString name; QString name;
const QStringList nativeFiles = VCSBase::VCSBaseSubmitEditor::currentProjectFiles(true, &name); const QStringList nativeFiles = currentProjectFiles(&name);
p4Diff(nativeFiles, name); p4Diff(nativeFiles, name);
} }
@@ -509,7 +530,7 @@ void PerforcePlugin::submit()
// Assemble file list of project // Assemble file list of project
QString name; QString name;
const QStringList nativeFiles = VCSBase::VCSBaseSubmitEditor::currentProjectFiles(true, &name); const QStringList nativeFiles = currentProjectFiles(&name);
PerforceResponse result2 = runP4Cmd(QStringList(QLatin1String("fstat")), nativeFiles, PerforceResponse result2 = runP4Cmd(QStringList(QLatin1String("fstat")), nativeFiles,
CommandToWindow|StdErrToWindow|ErrorToWindow); CommandToWindow|StdErrToWindow|ErrorToWindow);
if (result2.error) { if (result2.error) {
@@ -846,7 +867,9 @@ Core::IEditor * PerforcePlugin::showOutputInEditor(const QString& title, const Q
e->setSuggestedFileName(s); e->setSuggestedFileName(s);
if (codec) if (codec)
e->setCodec(codec); e->setCodec(codec);
return e->editableInterface(); Core::IEditor *ie = e->editableInterface();
Core::EditorManager::instance()->activateEditor(ie);
return ie;
} }
QStringList PerforcePlugin::environment() const QStringList PerforcePlugin::environment() const

View File

@@ -1039,7 +1039,9 @@ Core::IEditor * SubversionPlugin::showOutputInEditor(const QString& title, const
e->setSource(source); e->setSource(source);
if (codec) if (codec)
e->setCodec(codec); e->setCodec(codec);
return e->editableInterface(); Core::IEditor *ie = e->editableInterface();
Core::EditorManager::instance()->activateEditor(ie);
return ie;
} }
SubversionSettings SubversionPlugin::settings() const SubversionSettings SubversionPlugin::settings() const

View File

@@ -115,4 +115,9 @@ void DiffHighlighter::setFormats(const QVector<QTextCharFormat> &s)
} }
} }
QRegExp DiffHighlighter::filePattern() const
{
return m_d->m_filePattern;
}
} // namespace VCSBase } // namespace VCSBase

View File

@@ -79,6 +79,8 @@ public:
// Set formats from a sequence of type QTextCharFormat // Set formats from a sequence of type QTextCharFormat
void setFormats(const QVector<QTextCharFormat> &s); void setFormats(const QVector<QTextCharFormat> &s);
QRegExp filePattern() const;
private: private:
DiffHighlighterPrivate *m_d; DiffHighlighterPrivate *m_d;
}; };

View File

@@ -35,12 +35,14 @@
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/uniqueidmanager.h> #include <coreplugin/uniqueidmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <projectexplorer/editorconfiguration.h> #include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <texteditor/fontsettings.h> #include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h> #include <texteditor/texteditorconstants.h>
#include <utils/qtcassert.h>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
@@ -55,10 +57,13 @@
#include <QtGui/QMenu> #include <QtGui/QMenu>
#include <QtGui/QTextCursor> #include <QtGui/QTextCursor>
#include <QtGui/QTextEdit> #include <QtGui/QTextEdit>
#include <QtGui/QComboBox>
#include <QtGui/QToolBar>
namespace VCSBase { namespace VCSBase {
// VCSBaseEditorEditable: An editable with no support for duplicates // VCSBaseEditorEditable: An editable with no support for duplicates
// Creates a browse combo in the toolbar for diff output.
class VCSBaseEditorEditable : public TextEditor::BaseTextEditorEditable class VCSBaseEditorEditable : public TextEditor::BaseTextEditorEditable
{ {
public: public:
@@ -73,12 +78,12 @@ public:
private: private:
const char *m_kind; const char *m_kind;
QList<int> m_context; QList<int> m_context;
}; };
VCSBaseEditorEditable::VCSBaseEditorEditable(VCSBaseEditor *editor, VCSBaseEditorEditable::VCSBaseEditorEditable(VCSBaseEditor *editor,
const VCSBaseEditorParameters *type) const VCSBaseEditorParameters *type) :
: BaseTextEditorEditable(editor), m_kind(type->kind) BaseTextEditorEditable(editor),
m_kind(type->kind)
{ {
Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance(); Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
m_context << uidm->uniqueIdentifier(QLatin1String(type->context)) m_context << uidm->uniqueIdentifier(QLatin1String(type->context))
@@ -90,6 +95,34 @@ QList<int> VCSBaseEditorEditable::context() const
return m_context; return m_context;
} }
// Diff editable: creates a browse combo in the toolbar for diff output.
class VCSBaseDiffEditorEditable : public VCSBaseEditorEditable
{
public:
VCSBaseDiffEditorEditable(VCSBaseEditor *, const VCSBaseEditorParameters *type);
virtual QToolBar *toolBar() { return m_toolBar; }
QComboBox *diffFileBrowseComboBox() const { return m_diffFileBrowseComboBox; }
private:
QComboBox *m_diffFileBrowseComboBox;
QToolBar *m_toolBar;
};
VCSBaseDiffEditorEditable::VCSBaseDiffEditorEditable(VCSBaseEditor *e, const VCSBaseEditorParameters *type) :
VCSBaseEditorEditable(e, type),
m_diffFileBrowseComboBox(new QComboBox),
m_toolBar(new QToolBar)
{
m_diffFileBrowseComboBox->setMinimumContentsLength(20);
m_diffFileBrowseComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
// Make the combo box prefer to expand
QSizePolicy policy = m_diffFileBrowseComboBox->sizePolicy();
policy.setHorizontalPolicy(QSizePolicy::Expanding);
m_diffFileBrowseComboBox->setSizePolicy(policy);
m_toolBar->addWidget(m_diffFileBrowseComboBox);
}
// ----------- VCSBaseEditorPrivate // ----------- VCSBaseEditorPrivate
struct VCSBaseEditorPrivate struct VCSBaseEditorPrivate
@@ -97,13 +130,20 @@ struct VCSBaseEditorPrivate
VCSBaseEditorPrivate(const VCSBaseEditorParameters *type, QObject *parent); VCSBaseEditorPrivate(const VCSBaseEditorParameters *type, QObject *parent);
const VCSBaseEditorParameters *m_parameters; const VCSBaseEditorParameters *m_parameters;
QAction *m_describeAction; QAction *m_describeAction;
QString m_currentChange; QString m_currentChange;
QString m_source; QString m_source;
QRegExp m_diffFilePattern;
QList<int> m_diffSections; // line number where this section starts
int m_cursorLine;
}; };
VCSBaseEditorPrivate::VCSBaseEditorPrivate(const VCSBaseEditorParameters *type, QObject *parent) VCSBaseEditorPrivate::VCSBaseEditorPrivate(const VCSBaseEditorParameters *type, QObject *parent) :
: m_parameters(type), m_describeAction(new QAction(parent)) m_parameters(type),
m_describeAction(new QAction(parent)),
m_cursorLine(-1)
{ {
} }
@@ -131,8 +171,13 @@ void VCSBaseEditor::init()
// Annotation highlighting depends on contents, which is set later on // Annotation highlighting depends on contents, which is set later on
connect(this, SIGNAL(textChanged()), this, SLOT(slotActivateAnnotation())); connect(this, SIGNAL(textChanged()), this, SLOT(slotActivateAnnotation()));
break; break;
case DiffOutput: case DiffOutput: {
baseTextDocument()->setSyntaxHighlighter(createDiffHighlighter()); DiffHighlighter *dh = createDiffHighlighter();
baseTextDocument()->setSyntaxHighlighter(dh);
d->m_diffFilePattern = dh->filePattern();
connect(this, SIGNAL(textChanged()), this, SLOT(slotPopulateDiffBrowser()));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(slotDiffCursorPositionChanged()));
}
break; break;
} }
} }
@@ -178,7 +223,87 @@ bool VCSBaseEditor::isModified() const
TextEditor::BaseTextEditorEditable *VCSBaseEditor::createEditableInterface() TextEditor::BaseTextEditorEditable *VCSBaseEditor::createEditableInterface()
{ {
return new VCSBaseEditorEditable(this, d->m_parameters); if (d->m_parameters->type != DiffOutput)
return new VCSBaseEditorEditable(this, d->m_parameters);
// Diff: set up diff file browsing
VCSBaseDiffEditorEditable *de = new VCSBaseDiffEditorEditable(this, d->m_parameters);
QComboBox *diffBrowseComboBox = de->diffFileBrowseComboBox();
connect(diffBrowseComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDiffBrowse(int)));
return de;
}
void VCSBaseEditor::slotPopulateDiffBrowser()
{
VCSBaseDiffEditorEditable *de = static_cast<VCSBaseDiffEditorEditable*>(editableInterface());
QComboBox *diffBrowseComboBox = de->diffFileBrowseComboBox();
diffBrowseComboBox->clear();
d->m_diffSections.clear();
// Create a list of section line numbers (diffed files)
// and populate combo with filenames.
const QTextBlock cend = document()->end();
int lineNumber = 0;
QString lastFileName;
for (QTextBlock it = document()->begin(); it != cend; it = it.next(), lineNumber++) {
const QString text = it.text();
// Check for a new diff section (not repeating the last filename)
if (d->m_diffFilePattern.exactMatch(text)) {
const QString file = fileNameFromDiffSpecification(it);
if (!file.isEmpty() && lastFileName != file) {
lastFileName = file;
// ignore any headers
d->m_diffSections.push_back(d->m_diffSections.empty() ? 0 : lineNumber);
diffBrowseComboBox->addItem(QFileInfo(file).fileName());
}
}
}
}
void VCSBaseEditor::slotDiffBrowse(int index)
{
// goto diffed file as indicated by index/line number
if (index < 0 || index >= d->m_diffSections.size())
return;
const int lineNumber = d->m_diffSections.at(index);
Core::EditorManager *editorManager = Core::EditorManager::instance();
editorManager->addCurrentPositionToNavigationHistory(true);
gotoLine(lineNumber + 1, 0); // TextEdit uses 1..n convention
editorManager->addCurrentPositionToNavigationHistory();
}
// Locate a line number in the list of diff sections.
static int sectionOfLine(int line, const QList<int> &sections)
{
const int sectionCount = sections.size();
if (!sectionCount)
return -1;
// The section at s indicates where the section begins.
for (int s = 0; s < sectionCount; s++) {
if (line < sections.at(s))
return s - 1;
}
return sectionCount - 1;
}
void VCSBaseEditor::slotDiffCursorPositionChanged()
{
// Adapt diff file browse combo to new position
// if the cursor goes across a file line.
QTC_ASSERT(d->m_parameters->type == DiffOutput, return)
const int newCursorLine = textCursor().blockNumber();
if (newCursorLine == d->m_cursorLine)
return;
// Which section does it belong to?
d->m_cursorLine = newCursorLine;
const int section = sectionOfLine(d->m_cursorLine, d->m_diffSections);
if (section != -1) {
VCSBaseDiffEditorEditable *de = static_cast<VCSBaseDiffEditorEditable*>(editableInterface());
QComboBox *diffBrowseComboBox = de->diffFileBrowseComboBox();
if (diffBrowseComboBox->currentIndex() != section) {
const bool blocked = diffBrowseComboBox->blockSignals(true);
diffBrowseComboBox->setCurrentIndex(section);
diffBrowseComboBox->blockSignals(blocked);
}
}
} }
void VCSBaseEditor::contextMenuEvent(QContextMenuEvent *e) void VCSBaseEditor::contextMenuEvent(QContextMenuEvent *e)

View File

@@ -142,6 +142,9 @@ public slots:
private slots: private slots:
void describe(); void describe();
void slotActivateAnnotation(); void slotActivateAnnotation();
void slotPopulateDiffBrowser();
void slotDiffBrowse(int);
void slotDiffCursorPositionChanged();
private: private:
// Implement to return a set of change identifiers in // Implement to return a set of change identifiers in