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())
|
if (CheckReadOnly())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
var startLineAndCol = CursorLineAndColumn;
|
||||||
|
int startTextPos = CursorTextPos;
|
||||||
|
var prevSelection = mSelection;
|
||||||
|
bool hadSelection = HasSelection();
|
||||||
|
|
||||||
if ((!HasSelection()) && (doComment != null))
|
if ((!HasSelection()) && (doComment != null))
|
||||||
{
|
{
|
||||||
CursorToLineEnd();
|
CursorToLineEnd();
|
||||||
|
@ -2177,12 +2182,13 @@ namespace IDE.ui
|
||||||
|
|
||||||
if ((HasSelection()) && (mSelection.Value.Length > 1))
|
if ((HasSelection()) && (mSelection.Value.Length > 1))
|
||||||
{
|
{
|
||||||
var startLineAndCol = CursorLineAndColumn;
|
|
||||||
|
|
||||||
UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedCommentBlock");
|
UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedCommentBlock");
|
||||||
mData.mUndoManager.Add(undoBatchStart);
|
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 minPos = mSelection.GetValueOrDefault().MinPos;
|
||||||
int maxPos = mSelection.GetValueOrDefault().MaxPos;
|
int maxPos = mSelection.GetValueOrDefault().MaxPos;
|
||||||
|
@ -2216,9 +2222,12 @@ namespace IDE.ui
|
||||||
if (undoBatchStart != null)
|
if (undoBatchStart != null)
|
||||||
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
||||||
|
|
||||||
CursorLineAndColumn = startLineAndCol;
|
if (startTextPos <= minPos)
|
||||||
|
CursorLineAndColumn = startLineAndCol;
|
||||||
|
else if (startTextPos < maxPos)
|
||||||
|
CursorTextPos = startTextPos + 2;
|
||||||
|
|
||||||
if (doComment == null)
|
if ((doComment == null) || (!hadSelection))
|
||||||
mSelection = null;
|
mSelection = null;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -2229,120 +2238,142 @@ namespace IDE.ui
|
||||||
|
|
||||||
public bool CommentLines()
|
public bool CommentLines()
|
||||||
{
|
{
|
||||||
bool? doComment = true;
|
|
||||||
if (CheckReadOnly())
|
if (CheckReadOnly())
|
||||||
return false;
|
return false;
|
||||||
bool noStar = false;
|
|
||||||
|
|
||||||
|
int startTextPos = CursorTextPos;
|
||||||
|
var prevSelection = mSelection;
|
||||||
|
bool hadSelection = HasSelection();
|
||||||
var startLineAndCol = CursorLineAndColumn;
|
var startLineAndCol = CursorLineAndColumn;
|
||||||
if ((!HasSelection()) && (doComment != null))
|
if (!HasSelection())
|
||||||
{
|
{
|
||||||
CursorToLineEnd();
|
CursorToLineEnd();
|
||||||
int cursorEndPos = CursorTextPos;
|
int cursorEndPos = CursorTextPos;
|
||||||
|
CursorToLineStart(false);
|
||||||
mSelection = .(CursorTextPos, cursorEndPos);
|
mSelection = .(CursorTextPos, cursorEndPos);
|
||||||
noStar = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true || (HasSelection()) && (mSelection.Value.Length > 0))
|
UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedCommentLines");
|
||||||
|
mData.mUndoManager.Add(undoBatchStart);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
while (minPos >= 0)
|
||||||
{
|
{
|
||||||
// set selection to begin from line start
|
var c = mData.mText[minPos - 1].mChar;
|
||||||
int lineIdx;
|
if (c == '\n')
|
||||||
int lineChar;
|
break;
|
||||||
GetLineCharAtIdx(mSelection.GetValueOrDefault().MinPos,out lineIdx, out lineChar);
|
minPos--;
|
||||||
MoveCursorTo(lineIdx, 0);
|
}
|
||||||
mSelection = .(CursorTextPos, mSelection.GetValueOrDefault().MaxPos);
|
|
||||||
|
int wantLineCol = -1;
|
||||||
|
int lineStartCol = 0;
|
||||||
|
bool didLineComment = false;
|
||||||
|
|
||||||
UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedCommentLines");
|
for (int i = minPos; i < maxPos; i++)
|
||||||
mData.mUndoManager.Add(undoBatchStart);
|
{
|
||||||
|
bool isSpace = false;
|
||||||
mData.mUndoManager.Add(new SetCursorAction(this));
|
var c = mData.mText[i].mChar;
|
||||||
|
if (didLineComment)
|
||||||
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 (firstCharPos >= 0 && SafeGetChar(firstCharPos) != '\n')
|
if (c == '\n')
|
||||||
{
|
{
|
||||||
firstCharPos--;
|
didLineComment = false;
|
||||||
|
lineStartCol = 0;
|
||||||
}
|
}
|
||||||
bool blank=true;
|
continue;
|
||||||
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)
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mSelection = EditSelection(minPos, maxPos + q);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (undoBatchStart != null)
|
bool commentNow = false;
|
||||||
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
if ((wantLineCol != -1) && (lineStartCol >= wantLineCol))
|
||||||
|
commentNow = true;
|
||||||
|
|
||||||
CursorLineAndColumn = startLineAndCol;
|
if (c == '\t')
|
||||||
|
{
|
||||||
|
lineStartCol += 4;
|
||||||
|
isSpace = true;
|
||||||
|
}
|
||||||
|
else if (c == ' ')
|
||||||
|
{
|
||||||
|
lineStartCol++;
|
||||||
|
isSpace = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (doComment == null)
|
if (!isSpace)
|
||||||
mSelection = null;
|
{
|
||||||
|
if (c == '\n')
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
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);
|
||||||
|
|
||||||
//return false;
|
if (undoBatchStart != null)
|
||||||
|
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
||||||
|
|
||||||
|
CursorLineAndColumn = startLineAndCol;
|
||||||
|
|
||||||
|
if (!hadSelection)
|
||||||
|
mSelection = null;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ToggleComment(bool? doComment = null)
|
public bool ToggleComment(bool? doComment = null)
|
||||||
{
|
{
|
||||||
if (CheckReadOnly())
|
if (CheckReadOnly())
|
||||||
return false;
|
return false;
|
||||||
bool noStar = false;
|
|
||||||
|
int startTextPos = CursorTextPos;
|
||||||
|
bool doLineComment = false;
|
||||||
var prevSelection = mSelection;
|
var prevSelection = mSelection;
|
||||||
|
|
||||||
var startLineAndCol = CursorLineAndColumn;
|
LineAndColumn? startLineAndCol = CursorLineAndColumn;
|
||||||
if ((!HasSelection()) && (doComment != null))
|
if (!HasSelection())
|
||||||
{
|
{
|
||||||
CursorToLineEnd();
|
CursorToLineEnd();
|
||||||
int cursorEndPos = CursorTextPos;
|
int cursorEndPos = CursorTextPos;
|
||||||
CursorToLineStart(false);
|
CursorToLineStart(false);
|
||||||
mSelection = .(CursorTextPos, cursorEndPos);
|
mSelection = .(CursorTextPos, cursorEndPos);
|
||||||
noStar = true;
|
doLineComment = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((HasSelection()) && (mSelection.Value.Length > 0))
|
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");
|
UndoBatchStart undoBatchStart = new UndoBatchStart("embeddedToggleComment");
|
||||||
mData.mUndoManager.Add(undoBatchStart);
|
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 minPos = mSelection.GetValueOrDefault().MinPos;
|
||||||
int maxPos = mSelection.GetValueOrDefault().MaxPos;
|
int maxPos = mSelection.GetValueOrDefault().MaxPos;
|
||||||
|
@ -2365,12 +2396,12 @@ namespace IDE.ui
|
||||||
int q = 0;
|
int q = 0;
|
||||||
var nc = trimmedStr.Count('\n');
|
var nc = trimmedStr.Count('\n');
|
||||||
|
|
||||||
if(afterTrimEnd == 0)
|
if (afterTrimEnd == 0)
|
||||||
{
|
{
|
||||||
if (undoBatchStart != null)
|
if (undoBatchStart != null)
|
||||||
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
||||||
|
|
||||||
CursorLineAndColumn = startLineAndCol;
|
CursorLineAndColumn = startLineAndCol.Value;
|
||||||
|
|
||||||
if (doComment == null)
|
if (doComment == null)
|
||||||
mSelection = null;
|
mSelection = null;
|
||||||
|
@ -2397,7 +2428,6 @@ namespace IDE.ui
|
||||||
CursorToLineEnd();
|
CursorToLineEnd();
|
||||||
int cursorEndPos = CursorTextPos;
|
int cursorEndPos = CursorTextPos;
|
||||||
mSelection = .(minPos, cursorEndPos);
|
mSelection = .(minPos, cursorEndPos);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if ((doComment != true) && (trimmedStr.StartsWith("/*")))
|
else if ((doComment != true) && (trimmedStr.StartsWith("/*")))
|
||||||
{
|
{
|
||||||
|
@ -2408,7 +2438,7 @@ namespace IDE.ui
|
||||||
mSelection = EditSelection(lastCharPos - 4, lastCharPos - 2);
|
mSelection = EditSelection(lastCharPos - 4, lastCharPos - 2);
|
||||||
DeleteChar();
|
DeleteChar();
|
||||||
|
|
||||||
if (doComment != null)
|
if (prevSelection != null)
|
||||||
mSelection = EditSelection(firstCharPos, lastCharPos - 4);
|
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'))
|
|| 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
|
{ //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;
|
CursorTextPos = firstCharPos;
|
||||||
if (noStar) {
|
if (doLineComment)
|
||||||
|
{
|
||||||
CursorTextPos = minPos;
|
CursorTextPos = minPos;
|
||||||
|
|
||||||
InsertAtCursor("//"); //goes here if no selection
|
InsertAtCursor("//"); //goes here if no selection
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -2427,17 +2457,17 @@ namespace IDE.ui
|
||||||
CursorTextPos = lastCharPos + 2;
|
CursorTextPos = lastCharPos + 2;
|
||||||
InsertAtCursor("*/");
|
InsertAtCursor("*/");
|
||||||
}
|
}
|
||||||
if (!noStar && doComment != null)
|
|
||||||
mSelection = EditSelection(firstCharPos, lastCharPos + 4);
|
mSelection = EditSelection(firstCharPos, lastCharPos + 4);
|
||||||
|
if (startTextPos <= minPos)
|
||||||
|
CursorLineAndColumn = startLineAndCol.Value;
|
||||||
|
else
|
||||||
|
CursorTextPos = startTextPos + 2;
|
||||||
|
startLineAndCol = null;
|
||||||
}
|
}
|
||||||
else if (doComment != false)
|
else if (doComment != false)
|
||||||
{
|
{
|
||||||
while (firstCharPos >= 0 && SafeGetChar(firstCharPos) != '\n')
|
for (int i = firstCharPos; i < maxPos + q; i++)
|
||||||
{
|
|
||||||
firstCharPos--;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = firstCharPos + 1; i < maxPos + q; i++)
|
|
||||||
{
|
{
|
||||||
CursorTextPos = i; // needed to add i < maxPos + q; for this to work with InsertAtCursor
|
CursorTextPos = i; // needed to add i < maxPos + q; for this to work with InsertAtCursor
|
||||||
InsertAtCursor("//"); q++; q++;
|
InsertAtCursor("//"); q++; q++;
|
||||||
|
@ -2457,9 +2487,10 @@ namespace IDE.ui
|
||||||
if (undoBatchStart != null)
|
if (undoBatchStart != null)
|
||||||
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
mData.mUndoManager.Add(undoBatchStart.mBatchEnd);
|
||||||
|
|
||||||
CursorLineAndColumn = startLineAndCol;
|
if (startLineAndCol != null)
|
||||||
|
CursorLineAndColumn = startLineAndCol.Value;
|
||||||
|
|
||||||
if (doComment == null)
|
if (prevSelection == null)
|
||||||
mSelection = null;
|
mSelection = null;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue