Commit d81d90a6 authored by dt's avatar dt

EditorManager: add a flag for embedded linenumbers

And make use of it to accept on the command line the patterns:
"filename" "+45", "filename" ":23", "filename:12" and "filename+42"

Task-Nr: QTCREATORBUG-2428
parent 034bb72d
......@@ -228,9 +228,9 @@ void CoreImpl::removeContextObject(IContext *context)
m_mainwindow->removeContextObject(context);
}
void CoreImpl::openFiles(const QStringList &arguments, bool switchMode)
void CoreImpl::openFiles(const QStringList &arguments, ICore::OpenFilesFlags flags)
{
m_mainwindow->openFiles(arguments, switchMode);
m_mainwindow->openFiles(arguments, flags);
}
void CoreImpl::emitNewItemsDialogRequested()
......
......@@ -88,7 +88,7 @@ public:
void addContextObject(IContext *context);
void removeContextObject(IContext *context);
void openFiles(const QStringList &fileNames, bool switchMode);
void openFiles(const QStringList &fileNames, ICore::OpenFilesFlags flags);
void emitNewItemsDialogRequested();
......
......@@ -100,7 +100,7 @@ void CorePlugin::extensionsInitialized()
void CorePlugin::remoteCommand(const QStringList & /* options */, const QStringList &args)
{
m_mainWindow->openFiles(args, true);
m_mainWindow->openFiles(args, ICore::SwitchMode);
m_mainWindow->activateWindow();
}
......
......@@ -1179,31 +1179,53 @@ IEditor *EditorManager::openEditor(const QString &fileName, const QString &edito
return openEditor(0, fileName, editorId, flags, newEditor);
}
int extractLineNumber(QString *fileName)
{
int i = fileName->length() - 1;
for (; i >= 0; --i) {
if (!fileName->at(i).isNumber())
break;
}
if (i == -1)
return -1;
if (fileName->at(i) == ':' || fileName->at(i) == '+') {
int result = fileName->mid(i+1).toInt();
*fileName = fileName->left(i);
return result;
}
return -1;
}
IEditor *EditorManager::openEditor(Core::Internal::EditorView *view, const QString &fileName,
const QString &editorId, OpenEditorFlags flags, bool *newEditor)
{
if (debugEditorManager)
qDebug() << Q_FUNC_INFO << fileName << editorId;
if (fileName.isEmpty())
QString fn = fileName;
int lineNumber = -1;
if (flags && EditorManager::CanContainLineNumber)
lineNumber = extractLineNumber(&fn);
if (fn.isEmpty())
return 0;
if (newEditor)
*newEditor = false;
const QList<IEditor *> editors = editorsForFileName(fileName);
const QList<IEditor *> editors = editorsForFileName(fn);
if (!editors.isEmpty())
return activateEditor(view, editors.first(), flags);
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
IEditor *editor = createEditor(editorId, fileName);
IEditor *editor = createEditor(editorId, fn);
// If we could not open the file in the requested editor, fall
// back to the default editor:
if (!editor)
editor = createEditor(QString(), fileName);
if (!editor || !editor->open(fileName)) {
editor = createEditor(QString(), fn);
if (!editor || !editor->open(fn)) {
QApplication::restoreOverrideCursor();
QMessageBox::critical(m_d->m_core->mainWindow(), tr("Opening File"), tr("Cannot open file %1!").arg(QDir::toNativeSeparators(fileName)));
QMessageBox::critical(m_d->m_core->mainWindow(), tr("Opening File"), tr("Cannot open file %1!").arg(QDir::toNativeSeparators(fn)));
delete editor;
editor = 0;
return 0;
......@@ -1216,6 +1238,10 @@ IEditor *EditorManager::openEditor(Core::Internal::EditorView *view, const QStri
IEditor *result = activateEditor(view, editor, flags);
if (editor == result)
restoreEditorState(editor);
if (flags && EditorManager::CanContainLineNumber)
editor->gotoLine(lineNumber, -1);
QApplication::restoreOverrideCursor();
return result;
}
......
......@@ -109,7 +109,8 @@ public:
enum OpenEditorFlag {
NoActivate = 1,
IgnoreNavigationHistory = 2,
ModeSwitch = 4
ModeSwitch = 4,
CanContainLineNumber = 8
};
Q_DECLARE_FLAGS(OpenEditorFlags, OpenEditorFlag)
......
......@@ -60,6 +60,7 @@ public:
virtual int currentLine() const { return 0; }
virtual int currentColumn() const { return 0; }
virtual void gotoLine(int line, int column = 0) { Q_UNUSED(line); Q_UNUSED(column); };
virtual bool isTemporary() const = 0;
......
......@@ -119,7 +119,8 @@ public:
virtual void addContextObject(IContext *context) = 0;
virtual void removeContextObject(IContext *context) = 0;
virtual void openFiles(const QStringList &fileNames, bool switchMode) = 0;
enum OpenFilesFlags { None = 0, SwitchMode = 1, CanContainLineNumbers = 2};
virtual void openFiles(const QStringList &fileNames, OpenFilesFlags flags = None) = 0;
signals:
void coreAboutToOpen();
......
......@@ -416,7 +416,7 @@ void MainWindow::dropEvent(QDropEvent *event)
QStringList files;
if (isDesktopFileManagerDrop(event->mimeData(), &files)) {
event->accept();
openFiles(files, true);
openFiles(files, ICore::SwitchMode);
} else {
event->ignore();
}
......@@ -793,7 +793,7 @@ void MainWindow::newFile()
void MainWindow::openFile()
{
openFiles(editorManager()->getOpenFileNames(), true);
openFiles(editorManager()->getOpenFileNames(), ICore::SwitchMode);
}
static QList<IFileFactory*> getNonEditorFileFactories()
......@@ -823,7 +823,7 @@ static IFileFactory *findFileFactory(const QList<IFileFactory*> &fileFactories,
}
// opens either an editor or loads a project
void MainWindow::openFiles(const QStringList &fileNames, bool switchMode)
void MainWindow::openFiles(const QStringList &fileNames, ICore::OpenFilesFlags flags)
{
QList<IFileFactory*> nonEditorFileFactories = getNonEditorFileFactories();
......@@ -832,13 +832,15 @@ void MainWindow::openFiles(const QStringList &fileNames, bool switchMode)
const QString absoluteFilePath = fi.absoluteFilePath();
if (IFileFactory *fileFactory = findFileFactory(nonEditorFileFactories, mimeDatabase(), fi)) {
fileFactory->open(absoluteFilePath);
if (switchMode)
if (flags && ICore::SwitchMode)
Core::ModeManager::instance()->activateMode(Core::Constants::MODE_EDIT);
} else {
EditorManager::OpenEditorFlag flags;
if (switchMode)
flags = EditorManager::ModeSwitch;
editorManager()->openEditor(absoluteFilePath, QString(), flags);
QFlags<EditorManager::OpenEditorFlag> emFlags;
if (flags && ICore::SwitchMode)
emFlags = EditorManager::ModeSwitch;
if (flags && ICore::CanContainLineNumbers)
emFlags |= EditorManager::CanContainLineNumber;
editorManager()->openEditor(absoluteFilePath, QString(), emFlags);
}
}
}
......
......@@ -32,6 +32,7 @@
#include "core_global.h"
#include "icontext.h"
#include "icore.h"
#include "dialogs/iwizard.h"
#include "eventfilteringmainwindow.h"
......@@ -97,7 +98,7 @@ public:
void removeContextObject(IContext *contex);
void resetContext();
void openFiles(const QStringList &fileNames, bool switchMode);
void openFiles(const QStringList &fileNames, ICore::OpenFilesFlags flags);
Core::ActionManager *actionManager() const;
Core::FileManager *fileManager() const;
......
......@@ -1240,7 +1240,18 @@ void ProjectExplorerPlugin::restoreSession()
connect(d->m_welcomePage, SIGNAL(requestSession(QString)), this, SLOT(loadSession(QString)));
connect(d->m_welcomePage, SIGNAL(requestProject(QString)), this, SLOT(loadProject(QString)));
Core::ICore::instance()->openFiles(arguments, false);
QStringList combinedList;
// Converts "filename" "+45" or "filename" ":23"
// into "filename+45" and "filename:23"
foreach (const QString &str, arguments) {
if (!combinedList.isEmpty() && (str.startsWith("+") || str.startsWith(":"))) {
combinedList.last().append(str);
} else {
combinedList << str;
}
}
Core::ICore::instance()->openFiles(combinedList, Core::ICore::CanContainLineNumbers);
updateActions();
}
......@@ -2375,7 +2386,7 @@ void ProjectExplorerPlugin::openOpenProjectDialog()
const QString path = fileMananger->useProjectsDirectory() ? fileMananger->projectsDirectory() : QString();
const QStringList files = fileMananger->getOpenFileNames(filters, path, &projectFilesFilter);
if (!files.isEmpty())
Core::ICore::instance()->openFiles(files, true);
Core::ICore::instance()->openFiles(files, Core::ICore::SwitchMode);
}
Q_EXPORT_PLUGIN(ProjectExplorerPlugin)
......@@ -335,7 +335,7 @@ void GettingStartedWelcomePageWidget::slotOpenExample()
tryFile = proFileInfo.path() + '/' + proFileInfo.baseName() + ".qml";
if(QFile::exists(tryFile))
files << tryFile;
Core::ICore::instance()->openFiles(files, true);
Core::ICore::instance()->openFiles(files, Core::ICore::SwitchMode);
if (!helpFile.isEmpty())
slotOpenContextHelpPage(helpFile);
}
......
......@@ -99,8 +99,6 @@ public:
virtual int find(const QString &string) const = 0;
virtual void gotoLine(int line, int column = 0) = 0;
virtual int position(PositionOperation posOp = Current, int at = -1) const = 0;
virtual void convertPosition(int pos, int *line, int *column) const = 0;
virtual QRect cursorRect(int pos = -1) const = 0;
......
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