- fix: crash when refactor symbol and cursor is at the end of the identifier

- fix: refactor symbol doesn't work for 1-length identifiers
This commit is contained in:
Roy Qu 2022-01-12 18:07:52 +08:00
parent d8413ab76c
commit 995b734334
4 changed files with 34 additions and 13 deletions

View File

@ -9,8 +9,8 @@ Red Panda C++ Version 0.13.2
- enhancement: auto update watch, local and memory view after expression evaluated - enhancement: auto update watch, local and memory view after expression evaluated
- enhancement: auto update watch, local and memory view after memory modified - enhancement: auto update watch, local and memory view after memory modified
- enhancement: modify values in the watch view by double click - enhancement: modify values in the watch view by double click
- fix: crash when refactor symbol and cursor is at the end of the identifier
- fix: refactor symbol doesn't work for 1-length identifiers
Red Panda C++ Version 0.13.1 Red Panda C++ Version 0.13.1
- enhancement: suppoort localization info in project templates - enhancement: suppoort localization info in project templates

View File

@ -114,16 +114,22 @@ 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)
QStringList expression = editor->getExpressionAtPosition(pos); QStringList expression;
QChar s=editor->charAt(pos);
if (!editor->isIdentChar(s)) {
expression = editor->getExpressionAtPosition(BufferCoord{pos.Char-1,pos.Line});
} else {
expression = editor->getExpressionAtPosition(pos);
}
// Find it's definition // Find it's definition
PStatement oldStatement = editor->parser()->findStatementOf( PStatement oldStatement = editor->parser()->findStatementOf(
editor->filename(), editor->filename(),
expression, expression,
pos.Line); pos.Line);
QString oldScope = fullParentName(oldStatement);
// definition of the symbol not found // definition of the symbol not found
if (!oldStatement) if (!oldStatement)
return; return;
QString oldScope = fullParentName(oldStatement);
// found but not in this file // found but not in this file
if (editor->filename() != oldStatement->fileName if (editor->filename() != oldStatement->fileName
|| editor->filename() != oldStatement->definitionFileName) { || editor->filename() != oldStatement->definitionFileName) {
@ -286,7 +292,7 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
//same name symbol , test if the same statement; //same name symbol , test if the same statement;
BufferCoord p; BufferCoord p;
p.Line = posY+1; p.Line = posY+1;
p.Char = start+1; p.Char = start;
QStringList expression = editor.getExpressionAtPosition(p); QStringList expression = editor.getExpressionAtPosition(p);
PStatement tokenStatement = parser->findStatementOf( PStatement tokenStatement = parser->findStatementOf(

View File

@ -1052,23 +1052,23 @@ QString SynEdit::wordAtCursor()
return wordAtRowCol(caretXY()); return wordAtRowCol(caretXY());
} }
QString SynEdit::wordAtRowCol(const BufferCoord &XY) QString SynEdit::wordAtRowCol(const BufferCoord &pos)
{ {
if ((XY.Line >= 1) && (XY.Line <= mLines->count())) { if ((pos.Line >= 1) && (pos.Line <= mLines->count())) {
QString line = mLines->getString(XY.Line - 1); QString line = mLines->getString(pos.Line - 1);
int Len = line.length(); int len = line.length();
if (Len == 0) if (len == 0)
return ""; return "";
if (XY.Char<1 || XY.Char>Len) if (pos.Char<1 || pos.Char>len)
return ""; return "";
int start = XY.Char - 1; int start = pos.Char - 1;
if ((start> 0) && !isIdentChar(line[start])) if ((start> 0) && !isIdentChar(line[start]))
start--; start--;
if (isIdentChar(line[start])) { if (isIdentChar(line[start])) {
int stop = start; int stop = start;
while ((stop < Len) && isIdentChar(line[stop])) while ((stop < len) && isIdentChar(line[stop]))
stop++; stop++;
while ((start-1 >=0) && isIdentChar(line[start - 1])) while ((start-1 >=0) && isIdentChar(line[start - 1]))
start--; start--;
@ -1079,6 +1079,20 @@ QString SynEdit::wordAtRowCol(const BufferCoord &XY)
return ""; return "";
} }
QChar SynEdit::charAt(const BufferCoord &pos)
{
if ((pos.Line >= 1) && (pos.Line <= mLines->count())) {
QString line = mLines->getString(pos.Line-1);
int len = line.length();
if (len == 0)
return QChar(0);
if (pos.Char<1 || pos.Char>len)
return QChar(0);
return line[pos.Char-1];
}
return QChar(0);
}
void SynEdit::setCaretAndSelection(const BufferCoord &ptCaret, const BufferCoord &ptBefore, const BufferCoord &ptAfter) void SynEdit::setCaretAndSelection(const BufferCoord &ptCaret, const BufferCoord &ptBefore, const BufferCoord &ptAfter)
{ {
SynSelectionMode vOldMode = mActiveSelectionMode; SynSelectionMode vOldMode = mActiveSelectionMode;

View File

@ -211,6 +211,7 @@ public:
QString wordAtCursor(); QString wordAtCursor();
QString wordAtRowCol(const BufferCoord& XY); QString wordAtRowCol(const BufferCoord& XY);
QChar charAt(const BufferCoord& pos);
int charColumns(QChar ch) const; int charColumns(QChar ch) const;
bool isPointInSelection(const BufferCoord& Value) const; bool isPointInSelection(const BufferCoord& Value) const;