Add selection support to TAB and Space conversion commands

Fix #12720, fix #12745, close #12739
This commit is contained in:
ArkadiuszMichalski 2022-12-31 11:57:40 +01:00 committed by Don Ho
parent 6c3659fe73
commit 1652236f32

View File

@ -1280,38 +1280,84 @@ bool Notepad_plus::replaceInOpenedFiles()
return true; return true;
} }
void Notepad_plus::wsTabConvert(spaceTab whichWay) void Notepad_plus::wsTabConvert(spaceTab whichWay)
{ {
// block selection is not supported
if ((_pEditView->execute(SCI_GETSELECTIONMODE) == SC_SEL_RECTANGLE) || (_pEditView->execute(SCI_GETSELECTIONMODE) == SC_SEL_THIN))
return;
intptr_t tabWidth = _pEditView->execute(SCI_GETTABWIDTH); intptr_t tabWidth = _pEditView->execute(SCI_GETTABWIDTH);
intptr_t currentPos = _pEditView->execute(SCI_GETCURRENTPOS); intptr_t currentPos = _pEditView->execute(SCI_GETCURRENTPOS);
intptr_t lastLine = _pEditView->lastZeroBasedLineNumber(); intptr_t currentLine = _pEditView->execute(SCI_LINEFROMPOSITION, currentPos);
intptr_t docLength = _pEditView->execute(SCI_GETLENGTH) + 1; intptr_t currentPosInLine = currentPos - _pEditView->execute(SCI_POSITIONFROMLINE, currentLine);
if (docLength < 2)
intptr_t startLine = 0;
intptr_t endLine = _pEditView->lastZeroBasedLineNumber();
intptr_t endLineCorrect = endLine;
intptr_t dataLength = _pEditView->execute(SCI_GETLENGTH) + 1;
intptr_t mainSelAnchor = _pEditView->execute(SCI_GETANCHOR);
bool isEntireDoc = (mainSelAnchor == currentPos);
// restore original selection if nothing has changed
auto restoreSelection = [this, mainSelAnchor, currentPos, isEntireDoc]()
{
if (!isEntireDoc)
{
_pEditView->execute(SCI_SETANCHOR, mainSelAnchor);
_pEditView->execute(SCI_SETCURRENTPOS, currentPos);
}
};
// auto-expand of partially selected lines
if (!isEntireDoc)
{
intptr_t startPos = _pEditView->execute(SCI_GETSELECTIONSTART);
startLine = _pEditView->execute(SCI_LINEFROMPOSITION, startPos);
intptr_t endPos = _pEditView->execute(SCI_GETSELECTIONEND);
endLine = endLineCorrect = _pEditView->execute(SCI_LINEFROMPOSITION, endPos);
if (startPos != _pEditView->execute(SCI_POSITIONFROMLINE, startLine))
startPos = _pEditView->execute(SCI_POSITIONFROMLINE, startLine);
if (endPos == _pEditView->execute(SCI_POSITIONFROMLINE, endLine))
endLineCorrect = endLine - 1;
else if (endPos < _pEditView->execute(SCI_GETLINEENDPOSITION, endLine))
endPos = _pEditView->execute(SCI_GETLINEENDPOSITION, endLine);
dataLength = endPos - startPos + 1;
_pEditView->execute(SCI_SETSEL, startPos, endPos);
}
if (dataLength < 2)
{
restoreSelection();
return; return;
}
intptr_t changeDataCount = 0;
intptr_t newCurrentPos = 0;
vector<intptr_t> folding;
_pEditView->execute(SCI_BEGINUNDOACTION);
for (intptr_t idx = startLine; idx < endLineCorrect + 1; ++idx)
{
intptr_t startPos = _pEditView->execute(SCI_POSITIONFROMLINE, idx);
intptr_t endPos = _pEditView->execute(SCI_GETLINEENDPOSITION, idx);
dataLength = endPos - startPos + 1;
char * source = new char[dataLength];
if (source == NULL)
continue;
source[dataLength - 1] = '\0'; // make sure to have correct data termination
_pEditView->execute(SCI_SETTARGETRANGE, startPos, endPos);
_pEditView->execute(SCI_GETTARGETTEXT, 0, reinterpret_cast<LPARAM>(source));
intptr_t count = 0; intptr_t count = 0;
intptr_t column = 0; intptr_t column = 0;
intptr_t newCurrentPos = 0;
intptr_t tabStop = tabWidth - 1; // remember, counting from zero ! intptr_t tabStop = tabWidth - 1; // remember, counting from zero !
bool onlyLeading = false; bool onlyLeading = false;
vector<int> bookmarks;
vector<int> folding;
for (int i=0; i<lastLine; ++i)
{
if (bookmarkPresent(i))
bookmarks.push_back(i);
if ((_pEditView->execute(SCI_GETFOLDLEVEL, i) & SC_FOLDLEVELHEADERFLAG))
if (_pEditView->execute(SCI_GETFOLDEXPANDED, i) == 0)
folding.push_back(i);
}
char * source = new char[docLength];
if (source == NULL)
return;
_pEditView->execute(SCI_GETTEXT, docLength, reinterpret_cast<LPARAM>(source));
if (whichWay == tab2Space) if (whichWay == tab2Space)
{ {
@ -1324,18 +1370,19 @@ void Notepad_plus::wsTabConvert(spaceTab whichWay)
if (count == 0) if (count == 0)
{ {
delete [] source; delete [] source;
return; continue;
} }
} }
// allocate tabwidth-1 chars extra per tab, just to be safe // allocate tabwidth-1 chars extra per tab, just to be safe
size_t newlen = docLength + count * (tabWidth - 1) + 1; size_t newLen = dataLength + count * (tabWidth - 1) + 1;
char * destination = new char[newlen]; char * destination = new char[newLen];
if (destination == NULL) if (destination == NULL)
{ {
delete [] source; delete [] source;
return; continue;
} }
char * dest = destination; char * dest = destination;
intptr_t changeDataLineCount = 0;
switch (whichWay) switch (whichWay)
{ {
@ -1350,7 +1397,9 @@ void Notepad_plus::wsTabConvert(spaceTab whichWay)
for (int j = 0; j < insertTabs; ++j) for (int j = 0; j < insertTabs; ++j)
{ {
*dest++ = ' '; *dest++ = ' ';
if (i <= currentPos) changeDataCount++;
changeDataLineCount++;
if (idx == currentLine && i < currentPosInLine)
++newCurrentPos; ++newCurrentPos;
} }
column += insertTabs; column += insertTabs;
@ -1358,7 +1407,7 @@ void Notepad_plus::wsTabConvert(spaceTab whichWay)
else else
{ {
*dest++ = source[i]; *dest++ = source[i];
if (i <= currentPos) if (idx == currentLine && i < currentPosInLine)
++newCurrentPos; ++newCurrentPos;
if ((source[i] == '\n') || (source[i] == '\r')) if ((source[i] == '\n') || (source[i] == '\r'))
column = 0; column = 0;
@ -1390,21 +1439,25 @@ void Notepad_plus::wsTabConvert(spaceTab whichWay)
if (counter >= 1) // counter is counted from 0, so counter >= max-1 if (counter >= 1) // counter is counted from 0, so counter >= max-1
{ {
*dest++ = '\t'; *dest++ = '\t';
changeDataCount++;
changeDataLineCount++;
i += counter; i += counter;
column += counter + 1; column += counter + 1;
counter = 0; counter = 0;
nextChar = true; nextChar = true;
if (i <= currentPos) if (idx == currentLine && i <= currentPosInLine)
++newCurrentPos; ++newCurrentPos;
break; break;
} }
else if (source[i + 1] == ' ' || source[i + 1] == '\t') // if followed by space or TAB, convert even a single space to TAB else if (source[i + 1] == ' ' || source[i + 1] == '\t') // if followed by space or TAB, convert even a single space to TAB
{ {
*dest++ = '\t'; *dest++ = '\t';
changeDataCount++;
changeDataLineCount++;
i++; i++;
column += 1; column += 1;
counter = 0; counter = 0;
if (i <= currentPos) if (idx == currentLine && i <= currentPosInLine)
++newCurrentPos; ++newCurrentPos;
} }
else // single space, don't convert it to TAB else // single space, don't convert it to TAB
@ -1413,7 +1466,7 @@ void Notepad_plus::wsTabConvert(spaceTab whichWay)
column += 1; column += 1;
counter = 0; counter = 0;
nextChar = true; nextChar = true;
if (i <= currentPos) if (idx == currentLine && i <= currentPosInLine)
++newCurrentPos; ++newCurrentPos;
break; break;
} }
@ -1431,11 +1484,13 @@ void Notepad_plus::wsTabConvert(spaceTab whichWay)
if (source[i] == ' ' && source[i + counter] == '\t') // spaces "absorbed" by a TAB on the right if (source[i] == ' ' && source[i + counter] == '\t') // spaces "absorbed" by a TAB on the right
{ {
*dest++ = '\t'; *dest++ = '\t';
changeDataCount++;
changeDataLineCount++;
i += counter; i += counter;
column = tabStop + 1; column = tabStop + 1;
tabStop += tabWidth; tabStop += tabWidth;
counter = 0; counter = 0;
if (i <= currentPos) if (idx == currentLine && i <= currentPosInLine)
++newCurrentPos; ++newCurrentPos;
continue; continue;
} }
@ -1471,7 +1526,7 @@ void Notepad_plus::wsTabConvert(spaceTab whichWay)
} }
} }
if (i <= currentPos) if (idx == currentLine && i < currentPosInLine)
++newCurrentPos; ++newCurrentPos;
} }
*dest = '\0'; *dest = '\0';
@ -1479,21 +1534,34 @@ void Notepad_plus::wsTabConvert(spaceTab whichWay)
} }
} }
_pEditView->execute(SCI_BEGINUNDOACTION); if ((_pEditView->execute(SCI_GETFOLDLEVEL, idx) & SC_FOLDLEVELHEADERFLAG))
_pEditView->execute(SCI_SETTEXT, 0, reinterpret_cast<LPARAM>(destination)); if (_pEditView->execute(SCI_GETFOLDEXPANDED, idx) == 0)
_pEditView->execute(SCI_GOTOPOS, newCurrentPos); folding.push_back(idx);
for (size_t i=0; i<bookmarks.size(); ++i) if (changeDataLineCount)
_pEditView->execute(SCI_MARKERADD, bookmarks[i], MARK_BOOKMARK); _pEditView->execute(SCI_REPLACETARGET, static_cast<WPARAM>(-1), reinterpret_cast<LPARAM>(destination));
for (size_t i=0; i<folding.size(); ++i)
_pEditView->fold(folding[i], false);
_pEditView->execute(SCI_ENDUNDOACTION);
// clean up // clean up
delete [] source; delete [] source;
delete [] destination; delete [] destination;
}
_pEditView->execute(SCI_ENDUNDOACTION);
if (changeDataCount)
{
if (!isEntireDoc)
_pEditView->execute(SCI_SETSEL, _pEditView->execute(SCI_POSITIONFROMLINE, startLine), endLineCorrect != endLine ? _pEditView->execute(SCI_POSITIONFROMLINE, endLine) : _pEditView->execute(SCI_GETLINEENDPOSITION, endLine));
else
_pEditView->execute(SCI_GOTOPOS, _pEditView->execute(SCI_POSITIONFROMLINE, currentLine) + newCurrentPos);
for (size_t i = 0; i < folding.size(); ++i)
_pEditView->fold(folding[i], false);
}
else
restoreSelection();
} }
void Notepad_plus::doTrim(trimOp whichPart) void Notepad_plus::doTrim(trimOp whichPart)