2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2010-03-30 12:11:32 +02:00
|
|
|
|
|
|
|
|
#include "cmakeeditor.h"
|
|
|
|
|
|
2022-07-08 17:07:33 +02:00
|
|
|
#include "cmakeautocompleter.h"
|
2013-08-30 12:55:06 +02:00
|
|
|
#include "cmakefilecompletionassist.h"
|
2015-08-24 08:43:18 +02:00
|
|
|
#include "cmakeindenter.h"
|
2022-07-08 17:07:33 +02:00
|
|
|
#include "cmakeprojectconstants.h"
|
2010-03-30 12:11:32 +02:00
|
|
|
|
2012-02-19 18:58:39 +04:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2020-04-17 15:30:05 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
2010-03-30 12:11:32 +02:00
|
|
|
#include <texteditor/texteditoractionhandler.h>
|
2014-08-20 00:07:43 +02:00
|
|
|
|
2020-04-17 15:30:05 +02:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QTextDocument>
|
2010-03-30 12:11:32 +02:00
|
|
|
|
2020-04-17 15:30:05 +02:00
|
|
|
#include <functional>
|
2010-03-30 12:11:32 +02:00
|
|
|
|
2014-08-20 00:07:43 +02:00
|
|
|
using namespace Core;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
namespace CMakeProjectManager {
|
|
|
|
|
namespace Internal {
|
2010-03-30 12:11:32 +02:00
|
|
|
|
|
|
|
|
//
|
2014-08-20 00:07:43 +02:00
|
|
|
// CMakeEditor
|
2010-03-30 12:11:32 +02:00
|
|
|
//
|
|
|
|
|
|
2019-01-28 11:47:50 +01:00
|
|
|
void CMakeEditor::contextHelp(const HelpCallback &callback) const
|
2014-06-17 11:33:58 +02:00
|
|
|
{
|
|
|
|
|
int pos = position();
|
|
|
|
|
|
|
|
|
|
QChar chr;
|
|
|
|
|
do {
|
|
|
|
|
--pos;
|
|
|
|
|
if (pos < 0)
|
|
|
|
|
break;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2018-01-17 16:06:13 +01:00
|
|
|
if (chr == QLatin1Char('(')) {
|
2019-08-05 13:40:36 +02:00
|
|
|
BaseTextEditor::contextHelp(callback);
|
2018-01-17 16:06:13 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2014-06-17 11:33:58 +02:00
|
|
|
} while (chr.unicode() != QChar::ParagraphSeparator);
|
|
|
|
|
|
|
|
|
|
++pos;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
while (chr.isSpace()) {
|
|
|
|
|
++pos;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
}
|
|
|
|
|
int begin = pos;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
++pos;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
} while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));
|
|
|
|
|
int end = pos;
|
|
|
|
|
|
|
|
|
|
while (chr.isSpace()) {
|
|
|
|
|
++pos;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Not a command
|
2018-01-17 16:06:13 +01:00
|
|
|
if (chr != QLatin1Char('(')) {
|
2019-08-05 13:40:36 +02:00
|
|
|
BaseTextEditor::contextHelp(callback);
|
2018-01-17 16:06:13 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2014-06-17 11:33:58 +02:00
|
|
|
|
2019-01-28 13:00:03 +01:00
|
|
|
const QString id = "command/" + textAt(begin, end - begin).toLower();
|
2019-08-05 13:40:36 +02:00
|
|
|
callback(
|
|
|
|
|
{{id, Utils::Text::wordUnderCursor(editorWidget()->textCursor())}, {}, HelpItem::Unknown});
|
2014-06-17 11:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
2010-03-30 12:11:32 +02:00
|
|
|
//
|
2014-08-20 00:07:43 +02:00
|
|
|
// CMakeEditorWidget
|
2010-03-30 12:11:32 +02:00
|
|
|
//
|
|
|
|
|
|
2020-01-29 04:15:25 +03:00
|
|
|
class CMakeEditorWidget final : public TextEditorWidget
|
2014-08-22 16:38:38 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2016-08-18 13:20:35 +02:00
|
|
|
~CMakeEditorWidget() final = default;
|
2014-08-22 16:38:38 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool save(const QString &fileName = QString());
|
2018-02-21 11:58:16 +01:00
|
|
|
void findLinkAt(const QTextCursor &cursor,
|
2022-06-03 15:17:33 +02:00
|
|
|
const Utils::LinkHandler &processLinkCallback,
|
2018-02-21 11:58:16 +01:00
|
|
|
bool resolveTarget = true,
|
|
|
|
|
bool inNextSplit = false) override;
|
2015-06-01 17:55:31 +02:00
|
|
|
void contextMenuEvent(QContextMenuEvent *e) override;
|
2014-08-22 16:38:38 +02:00
|
|
|
};
|
|
|
|
|
|
2012-02-19 18:58:39 +04:00
|
|
|
void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
|
|
|
|
|
{
|
2012-07-24 23:30:32 +04:00
|
|
|
showDefaultContextMenu(e, Constants::M_CONTEXT);
|
2012-02-19 18:58:39 +04:00
|
|
|
}
|
|
|
|
|
|
2021-04-12 15:29:08 +02:00
|
|
|
static bool mustBeQuotedInFileName(const QChar &c)
|
2012-02-19 18:58:39 +04:00
|
|
|
{
|
2021-04-12 15:29:08 +02:00
|
|
|
return c.isSpace() || c == '"' || c == '(' || c == ')';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isValidFileNameChar(const QString &block, int pos)
|
|
|
|
|
{
|
|
|
|
|
const QChar c = block.at(pos);
|
|
|
|
|
return !mustBeQuotedInFileName(c) || (pos > 0 && block.at(pos - 1) == '\\');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QString unescape(const QString &s)
|
|
|
|
|
{
|
|
|
|
|
QString result;
|
|
|
|
|
int i = 0;
|
|
|
|
|
const int size = s.size();
|
|
|
|
|
while (i < size) {
|
|
|
|
|
const QChar c = s.at(i);
|
|
|
|
|
if (c == '\\' && i < size - 1) {
|
|
|
|
|
const QChar nc = s.at(i + 1);
|
|
|
|
|
if (mustBeQuotedInFileName(nc)) {
|
|
|
|
|
result += nc;
|
|
|
|
|
i += 2;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result += c;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2012-02-19 18:58:39 +04:00
|
|
|
}
|
|
|
|
|
|
2018-02-21 11:58:16 +01:00
|
|
|
void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
|
2022-06-03 15:17:33 +02:00
|
|
|
const Utils::LinkHandler &processLinkCallback,
|
2018-02-21 11:58:16 +01:00
|
|
|
bool/* resolveTarget*/,
|
|
|
|
|
bool /*inNextSplit*/)
|
2012-02-19 18:58:39 +04:00
|
|
|
{
|
2017-10-05 09:45:33 +02:00
|
|
|
Utils::Link link;
|
2012-02-19 18:58:39 +04:00
|
|
|
|
2018-11-02 14:17:12 +01:00
|
|
|
int line = 0;
|
|
|
|
|
int column = 0;
|
|
|
|
|
convertPosition(cursor.position(), &line, &column);
|
|
|
|
|
const int positionInBlock = column - 1;
|
2012-02-19 18:58:39 +04:00
|
|
|
|
|
|
|
|
const QString block = cursor.block().text();
|
|
|
|
|
|
|
|
|
|
// check if the current position is commented out
|
|
|
|
|
const int hashPos = block.indexOf(QLatin1Char('#'));
|
|
|
|
|
if (hashPos >= 0 && hashPos < positionInBlock)
|
2018-02-21 11:58:16 +01:00
|
|
|
return processLinkCallback(link);
|
2012-02-19 18:58:39 +04:00
|
|
|
|
|
|
|
|
// find the beginning of a filename
|
|
|
|
|
QString buffer;
|
|
|
|
|
int beginPos = positionInBlock - 1;
|
|
|
|
|
while (beginPos >= 0) {
|
2021-04-12 15:29:08 +02:00
|
|
|
if (isValidFileNameChar(block, beginPos)) {
|
|
|
|
|
buffer.prepend(block.at(beginPos));
|
2012-02-19 18:58:39 +04:00
|
|
|
beginPos--;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find the end of a filename
|
|
|
|
|
int endPos = positionInBlock;
|
|
|
|
|
while (endPos < block.count()) {
|
2021-04-12 15:29:08 +02:00
|
|
|
if (isValidFileNameChar(block, endPos)) {
|
|
|
|
|
buffer.append(block.at(endPos));
|
2012-02-19 18:58:39 +04:00
|
|
|
endPos++;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buffer.isEmpty())
|
2018-02-21 11:58:16 +01:00
|
|
|
return processLinkCallback(link);
|
2012-02-19 18:58:39 +04:00
|
|
|
|
2014-12-21 21:54:30 +02:00
|
|
|
QDir dir(textDocument()->filePath().toFileInfo().absolutePath());
|
2019-07-26 15:09:12 +02:00
|
|
|
buffer.replace("${CMAKE_CURRENT_SOURCE_DIR}", dir.path());
|
|
|
|
|
buffer.replace("${CMAKE_CURRENT_LIST_DIR}", dir.path());
|
|
|
|
|
// TODO: Resolve more variables
|
|
|
|
|
|
2021-04-12 15:29:08 +02:00
|
|
|
QString fileName = dir.filePath(unescape(buffer));
|
2012-02-19 18:58:39 +04:00
|
|
|
QFileInfo fi(fileName);
|
|
|
|
|
if (fi.exists()) {
|
|
|
|
|
if (fi.isDir()) {
|
|
|
|
|
QDir subDir(fi.absoluteFilePath());
|
2012-11-21 23:54:06 +02:00
|
|
|
QString subProject = subDir.filePath(QLatin1String("CMakeLists.txt"));
|
2014-10-24 10:28:28 +02:00
|
|
|
if (QFileInfo::exists(subProject))
|
2012-02-19 18:58:39 +04:00
|
|
|
fileName = subProject;
|
|
|
|
|
else
|
2018-02-21 11:58:16 +01:00
|
|
|
return processLinkCallback(link);
|
2012-02-19 18:58:39 +04:00
|
|
|
}
|
2021-05-25 10:11:31 +02:00
|
|
|
link.targetFilePath = Utils::FilePath::fromString(fileName);
|
2013-02-06 14:23:18 +01:00
|
|
|
link.linkTextStart = cursor.position() - positionInBlock + beginPos + 1;
|
|
|
|
|
link.linkTextEnd = cursor.position() - positionInBlock + endPos;
|
2012-02-19 18:58:39 +04:00
|
|
|
}
|
2018-02-21 11:58:16 +01:00
|
|
|
processLinkCallback(link);
|
2012-02-19 18:58:39 +04:00
|
|
|
}
|
|
|
|
|
|
2016-05-30 15:23:52 +02:00
|
|
|
static TextDocument *createCMakeDocument()
|
2010-03-30 12:11:32 +02:00
|
|
|
{
|
2016-05-30 15:23:52 +02:00
|
|
|
auto doc = new TextDocument;
|
|
|
|
|
doc->setId(Constants::CMAKE_EDITOR_ID);
|
2020-04-07 14:44:22 +02:00
|
|
|
doc->setMimeType(QLatin1String(Constants::CMAKE_MIMETYPE));
|
2016-05-30 15:23:52 +02:00
|
|
|
return doc;
|
2010-03-30 12:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-20 00:07:43 +02:00
|
|
|
//
|
|
|
|
|
// CMakeEditorFactory
|
|
|
|
|
//
|
|
|
|
|
|
2015-02-04 17:54:46 +01:00
|
|
|
CMakeEditorFactory::CMakeEditorFactory()
|
2014-08-20 00:07:43 +02:00
|
|
|
{
|
|
|
|
|
setId(Constants::CMAKE_EDITOR_ID);
|
2020-02-11 17:51:59 +01:00
|
|
|
setDisplayName(QCoreApplication::translate("OpenWith::Editors", "CMake Editor"));
|
2020-04-07 14:44:22 +02:00
|
|
|
addMimeType(Constants::CMAKE_MIMETYPE);
|
|
|
|
|
addMimeType(Constants::CMAKE_PROJECT_MIMETYPE);
|
2014-08-20 00:07:43 +02:00
|
|
|
|
2014-08-22 16:38:38 +02:00
|
|
|
setEditorCreator([]() { return new CMakeEditor; });
|
|
|
|
|
setEditorWidgetCreator([]() { return new CMakeEditorWidget; });
|
2016-05-30 15:23:52 +02:00
|
|
|
setDocumentCreator(createCMakeDocument);
|
2019-01-16 09:37:54 +01:00
|
|
|
setIndenterCreator([](QTextDocument *doc) { return new CMakeIndenter(doc); });
|
2015-02-03 09:18:57 +01:00
|
|
|
setUseGenericHighlighter(true);
|
2017-04-24 16:01:14 +02:00
|
|
|
setCommentDefinition(Utils::CommentDefinition::HashStyle);
|
2014-10-15 00:36:39 +02:00
|
|
|
setCodeFoldingSupported(true);
|
2014-08-22 16:38:38 +02:00
|
|
|
|
2015-02-04 17:54:46 +01:00
|
|
|
setCompletionAssistProvider(new CMakeFileCompletionAssistProvider);
|
2015-08-24 08:43:18 +02:00
|
|
|
setAutoCompleterCreator([]() { return new CMakeAutoCompleter; });
|
2014-10-01 22:39:47 +02:00
|
|
|
|
2014-09-02 12:25:20 +02:00
|
|
|
setEditorActionHandlers(TextEditorActionHandler::UnCommentSelection
|
2015-08-24 08:43:18 +02:00
|
|
|
| TextEditorActionHandler::JumpToFileUnderCursor
|
|
|
|
|
| TextEditorActionHandler::Format);
|
2014-08-20 00:07:43 +02:00
|
|
|
|
|
|
|
|
ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT);
|
|
|
|
|
contextMenu->addAction(ActionManager::command(TextEditor::Constants::JUMP_TO_FILE_UNDER_CURSOR));
|
2014-09-02 12:25:20 +02:00
|
|
|
contextMenu->addSeparator(Context(Constants::CMAKE_EDITOR_ID));
|
2014-08-20 00:07:43 +02:00
|
|
|
contextMenu->addAction(ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CMakeProjectManager
|