- fix: wront indent when insert line break

This commit is contained in:
royqh1979@gmail.com 2021-09-01 16:17:31 +08:00
parent 4d16ef70c8
commit fe4c6f0659
2 changed files with 54 additions and 56 deletions

View File

@ -1839,10 +1839,11 @@ void SynEdit::insertLine(bool moveCaret)
mUndoList->AddChange(SynChangeReason::crDelete, iUndoBegin, iUndoEnd, helper, mUndoList->AddChange(SynChangeReason::crDelete, iUndoBegin, iUndoEnd, helper,
mActiveSelectionMode); mActiveSelectionMode);
} }
QString Temp = lineText(); QString Temp = lineText();
QString Temp2 = Temp; QString Temp2 = Temp;
QString Temp3; QString Temp3;
int SpaceCount1,SpaceCount2; int SpaceCount2;
PSynHighlighterAttribute Attr; PSynHighlighterAttribute Attr;
// This is sloppy, but the Right Thing would be to track the column of markers // This is sloppy, but the Right Thing would be to track the column of markers
@ -1851,63 +1852,55 @@ void SynEdit::insertLine(bool moveCaret)
int Len = Temp.length(); int Len = Temp.length();
if (Len > 0) { if (Len > 0) {
if (Len >= mCaretX) { if (Len >= mCaretX) {
if (mCaretX > 1) { if (mCaretX <= 1) {
Temp = lineText().mid(0, mCaretX - 1);
Temp3 = Temp;
SpaceCount1 = leftSpaces(Temp);
Temp2.remove(0, mCaretX - 1);
ProperSetLine(mCaretY-1,Temp);
QString Temp4;
if (mOptions.testFlag(eoAutoIndent)) {
Temp4=GetLeftSpacing(SpaceCount1, true);
Temp2=TrimLeft(Temp2);
}
if (mOptions.testFlag(eoAddIndent) &&
GetHighlighterAttriAtRowCol(BufferCoord{Temp3.length(), mCaretY},
Temp3, Attr)) { // only add indent to source files
if (Attr != mHighlighter->commentAttribute()) { // and outside of comments
if ((!Temp.isEmpty() && Temp[Temp.length()-1] ==':')
|| (
(Temp[Temp.length()-1] =='{')
&& ((Temp2.isEmpty()) || (Temp2[0]!='}'))
)) { // add more indent for these too
if (!mOptions.testFlag(eoTabsToSpaces)) {
Temp4 = GetLeftSpacing(SpaceCount1+mTabWidth,true);
}
}
}
}
mLines->Insert(mCaretY, Temp4+Temp2);
nLinesInserted++;
SpaceCount1 = mLines->getString(mCaretY).length(); //???
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2,
SynSelectionMode::smNormal);
if ((Temp.length()>0) && (Temp[Temp.length()-1] == '{') &&
(Temp2.length()>0) && (Temp2[0]=='}')) {
if (mOptions.testFlag(eoAddIndent)) {
Temp4 = GetLeftSpacing(leftSpaces(Temp)+mTabWidth,true)+Temp4;
} else {
Temp4=GetLeftSpacing(leftSpaces(Temp), true);
}
mLines->Insert(mCaretY, Temp4);
nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
SynSelectionMode::smNormal);
if (moveCaret)
internalSetCaretXY(BufferCoord{Temp4.length()+1, mCaretY + 1});
} else {
if (moveCaret)
internalSetCaretXY(BufferCoord{SpaceCount1+1,mCaretY + 1});
}
} else {
mLines->Insert(mCaretY - 1, ""); mLines->Insert(mCaretY - 1, "");
nLinesInserted++; nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2, mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), Temp2,
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
if (moveCaret) if (moveCaret)
internalSetCaretY(mCaretY + 1); internalSetCaretY(mCaretY + 1);
} else {
QString leftLineText = lineText().mid(0, mCaretX - 1);
QString rightLineText = lineText().mid(mCaretX-1);
int indentSpacesOfLeftLineText = leftSpaces(leftLineText);
int indentSpaces = indentSpacesOfLeftLineText;
bool notInComment=true;
ProperSetLine(mCaretY-1,leftLineText);
if (mOptions.testFlag(eoAutoIndent)) {
rightLineText=TrimLeft(rightLineText);
}
if (GetHighlighterAttriAtRowCol(BufferCoord{leftLineText.length(), mCaretY},
leftLineText, Attr)) {
notInComment = (Attr != mHighlighter->commentAttribute());
}
leftLineText = leftLineText.trimmed();
if (mOptions.testFlag(eoAddIndent)) { // only add indent to source files
if (notInComment) { // and outside of comments
if (leftLineText.endsWith(':') || leftLineText.endsWith('{'))
indentSpaces+=mTabWidth;
if (rightLineText.startsWith('}'))
indentSpaces-=mTabWidth;
}
}
QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->Insert(mCaretY, indentSpacesForRightLineText+rightLineText);
nLinesInserted++;
//SpaceCount1 = mLines->getString(mCaretY).length(); //???
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), rightLineText,
SynSelectionMode::smNormal);
//insert new line in middle of "{" and "}"
if (notInComment && leftLineText.endsWith('{') && rightLineText.startsWith('}')) {
indentSpaces = indentSpacesOfLeftLineText;
indentSpaces += mTabWidth;
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
mLines->Insert(mCaretY, indentSpacesForRightLineText);
nLinesInserted++;
mUndoList->AddChange(SynChangeReason::crLineBreak, caretXY(), caretXY(), "",
SynSelectionMode::smNormal);
}
if (moveCaret)
internalSetCaretXY(BufferCoord{indentSpacesForRightLineText.length()+1,mCaretY + 1});
} }
} else { } else {
SpaceCount2 = 0; SpaceCount2 = 0;
@ -1929,7 +1922,8 @@ void SynEdit::insertLine(bool moveCaret)
if (mOptions.testFlag(eoAddIndent) && GetHighlighterAttriAtRowCol(BufferCoord{Temp.length(), mCaretY}, if (mOptions.testFlag(eoAddIndent) && GetHighlighterAttriAtRowCol(BufferCoord{Temp.length(), mCaretY},
Temp, Attr)) { // only add indent to source files Temp, Attr)) { // only add indent to source files
if (Attr != mHighlighter->commentAttribute()) { // and outside of comments if (Attr != mHighlighter->commentAttribute()) { // and outside of comments
if (Temp.length() && (Temp[Temp.length()-1] == '{' || Temp[Temp.length()-1] == ':')) { // add more indent for these too Temp = Temp.trimmed();
if (Temp.endsWith('{') || Temp.endsWith(':')) { // add more indent for these too
Temp4=GetLeftSpacing(mTabWidth,true)+Temp4; Temp4=GetLeftSpacing(mTabWidth,true)+Temp4;
} }
} }
@ -4137,10 +4131,11 @@ void SynEdit::MoveCaretToLineEnd(bool isSelection)
int vNewX; int vNewX;
if (mOptions.testFlag(SynEditorOption::eoEnhanceEndKey)) { if (mOptions.testFlag(SynEditorOption::eoEnhanceEndKey)) {
QString vText = lineText(); QString vText = lineText();
int vLastNonBlank = vText.length(); int vLastNonBlank = vText.length()-1;
int vMinX = 0; int vMinX = 0;
while ((vLastNonBlank > vMinX) && (vText[vLastNonBlank] == ' ' || vText[vLastNonBlank] =='\t')) while ((vLastNonBlank >= vMinX) && (vText[vLastNonBlank] == ' ' || vText[vLastNonBlank] =='\t'))
vLastNonBlank--; vLastNonBlank--;
vLastNonBlank++;
vNewX = mCaretX; vNewX = mCaretX;
if (vNewX > vLastNonBlank) if (vNewX > vLastNonBlank)
vNewX = vText.length() + 1; vNewX = vText.length() + 1;

View File

@ -57,6 +57,9 @@
</item> </item>
<item> <item>
<widget class="QLabel" name="lblBraceStyle"> <widget class="QLabel" name="lblBraceStyle">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="text"> <property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Note for the predefined format style&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Note for the predefined format style&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property> </property>
@ -846,7 +849,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="Editor" name="frame"> <widget class="Editor" name="editDemo">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>0</width> <width>0</width>