Browse Source

ported Indent and Comment commands to SharpDevelop 4.0

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4431 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 16 years ago
parent
commit
af5a6c6ade
  1. 22
      AddIns/ICSharpCode.SharpDevelop.addin
  2. 10
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs
  3. 24
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/AvalonEdit.AddIn.addin
  4. 5
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlFormattingStrategy.cs
  5. 3
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  6. 33
      src/Main/Base/Project/Src/Editor/Commands/CommentRegion.cs
  7. 43
      src/Main/Base/Project/Src/Editor/Commands/IndentSelection.cs
  8. 179
      src/Main/Base/Project/Src/Editor/IFormattingStrategy.cs

22
AddIns/ICSharpCode.SharpDevelop.addin

@ -1236,7 +1236,7 @@
class = "ICSharpCode.SharpDevelop.Commands.ClearRecentProjects" /> class = "ICSharpCode.SharpDevelop.Commands.ClearRecentProjects" />
</MenuItem> </MenuItem>
<MenuItem id = "ExitSC:eparator" type = "Separator" /> <MenuItem id = "ExitSeparator" type = "Separator" />
<MenuItem id = "Exit" <MenuItem id = "Exit"
label = "${res:XML.MainMenu.FileMenu.Exit}" label = "${res:XML.MainMenu.FileMenu.Exit}"
icon = "Icons.16x16.CancelIcon" icon = "Icons.16x16.CancelIcon"
@ -1777,21 +1777,20 @@
label = "${res:XML.MainMenu.FileMenu.Close}" label = "${res:XML.MainMenu.FileMenu.Close}"
icon = "Icons.16x16.CloseFileIcon" icon = "Icons.16x16.CloseFileIcon"
class ="ICSharpCode.SharpDevelop.Commands.CloseFile"/> class ="ICSharpCode.SharpDevelop.Commands.CloseFile"/>
</Path>
<!-- DefaultTextEditor=old editor. TODO: remove this -->
<Path name = "/SharpDevelop/ViewContent/DefaultTextEditor/ContextMenu">
<Include id="DefaultEditor" path="/SharpDevelop/ViewContent/TextEditor/ContextMenu"/>
<MenuItem id = "Separator2" type = "Separator" /> <MenuItem id = "Separator2" type = "Separator" />
<MenuItem id = "Comment" <MenuItem id = "Comment"
icon = "Icons.16x16.CommentRegion" icon = "Icons.16x16.CommentRegion"
label = "${res:XML.TextAreaContextMenu.CommentUncommentSelection}" label = "${res:XML.TextAreaContextMenu.CommentUncommentSelection}"
class= "ICSharpCode.SharpDevelop.DefaultEditor.Commands.CommentRegion"/> class = "ICSharpCode.SharpDevelop.Editor.Commands.CommentRegion"/>
<MenuItem id = "Indent" <MenuItem id = "Indent"
label = "${res:XML.TextAreaContextMenu.Indent}" label = "${res:XML.TextAreaContextMenu.Indent}"
shortcut = "Control|I" shortcut = "Control|I"
class = "ICSharpCode.SharpDevelop.DefaultEditor.Commands.IndentSelection" /> class = "ICSharpCode.SharpDevelop.Editor.Commands.IndentSelection" />
</Path>
<!-- DefaultTextEditor=old editor. TODO: remove this -->
<Path name = "/SharpDevelop/ViewContent/DefaultTextEditor/ContextMenu">
<Include id="DefaultEditor" path="/SharpDevelop/ViewContent/TextEditor/ContextMenu"/>
<MenuItem id = "FileMode" label = "${res:XML.TextAreaContextMenu.FileMode}" type="Menu"> <MenuItem id = "FileMode" label = "${res:XML.TextAreaContextMenu.FileMode}" type="Menu">
<MenuItem id = "HighlightBuilder" type="Builder" class = "ICSharpCode.SharpDevelop.DefaultEditor.Commands.HighlightingTypeBuilder" /> <MenuItem id = "HighlightBuilder" type="Builder" class = "ICSharpCode.SharpDevelop.DefaultEditor.Commands.HighlightingTypeBuilder" />
</MenuItem> </MenuItem>
@ -1809,11 +1808,11 @@
<Path name = "/SharpDevelop/Workbench/ToolBar/Standard"> <Path name = "/SharpDevelop/Workbench/ToolBar/Standard">
<ToolbarItem id = "SearchSeparator" type = "Separator"/> <ToolbarItem id = "SearchSeparator" type = "Separator"/>
<Condition name = "WindowActive" activewindow="ICSharpCode.SharpDevelop.ITextEditorProvider" action="Disable"> <Condition name = "WindowActive" activewindow="ICSharpCode.SharpDevelop.Editor.ITextEditorProvider" action="Disable">
<ToolbarItem id = "Comment" <ToolbarItem id = "Comment"
icon = "Icons.16x16.CommentRegion" icon = "Icons.16x16.CommentRegion"
tooltip = "${res:XML.TextAreaContextMenu.CommentUncommentSelection}" tooltip = "${res:XML.TextAreaContextMenu.CommentUncommentSelection}"
class = "ICSharpCode.SharpDevelop.DefaultEditor.Commands.CommentRegion"/> class = "ICSharpCode.SharpDevelop.Editor.Commands.CommentRegion"/>
<!-- <ToolbarItem id = "Uncomment" <!-- <ToolbarItem id = "Uncomment"
icon = "Icons.16x16.UnCommentRegion" icon = "Icons.16x16.UnCommentRegion"
tooltip = "${res:XML.TextAreaContextMenu.UncommentSelection}" tooltip = "${res:XML.TextAreaContextMenu.UncommentSelection}"
@ -1842,6 +1841,9 @@
<Path name = "/SharpDevelop/Workbench/MainMenu/Edit"> <Path name = "/SharpDevelop/Workbench/MainMenu/Edit">
<MenuItem insertafter = "Delete" id = "Separator2" type = "Separator" /> <MenuItem insertafter = "Delete" id = "Separator2" type = "Separator" />
<MenuItem id = "Format" label = "${res:XML.MainMenu.EditMenu.FormatMenu}" type="Menu"> <MenuItem id = "Format" label = "${res:XML.MainMenu.EditMenu.FormatMenu}" type="Menu">
<MenuItem id = "Separator4" type = "Separator" />
<Include id = "Comment" item = "/SharpDevelop/ViewContent/TextEditor/ContextMenu/Comment" />
<Include id = "Indent" item = "/SharpDevelop/ViewContent/TextEditor/ContextMenu/Indent" />
</MenuItem> </MenuItem>
<MenuItem id = "Folding" label = "${res:XML.MainMenu.EditMenu.FoldingMenu}" type="Menu"> <MenuItem id = "Folding" label = "${res:XML.MainMenu.EditMenu.FoldingMenu}" type="Menu">
<Condition name = "WindowActive" activewindow="ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.ITextEditorControlProvider" action="Disable"> <Condition name = "WindowActive" activewindow="ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.ITextEditorControlProvider" action="Disable">

10
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

@ -5,15 +5,18 @@
// <version>$Revision$</version> // <version>$Revision$</version>
// </file> // </file>
using ICSharpCode.SharpDevelop.Editor;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Text; using System.Text;
using ICSharpCode.NRefactory;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.Refactoring; using ICSharpCode.SharpDevelop.Dom.Refactoring;
using ICSharpCode.SharpDevelop.Editor;
namespace CSharpBinding.FormattingStrategy namespace CSharpBinding.FormattingStrategy
{ {
@ -543,6 +546,11 @@ namespace CSharpBinding.FormattingStrategy
} }
#endregion #endregion
public override void SurroundSelectionWithComment(ITextEditor editor)
{
SurroundSelectionWithSingleLineComment(editor, "//");
}
/* /*
#region SearchBracketBackward #region SearchBracketBackward
public override int SearchBracketBackward(IDocument document, int offset, char openBracket, char closingBracket) public override int SearchBracketBackward(IDocument document, int offset, char openBracket, char closingBracket)

24
src/AddIns/DisplayBindings/AvalonEdit.AddIn/AvalonEdit.AddIn.addin

@ -25,52 +25,54 @@
<Path name = "/SharpDevelop/Workbench/MainMenu/Edit/Format"> <Path name = "/SharpDevelop/Workbench/MainMenu/Edit/Format">
<Condition name = "WindowActive" activewindow="ICSharpCode.SharpDevelop.Editor.ITextEditorProvider" action="Disable"> <Condition name = "WindowActive" activewindow="ICSharpCode.SharpDevelop.Editor.ITextEditorProvider" action="Disable">
<MenuItem id = "RemoveLeadingWs" <MenuItem id = "RemoveLeadingWs"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.RlWs}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.RlWs}"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.RemoveLeadingWhitespace"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.RemoveLeadingWhitespace"/>
<MenuItem id = "RemoveTrailingWs" <MenuItem id = "RemoveTrailingWs"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.RtWs}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.RtWs}"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.RemoveTrailingWhitespace"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.RemoveTrailingWhitespace"/>
<MenuItem id = "Seperator1" type = "Separator" /> <MenuItem id = "Seperator1" type = "Separator" insertbefore = "Separator4" />
<MenuItem id = "UpperCase" <MenuItem id = "UpperCase"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.UpperCase}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.UpperCase}"
icon = "Icons.16x16.LowerToUpperCase" icon = "Icons.16x16.LowerToUpperCase"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertToUppercase"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertToUppercase"/>
<MenuItem id = "LowerCase" <MenuItem id = "LowerCase"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.LowerCase}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.LowerCase}"
icon = "Icons.16x16.UpperToLowerCase" icon = "Icons.16x16.UpperToLowerCase"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertToLowercase"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertToLowercase"/>
<MenuItem id = "Capitalize" <MenuItem id = "Capitalize"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.Capitalize}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.Capitalize}"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertToTitleCase"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertToTitleCase"/>
<MenuItem id = "InvertCase" <MenuItem id = "InvertCase"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.InvertCase}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.InvertCase}"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.InvertCase"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.InvertCase"/>
<MenuItem id = "Separator2" type = "Separator" /> <MenuItem id = "Separator2" type = "Separator" />
<MenuItem id = "SortSelection" <MenuItem id = "SortSelection"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.SortLines}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.SortLines}"
class ="ICSharpCode.SharpDevelop.Editor.Commands.SortSelection"/> class ="ICSharpCode.SharpDevelop.Editor.Commands.SortSelection"/>
<MenuItem id = "Separator3" type = "Separator" /> <MenuItem id = "Separator3" type = "Separator" insertbefore = "Separator4" />
<MenuItem id = "Tabs2Spaces" <MenuItem id = "Tabs2Spaces"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.Tab2Space}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.Tab2Space}"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertTabsToSpaces"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertTabsToSpaces"/>
<MenuItem id = "Spaces2Tabs" <MenuItem id = "Spaces2Tabs"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.Space2Tab}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.Space2Tab}"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertSpacesToTabs"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertSpacesToTabs"/>
<MenuItem id = "LeadingTabs2Spaces" <MenuItem id = "LeadingTabs2Spaces"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.LdTab2Space}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.LdTab2Space}"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertLeadingTabsToSpaces"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertLeadingTabsToSpaces"/>
<MenuItem id = "LeadingSpaces2Tabs" <MenuItem id = "LeadingSpaces2Tabs"
insertbefore = "Separator4"
label = "${res:XML.MainMenu.EditMenu.FormatMenu.LdSpace2Tab}" label = "${res:XML.MainMenu.EditMenu.FormatMenu.LdSpace2Tab}"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertLeadingSpacesToTabs"/> command = "ICSharpCode.AvalonEdit.AvalonEditCommands.ConvertLeadingSpacesToTabs"/>
<MenuItem id = "Separator4" type = "Separator" />
<MenuItem id = "Comment"
icon = "Icons.16x16.CommentRegion"
label = "${res:XML.TextAreaContextMenu.CommentUncommentSelection}"
class = "ICSharpCode.SharpDevelop.Editor.Commands.CommentRegion"/>
<MenuItem id = "Indent"
label = "${res:XML.TextAreaContextMenu.Indent}"
shortcut = "Control|I"
command = "ICSharpCode.AvalonEdit.AvalonEditCommands.IndentSelection"/>
</Condition> </Condition>
</Path> </Path>
</AddIn> </AddIn>

5
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlFormattingStrategy.cs

@ -102,6 +102,11 @@ namespace ICSharpCode.XmlEditor
} }
} }
public override void SurroundSelectionWithComment(ITextEditor editor)
{
SurroundSelectionWithBlockComment(editor, "<!--", "-->");
}
static void TryIndent(ITextEditor editor, int begin, int end) static void TryIndent(ITextEditor editor, int begin, int end)
{ {
string currentIndentation = ""; string currentIndentation = "";

3
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -90,6 +90,8 @@
<Compile Include="Src\Editor\CodeCompletion\OverrideCompletionItemProvider.cs" /> <Compile Include="Src\Editor\CodeCompletion\OverrideCompletionItemProvider.cs" />
<Compile Include="Src\Editor\CodeCompletion\TemplateCompletionItemProvider.cs" /> <Compile Include="Src\Editor\CodeCompletion\TemplateCompletionItemProvider.cs" />
<Compile Include="Src\Editor\CodeCompletion\TextCompletionItemProvider.cs" /> <Compile Include="Src\Editor\CodeCompletion\TextCompletionItemProvider.cs" />
<Compile Include="Src\Editor\Commands\CommentRegion.cs" />
<Compile Include="Src\Editor\Commands\IndentSelection.cs" />
<Compile Include="Src\Editor\DocumentUtilitites.cs" /> <Compile Include="Src\Editor\DocumentUtilitites.cs" />
<Compile Include="Src\Editor\IDocument.cs" /> <Compile Include="Src\Editor\IDocument.cs" />
<Compile Include="Src\Editor\IDocumentLine.cs" /> <Compile Include="Src\Editor\IDocumentLine.cs" />
@ -816,6 +818,7 @@
</ProjectReference> </ProjectReference>
<Folder Include="Src\Editor\AvalonEdit" /> <Folder Include="Src\Editor\AvalonEdit" />
<Folder Include="Src\Editor\CodeCompletion" /> <Folder Include="Src\Editor\CodeCompletion" />
<Folder Include="Src\Editor\Commands" />
<Folder Include="Src\Editor\Search" /> <Folder Include="Src\Editor\Search" />
<Folder Include="Src\Gui\Pads\TaskList" /> <Folder Include="Src\Gui\Pads\TaskList" />
<Content Include="..\..\..\..\AddIns\ICSharpCode.SharpDevelop.addin"> <Content Include="..\..\..\..\AddIns\ICSharpCode.SharpDevelop.addin">

33
src/Main/Base/Project/Src/Editor/Commands/CommentRegion.cs

@ -0,0 +1,33 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.SharpDevelop.Gui;
using System;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop;
namespace ICSharpCode.SharpDevelop.Editor.Commands
{
/// <summary>
/// Description of CommentRegion
/// </summary>
public class CommentRegion : AbstractMenuCommand
{
/// <summary>
/// Starts the command
/// </summary>
public override void Run()
{
ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
if (provider == null)
return;
provider.TextEditor.FormattingStrategy.SurroundSelectionWithComment(provider.TextEditor);
}
}
}

43
src/Main/Base/Project/Src/Editor/Commands/IndentSelection.cs

@ -0,0 +1,43 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.SharpDevelop.Gui;
using System;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Editor;
namespace ICSharpCode.SharpDevelop.Editor.Commands
{
/// <summary>
/// Description of IndentSelection
/// </summary>
public class IndentSelection : AbstractMenuCommand
{
/// <summary>
/// Starts the command
/// </summary>
public override void Run()
{
ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
if (provider == null)
return;
int beginLine = 1;
int endLine = provider.TextEditor.Document.TotalNumberOfLines;
if (provider.TextEditor.SelectionLength != 0) {
beginLine = provider.TextEditor.Document.GetLineForOffset(provider.TextEditor.SelectionStart).LineNumber;
endLine = provider.TextEditor.Document.GetLineForOffset(provider.TextEditor.SelectionStart + provider.TextEditor.SelectionLength).LineNumber;
}
provider.TextEditor.FormattingStrategy.IndentLines(provider.TextEditor, beginLine, endLine);
}
}
}

179
src/Main/Base/Project/Src/Editor/IFormattingStrategy.cs

@ -6,6 +6,8 @@
// </file> // </file>
using System; using System;
using ICSharpCode.NRefactory;
using System.Collections.Generic;
namespace ICSharpCode.SharpDevelop.Editor namespace ICSharpCode.SharpDevelop.Editor
{ {
@ -28,6 +30,11 @@ namespace ICSharpCode.SharpDevelop.Editor
/// This function sets the indentation in a range of lines. /// This function sets the indentation in a range of lines.
/// </summary> /// </summary>
void IndentLines(ITextEditor editor, int beginLine, int endLine); void IndentLines(ITextEditor editor, int beginLine, int endLine);
/// <summary>
/// This function surrounds the selected text with a comment.
/// </summary>
void SurroundSelectionWithComment(ITextEditor editor);
} }
public class DefaultFormattingStrategy : IFormattingStrategy public class DefaultFormattingStrategy : IFormattingStrategy
@ -59,5 +66,177 @@ namespace ICSharpCode.SharpDevelop.Editor
} }
} }
} }
public virtual void SurroundSelectionWithComment(ITextEditor editor)
{
}
/// <summary>
/// Default implementation for single line comments.
/// </summary>
protected void SurroundSelectionWithSingleLineComment(ITextEditor editor, string comment)
{
using (editor.Document.OpenUndoGroup()) {
Location startPosition = editor.Document.OffsetToPosition(editor.SelectionStart);
Location endPosition = editor.Document.OffsetToPosition(editor.SelectionStart + editor.SelectionLength);
List<IDocumentLine> lines = new List<IDocumentLine>();
bool removeComment = true;
for (int i = startPosition.Line; i <= endPosition.Line; i++) {
lines.Add(editor.Document.GetLine(i));
if (!lines[i - startPosition.Line].Text.Trim().StartsWith(comment))
removeComment = false;
}
foreach (IDocumentLine line in lines) {
if (removeComment) {
editor.Document.Remove(line.Offset + line.Text.IndexOf(comment), comment.Length);
} else {
editor.Document.Insert(line.Offset, comment);
}
}
}
}
/// <summary>
/// Default implementation for multiline comments.
/// </summary>
protected void SurroundSelectionWithBlockComment(ITextEditor editor, string blockStart, string blockEnd)
{
using (editor.Document.OpenUndoGroup()) {
int startOffset = editor.SelectionStart;
int endOffset = editor.SelectionStart + editor.SelectionLength;
if (editor.SelectionLength == 0) {
IDocumentLine line = editor.Document.GetLineForOffset(editor.SelectionStart);
startOffset = line.Offset;
endOffset = line.Offset + line.Length;
}
BlockCommentRegion region = FindSelectedCommentRegion(editor, blockStart, blockEnd);
if (region != null) {
editor.Document.Remove(region.EndOffset, region.CommentEnd.Length);
editor.Document.Remove(region.StartOffset, region.CommentStart.Length);
} else {
editor.Document.Insert(endOffset, blockEnd);
editor.Document.Insert(startOffset, blockStart);
}
}
}
public static BlockCommentRegion FindSelectedCommentRegion(ITextEditor editor, string commentStart, string commentEnd)
{
IDocument document = editor.Document;
if (document.TextLength == 0) {
return null;
}
// Find start of comment in selected text.
int commentEndOffset = -1;
string selectedText = editor.SelectedText;
int commentStartOffset = selectedText.IndexOf(commentStart);
if (commentStartOffset >= 0) {
commentStartOffset += editor.SelectionStart;
}
// Find end of comment in selected text.
if (commentStartOffset >= 0) {
commentEndOffset = selectedText.IndexOf(commentEnd, commentStartOffset + commentStart.Length - editor.SelectionStart);
} else {
commentEndOffset = selectedText.IndexOf(commentEnd);
}
if (commentEndOffset >= 0) {
commentEndOffset += editor.SelectionStart;
}
// Find start of comment before or partially inside the
// selected text.
int commentEndBeforeStartOffset = -1;
if (commentStartOffset == -1) {
int offset = editor.SelectionStart + editor.SelectionLength + commentStart.Length - 1;
if (offset > document.TextLength) {
offset = document.TextLength;
}
string text = document.GetText(0, offset);
commentStartOffset = text.LastIndexOf(commentStart);
if (commentStartOffset >= 0) {
// Find end of comment before comment start.
commentEndBeforeStartOffset = text.IndexOf(commentEnd, commentStartOffset, editor.SelectionStart - commentStartOffset);
if (commentEndBeforeStartOffset > commentStartOffset) {
commentStartOffset = -1;
}
}
}
// Find end of comment after or partially after the
// selected text.
if (commentEndOffset == -1) {
int offset = editor.SelectionStart + 1 - commentEnd.Length;
if (offset < 0) {
offset = editor.SelectionStart;
}
string text = document.GetText(offset, document.TextLength - offset);
commentEndOffset = text.IndexOf(commentEnd);
if (commentEndOffset >= 0) {
commentEndOffset += offset;
}
}
if (commentStartOffset != -1 && commentEndOffset != -1) {
return new BlockCommentRegion(commentStart, commentEnd, commentStartOffset, commentEndOffset);
}
return null;
}
}
public class BlockCommentRegion
{
public string CommentStart { get; private set; }
public string CommentEnd { get; private set; }
public int StartOffset { get; private set; }
public int EndOffset { get; private set; }
/// <summary>
/// The end offset is the offset where the comment end string starts from.
/// </summary>
public BlockCommentRegion(string commentStart, string commentEnd, int startOffset, int endOffset)
{
this.CommentStart = commentStart;
this.CommentEnd = commentEnd;
this.StartOffset = startOffset;
this.EndOffset = endOffset;
}
public override int GetHashCode()
{
int hashCode = 0;
unchecked {
if (CommentStart != null) hashCode += 1000000007 * CommentStart.GetHashCode();
if (CommentEnd != null) hashCode += 1000000009 * CommentEnd.GetHashCode();
hashCode += 1000000021 * StartOffset.GetHashCode();
hashCode += 1000000033 * EndOffset.GetHashCode();
}
return hashCode;
}
public override bool Equals(object obj)
{
BlockCommentRegion other = obj as BlockCommentRegion;
if (other == null) return false;
return this.CommentStart == other.CommentStart &&
this.CommentEnd == other.CommentEnd &&
this.StartOffset == other.StartOffset &&
this.EndOffset == other.EndOffset;
}
} }
} }

Loading…
Cancel
Save