diff --git a/IDE/src/ui/SourceEditWidgetContent.bf b/IDE/src/ui/SourceEditWidgetContent.bf index dbb19ef7..d632f9a7 100644 --- a/IDE/src/ui/SourceEditWidgetContent.bf +++ b/IDE/src/ui/SourceEditWidgetContent.bf @@ -2238,6 +2238,147 @@ namespace IDE.ui return false; } + public bool ToggleCommentAlt(bool? doComment = null) + { + if (CheckReadOnly()) + return false; + bool noStar = false; + if ((!HasSelection()) && (doComment != null)) + { + CursorToLineEnd(); + int cursorEndPos = CursorTextPos; + CursorToLineStart(false); + mSelection = .(CursorTextPos, cursorEndPos); + noStar = true; + } + + if ((HasSelection()) && (mSelection.Value.Length > 0)) + { + var startLineAndCol = CursorLineAndColumn; + + UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedToggleComment"); + mData.mUndoManager.Add(undoBatchStart); + + mData.mUndoManager.Add(new SetCursorAction(this)); + + int minPos = mSelection.GetValueOrDefault().MinPos; + int maxPos = mSelection.GetValueOrDefault().MaxPos; + mSelection = null; + + var str = scope String(); + ExtractString(minPos, maxPos - minPos, str); + var trimmedStr = scope String(); + trimmedStr.Append(str); + int32 startLen = (int32)trimmedStr.Length; + trimmedStr.TrimStart(); + int32 afterTrimStart = (int32)trimmedStr.Length; + trimmedStr.TrimEnd(); + int32 afterTrimEnd = (int32)trimmedStr.Length; + trimmedStr.Append('\n'); + + int firstCharPos = minPos + (startLen - afterTrimStart); + int lastCharPos = maxPos - (afterTrimStart - afterTrimEnd); + if ((doComment != true) && (trimmedStr.Contains("//"))) + { + for (int i = firstCharPos; i < lastCharPos - 1; i++) + { + if (minPos == 0 || (minPos>0 && SafeGetChar(i - 1) == '\n' || SafeGetChar(i - 1) == '\t')) + if (SafeGetChar(i - 0) == '/' && SafeGetChar(i + 1) == '/') + { + mSelection = EditSelection(i - 0, i + 2); + DeleteSelection(); + lastCharPos -= 2; + while (i < maxPos && SafeGetChar(i) != '\n') + { + i++; + } + } + } + mSelection = EditSelection(minPos, lastCharPos); + } + int q = 0; + var nc = trimmedStr.Count('\n'); + if ((doComment != true) && (trimmedStr.StartsWith("/*"))) + { + if (trimmedStr.EndsWith("*/\n")) + { + mSelection = EditSelection(firstCharPos, firstCharPos + 2); + DeleteChar(); + mSelection = EditSelection(lastCharPos - 4, lastCharPos - 2); + DeleteChar(); + + if (doComment != null) + mSelection = EditSelection(firstCharPos, lastCharPos - 4); + } + } + else if (doComment != false && nc<=1 && minPos >=0 && (SafeGetChar(minPos-1) != ' ' && SafeGetChar(minPos-1) != '\t') && SafeGetChar(minPos-1) != '\n') + { //if selection is from beginning of the line then we want to use // comment, that's why the check for line count and ' ' and tab + CursorTextPos = firstCharPos; + if (noStar) { + CursorTextPos = minPos; + + InsertAtCursor("//"); //goes here if no selection + } + else + { + InsertAtCursor("/*"); + CursorTextPos = lastCharPos + 2; + InsertAtCursor("*/"); + } + if (!noStar && doComment != null) + mSelection = EditSelection(firstCharPos, lastCharPos + 4); + } + else if (doComment != false && nc>=1 && minPos >0 && (SafeGetChar(maxPos-1) != ' ' && SafeGetChar(maxPos-1) != '\t') && SafeGetChar(maxPos-1) != '\n') + { + CursorTextPos = firstCharPos; + if (noStar) { + CursorTextPos = minPos; + + InsertAtCursor("//"); //goes here if no selection + } + else + { + InsertAtCursor("/*"); + CursorTextPos = lastCharPos + 2; + InsertAtCursor("*/"); + } + if (!noStar && doComment != null) + mSelection = EditSelection(firstCharPos, lastCharPos + 4); + } + else if (doComment != false) + { + while (firstCharPos >= 0 && SafeGetChar(firstCharPos) != '\n') + { + firstCharPos--; + } + + for (int i = firstCharPos + 1; i < maxPos + q; i++) + { + CursorTextPos = i; // needed to add i < maxPos + q; for this to work with InsertAtCursor + InsertAtCursor("//"); q++; q++; + + while (SafeGetChar(i) != '\n' && i < maxPos + q) + { + i++; + } + } + mSelection = EditSelection(minPos, maxPos + q); + } + + if (undoBatchStart != null) + mData.mUndoManager.Add(undoBatchStart.mBatchEnd); + + CursorLineAndColumn = startLineAndCol; + + if (doComment == null) + mSelection = null; + + return true; + } + + return false; + } + public void DeleteAllRight() { int startPos;