diff --git a/BeefLibs/Beefy2D/src/widgets/EditWidget.bf b/BeefLibs/Beefy2D/src/widgets/EditWidget.bf index f4c51ad1..870fc023 100644 --- a/BeefLibs/Beefy2D/src/widgets/EditWidget.bf +++ b/BeefLibs/Beefy2D/src/widgets/EditWidget.bf @@ -1548,7 +1548,19 @@ namespace Beefy.widgets int lineChar; GetLineCharAtIdx(textPos, out line, out lineChar); - bool wasLineEnd = mData.mText[textPos].mChar == '\n'; + bool wasLineEnd = false; + for (int i = textPos; i < mData.mTextLength; i++) + { + char8 c = mData.mText[i].mChar; + if (c == '\n') + { + wasLineEnd = true; + break; + } + else if (!c.IsWhiteSpace) + break; + } + String lineText = scope String(); GetLineText(line, lineText); diff --git a/IDE/Tests/IndentTest/BeefProj.toml b/IDE/Tests/IndentTest/BeefProj.toml new file mode 100644 index 00000000..92dd597d --- /dev/null +++ b/IDE/Tests/IndentTest/BeefProj.toml @@ -0,0 +1,9 @@ +FileVersion = 1 + +[Project] +Name = "IDETest" +TargetType = "BeefWindowsApplication" +StartupObject = "IDETest.Program" + +[Configs.Debug.Win64] +BeefLibType = "DynamicDebug" diff --git a/IDE/Tests/IndentTest/BeefSpace.toml b/IDE/Tests/IndentTest/BeefSpace.toml new file mode 100644 index 00000000..adcbe4f3 --- /dev/null +++ b/IDE/Tests/IndentTest/BeefSpace.toml @@ -0,0 +1,12 @@ +FileVersion = 1 +Projects = {IDETest = {Path = "."}} +Unlocked = ["corlib"] + +[Workspace] +StartupProject = "IDETest" + +[Configs.Debug.Win64] +IntermediateType = "ObjectAndIRCode" + +[Configs.Debug.Win32] +IntermediateType = "ObjectAndIRCode" diff --git a/IDE/Tests/IndentTest/scripts/Indent01.txt b/IDE/Tests/IndentTest/scripts/Indent01.txt new file mode 100644 index 00000000..6792350f --- /dev/null +++ b/IDE/Tests/IndentTest/scripts/Indent01.txt @@ -0,0 +1,46 @@ +ShowFile("src/Indent01.bf") + +GotoText("//Test01") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 17) + +GotoText("//Test02") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 21) + +GotoText("//Test03") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 21) + +GotoText("//Test04") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 25) + +GotoText("//Test05") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 25) + +GotoText("//Test06") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 21) + +GotoText("//Test07") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 25) + +GotoText("//Test08") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 21) + +GotoText("//Test09") +AdjustCursor(0, -1) +CursorToLineEnd() +AssertIsAtColumn("Indent01.bf", 17) diff --git a/IDE/Tests/IndentTest/src/Indent01.bf b/IDE/Tests/IndentTest/src/Indent01.bf new file mode 100644 index 00000000..c0d6bd81 --- /dev/null +++ b/IDE/Tests/IndentTest/src/Indent01.bf @@ -0,0 +1,100 @@ +namespace IDETest +{ + class Indent01 + { + static void Test01() + { + if (true) + + //Test01 + } + + static void Test02() + { + if (true) + if (true) + + //Test02 + } + + static void Test03() + { + if (true) + if (true) + ; + else + + //Test03 + } + + static void Test04() + { + if (true) + if (true) + for () + + //Test04 + } + + static void Test05() + { + if (true) + if (true) + ; + else + for () + + //Test05 + } + + static void Test06() + { + if (true) + if (true) + ; + else if (true) + + //Test06 + } + + static void Test07() + { + if (true) + if (true) + ; + else if (true) + for (int i < 10) + + //Test07 + } + + static void Test08() + { + if (true) + if (true) + ; + else if (true) + for (int i < 10) + ; + else + + //Test08 + } + + static void Test09() + { + if (true) + if (true) + ; + else if (true) + for (int i < 10) + ; + else + ; + + for (int i < 20) + + //Test09 + } + } +} diff --git a/IDE/src/ui/SourceEditWidgetContent.bf b/IDE/src/ui/SourceEditWidgetContent.bf index 2ce96bdc..01e077bc 100644 --- a/IDE/src/ui/SourceEditWidgetContent.bf +++ b/IDE/src/ui/SourceEditWidgetContent.bf @@ -828,6 +828,7 @@ namespace IDE.ui int ifDepth = 0; bool isDoingCtl = false; bool justFinishedCtlCond = false; + bool justFinishedCtl = false; int ctlDepth = 0; // for, while, using int extraTab = 1; @@ -906,20 +907,25 @@ namespace IDE.ui { if (blockDepth == 0) { + if (((justFinishedIf) || (justFinishedCtl)) && (keywordStr != "else")) + { + // This catches the case of: + // if (a) Thing(); + // if (b) ... + ifDepth = 0; + justFinishedIf = false; + ifCtlDepthStack.Clear(); + ctlDepth = 0; + + isDoingCtl = false; + } + + justFinishedCtl = false; + justFinishedIf = false; + switch (keywordStr) { case "if": - if (justFinishedIf) - { - // This catches the case of: - // if (a) Thing(); - // if (b) ... - ifDepth = 0; - justFinishedIf = false; - ifCtlDepthStack.Clear(); - ctlDepth = 0; - } - if (isDoingCtl) { ctlDepth++; @@ -947,7 +953,11 @@ namespace IDE.ui caseStartPos = checkIdx - 4; inCaseExpr = true; } + case "switch": + ifDepth = 0; } + + doingElse = keywordStr == "else"; } keywordStr.Clear(); @@ -1052,6 +1062,9 @@ namespace IDE.ui startedWithType = false; checkedStartedWithType = false; + if (isDoingCtl) + justFinishedCtl = true; + if (ifDepth == 0) { isDoingCtl = false; @@ -1075,7 +1088,6 @@ namespace IDE.ui doingElse = false; } - if (justFinishedIf) { // We had a non-else statement @@ -1092,7 +1104,6 @@ namespace IDE.ui if (ifDepth > 0) justFinishedIf = true; - //NEW inCaseExpr = false; inCaseExprNext = false; inCaseExprNextLine = false; @@ -1147,6 +1158,7 @@ namespace IDE.ui } + if ((ctlDepth > 0) && ((!justFinishedIf) || (insertingElseStmt))) extraTab += ctlDepth; diff --git a/bin/test_ide.bat b/bin/test_ide.bat index 15168669..8ea5b0a5 100644 --- a/bin/test_ide.bat +++ b/bin/test_ide.bat @@ -31,6 +31,14 @@ PUSHD %~dp0..\ @CALL :TEST @IF !ERRORLEVEL! NEQ 0 GOTO HADERROR +@SET TESTPATH=IDE\Tests\BugW002 +@CALL :TEST +@IF !ERRORLEVEL! NEQ 0 GOTO HADERROR + +@SET TESTPATH=IDE\Tests\IndentTest +@CALL :TEST +@IF !ERRORLEVEL! NEQ 0 GOTO HADERROR + @GOTO :EMPTYTEST :TEST