- enhancement: use the new expression parser to implement rename symbol

- fix: rename symbol shouldn't remove empty lines
This commit is contained in:
Roy Qu 2021-12-19 10:16:46 +08:00
parent 109ea5a0ae
commit 33e8042944
6 changed files with 70 additions and 53 deletions

View File

@ -2,6 +2,8 @@ Version 0.11.3 For Dev-C++ 7 Beta
- fix: use pixel size for fonts, to fit different dpi in multiple displays - fix: use pixel size for fonts, to fit different dpi in multiple displays
- enhancement: use the new expression parser to parse info for tips - enhancement: use the new expression parser to parse info for tips
- enhancement: better highlight processing for preprocess directives - enhancement: better highlight processing for preprocess directives
- enhancement: use the new expression parser to implement rename symbol
- fix: rename symbol shouldn't remove empty lines
Version 0.11.2 For Dev-C++ 7 Beta Version 0.11.2 For Dev-C++ 7 Beta
- fix: button "run all problem cases" not disabled when compiling or debugging - fix: button "run all problem cases" not disabled when compiling or debugging

View File

@ -29,7 +29,7 @@ bool CppRefacter::findOccurence(Editor *editor, const BufferCoord &pos)
PStatement statement = editor->parser()->findStatementOf( PStatement statement = editor->parser()->findStatementOf(
editor->filename(), editor->filename(),
expression, expression,
editor->parser()->findAndScanBlockAt(editor->filename(),pos.Line)); pos.Line);
// definition of the symbol not found // definition of the symbol not found
if (!statement) if (!statement)
return false; return false;
@ -98,12 +98,11 @@ void CppRefacter::renameSymbol(Editor *editor, const BufferCoord &pos, const QSt
editor->parser()->unFreeze(); editor->parser()->unFreeze();
}); });
// get full phrase (such as s.name instead of name) // get full phrase (such as s.name instead of name)
BufferCoord pBeginPos,pEndPos; QStringList expression = editor->getExpressionAtPosition(pos);
QString phrase = getWordAtPosition(editor,pos,pBeginPos,pEndPos,Editor::WordPurpose::wpInformation);
// Find it's definition // Find it's definition
PStatement oldStatement = editor->parser()->findStatementOf( PStatement oldStatement = editor->parser()->findStatementOf(
editor->filename(), editor->filename(),
phrase, expression,
pos.Line); pos.Line);
QString oldScope = fullParentName(oldStatement); QString oldScope = fullParentName(oldStatement);
// definition of the symbol not found // definition of the symbol not found
@ -118,10 +117,11 @@ void CppRefacter::renameSymbol(Editor *editor, const BufferCoord &pos, const QSt
return; return;
} }
QString newPhrase = phrase.mid(0,phrase.length()-word.length()) + newWord; QStringList newExpression = expression;
newExpression[newExpression.count()-1]=newWord;
PStatement newStatement = editor->parser()->findStatementOf( PStatement newStatement = editor->parser()->findStatementOf(
editor->filename(), editor->filename(),
newPhrase, newExpression,
pos.Line); pos.Line);
if (newStatement && fullParentName(newStatement) == oldScope) { if (newStatement && fullParentName(newStatement) == oldScope) {
QMessageBox::critical(editor, QMessageBox::critical(editor,
@ -177,7 +177,7 @@ PSearchResultTreeItem CppRefacter::findOccurenceInFile(
parentItem->filename = filename; parentItem->filename = filename;
parentItem->parent = nullptr; parentItem->parent = nullptr;
QStringList buffer; QStringList buffer;
SynEdit editor; Editor editor(nullptr);
if (pMainWindow->editorList()->getContentFromOpenedEditor( if (pMainWindow->editorList()->getContentFromOpenedEditor(
filename,buffer)){ filename,buffer)){
editor.lines()->setContents(buffer); editor.lines()->setContents(buffer);
@ -204,16 +204,18 @@ PSearchResultTreeItem CppRefacter::findOccurenceInFile(
while (!editor.highlighter()->eol()) { while (!editor.highlighter()->eol()) {
int start = editor.highlighter()->getTokenPos() + 1; int start = editor.highlighter()->getTokenPos() + 1;
QString token = editor.highlighter()->getToken(); QString token = editor.highlighter()->getToken();
PSynHighlighterAttribute attr = editor.highlighter()->getTokenAttribute();
if (!attr || attr!=editor.highlighter()->commentAttribute()) {
if (token == statement->command) { if (token == statement->command) {
//same name symbol , test if the same statement; //same name symbol , test if the same statement;
BufferCoord p,pBeginPos,pEndPos; BufferCoord p;
p.Line = posY+1; p.Line = posY+1;
p.Char = start; p.Char = start+1;
QString phrase = getWordAtPosition(&editor, p, pBeginPos,pEndPos,
Editor::WordPurpose::wpInformation); QStringList expression = editor.getExpressionAtPosition(p);
PStatement tokenStatement = parser->findStatementOf( PStatement tokenStatement = parser->findStatementOf(
filename, filename,
phrase, p.Line); expression, p.Line);
if (tokenStatement if (tokenStatement
&& (tokenStatement->line == statement->line) && (tokenStatement->line == statement->line)
&& (tokenStatement->fileName == statement->fileName)) { && (tokenStatement->fileName == statement->fileName)) {
@ -221,13 +223,14 @@ PSearchResultTreeItem CppRefacter::findOccurenceInFile(
item->filename = filename; item->filename = filename;
item->line = p.Line; item->line = p.Line;
item->start = start; item->start = start;
item->len = phrase.length(); item->len = token.length();
item->parent = parentItem.get(); item->parent = parentItem.get();
item->text = line; item->text = line;
item->text.replace('\t',' '); item->text.replace('\t',' ');
parentItem->results.append(item); parentItem->results.append(item);
} }
} }
}
editor.highlighter()->next(); editor.highlighter()->next();
} }
posY++; posY++;
@ -238,7 +241,7 @@ PSearchResultTreeItem CppRefacter::findOccurenceInFile(
void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &statement, const QString &newWord, const PCppParser &parser) void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &statement, const QString &newWord, const PCppParser &parser)
{ {
QStringList buffer; QStringList buffer;
SynEdit editor; Editor editor(nullptr);
if (pMainWindow->editorList()->getContentFromOpenedEditor( if (pMainWindow->editorList()->getContentFromOpenedEditor(
filename,buffer)){ filename,buffer)){
editor.lines()->setContents(buffer); editor.lines()->setContents(buffer);
@ -251,10 +254,6 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
int posY = 0; int posY = 0;
while (posY < editor.lines()->count()) { while (posY < editor.lines()->count()) {
QString line = editor.lines()->getString(posY); QString line = editor.lines()->getString(posY);
if (line.isEmpty()) {
posY++;
continue;
}
if (posY == 0) { if (posY == 0) {
editor.highlighter()->resetState(); editor.highlighter()->resetState();
@ -269,14 +268,14 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
QString token = editor.highlighter()->getToken(); QString token = editor.highlighter()->getToken();
if (token == statement->command) { if (token == statement->command) {
//same name symbol , test if the same statement; //same name symbol , test if the same statement;
BufferCoord p,pBeginPos,pEndPos; BufferCoord p;
p.Line = posY+1; p.Line = posY+1;
p.Char = start; p.Char = start+1;
QString phrase = getWordAtPosition(&editor, p, pBeginPos,pEndPos,
Editor::WordPurpose::wpInformation); QStringList expression = editor.getExpressionAtPosition(p);
PStatement tokenStatement = parser->findStatementOf( PStatement tokenStatement = parser->findStatementOf(
filename, filename,
phrase, p.Line); expression, p.Line);
if (tokenStatement if (tokenStatement
&& (tokenStatement->line == statement->line) && (tokenStatement->line == statement->line)
&& (tokenStatement->fileName == statement->fileName)) { && (tokenStatement->fileName == statement->fileName)) {

View File

@ -1644,35 +1644,38 @@ QStringList Editor::getExpressionAtPosition(
int ch = pos.Char-1; int ch = pos.Char-1;
int symbolMatchingLevel = 0; int symbolMatchingLevel = 0;
LastSymbolType lastSymbolType=LastSymbolType::None; LastSymbolType lastSymbolType=LastSymbolType::None;
PSynHighlighter highlighter = highlighterManager.getHighlighter(mFilename);
if (!highlighter)
return result;
while (true) { while (true) {
if (line>=lines()->count() || line<0) if (line>=lines()->count() || line<0)
break; break;
QStringList tokens; QStringList tokens;
if (line==0) { if (line==0) {
highlighter()->resetState(); highlighter->resetState();
} else { } else {
highlighter()->setState(lines()->ranges(line-1)); highlighter->setState(lines()->ranges(line-1));
} }
QString sLine = lines()->getString(line); QString sLine = lines()->getString(line);
highlighter()->setLine(sLine,line-1); highlighter->setLine(sLine,line-1);
while (!highlighter()->eol()) { while (!highlighter->eol()) {
int start = highlighter()->getTokenPos() + 1; int start = highlighter->getTokenPos();
QString token = highlighter()->getToken(); QString token = highlighter->getToken();
int endPos = start + token.length()-1; int endPos = start + token.length()-1;
if (start>ch) { if (start>ch) {
break; break;
} }
PSynHighlighterAttribute attr = highlighter()->getTokenAttribute(); PSynHighlighterAttribute attr = highlighter->getTokenAttribute();
if ( (line == pos.Line-1) if ( (line == pos.Line-1)
&& (start<=ch) && (ch<=endPos)) { && (start<=ch) && (ch<=endPos)) {
if (attr==highlighter()->commentAttribute() || attr == highlighter()->stringAttribute()) { if (attr==highlighter->commentAttribute() || attr == highlighter->stringAttribute()) {
return result; return result;
} }
} }
if (attr!=highlighter()->commentAttribute() && attr!=highlighter()->whitespaceAttribute()){ if (attr!=highlighter->commentAttribute() && attr!=highlighter->whitespaceAttribute()){
tokens.append(token); tokens.append(token);
} }
highlighter()->next(); highlighter->next();
} }
for (int i=tokens.count()-1;i>=0;i--) { for (int i=tokens.count()-1;i>=0;i--) {
QString token = tokens[i]; QString token = tokens[i];
@ -3057,7 +3060,7 @@ QString Editor::getParserHint(const QStringList& expression,const QString &s, in
QString result; QString result;
PStatement statement = mParser->findStatementOf( PStatement statement = mParser->findStatementOf(
mFilename,expression, mFilename,expression,
mParser->findAndScanBlockAt(mFilename,line)); line);
if (!statement) if (!statement)
return result; return result;
if (statement->kind == StatementKind::skFunction if (statement->kind == StatementKind::skFunction

View File

@ -5116,12 +5116,11 @@ void MainWindow::on_actionRename_Symbol_triggered()
BufferCoord oldCaretXY = editor->caretXY(); BufferCoord oldCaretXY = editor->caretXY();
if (editor->inProject() && mProject) { if (editor->inProject() && mProject) {
mProject->cppParser()->parseFileList(); mProject->cppParser()->parseFileList();
BufferCoord pBeginPos,pEndPos; QStringList expression = editor->getExpressionAtPosition(oldCaretXY);
QString phrase = getWordAtPosition(editor,oldCaretXY,pBeginPos,pEndPos,Editor::WordPurpose::wpInformation);
// Find it's definition // Find it's definition
PStatement oldStatement = editor->parser()->findStatementOf( PStatement oldStatement = editor->parser()->findStatementOf(
editor->filename(), editor->filename(),
phrase, expression,
oldCaretXY.Line); oldCaretXY.Line);
// definition of the symbol not found // definition of the symbol not found
if (!oldStatement) if (!oldStatement)

View File

@ -246,6 +246,8 @@ PStatement CppParser::findStatement(const QString &fullname)
PStatement CppParser::findStatementOf(const QString &fileName, const QString &phrase, int line) PStatement CppParser::findStatementOf(const QString &fileName, const QString &phrase, int line)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (mParsing)
return PStatement();
return findStatementOf(fileName,phrase,findAndScanBlockAt(fileName,line)); return findStatementOf(fileName,phrase,findAndScanBlockAt(fileName,line));
} }
@ -447,6 +449,14 @@ PStatement CppParser::findStatementOf(const QString &fileName, const QStringList
} }
PStatement CppParser::findStatementOf(const QString &fileName, const QStringList &expression, int line)
{
QMutexLocker locker(&mMutex);
if (mParsing)
return PStatement();
return findStatementOf(fileName,expression,findAndScanBlockAt(fileName,line));
}
PStatement CppParser::findStatementStartingFrom(const QString &fileName, const QString &phrase, const PStatement& startScope) PStatement CppParser::findStatementStartingFrom(const QString &fileName, const QString &phrase, const PStatement& startScope)
{ {
PStatement scopeStatement = startScope; PStatement scopeStatement = startScope;

View File

@ -53,6 +53,10 @@ public:
PStatement findStatementOf(const QString& fileName, PStatement findStatementOf(const QString& fileName,
const QStringList& expression, const QStringList& expression,
const PStatement& currentScope); const PStatement& currentScope);
PStatement findStatementOf(const QString& fileName,
const QStringList& expression,
int line);
/** /**
* @brief evaluate the expression * @brief evaluate the expression
* @param fileName * @param fileName