1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-11 04:52:21 +02:00

Reworked IDE paren pair support

This commit is contained in:
Brian Fiete 2020-10-17 12:59:10 -07:00
parent 95c603ada5
commit 743a8da08b
2 changed files with 60 additions and 20 deletions

View file

@ -1116,7 +1116,7 @@ namespace Beefy.widgets
if (moveCursor) if (moveCursor)
{ {
textPos += insertStr.Length; textPos += insertStr.Length;
MoveCursorToIdx((int32)textPos); MoveCursorToIdx((int32)textPos, false, .FromTyping);
if (mEnsureCursorVisibleOnModify) if (mEnsureCursorVisibleOnModify)
EnsureCursorVisible(); EnsureCursorVisible();
} }
@ -1519,7 +1519,7 @@ namespace Beefy.widgets
ContentChanged(); ContentChanged();
if (offset != 0) if (offset != 0)
{ {
MoveCursorToIdx(textPos); MoveCursorToIdx(textPos, false, .FromTyping);
EnsureCursorVisible(); EnsureCursorVisible();
} }
} }
@ -1675,12 +1675,12 @@ namespace Beefy.widgets
return true; return true;
} }
public void InsertCharPair(String char8Pair) public void InsertCharPair(String charPair)
{ {
if (CheckReadOnly()) if (CheckReadOnly())
return; return;
InsertAtCursor(char8Pair); InsertAtCursor(charPair);
MoveCursorToIdx(CursorTextPos - 1); MoveCursorToIdx(CursorTextPos - 1, false, .FromTyping);
mJustInsertedCharPair = true; mJustInsertedCharPair = true;
} }
@ -2865,9 +2865,15 @@ namespace Beefy.widgets
y = 0; y = 0;
} }
public enum CursorMoveKind
{
FromTyping,
Unknown
}
// We used to have a split between PhysCursorMoved and CursorMoved. CursorMoved has a "ResetWantX" and was non-virtual... uh- // We used to have a split between PhysCursorMoved and CursorMoved. CursorMoved has a "ResetWantX" and was non-virtual... uh-
// so what was that for? // so what was that for?
public virtual void PhysCursorMoved() public virtual void PhysCursorMoved(CursorMoveKind moveKind)
{ {
mJustInsertedCharPair = false; mJustInsertedCharPair = false;
mShowCursorAtLineEnd = false; mShowCursorAtLineEnd = false;
@ -2880,7 +2886,7 @@ namespace Beefy.widgets
public void CursorMoved() public void CursorMoved()
{ {
PhysCursorMoved(); PhysCursorMoved(.Unknown);
/*mJustInsertedCharPair = false; /*mJustInsertedCharPair = false;
mShowCursorAtLineEnd = false; mShowCursorAtLineEnd = false;
@ -3350,7 +3356,7 @@ namespace Beefy.widgets
//public void MoveCursorTo //public void MoveCursorTo
public void MoveCursorTo(int line, int char8Idx, bool centerCursor = false, int movingDir = 0) public void MoveCursorTo(int line, int char8Idx, bool centerCursor = false, int movingDir = 0, CursorMoveKind cursorMoveKind = .Unknown)
{ {
int useCharIdx = char8Idx; int useCharIdx = char8Idx;
@ -3391,7 +3397,7 @@ namespace Beefy.widgets
mCursorBlinkTicks = 0; mCursorBlinkTicks = 0;
if (mEnsureCursorVisibleOnModify) if (mEnsureCursorVisibleOnModify)
EnsureCursorVisible(true, centerCursor); EnsureCursorVisible(true, centerCursor);
PhysCursorMoved(); PhysCursorMoved(cursorMoveKind);
} }
public void ResetWantX() public void ResetWantX()
@ -3400,12 +3406,12 @@ namespace Beefy.widgets
mCursorWantX = x; mCursorWantX = x;
} }
public void MoveCursorToIdx(int index, bool centerCursor = false) public void MoveCursorToIdx(int index, bool centerCursor = false, CursorMoveKind cursorMoveKind = .Unknown)
{ {
int aLine; int aLine;
int aCharIdx; int aCharIdx;
GetLineCharAtIdx(index, out aLine, out aCharIdx); GetLineCharAtIdx(index, out aLine, out aCharIdx);
MoveCursorTo(aLine, aCharIdx, centerCursor); MoveCursorTo(aLine, aCharIdx, centerCursor, 0, cursorMoveKind);
} }
public void MoveCursorToCoord(float x, float y) public void MoveCursorToCoord(float x, float y)
@ -3423,7 +3429,7 @@ namespace Beefy.widgets
GetLineAndColumnAtCoord(x, y, out line, out column); GetLineAndColumnAtCoord(x, y, out line, out column);
CursorLineAndColumn = LineAndColumn(line, column); CursorLineAndColumn = LineAndColumn(line, column);
mCursorBlinkTicks = 0; mCursorBlinkTicks = 0;
PhysCursorMoved(); PhysCursorMoved(.Unknown);
mShowCursorAtLineEnd = false; mShowCursorAtLineEnd = false;
} }
else else

View file

@ -218,7 +218,7 @@ namespace IDE.ui
) ~ delete _; ) ~ delete _;
bool mHasCustomColors; bool mHasCustomColors;
FastCursorState mFastCursorState ~ delete _; FastCursorState mFastCursorState ~ delete _;
public HashSet<int32> mCurParenPairIdSet = new .() ~ delete _;
public List<PersistentTextPosition> PersistentTextPositions public List<PersistentTextPosition> PersistentTextPositions
{ {
@ -1439,7 +1439,7 @@ namespace IDE.ui
InsertAtCursor(useString); InsertAtCursor(useString);
// If we paste in "if (a)\n\tDoThing();", we want "DoThing();" to be indented, so we need to fix this. // If we paste in "if (a)\n\tDoThing();", we want "DoThing();" to be indented, so we need to fix this.
// This isn't a problem if we had "if (a)\n\tDoThing()\nDoOtherThing();" because minColumn would match // This isn't a problem if we had "if (a)\n\tDoThing()\nDoOtherThing(1);" because minColumn would match
// DoOtherThing so DoThing would indent properly (for example) // DoOtherThing so DoThing would indent properly (for example)
if ((didFormattedPaste) && (lineAndColumn.mLine < GetLineCount() - 1)) if ((didFormattedPaste) && (lineAndColumn.mLine < GetLineCount() - 1))
{ {
@ -2674,6 +2674,28 @@ namespace IDE.ui
mAutoComplete.CloseListWindow();*/ mAutoComplete.CloseListWindow();*/
} }
bool IsCurrentPairClosing(int cursorIdx)
{
int32 closeId = mData.mTextIdData.GetIdAtIndex(cursorIdx);
if (closeId != -1)
{
int32 openId = closeId - 1;
if (mCurParenPairIdSet.Contains(openId))
{
int openCursorIdx = mData.mTextIdData.GetIndexFromId(openId);
if (openCursorIdx != -1)
return true;
}
}
return false;
}
void InsertCharPair(String charPair)
{
mCurParenPairIdSet.Add(mData.mNextCharId);
base.InsertCharPair(charPair);
}
public override void KeyChar(char32 keyChar) public override void KeyChar(char32 keyChar)
{ {
scope AutoBeefPerf("SEWC.KeyChar"); scope AutoBeefPerf("SEWC.KeyChar");
@ -3037,7 +3059,11 @@ namespace IDE.ui
if (cursorTextPos < mData.mTextLength) if (cursorTextPos < mData.mTextLength)
{ {
char8UnderCursor = (char8)mData.mText[cursorTextPos].mChar; char8UnderCursor = (char8)mData.mText[cursorTextPos].mChar;
cursorInOpenSpace = ((char8UnderCursor == ')') || (char8UnderCursor == ']') || (char8UnderCursor == (char8)0) || (char8UnderCursor.IsWhiteSpace)); cursorInOpenSpace = ((char8UnderCursor == ')') || (char8UnderCursor == ']') || (char8UnderCursor == ';') || (char8UnderCursor == (char8)0) || (char8UnderCursor.IsWhiteSpace));
if (((keyChar == '(') && (char8UnderCursor == ')')) ||
((keyChar == '[') && (char8UnderCursor == ']')))
cursorInOpenSpace = IsCurrentPairClosing(cursorTextPos);
if ((char8UnderCursor == keyChar) && (!HasSelection())) if ((char8UnderCursor == keyChar) && (!HasSelection()))
{ {
@ -3059,7 +3085,8 @@ namespace IDE.ui
if (!ignore) if (!ignore)
{ {
if ((mData.mText[checkPos].mDisplayTypeId == (int32)wantElementType) && if ((mData.mText[checkPos].mDisplayTypeId == (int32)wantElementType) &&
((keyChar == '"') || (keyChar == '\'') || (keyChar == ')') || (keyChar == ']') || (keyChar == '>') || (keyChar == '}'))) ((keyChar == '"') || (keyChar == '\'') || (keyChar == ')') || (keyChar == ']') || (keyChar == '>') || (keyChar == '}')) &&
(IsCurrentPairClosing(checkPos)))
{ {
mJustInsertedCharPair = false; mJustInsertedCharPair = false;
CursorTextPos++; CursorTextPos++;
@ -3176,7 +3203,9 @@ namespace IDE.ui
base.KeyChar(keyChar); base.KeyChar(keyChar);
} }
else if ((keyChar == '(') && (cursorInOpenSpace)) else if ((keyChar == '(') && (cursorInOpenSpace))
InsertCharPair("()"); {
InsertCharPair("()");
}
else if ((keyChar == '{') && (cursorInOpenSpace)) else if ((keyChar == '{') && (cursorInOpenSpace))
{ {
/*int lineStart; /*int lineStart;
@ -4104,11 +4133,16 @@ namespace IDE.ui
mVirtualCursorPos.ValueRef.mColumn = (.)Math.Min(mVirtualCursorPos.Value.mColumn, Math.Max(virtualEnd, lineEnd)); mVirtualCursorPos.ValueRef.mColumn = (.)Math.Min(mVirtualCursorPos.Value.mColumn, Math.Max(virtualEnd, lineEnd));
} }
public override void PhysCursorMoved() public override void PhysCursorMoved(CursorMoveKind moveKind)
{ {
//Debug.WriteLine("Cursor moved {0} {1}", CursorLineAndColumn.mLine, CursorLineAndColumn.mColumn); //Debug.WriteLine("Cursor moved {0} {1} {2}", CursorLineAndColumn.mLine, CursorLineAndColumn.mColumn, moveKind);
base.PhysCursorMoved(); if (moveKind != .FromTyping)
{
mCurParenPairIdSet.Clear();
}
base.PhysCursorMoved(moveKind);
mCursorStillTicks = 0; mCursorStillTicks = 0;
if ((mSourceViewPanel != null) && (mSourceViewPanel.mHoverWatch != null)) if ((mSourceViewPanel != null) && (mSourceViewPanel.mHoverWatch != null))