diff options
Diffstat (limited to 'main/src/addins/CSharpBinding/MonoDevelop.CSharp.Highlighting/CSharpSelectionSurroundingProvider.cs')
-rw-r--r-- | main/src/addins/CSharpBinding/MonoDevelop.CSharp.Highlighting/CSharpSelectionSurroundingProvider.cs | 115 |
1 files changed, 70 insertions, 45 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Highlighting/CSharpSelectionSurroundingProvider.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Highlighting/CSharpSelectionSurroundingProvider.cs index 9b252d0c6e..57f283bbb8 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Highlighting/CSharpSelectionSurroundingProvider.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Highlighting/CSharpSelectionSurroundingProvider.cs @@ -24,78 +24,103 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; -using Mono.TextEditor; using MonoDevelop.CSharp.Formatting; +using MonoDevelop.Ide.Editor; +using MonoDevelop.Ide.Editor.Extension; namespace MonoDevelop.CSharp.Highlighting { - class CSharpSelectionSurroundingProvider : DefaultSelectionSurroundingProvider + class CSharpSelectionSurroundingProvider : SelectionSurroundingProvider { - MonoDevelop.Ide.Gui.Document document; + readonly DocumentContext context; + readonly TextEditor editor; - public CSharpSelectionSurroundingProvider (MonoDevelop.Ide.Gui.Document document) + public CSharpSelectionSurroundingProvider (MonoDevelop.Ide.Editor.TextEditor editor, DocumentContext context) { - this.document = document; + this.editor = editor; + this.context = context; } - public override bool GetSelectionSurroundings (TextEditorData textEditorData, uint unicodeKey, out string start, out string end) + #region SelectionSurroundingProvider implementation + + public override bool GetSelectionSurroundings (uint unicodeKey, out string start, out string end) { - if (unicodeKey == '/') { + switch ((char)unicodeKey) { + case '"': + start = editor.SelectionRegion.BeginLine != editor.SelectionRegion.EndLine ? "@\"" : "\""; + end = "\""; + return true; + case '\'': + start = end = "'"; + return true; + case '(': + start = "("; + end = ")"; + return true; + case '<': + start = "<"; + end = ">"; + return true; + case '[': + start = "["; + end = "]"; + return true; + case '{': + start = "{"; + end = "}"; + return true; + case '/': start = "/*"; end = "*/"; return true; + default: + start = end = ""; + return false; } - - if (unicodeKey == '"') { - start = textEditorData.MainSelection.Anchor.Line != textEditorData.MainSelection.Lead.Line ? "@\"" : "\""; - end = "\""; - return true; - } - return base.GetSelectionSurroundings (textEditorData, unicodeKey, out start, out end); } - public override void HandleSpecialSelectionKey (TextEditorData textEditorData,uint unicodeKey) + public override void HandleSpecialSelectionKey (uint unicodeKey) { string start, end; - GetSelectionSurroundings (textEditorData, unicodeKey, out start, out end); - var selection = textEditorData.MainSelection; + ((SelectionSurroundingProvider)this).GetSelectionSurroundings (unicodeKey, out start, out end); + + if (editor.SelectionMode == SelectionMode.Block) { + var selection = editor.SelectionRegion; + int startCol = System.Math.Min (selection.Begin.Column, selection.End.Column) - 1; + int endCol = System.Math.Max (selection.Begin.Column, selection.End.Column); + + int minLine = System.Math.Min (selection.Begin.Line, selection.End.Line); + int maxLine = System.Math.Max (selection.BeginLine, selection.End.Line); + - if (textEditorData.MainSelection.SelectionMode == SelectionMode.Block) { - int startCol = System.Math.Min (selection.Anchor.Column, selection.Lead.Column) - 1; - int endCol = System.Math.Max (selection.Anchor.Column, selection.Lead.Column); - for (int lineNumber = selection.MinLine; lineNumber <= selection.MaxLine; lineNumber++) { - DocumentLine lineSegment = textEditorData.GetLine (lineNumber); + for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) { + var lineSegment = editor.GetLine (lineNumber); if (lineSegment.Offset + startCol < lineSegment.EndOffset) - textEditorData.Insert (lineSegment.Offset + startCol, start); + editor.InsertText (lineSegment.Offset + startCol, start); if (lineSegment.Offset + endCol < lineSegment.EndOffset) - textEditorData.Insert (lineSegment.Offset + endCol, end); + editor.InsertText (lineSegment.Offset + endCol, end); } - textEditorData.MainSelection = new Selection ( - new DocumentLocation (selection.Anchor.Line, endCol == selection.Anchor.Column ? endCol + start.Length : startCol + 1 + start.Length), - new DocumentLocation (selection.Lead.Line, endCol == selection.Anchor.Column ? startCol + 1 + start.Length : endCol + start.Length), - Mono.TextEditor.SelectionMode.Block); - textEditorData.Document.CommitMultipleLineUpdate (textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine); +// textEditorData.MainSelection = new Selection ( +// new DocumentLocation (selection.Anchor.Line, endCol == selection.Anchor.Column ? endCol + start.Length : startCol + 1 + start.Length), +// new DocumentLocation (selection.Lead.Line, endCol == selection.Anchor.Column ? startCol + 1 + start.Length : endCol + start.Length), +// Mono.TextEditor.SelectionMode.Block); } else { - int anchorOffset = selection.GetAnchorOffset (textEditorData); - int leadOffset = selection.GetLeadOffset (textEditorData); - if (leadOffset < anchorOffset) { - int tmp = anchorOffset; - anchorOffset = leadOffset; - leadOffset = tmp; - } - textEditorData.Insert (anchorOffset, start); - textEditorData.Insert (leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end); - // textEditorData.SetSelection (anchorOffset + start.Length, leadOffset + start.Length); + var selectionRange = editor.SelectionRange; + int anchorOffset = selectionRange.Offset; + int leadOffset = selectionRange.EndOffset; + + editor.InsertText (anchorOffset, start); + editor.InsertText (leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end); + // textEditorData.SetSelection (anchorOffset + start.Length, leadOffset + start.Length); if (CSharpTextEditorIndentation.OnTheFlyFormatting) { - var l1 = textEditorData.GetLineByOffset (anchorOffset); - var l2 = textEditorData.GetLineByOffset (leadOffset); - OnTheFlyFormatter.Format (document, l1.Offset, l2.EndOffsetIncludingDelimiter); + var l1 = editor.GetLineByOffset (anchorOffset); + var l2 = editor.GetLineByOffset (leadOffset); + OnTheFlyFormatter.Format (editor, context, l1.Offset, l2.EndOffsetIncludingDelimiter); } } } - + #endregion } -} - +}
\ No newline at end of file |