Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit f3aeb8b

Browse files
committed
added first steps of multi commenting
1 parent 9f16660 commit f3aeb8b

File tree

7 files changed

+67
-29
lines changed

7 files changed

+67
-29
lines changed

Interop/HotkeyControl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class HotkeyControl
2323
{ "ReformatAll", "Ctrl+Shift+R" },
2424
{ "GoToLine", "Ctrl+G" },
2525
{ "CommentLine", "Ctrl+K" },
26+
{ "UncommentLine", "Ctrl+Shift+K" },
2627
{ "TransformUppercase", "Ctrl+U" },
2728
{ "TransformLowercase", "Ctrl+Shift+U" },
2829
{ "DeleteLine", "Ctrl+D" },

Interop/TranslationProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ private void FillToEnglishDefaults()
164164
language.Add("Folding", "Foldings");
165165
language.Add("FoldingsExpand", "Expand all");
166166
language.Add("FoldingsCollapse", "Collapse all");
167-
language.Add("CommentLine", "Toggle comment");
167+
language.Add("CommentLine", "Comment selection");
168+
language.Add("UncommentLine", "Uncomment selection");
168169
language.Add("SelectAll", "Select all");
169170
language.Add("SearchReplace", "Find & Replace");
170171
language.Add("Tools", "Tools");

UI/Components/EditorElement/EditorElement.xaml.cs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ public async void Close(bool ForcedToSave = false, bool CheckSavings = true)
740740
{
741741
regularyTimer.Stop();
742742
regularyTimer.Close();
743+
743744
if (fileWatcher != null)
744745
{
745746
fileWatcher.EnableRaisingEvents = false;
@@ -766,45 +767,73 @@ public async void Close(bool ForcedToSave = false, bool CheckSavings = true)
766767
}
767768
}
768769

770+
Parent = null;
771+
769772
Program.MainWindow.EditorsReferences.Remove(this);
770773
Program.MainWindow.MenuI_ReopenLastClosedTab.IsEnabled = true;
771774
Program.RecentFilesStack.Push(FullFilePath);
772-
Parent = null; //to prevent a ring depency which disables the GC from work
773775
Program.MainWindow.UpdateWindowTitle();
774776
}
775777

776-
public void ToggleCommentOnLine()
778+
public void ToggleComment(bool comment)
777779
{
778-
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
779-
var lineText = editor.Document.GetText(line);
780-
var leadingWhiteSpaces = 0;
781-
foreach (var l in lineText)
780+
// Get the selection segments
781+
var selectionSegments = editor.TextArea.Selection.Segments;
782+
783+
var lineList = new List<DocumentLine>();
784+
var document = editor.TextArea.Document;
785+
786+
// If there's no selection, add to lineList the line the caret is standing on
787+
if (!selectionSegments.Any())
782788
{
783-
if (char.IsWhiteSpace(l))
784-
{
785-
leadingWhiteSpaces++;
786-
}
787-
else
789+
lineList.Add(document.GetLineByOffset(editor.TextArea.Caret.Offset));
790+
}
791+
else
792+
{
793+
// Get all the lines from that selection and store them in a list
794+
foreach (var segment in selectionSegments)
788795
{
789-
break;
796+
var lineStart = document.GetLineByOffset(segment.StartOffset).LineNumber;
797+
var lineEnd = document.GetLineByOffset(segment.EndOffset).LineNumber;
798+
for (var i = lineStart; i <= lineEnd; i++)
799+
{
800+
lineList.Add(editor.Document.GetLineByNumber(i));
801+
}
790802
}
791803
}
792804

793-
lineText = lineText.Trim();
794-
if (lineText.Length > 1)
805+
// For each line, apply comment logic
806+
foreach (var line in lineList)
795807
{
796-
if (lineText[0] == '/' && lineText[1] == '/')
808+
var lineText = editor.Document.GetText(line);
809+
var leadingWhiteSpaces = 0;
810+
foreach (var l in lineText)
797811
{
798-
editor.Document.Remove(line.Offset + leadingWhiteSpaces, 2);
812+
if (char.IsWhiteSpace(l))
813+
{
814+
leadingWhiteSpaces++;
815+
}
816+
else
817+
{
818+
break;
819+
}
799820
}
800-
else
821+
lineText = lineText.Trim();
822+
if (lineText.Length > 1)
801823
{
802-
editor.Document.Insert(line.Offset + leadingWhiteSpaces, "//");
824+
if (!comment && lineText[0] == '/' && lineText[1] == '/')
825+
{
826+
editor.Document.Remove(line.Offset + leadingWhiteSpaces, 3);
827+
}
828+
else if (comment && lineText[0] != '/' && lineText[1] != '/')
829+
{
830+
editor.Document.Insert(line.Offset + leadingWhiteSpaces, "// ");
831+
}
832+
}
833+
else if (comment)
834+
{
835+
editor.Document.Insert(line.Offset + leadingWhiteSpaces, "// ");
803836
}
804-
}
805-
else
806-
{
807-
editor.Document.Insert(line.Offset + leadingWhiteSpaces, "//");
808837
}
809838
}
810839

UI/MainWindow/MainWindow.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@
140140
<MenuItem x:Name="MenuI_FoldingsCollapse" Header="Collapse All" Click="Menu_CollapseAll" />
141141
</MenuItem>
142142
<MenuItem x:Name="MenuI_GoToLine" Header="Jump To" Click="Menu_JumpTo"/>
143-
<MenuItem x:Name="MenuI_CommentLine" Header="Toggle comment (//)" Click="Menu_ToggleCommentLine"/>
143+
<MenuItem x:Name="MenuI_CommentLine" Header="Comment selection (//)" Click="Menu_CommentLine"/>
144+
<MenuItem x:Name="MenuI_UncommentLine" Header="Uncomment selection (//)" Click="Menu_UncommentLine"/>
144145
<MenuItem x:Name="MenuI_SelectAll" Header="Select All" Click="Menu_SelectAll"/>
145146
<Separator />
146147
<MenuItem x:Name="MenuI_SearchReplace" Header="Find &amp; Replace" Click="Menu_FindAndReplace"/>

UI/MainWindow/MainWindowCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,12 @@ private void Command_SelectAll()
425425
/// <summary>
426426
/// Comment or uncomment the line the caret is in.
427427
/// </summary>
428-
private void Command_ToggleCommentLine()
428+
private void Command_ToggleCommentLine(bool comment)
429429
{
430430
var ee = GetCurrentEditorElement();
431431
if (ee != null && !ee.editor.IsReadOnly)
432432
{
433-
ee.ToggleCommentOnLine();
433+
ee.ToggleComment(comment);
434434
}
435435
}
436436

UI/MainWindow/MainWindowInputHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private void LoadCommandsDictionary()
7676
{ "ReformatCurrent", () => Command_TidyCode(false) },
7777
{ "ReformatAll", () => Command_TidyCode(true) },
7878
{ "GoToLine", Command_GoToLine },
79-
{ "CommentLine", Command_ToggleCommentLine },
79+
{ "CommentLine", () => Command_ToggleCommentLine(true) },
80+
{ "UncommentLine", () => Command_ToggleCommentLine(false) },
8081
{ "TransformUppercase", () => Command_ChangeCase(true) },
8182
{ "TransformLowercase", () => Command_ChangeCase(false) },
8283
{ "DeleteLine", Command_DeleteLine },

UI/MainWindow/MainWindowMenuHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,14 @@ private void Menu_JumpTo(object sender, RoutedEventArgs e)
157157
Command_GoToLine();
158158
}
159159

160-
private void Menu_ToggleCommentLine(object sender, RoutedEventArgs e)
160+
private void Menu_CommentLine(object sender, RoutedEventArgs e)
161161
{
162-
Command_ToggleCommentLine();
162+
Command_ToggleCommentLine(true);
163+
}
164+
165+
private void Menu_UncommentLine(object sender, RoutedEventArgs e)
166+
{
167+
Command_ToggleCommentLine(false);
163168
}
164169

165170
private void Menu_SelectAll(object sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)