2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-09-03 16:39:20 +08:00
|
|
|
#include "cpprefacter.h"
|
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "editor.h"
|
|
|
|
#include "editorlist.h"
|
|
|
|
#include <QFile>
|
2021-10-04 11:07:35 +08:00
|
|
|
#include <QMessageBox>
|
2021-10-24 13:03:54 +08:00
|
|
|
#include <QTextCodec>
|
2021-09-03 16:39:20 +08:00
|
|
|
#include "HighlighterManager.h"
|
2021-10-04 11:07:35 +08:00
|
|
|
#include "project.h"
|
2021-09-03 16:39:20 +08:00
|
|
|
|
|
|
|
CppRefacter::CppRefacter(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-25 09:55:18 +08:00
|
|
|
bool CppRefacter::findOccurence(Editor *editor, const QSynedit::BufferCoord &pos)
|
2021-09-03 16:39:20 +08:00
|
|
|
{
|
2021-11-05 10:44:23 +08:00
|
|
|
if (!editor->parser())
|
|
|
|
return false;
|
2021-09-03 16:39:20 +08:00
|
|
|
if (!editor->parser()->freeze())
|
|
|
|
return false;
|
|
|
|
auto action = finally([&editor]{
|
|
|
|
editor->parser()->unFreeze();
|
|
|
|
});
|
|
|
|
// get full phrase (such as s.name instead of name)
|
2021-12-18 23:36:58 +08:00
|
|
|
QStringList expression = editor->getExpressionAtPosition(pos);
|
2021-09-03 16:39:20 +08:00
|
|
|
// Find it's definition
|
|
|
|
PStatement statement = editor->parser()->findStatementOf(
|
|
|
|
editor->filename(),
|
2021-12-18 23:36:58 +08:00
|
|
|
expression,
|
2022-07-04 10:01:40 +08:00
|
|
|
pos.line);
|
2021-09-03 16:39:20 +08:00
|
|
|
// definition of the symbol not found
|
|
|
|
if (!statement)
|
|
|
|
return false;
|
|
|
|
|
2021-10-04 11:07:35 +08:00
|
|
|
std::shared_ptr<Project> project = pMainWindow->project();
|
|
|
|
if (editor->inProject() && project) {
|
2021-11-05 10:44:23 +08:00
|
|
|
doFindOccurenceInProject(statement,project,editor->parser());
|
2021-10-04 11:07:35 +08:00
|
|
|
} else {
|
2021-11-05 10:44:23 +08:00
|
|
|
doFindOccurenceInEditor(statement,editor,editor->parser());
|
2021-10-04 11:07:35 +08:00
|
|
|
}
|
|
|
|
pMainWindow->searchResultModel()->notifySearchResultsUpdated();
|
|
|
|
return true;
|
|
|
|
}
|
2021-09-03 16:39:20 +08:00
|
|
|
|
2021-11-05 10:44:23 +08:00
|
|
|
bool CppRefacter::findOccurence(const QString &statementFullname, SearchFileScope scope)
|
|
|
|
{
|
|
|
|
PCppParser parser;
|
2021-11-13 17:15:07 +08:00
|
|
|
Editor * editor=nullptr;
|
2021-11-05 10:44:23 +08:00
|
|
|
std::shared_ptr<Project> project;
|
|
|
|
if (scope == SearchFileScope::currentFile) {
|
|
|
|
editor = pMainWindow->editorList()->getEditor();
|
|
|
|
if (!editor)
|
|
|
|
return false;
|
|
|
|
parser = editor->parser();
|
|
|
|
} else if (scope == SearchFileScope::wholeProject) {
|
|
|
|
project = pMainWindow->project();
|
|
|
|
if (!project)
|
|
|
|
return false;
|
|
|
|
parser = project->cppParser();
|
|
|
|
}
|
|
|
|
if (!parser)
|
|
|
|
return false;
|
|
|
|
{
|
|
|
|
parser->freeze();
|
|
|
|
auto action = finally([&parser]{
|
|
|
|
parser->unFreeze();
|
|
|
|
});
|
|
|
|
PStatement statement = parser->findStatement(statementFullname);
|
|
|
|
// definition of the symbol not found
|
|
|
|
if (!statement)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (scope == SearchFileScope::wholeProject) {
|
|
|
|
doFindOccurenceInProject(statement,project,parser);
|
|
|
|
} else if (scope == SearchFileScope::currentFile) {
|
|
|
|
doFindOccurenceInEditor(statement, editor,parser);
|
|
|
|
}
|
|
|
|
pMainWindow->searchResultModel()->notifySearchResultsUpdated();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 11:07:35 +08:00
|
|
|
static QString fullParentName(PStatement statement) {
|
|
|
|
PStatement parent = statement->parentScope.lock();
|
|
|
|
if (parent) {
|
|
|
|
return parent->fullName;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 09:55:18 +08:00
|
|
|
void CppRefacter::renameSymbol(Editor *editor, const QSynedit::BufferCoord &pos, const QString &newWord)
|
2021-10-04 11:07:35 +08:00
|
|
|
{
|
|
|
|
if (!editor->parser()->freeze())
|
|
|
|
return;
|
|
|
|
auto action = finally([&editor]{
|
|
|
|
editor->parser()->unFreeze();
|
|
|
|
});
|
|
|
|
// get full phrase (such as s.name instead of name)
|
2022-01-12 18:07:52 +08:00
|
|
|
QStringList expression;
|
|
|
|
QChar s=editor->charAt(pos);
|
|
|
|
if (!editor->isIdentChar(s)) {
|
2022-09-25 09:55:18 +08:00
|
|
|
expression = editor->getExpressionAtPosition(QSynedit::BufferCoord{pos.ch-1,pos.line});
|
2022-01-12 18:07:52 +08:00
|
|
|
} else {
|
|
|
|
expression = editor->getExpressionAtPosition(pos);
|
|
|
|
}
|
2021-10-04 11:07:35 +08:00
|
|
|
// Find it's definition
|
|
|
|
PStatement oldStatement = editor->parser()->findStatementOf(
|
|
|
|
editor->filename(),
|
2021-12-19 10:16:46 +08:00
|
|
|
expression,
|
2022-07-04 10:01:40 +08:00
|
|
|
pos.line);
|
2021-10-04 11:07:35 +08:00
|
|
|
// definition of the symbol not found
|
|
|
|
if (!oldStatement)
|
|
|
|
return;
|
2022-01-12 18:07:52 +08:00
|
|
|
QString oldScope = fullParentName(oldStatement);
|
2021-10-04 11:07:35 +08:00
|
|
|
// found but not in this file
|
|
|
|
if (editor->filename() != oldStatement->fileName
|
|
|
|
|| editor->filename() != oldStatement->definitionFileName) {
|
|
|
|
QMessageBox::critical(editor,
|
|
|
|
tr("Rename Symbol Error"),
|
|
|
|
tr("Can't rename symbols not defined in this file."));
|
|
|
|
return;
|
|
|
|
}
|
2021-09-03 16:39:20 +08:00
|
|
|
|
2021-12-19 10:16:46 +08:00
|
|
|
QStringList newExpression = expression;
|
|
|
|
newExpression[newExpression.count()-1]=newWord;
|
2021-10-04 11:07:35 +08:00
|
|
|
PStatement newStatement = editor->parser()->findStatementOf(
|
2021-09-03 16:39:20 +08:00
|
|
|
editor->filename(),
|
2021-12-19 10:16:46 +08:00
|
|
|
newExpression,
|
2022-07-04 10:01:40 +08:00
|
|
|
pos.line);
|
2021-10-04 11:07:35 +08:00
|
|
|
if (newStatement && fullParentName(newStatement) == oldScope) {
|
|
|
|
QMessageBox::critical(editor,
|
|
|
|
tr("Rename Symbol Error"),
|
|
|
|
tr("New symbol already exists!"));
|
|
|
|
return;
|
2021-09-03 16:39:20 +08:00
|
|
|
}
|
2021-10-20 18:05:43 +08:00
|
|
|
renameSymbolInFile(editor->filename(),oldStatement,newWord, editor->parser());
|
2021-09-03 16:39:20 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 10:44:23 +08:00
|
|
|
void CppRefacter::doFindOccurenceInEditor(PStatement statement , Editor *editor, const PCppParser &parser)
|
|
|
|
{
|
|
|
|
PSearchResults results = pMainWindow->searchResultModel()->addSearchResults(
|
|
|
|
statement->command,
|
|
|
|
statement->fullName,
|
|
|
|
SearchFileScope::currentFile
|
|
|
|
);
|
|
|
|
PSearchResultTreeItem item = findOccurenceInFile(
|
|
|
|
editor->filename(),
|
|
|
|
statement,
|
|
|
|
parser);
|
|
|
|
if (item && !(item->results.isEmpty())) {
|
|
|
|
results->results.append(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CppRefacter::doFindOccurenceInProject(PStatement statement, std::shared_ptr<Project> project, const PCppParser &parser)
|
|
|
|
{
|
|
|
|
PSearchResults results = pMainWindow->searchResultModel()->addSearchResults(
|
|
|
|
statement->command,
|
|
|
|
statement->fullName,
|
|
|
|
SearchFileScope::wholeProject
|
|
|
|
);
|
|
|
|
foreach (const PProjectUnit& unit, project->units()) {
|
2022-02-13 20:08:18 +08:00
|
|
|
if (isCFile(unit->fileName()) || isHFile(unit->fileName())) {
|
2021-11-05 10:44:23 +08:00
|
|
|
PSearchResultTreeItem item = findOccurenceInFile(
|
|
|
|
unit->fileName(),
|
|
|
|
statement,
|
|
|
|
parser);
|
|
|
|
if (item && !(item->results.isEmpty())) {
|
|
|
|
results->results.append(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 16:39:20 +08:00
|
|
|
PSearchResultTreeItem CppRefacter::findOccurenceInFile(
|
|
|
|
const QString &filename,
|
|
|
|
const PStatement &statement,
|
|
|
|
const PCppParser& parser)
|
|
|
|
{
|
|
|
|
PSearchResultTreeItem parentItem = std::make_shared<SearchResultTreeItem>();
|
|
|
|
parentItem->filename = filename;
|
|
|
|
parentItem->parent = nullptr;
|
|
|
|
QStringList buffer;
|
2021-12-19 10:16:46 +08:00
|
|
|
Editor editor(nullptr);
|
2021-09-03 16:39:20 +08:00
|
|
|
if (pMainWindow->editorList()->getContentFromOpenedEditor(
|
|
|
|
filename,buffer)){
|
2022-04-19 21:18:41 +08:00
|
|
|
editor.document()->setContents(buffer);
|
2021-09-03 16:39:20 +08:00
|
|
|
} else {
|
|
|
|
QByteArray encoding;
|
2022-04-19 21:18:41 +08:00
|
|
|
editor.document()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
|
2021-09-03 16:39:20 +08:00
|
|
|
}
|
|
|
|
editor.setHighlighter(HighlighterManager().getCppHighlighter());
|
|
|
|
int posY = 0;
|
2022-04-19 21:18:41 +08:00
|
|
|
while (posY < editor.document()->count()) {
|
|
|
|
QString line = editor.document()->getString(posY);
|
2021-09-03 16:39:20 +08:00
|
|
|
if (line.isEmpty()) {
|
|
|
|
posY++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (posY == 0) {
|
|
|
|
editor.highlighter()->resetState();
|
|
|
|
} else {
|
|
|
|
editor.highlighter()->setState(
|
2022-04-19 21:18:41 +08:00
|
|
|
editor.document()->ranges(posY-1));
|
2021-09-03 16:39:20 +08:00
|
|
|
}
|
|
|
|
editor.highlighter()->setLine(line,posY);
|
|
|
|
while (!editor.highlighter()->eol()) {
|
|
|
|
int start = editor.highlighter()->getTokenPos() + 1;
|
|
|
|
QString token = editor.highlighter()->getToken();
|
2022-09-25 09:55:18 +08:00
|
|
|
QSynedit::PSynHighlighterAttribute attr = editor.highlighter()->getTokenAttribute();
|
2021-12-19 10:16:46 +08:00
|
|
|
if (!attr || attr!=editor.highlighter()->commentAttribute()) {
|
|
|
|
if (token == statement->command) {
|
|
|
|
//same name symbol , test if the same statement;
|
2022-09-25 09:55:18 +08:00
|
|
|
QSynedit::BufferCoord p;
|
2022-07-04 10:01:40 +08:00
|
|
|
p.line = posY+1;
|
|
|
|
p.ch = start+1;
|
2021-12-19 10:16:46 +08:00
|
|
|
|
|
|
|
QStringList expression = editor.getExpressionAtPosition(p);
|
|
|
|
PStatement tokenStatement = parser->findStatementOf(
|
|
|
|
filename,
|
2022-07-04 10:01:40 +08:00
|
|
|
expression, p.line);
|
2021-12-19 10:16:46 +08:00
|
|
|
if (tokenStatement
|
|
|
|
&& (tokenStatement->line == statement->line)
|
|
|
|
&& (tokenStatement->fileName == statement->fileName)) {
|
|
|
|
PSearchResultTreeItem item = std::make_shared<SearchResultTreeItem>();
|
|
|
|
item->filename = filename;
|
2022-07-04 10:01:40 +08:00
|
|
|
item->line = p.line;
|
2021-12-19 10:16:46 +08:00
|
|
|
item->start = start;
|
|
|
|
item->len = token.length();
|
|
|
|
item->parent = parentItem.get();
|
|
|
|
item->text = line;
|
|
|
|
item->text.replace('\t',' ');
|
|
|
|
parentItem->results.append(item);
|
|
|
|
}
|
2021-09-03 16:39:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
editor.highlighter()->next();
|
|
|
|
}
|
|
|
|
posY++;
|
|
|
|
}
|
|
|
|
return parentItem;
|
|
|
|
}
|
2021-10-04 11:07:35 +08:00
|
|
|
|
2021-10-20 18:05:43 +08:00
|
|
|
void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &statement, const QString &newWord, const PCppParser &parser)
|
2021-10-04 11:07:35 +08:00
|
|
|
{
|
|
|
|
QStringList buffer;
|
2021-12-19 10:16:46 +08:00
|
|
|
Editor editor(nullptr);
|
2021-10-04 11:07:35 +08:00
|
|
|
if (pMainWindow->editorList()->getContentFromOpenedEditor(
|
|
|
|
filename,buffer)){
|
2022-04-19 21:18:41 +08:00
|
|
|
editor.document()->setContents(buffer);
|
2021-10-04 11:07:35 +08:00
|
|
|
} else {
|
|
|
|
QByteArray encoding;
|
2022-04-19 21:18:41 +08:00
|
|
|
editor.document()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
|
2021-10-04 11:07:35 +08:00
|
|
|
}
|
|
|
|
QStringList newContents;
|
|
|
|
editor.setHighlighter(HighlighterManager().getCppHighlighter());
|
|
|
|
int posY = 0;
|
2022-04-19 21:18:41 +08:00
|
|
|
while (posY < editor.document()->count()) {
|
|
|
|
QString line = editor.document()->getString(posY);
|
2021-10-04 11:07:35 +08:00
|
|
|
|
|
|
|
if (posY == 0) {
|
|
|
|
editor.highlighter()->resetState();
|
|
|
|
} else {
|
|
|
|
editor.highlighter()->setState(
|
2022-04-19 21:18:41 +08:00
|
|
|
editor.document()->ranges(posY-1));
|
2021-10-04 11:07:35 +08:00
|
|
|
}
|
|
|
|
editor.highlighter()->setLine(line,posY);
|
|
|
|
QString newLine;
|
|
|
|
while (!editor.highlighter()->eol()) {
|
|
|
|
int start = editor.highlighter()->getTokenPos() + 1;
|
|
|
|
QString token = editor.highlighter()->getToken();
|
|
|
|
if (token == statement->command) {
|
|
|
|
//same name symbol , test if the same statement;
|
2022-09-25 09:55:18 +08:00
|
|
|
QSynedit::BufferCoord p;
|
2022-07-04 10:01:40 +08:00
|
|
|
p.line = posY+1;
|
|
|
|
p.ch = start;
|
2021-12-19 10:16:46 +08:00
|
|
|
|
|
|
|
QStringList expression = editor.getExpressionAtPosition(p);
|
2021-10-04 11:07:35 +08:00
|
|
|
PStatement tokenStatement = parser->findStatementOf(
|
|
|
|
filename,
|
2022-07-04 10:01:40 +08:00
|
|
|
expression, p.line);
|
2021-10-04 11:07:35 +08:00
|
|
|
if (tokenStatement
|
|
|
|
&& (tokenStatement->line == statement->line)
|
|
|
|
&& (tokenStatement->fileName == statement->fileName)) {
|
|
|
|
token = newWord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newLine += token;
|
|
|
|
editor.highlighter()->next();
|
|
|
|
}
|
|
|
|
newContents.append(newLine);
|
|
|
|
posY++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Editor * oldEditor = pMainWindow->editorList()->getOpenedEditorByFilename(filename);
|
|
|
|
if (oldEditor) {
|
2022-09-25 09:55:18 +08:00
|
|
|
QSynedit::BufferCoord oldXY=oldEditor->caretXY();
|
2022-06-12 22:54:34 +08:00
|
|
|
int topLine = oldEditor->topLine();
|
|
|
|
int leftChar = oldEditor->leftChar();
|
|
|
|
oldEditor->beginUndoBlock();
|
|
|
|
oldEditor->addLeftTopToUndo();
|
|
|
|
oldEditor->addCaretToUndo();
|
2022-06-23 13:05:10 +08:00
|
|
|
oldEditor->replaceAll(newContents.join(oldEditor->lineBreak()));
|
2022-06-12 22:54:34 +08:00
|
|
|
oldEditor->setTopLine(topLine);
|
|
|
|
oldEditor->setLeftChar(leftChar);
|
|
|
|
oldEditor->setCaretXY(oldXY);
|
|
|
|
oldEditor->endUndoBlock();
|
2021-10-04 11:07:35 +08:00
|
|
|
} else {
|
|
|
|
QByteArray realEncoding;
|
|
|
|
QFile file(filename);
|
2022-04-19 21:18:41 +08:00
|
|
|
editor.document()->saveToFile(file,ENCODING_AUTO_DETECT,
|
2022-03-12 17:37:53 +08:00
|
|
|
pSettings->editor().defaultEncoding(),
|
2021-10-24 13:03:54 +08:00
|
|
|
realEncoding);
|
2021-10-04 11:07:35 +08:00
|
|
|
}
|
|
|
|
}
|