mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-23 10:08:00 +02:00
Comment tweaks
`//` is placed at the column where text starts on the first line Toggle comment can insert a line comment with no selection Selection state is retained Undo restores selection and cursor position
This commit is contained in:
parent
74a63f7561
commit
4c421033ab
1 changed files with 124 additions and 93 deletions
|
@ -2167,6 +2167,11 @@ namespace IDE.ui
|
|||
if (CheckReadOnly())
|
||||
return false;
|
||||
|
||||
var startLineAndCol = CursorLineAndColumn;
|
||||
int startTextPos = CursorTextPos;
|
||||
var prevSelection = mSelection;
|
||||
bool hadSelection = HasSelection();
|
||||
|
||||
if ((!HasSelection()) && (doComment != null))
|
||||
{
|
||||
CursorToLineEnd();
|
||||
|
@ -2177,12 +2182,13 @@ namespace IDE.ui
|
|||
|
||||
if ((HasSelection()) && (mSelection.Value.Length > 1))
|
||||
{
|
||||
var startLineAndCol = CursorLineAndColumn;
|
||||
|
||||
UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedCommentBlock");
|
||||
mData.mUndoManager.Add(undoBatchStart);
|
||||
|
||||
mData.mUndoManager.Add(new SetCursorAction(this));
|
||||
var setCursorAction = new SetCursorAction(this);
|
||||
setCursorAction.mSelection = prevSelection;
|
||||
setCursorAction.mCursorTextPos = (.)startTextPos;
|
||||
mData.mUndoManager.Add(setCursorAction);
|
||||
|
||||
int minPos = mSelection.GetValueOrDefault().MinPos;
|
||||
int maxPos = mSelection.GetValueOrDefault().MaxPos;
|
||||
|
@ -2216,9 +2222,12 @@ namespace IDE.ui
|
|||
if (undoBatchStart != null)
|
||||
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
||||
|
||||
if (startTextPos <= minPos)
|
||||
CursorLineAndColumn = startLineAndCol;
|
||||
else if (startTextPos < maxPos)
|
||||
CursorTextPos = startTextPos + 2;
|
||||
|
||||
if (doComment == null)
|
||||
if ((doComment == null) || (!hadSelection))
|
||||
mSelection = null;
|
||||
|
||||
return true;
|
||||
|
@ -2229,120 +2238,142 @@ namespace IDE.ui
|
|||
|
||||
public bool CommentLines()
|
||||
{
|
||||
bool? doComment = true;
|
||||
if (CheckReadOnly())
|
||||
return false;
|
||||
bool noStar = false;
|
||||
|
||||
int startTextPos = CursorTextPos;
|
||||
var prevSelection = mSelection;
|
||||
bool hadSelection = HasSelection();
|
||||
var startLineAndCol = CursorLineAndColumn;
|
||||
if ((!HasSelection()) && (doComment != null))
|
||||
if (!HasSelection())
|
||||
{
|
||||
CursorToLineEnd();
|
||||
int cursorEndPos = CursorTextPos;
|
||||
|
||||
CursorToLineStart(false);
|
||||
mSelection = .(CursorTextPos, cursorEndPos);
|
||||
noStar = true;
|
||||
}
|
||||
|
||||
if (true || (HasSelection()) && (mSelection.Value.Length > 0))
|
||||
{
|
||||
// set selection to begin from line start
|
||||
int lineIdx;
|
||||
int lineChar;
|
||||
GetLineCharAtIdx(mSelection.GetValueOrDefault().MinPos,out lineIdx, out lineChar);
|
||||
MoveCursorTo(lineIdx, 0);
|
||||
mSelection = .(CursorTextPos, mSelection.GetValueOrDefault().MaxPos);
|
||||
|
||||
UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedCommentLines");
|
||||
mData.mUndoManager.Add(undoBatchStart);
|
||||
|
||||
mData.mUndoManager.Add(new SetCursorAction(this));
|
||||
var setCursorAction = new SetCursorAction(this);
|
||||
setCursorAction.mSelection = prevSelection;
|
||||
setCursorAction.mCursorTextPos = (.)startTextPos;
|
||||
mData.mUndoManager.Add(setCursorAction);
|
||||
|
||||
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);
|
||||
|
||||
int q = 0;
|
||||
|
||||
if (doComment != false)
|
||||
while (minPos >= 0)
|
||||
{
|
||||
while (firstCharPos >= 0 && SafeGetChar(firstCharPos) != '\n')
|
||||
{
|
||||
firstCharPos--;
|
||||
var c = mData.mText[minPos - 1].mChar;
|
||||
if (c == '\n')
|
||||
break;
|
||||
minPos--;
|
||||
}
|
||||
bool blank=true;
|
||||
for (int i = firstCharPos + 1; i < maxPos + q; i++)
|
||||
{
|
||||
blank=false;
|
||||
CursorTextPos = i; // needed to add i < maxPos + q; for this to work with InsertAtCursor
|
||||
InsertAtCursor("//"); q++; q++;
|
||||
|
||||
while (SafeGetChar(i) != '\n' && i < maxPos + q)
|
||||
int wantLineCol = -1;
|
||||
int lineStartCol = 0;
|
||||
bool didLineComment = false;
|
||||
|
||||
for (int i = minPos; i < maxPos; i++)
|
||||
{
|
||||
i++;
|
||||
bool isSpace = false;
|
||||
var c = mData.mText[i].mChar;
|
||||
if (didLineComment)
|
||||
{
|
||||
if (c == '\n')
|
||||
{
|
||||
didLineComment = false;
|
||||
lineStartCol = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
bool commentNow = false;
|
||||
if ((wantLineCol != -1) && (lineStartCol >= wantLineCol))
|
||||
commentNow = true;
|
||||
|
||||
if (c == '\t')
|
||||
{
|
||||
lineStartCol += 4;
|
||||
isSpace = true;
|
||||
}
|
||||
else if (c == ' ')
|
||||
{
|
||||
lineStartCol++;
|
||||
isSpace = true;
|
||||
}
|
||||
|
||||
if (!isSpace)
|
||||
{
|
||||
if (c == '\n')
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
commentNow = true;
|
||||
if (wantLineCol == -1)
|
||||
wantLineCol = lineStartCol;
|
||||
}
|
||||
|
||||
if (commentNow)
|
||||
{
|
||||
CursorTextPos = i;
|
||||
String str = scope .();
|
||||
while (lineStartCol + 4 <= wantLineCol)
|
||||
{
|
||||
lineStartCol += 4;
|
||||
str.Append("\t");
|
||||
}
|
||||
str.Append("//");
|
||||
InsertAtCursor(str);
|
||||
didLineComment = true;
|
||||
maxPos += str.Length;
|
||||
}
|
||||
}
|
||||
mSelection = EditSelection(minPos, maxPos + q);
|
||||
}
|
||||
mSelection = EditSelection(minPos, maxPos);
|
||||
|
||||
if (undoBatchStart != null)
|
||||
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
||||
|
||||
CursorLineAndColumn = startLineAndCol;
|
||||
|
||||
if (doComment == null)
|
||||
if (!hadSelection)
|
||||
mSelection = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//return false;
|
||||
}
|
||||
|
||||
public bool ToggleComment(bool? doComment = null)
|
||||
{
|
||||
if (CheckReadOnly())
|
||||
return false;
|
||||
bool noStar = false;
|
||||
|
||||
int startTextPos = CursorTextPos;
|
||||
bool doLineComment = false;
|
||||
var prevSelection = mSelection;
|
||||
|
||||
var startLineAndCol = CursorLineAndColumn;
|
||||
if ((!HasSelection()) && (doComment != null))
|
||||
LineAndColumn? startLineAndCol = CursorLineAndColumn;
|
||||
if (!HasSelection())
|
||||
{
|
||||
CursorToLineEnd();
|
||||
int cursorEndPos = CursorTextPos;
|
||||
CursorToLineStart(false);
|
||||
mSelection = .(CursorTextPos, cursorEndPos);
|
||||
noStar = true;
|
||||
doLineComment = true;
|
||||
}
|
||||
|
||||
if ((HasSelection()) && (mSelection.Value.Length > 0))
|
||||
{
|
||||
int lineIdx;
|
||||
int lineChar;
|
||||
GetLineCharAtIdx(mSelection.GetValueOrDefault().MinPos, out lineIdx, out lineChar);
|
||||
MoveCursorTo(lineIdx, 0);
|
||||
mSelection = .(CursorTextPos, mSelection.GetValueOrDefault().MaxPos);
|
||||
|
||||
|
||||
UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedToggleComment");
|
||||
mData.mUndoManager.Add(undoBatchStart);
|
||||
|
||||
mData.mUndoManager.Add(new SetCursorAction(this));
|
||||
var setCursorAction = new SetCursorAction(this);
|
||||
setCursorAction.mSelection = prevSelection;
|
||||
setCursorAction.mCursorTextPos = (.)startTextPos;
|
||||
mData.mUndoManager.Add(setCursorAction);
|
||||
|
||||
int minPos = mSelection.GetValueOrDefault().MinPos;
|
||||
int maxPos = mSelection.GetValueOrDefault().MaxPos;
|
||||
|
@ -2365,12 +2396,12 @@ namespace IDE.ui
|
|||
int q = 0;
|
||||
var nc = trimmedStr.Count('\n');
|
||||
|
||||
if(afterTrimEnd == 0)
|
||||
if (afterTrimEnd == 0)
|
||||
{
|
||||
if (undoBatchStart != null)
|
||||
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
||||
|
||||
CursorLineAndColumn = startLineAndCol;
|
||||
CursorLineAndColumn = startLineAndCol.Value;
|
||||
|
||||
if (doComment == null)
|
||||
mSelection = null;
|
||||
|
@ -2397,7 +2428,6 @@ namespace IDE.ui
|
|||
CursorToLineEnd();
|
||||
int cursorEndPos = CursorTextPos;
|
||||
mSelection = .(minPos, cursorEndPos);
|
||||
|
||||
}
|
||||
else if ((doComment != true) && (trimmedStr.StartsWith("/*")))
|
||||
{
|
||||
|
@ -2408,7 +2438,7 @@ namespace IDE.ui
|
|||
mSelection = EditSelection(lastCharPos - 4, lastCharPos - 2);
|
||||
DeleteChar();
|
||||
|
||||
if (doComment != null)
|
||||
if (prevSelection != null)
|
||||
mSelection = EditSelection(firstCharPos, lastCharPos - 4);
|
||||
}
|
||||
}
|
||||
|
@ -2416,9 +2446,9 @@ namespace IDE.ui
|
|||
|| nc >= 1 && (SafeGetChar(maxPos-1) != ' ' && SafeGetChar(maxPos-1) != '\t') && SafeGetChar(maxPos-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) {
|
||||
if (doLineComment)
|
||||
{
|
||||
CursorTextPos = minPos;
|
||||
|
||||
InsertAtCursor("//"); //goes here if no selection
|
||||
}
|
||||
else
|
||||
|
@ -2427,17 +2457,17 @@ namespace IDE.ui
|
|||
CursorTextPos = lastCharPos + 2;
|
||||
InsertAtCursor("*/");
|
||||
}
|
||||
if (!noStar && doComment != null)
|
||||
|
||||
mSelection = EditSelection(firstCharPos, lastCharPos + 4);
|
||||
if (startTextPos <= minPos)
|
||||
CursorLineAndColumn = startLineAndCol.Value;
|
||||
else
|
||||
CursorTextPos = startTextPos + 2;
|
||||
startLineAndCol = null;
|
||||
}
|
||||
else if (doComment != false)
|
||||
{
|
||||
while (firstCharPos >= 0 && SafeGetChar(firstCharPos) != '\n')
|
||||
{
|
||||
firstCharPos--;
|
||||
}
|
||||
|
||||
for (int i = firstCharPos + 1; i < maxPos + q; i++)
|
||||
for (int i = firstCharPos; i < maxPos + q; i++)
|
||||
{
|
||||
CursorTextPos = i; // needed to add i < maxPos + q; for this to work with InsertAtCursor
|
||||
InsertAtCursor("//"); q++; q++;
|
||||
|
@ -2457,9 +2487,10 @@ namespace IDE.ui
|
|||
if (undoBatchStart != null)
|
||||
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
||||
|
||||
CursorLineAndColumn = startLineAndCol;
|
||||
if (startLineAndCol != null)
|
||||
CursorLineAndColumn = startLineAndCol.Value;
|
||||
|
||||
if (doComment == null)
|
||||
if (prevSelection == null)
|
||||
mSelection = null;
|
||||
|
||||
return true;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue