1 Star 0 Fork 0

旷野芦苇殇/texteditor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
refactoringchanges.cpp 7.67 KB
一键复制 编辑 原始数据 按行查看 历史
旷野芦苇殇 提交于 2024-08-12 18:01 +08: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
#include "refactoringchanges.h"
#include "texteditor.h"
#include "textdocument.h"
#include "core/icore.h"
#include "utils/algorithm.h"
#include "utils/fileutils.h"
#include "utils/qtcassert.h"
#include <QFile>
#include <QFileInfo>
#include <QTextBlock>
#include <QTextCodec>
#include <QTextCursor>
#include <QTextDocument>
#include <QDebug>
#include <QApplication>
using namespace Core;
using namespace Utils;
namespace TextEditor {
RefactoringChanges::RefactoringChanges(RefactoringChangesData *data)
: m_data(data ? data : new RefactoringChangesData)
{}
RefactoringChanges::~RefactoringChanges() = default;
RefactoringSelections RefactoringChanges::rangesToSelections(QTextDocument *document,
const QList<Range> &ranges)
{
RefactoringSelections selections;
for (const Range &range : ranges) {
QTextCursor start(document);
start.setPosition(range.start);
start.setKeepPositionOnInsert(true);
QTextCursor end(document);
end.setPosition(qMin(range.end, document->characterCount() - 1));
selections.push_back({start, end});
}
return selections;
}
bool RefactoringChanges::createFile(const FilePath &filePath,
const QString &contents,
bool reindent,
bool openEditor) const
{
if (filePath.exists())
return false;
// Create a text document for the new file:
auto document = new QTextDocument;
QTextCursor cursor(document);
cursor.beginEditBlock();
cursor.insertText(contents);
// Reindent the contents:
if (reindent) {
cursor.select(QTextCursor::Document);
m_data->indentSelection(cursor, filePath, nullptr);
}
cursor.endEditBlock();
// Write the file to disk:
TextFileFormat format;
format.codec = QTextCodec::codecForLocale();
QString error;
bool saveOk = format.writeFile(filePath, document->toPlainText(), &error);
delete document;
if (!saveOk)
return false;
m_data->fileChanged(filePath);
if (openEditor)
RefactoringChanges::openEditor(filePath, /*bool activate =*/ false, -1, -1);
return true;
}
bool RefactoringChanges::removeFile(const FilePath &filePath) const
{
if (!filePath.exists())
return false;
// ### implement!
qWarning() << "RefactoringChanges::removeFile is not implemented";
return true;
}
TextEditorWidget *RefactoringChanges::openEditor(const FilePath &filePath,
bool activate,
int line,
int column)
{
return nullptr;
}
RefactoringFilePtr RefactoringChanges::file(TextEditorWidget *editor)
{
return RefactoringFilePtr(new RefactoringFile(editor));
}
RefactoringFilePtr RefactoringChanges::file(const FilePath &filePath) const
{
return RefactoringFilePtr(new RefactoringFile(filePath, m_data));
}
RefactoringFile::RefactoringFile(QTextDocument *document, const FilePath &filePath)
: m_filePath(filePath)
, m_document(document)
{ }
RefactoringFile::RefactoringFile(TextEditorWidget *editor)
: m_filePath(editor->textDocument()->filePath())
, m_editor(editor)
{ }
RefactoringFile::RefactoringFile(const FilePath &filePath,
const QSharedPointer<RefactoringChangesData> &data)
: m_filePath(filePath)
, m_data(data)
{
}
RefactoringFile::~RefactoringFile()
{
delete m_document;
}
bool RefactoringFile::isValid() const
{
if (m_filePath.isEmpty())
return false;
return document();
}
const QTextDocument *RefactoringFile::document() const
{
return mutableDocument();
}
QTextDocument *RefactoringFile::mutableDocument() const
{
if (m_editor)
return m_editor->document();
if (!m_document) {
QString fileContents;
// always make a QTextDocument to avoid excessive null checks
m_document = new QTextDocument(fileContents);
}
return m_document;
}
const QTextCursor RefactoringFile::cursor() const
{
if (m_editor)
return m_editor->textCursor();
if (!m_filePath.isEmpty()) {
if (QTextDocument *doc = mutableDocument())
return QTextCursor(doc);
}
return QTextCursor();
}
FilePath RefactoringFile::filePath() const
{
return m_filePath;
}
TextEditorWidget *RefactoringFile::editor() const
{
return m_editor;
}
int RefactoringFile::position(int line, int column) const
{
QTC_ASSERT(line != 0, return -1);
QTC_ASSERT(column != 0, return -1);
if (const QTextDocument *doc = document())
return doc->findBlockByNumber(line - 1).position() + column - 1;
return -1;
}
void RefactoringFile::lineAndColumn(int offset, int *line, int *column) const
{
QTC_ASSERT(line, return);
QTC_ASSERT(column, return);
QTC_ASSERT(offset >= 0, return);
QTextCursor c(cursor());
c.setPosition(offset);
*line = c.blockNumber() + 1;
*column = c.positionInBlock() + 1;
}
QChar RefactoringFile::charAt(int pos) const
{
if (const QTextDocument *doc = document())
return doc->characterAt(pos);
return QChar();
}
QString RefactoringFile::textOf(int start, int end) const
{
QTextCursor c = cursor();
c.setPosition(start);
c.setPosition(end, QTextCursor::KeepAnchor);
return c.selectedText();
}
QString RefactoringFile::textOf(const Range &range) const
{
return textOf(range.start, range.end);
}
ChangeSet RefactoringFile::changeSet() const
{
return m_changes;
}
void RefactoringFile::setChangeSet(const ChangeSet &changeSet)
{
if (m_filePath.isEmpty())
return;
m_changes = changeSet;
}
void RefactoringFile::appendIndentRange(const Range &range)
{
if (m_filePath.isEmpty())
return;
m_indentRanges.append(range);
}
void RefactoringFile::appendReindentRange(const Range &range)
{
if (m_filePath.isEmpty())
return;
m_reindentRanges.append(range);
}
void RefactoringFile::setOpenEditor(bool activate, int pos)
{
m_openEditor = true;
m_activateEditor = activate;
m_editorCursorPosition = pos;
}
bool RefactoringFile::apply()
{
return false;
}
void RefactoringFile::indentOrReindent(const RefactoringSelections &ranges,
RefactoringFile::IndentType indent)
{
TextDocument * document = m_editor ? m_editor->textDocument() : nullptr;
for (const auto &[position, anchor]: ranges) {
QTextCursor selection(anchor);
selection.setPosition(position.position(), QTextCursor::KeepAnchor);
if (indent == Indent)
m_data->indentSelection(selection, m_filePath, document);
else
m_data->reindentSelection(selection, m_filePath, document);
}
}
void RefactoringFile::fileChanged()
{
if (!m_filePath.isEmpty())
m_data->fileChanged(m_filePath);
}
RefactoringChangesData::~RefactoringChangesData() = default;
void RefactoringChangesData::indentSelection(const QTextCursor &,
const FilePath &,
const TextDocument *) const
{
qWarning() << Q_FUNC_INFO << "not implemented";
}
void RefactoringChangesData::reindentSelection(const QTextCursor &,
const FilePath &,
const TextDocument *) const
{
qWarning() << Q_FUNC_INFO << "not implemented";
}
void RefactoringChangesData::fileChanged(const FilePath &)
{
}
} // namespace TextEditor
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fengzhu2007/texteditor.git
git@gitee.com:fengzhu2007/texteditor.git
fengzhu2007
texteditor
texteditor
main

搜索帮助