Skip to content

Commit

Permalink
Added shortcut to toggle lines comment (Ctrl+/).
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnlaan committed Jun 26, 2024
1 parent 5770959 commit 6157099
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ISHelp/isetup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3137,6 +3137,9 @@ Filename: "{win}\MYPROG.INI"; Section: "InstallSettings"; Key: "InstallPath"; St
<tr>
<td>Unindent lines.</td><td>-</td><td>Ctrl+[</td>
</tr>
<tr>
<td>Toggle lines comment.</td><td>Ctrl+/</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
Expand Down
78 changes: 76 additions & 2 deletions Projects/Src/CompForm.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
unit CompForm;
unit CompForm;

{
Inno Setup
Expand Down Expand Up @@ -1111,6 +1111,78 @@ procedure TCompileForm.FormKeyDown(Sender: TObject; var Key: Word;

procedure TCompileForm.MemoKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);

procedure ToggleLinesComment(const AMemo: TCompScintEdit);
begin
{ Based on SciTE 5.50's SciTEBase::StartBlockComment }

var Selection := AMemo.Selection;
var CaretPosition := AMemo.CaretPosition;
// checking if caret is located in _beginning_ of selected block
var MoveCaret := CaretPosition < Selection.EndPos;
var SelStartLine := AMemo.GetLineFromPosition(Selection.StartPos);
var SelEndLine := AMemo.GetLineFromPosition(Selection.EndPos);
var Lines := SelEndLine - SelStartLine;
var FirstSelLineStart := AMemo.GetPositionFromLine(SelStartLine);
// "caret return" is part of the last selected line
if (Lines > 0) and (Selection.EndPos = AMemo.GetPositionFromLine(SelEndLine)) then
Dec(SelEndLine);
{ We rely on the styler to identify [Code] section lines, but we
may be searching into areas that haven't been styled yet }
AMemo.StyleNeeded(Selection.EndPos);
AMemo.BeginUndoAction;
var LastLongCommentLength := 0;
for var I := SelStartLine to SelEndLine do begin
var LineIndent := AMemo.GetLineIndentPosition(I);
var LineEnd := AMemo.GetLineEndPosition(I);
var LineBuf := AMemo.GetTextRange(LineIndent, LineEnd);
// empty lines are not commented
if LineBuf = '' then
Continue;
var Comment: String;
if LineBuf.StartsWith('//') or
(FMemosStyler.GetSectionFromLineState(AMemo.Lines.State[I]) = scCode) then
Comment := '//'
else
Comment := ';';
var LongComment := Comment + ' ';
LastLongCommentLength := Length(LongComment);
var RawLongComment := AMemo.ConvertStringToRawString(LongComment);
if LineBuf.StartsWith(Comment) then begin
var CommentLength := Length(Comment);
if LineBuf.StartsWith(LongComment) then begin
// Removing comment with space after it.
CommentLength := Length(LongComment);
end;
AMemo.Selection := TScintRange.Create(LineIndent, LineIndent + CommentLength);
AMemo.SelText := '';
if I = SelStartLine then // is this the first selected line?
Dec(Selection.StartPos, CommentLength);
Dec(Selection.EndPos, CommentLength); // every iteration
Continue;
end;
if I = SelStartLine then // is this the first selected line?
Inc(Selection.StartPos, Length(LongComment));
Inc(Selection.EndPos, Length(LongComment)); // every iteration
AMemo.CallStr(SCI_INSERTTEXT, LineIndent, RawLongComment);
end;
// after uncommenting selection may promote itself to the lines
// before the first initially selected line;
// another problem - if only comment symbol was selected;
if Selection.StartPos < FirstSelLineStart then begin
if Selection.StartPos >= Selection.EndPos - (LastLongCommentLength - 1) then
Selection.EndPos := FirstSelLineStart;
Selection.StartPos := FirstSelLineStart;
end;
if MoveCaret then begin
// moving caret to the beginning of selected block
AMemo.CaretPosition := Selection.EndPos;
AMemo.Call(SCI_SETCURRENTPOS, Selection.StartPos, 0);
end else
AMemo.Selection := Selection;
AMemo.EndUndoAction;
end;

begin
if (Key in [VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, VK_HOME, VK_END]) then begin
var Memo := Sender as TCompScintEdit;
Expand Down Expand Up @@ -1167,6 +1239,8 @@ procedure TCompileForm.MemoKeyDown(Sender: TObject; var Key: Word;
FActiveMemo.SetEmptySelection;
FActiveMemo.ScrollCaretIntoView;
end;
ccToggleLinesComment:
ToggleLinesComment(FActiveMemo);
else if ComplexCommand <> ccNone then
raise Exception.Create('Unknown ComplexCommand');
end;
Expand Down Expand Up @@ -2306,7 +2380,7 @@ procedure TCompileForm.FPrintClick(Sender: TObject);
end;
sHeader := Format('%s - %s', [sHeader, DateTimeToStr(Now())]);

{ Based on Scintilla 2.22's SciTEWin::Print }
{ Based on SciTE 2.22's SciTEWin::Print }

ZeroMemory(@pdlg, SizeOf(pdlg));
pdlg.lStructSize := SizeOf(pdlg);
Expand Down
3 changes: 2 additions & 1 deletion Projects/Src/CompScintEdit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ interface
from the container }
TCompScintComplexCommand = (ccNone, ccSelectNextOccurrence,
ccSelectAllOccurrences, ccSelectAllFindMatches, ccSimplifySelection,
ccUnfoldLine, ccFoldLine);
ccUnfoldLine, ccFoldLine, ccToggleLinesComment);

TCompScintEdit = class(TScintEdit)
private
Expand Down Expand Up @@ -364,6 +364,7 @@ procedure TCompScintEdit.UpdateComplexCommands;
AddComplexCommand(ShortCut(VK_ESCAPE, []), ccSimplifySelection);
AddComplexCommand(ShortCut(VK_OEM_6, [ssShift, ssCtrl]), ccUnfoldLine);
AddComplexCommand(ShortCut(VK_OEM_4, [ssShift, ssCtrl]), ccFoldLine);
AddComplexCommand(ShortCut(VK_OEM_2, [ssCtrl]), ccToggleLinesComment);
end;

procedure TCompScintEdit.SetUseFolding(const Value: Boolean);
Expand Down
1 change: 1 addition & 0 deletions whatsnew.htm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<li>The editor can now be scrolled horizontally instead of vertically by holding the Shift key while rotating the mouse wheel. Horizontal scroll wheels are now also supported.</li>
<li>Cut (Ctrl+X or Shift+Delete) and Copy (Ctrl+C or Ctrl+Insert) now cut or copy the entire line if there's no selection, consistent with most other modern editors.</li>
<li>Added shortcuts to move selected lines up or down (Alt+Up and Alt+Down).</li>
<li>Added shortcut to toggle lines comment (Ctrl+/).</li>
<li>Added a right-click popup menu to the editor's gutter column for breakpoints.</li>
</ul>
<p>Other changes:</p>
Expand Down

0 comments on commit 6157099

Please sign in to comment.