From d11b09dd078dccfbc77f08a2b70084591de9e8e8 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sun, 6 Sep 2020 05:27:37 -0700 Subject: [PATCH] Removed System.Tuple type --- BeefLibs/Beefy2D/src/widgets/EditWidget.bf | 18 +- BeefLibs/corlib/src/Tuple.bf | 27 -- IDE/src/ui/ProjectProperties.bf | 8 +- IDE/src/ui/SourceEditWidgetContent.bf | 286 ++++++++++++++++++++- IDE/src/ui/TargetedPropertiesDialog.bf | 33 ++- IDE/src/ui/WorkspaceProperties.bf | 8 +- 6 files changed, 316 insertions(+), 64 deletions(-) delete mode 100644 BeefLibs/corlib/src/Tuple.bf diff --git a/BeefLibs/Beefy2D/src/widgets/EditWidget.bf b/BeefLibs/Beefy2D/src/widgets/EditWidget.bf index e2251120..6f39e5c4 100644 --- a/BeefLibs/Beefy2D/src/widgets/EditWidget.bf +++ b/BeefLibs/Beefy2D/src/widgets/EditWidget.bf @@ -255,8 +255,8 @@ namespace Beefy.widgets public class IndentTextAction : TextAction { // InsertCharList is for block indent, RemoveCharList is for unindent (shift-tab) - public List> mRemoveCharList = new List>() ~ delete _; - public List> mInsertCharList = new List>() ~ delete _; + public List<(int32, char8)> mRemoveCharList = new .() ~ delete _; + public List<(int32, char8)> mInsertCharList = new .() ~ delete _; public EditSelection mNewSelection; public this(EditWidgetContent editWidget) @@ -272,13 +272,13 @@ namespace Beefy.widgets { var idxChar = mRemoveCharList[idx]; - editWidgetContent.InsertText(idxChar.Item1, ToStackString!(idxChar.Item2)); + editWidgetContent.InsertText(idxChar.0, ToStackString!(idxChar.1)); } for (int idx = mInsertCharList.Count - 1; idx >= 0; idx--) { var idxChar = mInsertCharList[idx]; - editWidgetContent.RemoveText(idxChar.Item1, 1); + editWidgetContent.RemoveText(idxChar.0, 1); } editWidgetContent.ContentChanged(); SetPreviousState(false); @@ -290,12 +290,12 @@ namespace Beefy.widgets var editWidgetContent = EditWidgetContent; SetPreviousState(true); for (var idxChar in mRemoveCharList) - editWidgetContent.RemoveText(idxChar.Item1, 1); + editWidgetContent.RemoveText(idxChar.0, 1); var charStr = scope String(' ', 1); for (var idxChar in mInsertCharList) { - charStr[0] = idxChar.Item2; - editWidgetContent.InsertText(idxChar.Item1, charStr); + charStr[0] = idxChar.1; + editWidgetContent.InsertText(idxChar.0, charStr); } editWidgetContent.ContentChanged(); editWidgetContent.mSelection = mNewSelection; @@ -3158,7 +3158,7 @@ namespace Beefy.widgets char8 c = (char8)mData.mText[lineStart + i].mChar; if (((c == '\t') && (i == 0)) || (c == ' ')) { - indentTextAction.mRemoveCharList.Add(Tuple((int32)lineStart + i + endAdjust, c)); + indentTextAction.mRemoveCharList.Add(((int32)lineStart + i + endAdjust, c)); //RemoveText(lineStart, 1); endAdjust--; } @@ -3181,7 +3181,7 @@ namespace Beefy.widgets GetLinePosition(lineIdx, out lineStart, out lineEnd); lineStart += endAdjust; //InsertText(lineStart, "\t"); - indentTextAction.mInsertCharList.Add(Tuple((int32)lineStart, '\t')); + indentTextAction.mInsertCharList.Add(((int32)lineStart, '\t')); endAdjust++; } diff --git a/BeefLibs/corlib/src/Tuple.bf b/BeefLibs/corlib/src/Tuple.bf deleted file mode 100644 index e9a4837a..00000000 --- a/BeefLibs/corlib/src/Tuple.bf +++ /dev/null @@ -1,27 +0,0 @@ -namespace System -{ - struct Tuple : IHashable where T1 : IHashable where T2 : IHashable - { - public T1 Item1; - public T2 Item2; - - public this() - { - Item1 = default(T1); - Item2 = default(T2); - } - - public this(T1 item1, T2 item2) - { - Item1 = item1; - Item2 = item2; - } - - public int GetHashCode() - { - int h1 = Item1.GetHashCode(); - int h2 = Item2.GetHashCode(); - return (((h1 << 5) + h1) ^ h2); - } - } -} diff --git a/IDE/src/ui/ProjectProperties.bf b/IDE/src/ui/ProjectProperties.bf index ffdb9276..0e749041 100644 --- a/IDE/src/ui/ProjectProperties.bf +++ b/IDE/src/ui/ProjectProperties.bf @@ -539,12 +539,12 @@ namespace IDE.ui if ((GetCategoryTargetedKind(categoryTypeInt) == .Config) && ((mConfigNames.Count == 1) && (mPlatformNames.Count == 1))) { - var key = Tuple(configName, platformName); + var key = ConfigPlatformPair(configName, platformName); var targetedConfigDataResult = mTargetedConfigDatas.GetValue(key); if (!(targetedConfigDataResult case .Ok(out targetedConfigData))) { - key.Item1 = new String(key.Item1); - key.Item2 = new String(key.Item2); + key.mConfig = new String(key.mConfig); + key.mPlatform = new String(key.mPlatform); targetedConfigData = new ConfigDataGroup((int32)CategoryType.COUNT); targetedConfigData.mTarget = key; mTargetedConfigDatas[key] = targetedConfigData; @@ -1021,7 +1021,7 @@ namespace IDE.ui projectProperties.HadExternalChanges(configName, platformName); } else - projectProperties.HadExternalChanges(targetedConfigData.mTarget.Item1, targetedConfigData.mTarget.Item2); + projectProperties.HadExternalChanges(targetedConfigData.mTarget.mConfig, targetedConfigData.mTarget.mPlatform); } else projectProperties.HadExternalChanges(null, null); diff --git a/IDE/src/ui/SourceEditWidgetContent.bf b/IDE/src/ui/SourceEditWidgetContent.bf index 203bd598..e278b49f 100644 --- a/IDE/src/ui/SourceEditWidgetContent.bf +++ b/IDE/src/ui/SourceEditWidgetContent.bf @@ -1763,7 +1763,7 @@ namespace IDE.ui for (i = 0; i < indentCount; i++) { InsertText(lineStart + i, "\t"); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i), '\t')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i), '\t')); endAdjust++; } @@ -1772,16 +1772,16 @@ namespace IDE.ui if (wantsContentTab) { InsertText(lineStart + i, "{\n\t"); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i), '{')); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i + 1), '\n')); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i + 2), '\t')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i), '{')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i + 1), '\n')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i + 2), '\t')); endAdjust += 3; } else { InsertText(lineStart + i, "{\n"); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i), '{')); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i + 1), '\n')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i), '{')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i + 1), '\n')); startAdjust -= 1; endAdjust += 2; } @@ -1803,19 +1803,19 @@ namespace IDE.ui for (i = 0; i < indentCount; i++) { InsertText(lineStart + i, "\t"); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i), '\t')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i), '\t')); } if (isEmbeddedEnd) { InsertText(lineStart + i, "}"); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i), '}')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i), '}')); } else { InsertText(lineStart + i, "}\n"); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i), '}')); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart + i + 1), '\n')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i), '}')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart + i + 1), '\n')); } newSel.mEndPos = (int32)(lineStart + i + 1); @@ -1828,7 +1828,7 @@ namespace IDE.ui if (c != '#') { InsertText(lineStart, "\t"); - indentTextAction.mInsertCharList.Add(Tuple((int32)(lineStart), '\t')); + indentTextAction.mInsertCharList.Add(((int32)(lineStart), '\t')); endAdjust++; } } @@ -2121,7 +2121,7 @@ namespace IDE.ui public void DuplicateLine() { - UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedToggleComment"); + UndoBatchStart undoBatchStart = new UndoBatchStart("duplicateLine"); mData.mUndoManager.Add(undoBatchStart); mData.mUndoManager.Add(new SetCursorAction(this)); @@ -2139,6 +2139,268 @@ namespace IDE.ui mData.mUndoManager.Add(undoBatchStart.mBatchEnd); } + bool GetStatementRange(int checkIdx, out int startIdx, out int endIdx, out char8 endChar) + { + startIdx = -1; + endIdx = -1; + endChar = 0; + + GetLineCharAtIdx(checkIdx, var line, var lineChar); + GetBlockStart(line, var foundBlockStartIdx, var blockOpenSpaceCount); + + bool expectingStatement = true; + int stmtStart = -1; + + int lastNonWS = -1; + int parenCount = 0; + int lastCrPos = -1; + + for (int checkPos = foundBlockStartIdx; true; checkPos++) + { + if (checkPos >= mData.mTextLength) + break; + + char8 checkC = mData.mText[checkPos].mChar; + if (checkC == '\n') + lastCrPos = checkPos; + + if (checkC.IsWhiteSpace) + continue; + + let displayType = (SourceElementType)mData.mText[checkPos].mDisplayTypeId; + if (displayType == .Comment) + continue; + + if (displayType == .Normal) + { + if (checkC == '(') + parenCount++; + else if (checkC == ')') + parenCount--; + } + + if (parenCount != 0) + continue; + + if ((displayType == .Normal) && + ((checkC == '{') || (checkC == '}') || (checkC == ';'))) + { + if ((checkPos >= checkIdx) && (!expectingStatement)) + { + endChar = checkC; + if (lastNonWS < checkIdx) + return false; + + startIdx = stmtStart; + endIdx = lastNonWS + 1; + return true; + } + + expectingStatement = true; + } + else if (expectingStatement) + { + if (lastCrPos >= checkIdx) + { + return false; + } + + expectingStatement = false; + stmtStart = checkPos; + } + + lastNonWS = checkPos; + } + + return false; + } + + void MoveSelection(int toLinePos) + { + /*if (GetStatementRange(CursorTextPos, var startIdx, var endIdx)) + { + mSelection = .(startIdx, endIdx); + } + + return;*/ + + UndoBatchStart undoBatchStart = new UndoBatchStart("moveSelection"); + mData.mUndoManager.Add(undoBatchStart); + + mData.mUndoManager.Add(new SetCursorAction(this)); + + var prevCursorLineAndColumn = CursorLineAndColumn; + var str = scope String(); + ExtractString(mSelection.Value.MinPos, mSelection.Value.Length, str); + DeleteSelection(); + + if (str.EndsWith('\n')) + str.RemoveFromEnd(1); + + int offsetLinePos = toLinePos; + bool movingDown = offsetLinePos > prevCursorLineAndColumn.mLine; + if (movingDown) + offsetLinePos -= str.Count('\n'); + + GetLinePosition(Math.Max(offsetLinePos, 0), var offsetLineStart, var offsetLineEnd); + String offsetText = scope .(); + ExtractString(offsetLineStart, offsetLineEnd - offsetLineStart, offsetText); + + if (movingDown) + { + GetLinePosition(Math.Max(offsetLinePos - 1, 0), var toLineStart, var toLineEnd); + String txt = scope .(); + ExtractString(toLineStart, toLineEnd - toLineStart, txt); + if (GetStatementRange(toLineStart, var stmtStartIdx, var stmtEndIdx, var stmtEndChar)) + { + String stmt = scope .(); + ExtractString(stmtStartIdx, stmtEndIdx - stmtStartIdx, stmt); + GetLineCharAtIdx(stmtEndIdx - 1, var stmtLine, var stmtLineChar); + offsetLinePos = stmtLine + 1; + GetLinePosition(Math.Max(offsetLinePos, 0), out offsetLineStart, out offsetLineEnd); + + if (stmtEndChar == '{') + offsetLinePos++; + } + } + else + { + /*for (int checkPos = offsetLineStart; checkPos < offsetLineEnd; checkPos++) + { + if (mData.mText[checkPos].mDisplayTypeId != 0) + continue; + char8 checkC = mData.mText[checkPos].mChar; + if (checkC.IsWhiteSpace) + continue; + if (checkC == '{') + { + if (offsetLinePos > prevCursorLineAndColumn.mLine) + offsetLinePos--; + } + break; + }*/ + + GetLinePosition(Math.Max(offsetLinePos, 0), var toLineStart, var toLineEnd); + String txt = scope .(); + ExtractString(toLineStart, toLineEnd - toLineStart, txt); + + if (!GetStatementRange(toLineStart, var stmtStartIdx, var stmtEndIdx, var stmtEndChar)) + { + if (stmtEndChar == '{') + GetLinePosition(Math.Max(offsetLinePos - 1, 0), out toLineStart, out toLineEnd); + } + + if (GetStatementRange(toLineStart, var stmtStartIdx, var stmtEndIdx, var stmtEndChar)) + { + String stmt = scope .(); + ExtractString(stmtStartIdx, stmtEndIdx - stmtStartIdx, stmt); + GetLineCharAtIdx(stmtStartIdx, var stmtLine, var stmtLineChar); + offsetLinePos = stmtLine; + GetLinePosition(Math.Max(offsetLinePos, 0), out offsetLineStart, out offsetLineEnd); + } + } + + + let wantCursorPos = LineAndColumn(Math.Max(offsetLinePos, 0), 0); + if (wantCursorPos.mLine >= GetLineCount()) + { + CursorTextPos = mData.mTextLength; + InsertAtCursor("\n"); + } + + CursorLineAndColumn = wantCursorPos; + PasteText(str, "line"); + CursorLineAndColumn = LineAndColumn(wantCursorPos.mLine, prevCursorLineAndColumn.mColumn); + + mData.mUndoManager.Add(undoBatchStart.mBatchEnd); + } + + public void MoveLine(VertDir dir) + { + int lineNum = CursorLineAndColumn.mLine; + GetLinePosition(lineNum, var lineStart, var lineEnd); + mSelection = .(lineStart, Math.Min(lineEnd + 1, mData.mTextLength)); + MoveSelection(lineNum + (int)dir); + } + + public void MoveStatement(VertDir dir) + { + int lineNum = CursorLineAndColumn.mLine; + GetLinePosition(lineNum, var lineStart, var lineEnd); + + int selStart = -1; + int selEnd = -1; + int parenDepth = 0; + + int checkPos = lineStart; + for ( ; checkPos < mData.mTextLength - 1; checkPos++) + { + char8 checkC = mData.mText[checkPos].mChar; + + if (selStart == -1) + { + if (checkC == '\n') + break; // Don't support moving empty lines + if (!checkC.IsWhiteSpace) + { + selStart = checkPos; + } + } + + if (mData.mText[checkPos].mDisplayTypeId != 0) + continue; + + if ((checkC == ';') && (parenDepth == 0)) + { + selEnd = checkPos + 1; + break; + } + + if (checkC == '{') + { + parenDepth++; + } + + if (checkC == '}') + { + parenDepth--; + if (parenDepth <= 0) + { + selEnd = checkPos + 1; + break; + } + } + } + + if (selEnd == -1) + return; + + for ( ; checkPos < mData.mTextLength - 1; checkPos++) + { + char8 checkC = mData.mText[checkPos].mChar; + selEnd = checkPos + 1; + if (checkC == '\n') + break; + } + + UndoBatchStart undoBatchStart = new UndoBatchStart("moveLine"); + mData.mUndoManager.Add(undoBatchStart); + + mData.mUndoManager.Add(new SetCursorAction(this)); + + mSelection = .(lineStart, selEnd); + + int toLine = lineNum + (int)dir; + if (dir == .Down) + { + GetLineCharAtIdx(selEnd, var line, var lineChar); + toLine = line; + } + MoveSelection(toLine); + + mData.mUndoManager.Add(undoBatchStart.mBatchEnd); + } + public override void ContentChanged() { base.ContentChanged(); diff --git a/IDE/src/ui/TargetedPropertiesDialog.bf b/IDE/src/ui/TargetedPropertiesDialog.bf index 26c4ac2b..d3d22c72 100644 --- a/IDE/src/ui/TargetedPropertiesDialog.bf +++ b/IDE/src/ui/TargetedPropertiesDialog.bf @@ -278,10 +278,27 @@ namespace IDE.ui protected DarkComboBox mConfigComboBox; protected DarkComboBox mPlatformComboBox; + public struct ConfigPlatformPair : IHashable + { + public String mConfig; + public String mPlatform; + + public this(String config, String platform) + { + mConfig = config; + mPlatform = platform; + } + + public int GetHashCode() + { + return mConfig.GetHashCode() ^ mPlatform.GetHashCode(); + } + } + protected class ConfigDataGroup { public bool mIsMultiTargeted; - public Tuple mTarget; + public ConfigPlatformPair mTarget; public PropPage[] mPropPages ~ delete _; public this(int categoryCount) @@ -296,7 +313,7 @@ namespace IDE.ui } } - protected Dictionary, ConfigDataGroup> mTargetedConfigDatas = new Dictionary, ConfigDataGroup>() ~ delete _; + protected Dictionary mTargetedConfigDatas = new .() ~ delete _; protected ConfigDataGroup mMultiTargetConfigData; protected List mConfigDatas = new List() ~ DeleteContainerAndItems!(_); @@ -328,8 +345,8 @@ namespace IDE.ui { for (var kv in mTargetedConfigDatas) { - delete kv.key.Item1; - delete kv.key.Item2; + delete kv.key.mConfig; + delete kv.key.mPlatform; delete kv.value; mConfigDatas.Remove(kv.value); } @@ -659,8 +676,8 @@ namespace IDE.ui if (mTargetedConfigDatas.GetAndRemove(.(configName, platformName)) case .Ok((var key, var configDataGroup))) { - delete key.Item1; - delete key.Item2; + delete key.mConfig; + delete key.mPlatform; if (configDataGroup.mPropPages[categoryType] == mPropPage) RemovePropPage(); mConfigDatas.Remove(configDataGroup); @@ -779,8 +796,8 @@ namespace IDE.ui { var targetedConfigName = targetedConfigData.mTarget; - bool isCurrent = (targetedConfigName.Item1 == null) || - ((mConfigNames.Contains(targetedConfigName.Item1)) && (mPlatformNames.Contains(targetedConfigName.Item2))); + bool isCurrent = (targetedConfigName.mConfig == null) || + ((mConfigNames.Contains(targetedConfigName.mConfig)) && (mPlatformNames.Contains(targetedConfigName.mPlatform))); for (int32 pageIdx = 0; pageIdx < targetedConfigData.mPropPages.Count; pageIdx++) { diff --git a/IDE/src/ui/WorkspaceProperties.bf b/IDE/src/ui/WorkspaceProperties.bf index 68a333b6..63f96cc1 100644 --- a/IDE/src/ui/WorkspaceProperties.bf +++ b/IDE/src/ui/WorkspaceProperties.bf @@ -401,12 +401,12 @@ namespace IDE.ui if ((GetCategoryTargetedKind(categoryTypeInt) != .None) && ((mConfigNames.Count == 1) && (mPlatformNames.Count == 1))) { - var key = Tuple(configName, platformName); + var key = ConfigPlatformPair(configName, platformName); mTargetedConfigDatas.TryGetValue(key, out targetedConfigData); if (targetedConfigData == null) { - key.Item1 = new String(key.Item1); - key.Item2 = new String(key.Item2); + key.mConfig = new String(key.mConfig); + key.mPlatform = new String(key.mPlatform); targetedConfigData = new ConfigDataGroup((int32)CategoryType.COUNT); targetedConfigData.mTarget = key; mConfigDatas.Add(targetedConfigData); @@ -873,7 +873,7 @@ namespace IDE.ui workspaceProperties.HadExternalChanges(configName, platformName); } else - workspaceProperties.HadExternalChanges(targetedConfigData.mTarget.Item1, targetedConfigData.mTarget.Item2); + workspaceProperties.HadExternalChanges(targetedConfigData.mTarget.mConfig, targetedConfigData.mTarget.mPlatform); } else workspaceProperties.HadExternalChanges(null, null);