I would like to add a hotkey to the SyntaxEditor for toggling comment/uncomment of highlighted text. For example, VS Code uses Ctrl+“/” and I would like to do the same.
Thanks,
Rob
I would like to add a hotkey to the SyntaxEditor for toggling comment/uncomment of highlighted text. For example, VS Code uses Ctrl+“/” and I would like to do the same.
Thanks,
Rob
Hi Rob,
You should use KeyList.Add functionality to customize shortcut keys like this:
syntaxEdit1.KeyList.Add(Keys.Oem2 | Keys.Control, new Alternet.Editor.KeyEvent(ToggleComment), 0, 0);
protected bool CanUncommentSelection(string start, string end)
{
var selRect = syntaxEdit1.Selection.SelectionRect;
string s = syntaxEdit1.Lines[selRect.Top].TrimStart();
if (!s.StartsWith(start))
return false;
s = syntaxEdit1.Lines[selRect.Right == 0 ? selRect.Bottom - 1 : selRect.Bottom].TrimEnd();
return s.EndsWith(end);
}
void ToggleComment()
{
bool comment = true;
var parser = syntaxEdit1.Lexer as ISyntaxParser;
if (parser != null)
{
string start;
string end;
if (parser.GetMultiLineComment(syntaxEdit1.Position, out start, out end))
{
if (CanUncommentSelection(start, end))
{
comment = false;
}
}
else
{
var commentStr = parser.GetSingleLineComment();
var pos = syntaxEdit1.Selection.IsEmpty ? syntaxEdit1.Position.Y : syntaxEdit1.Selection.SelectionRect.Top;
if (!string.IsNullOrEmpty(commentStr))
{
var str = syntaxEdit1.Lines[pos].Trim();
if (str.StartsWith(commentStr))
comment = false;
}
}
}
if (comment)
syntaxEdit1.Selection.CommentSelection();
else
syntaxEdit1.Selection.UncommentSelection();
}
Regards,
Andrew
Thanks Andrew! I will give that a try.
Thanks, it works perfectly.