2
// ISelectionSurroundingProvider.cs
5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
namespace Mono.TextEditor
31
/// A selection surrounding provider handles a special handling how the text editor behaves when the user
32
/// types a key with a selection. The selection can be surrounded instead of beeing replaced.
34
public interface ISelectionSurroundingProvider
37
/// Gets the selection surroundings for a given unicode key.
40
/// true, if the key is valid for a surrounding action.
42
/// <param name='unicodeKey'>
43
/// The key to handle.
45
/// <param name='start'>
46
/// The start of the surrounding
48
/// <param name='end'>
49
/// The end of the surrounding
51
bool GetSelectionSurroundings (TextEditorData textEditorData, uint unicodeKey, out string start, out string end);
53
void HandleSpecialSelectionKey (TextEditorData textEditorData, uint unicodeKey);
57
/// Null selection surrounding provider. Basically turns off that feature.
59
public sealed class NullSelectionSurroundingProvider : ISelectionSurroundingProvider
61
public bool GetSelectionSurroundings (TextEditorData textEditorData, uint unicodeKey, out string start, out string end)
67
public void HandleSpecialSelectionKey (TextEditorData textEditorData, uint unicodeKey)
69
throw new NotSupportedException ();
74
/// Default selection surrounding provider.
76
public class DefaultSelectionSurroundingProvider : ISelectionSurroundingProvider
78
public virtual bool GetSelectionSurroundings (TextEditorData textEditorData, uint unicodeKey, out string start, out string end)
80
switch ((char)unicodeKey) {
109
public virtual void HandleSpecialSelectionKey (TextEditorData textEditorData,uint unicodeKey)
112
GetSelectionSurroundings (textEditorData, unicodeKey, out start, out end);
114
if (textEditorData.MainSelection.SelectionMode == SelectionMode.Block) {
115
var selection = textEditorData.MainSelection;
116
int startCol = System.Math.Min (selection.Anchor.Column, selection.Lead.Column) - 1;
117
int endCol = System.Math.Max (selection.Anchor.Column, selection.Lead.Column);
118
for (int lineNumber = selection.MinLine; lineNumber <= selection.MaxLine; lineNumber++) {
119
DocumentLine lineSegment = textEditorData.GetLine (lineNumber);
121
if (lineSegment.Offset + startCol < lineSegment.EndOffset)
122
textEditorData.Insert (lineSegment.Offset + startCol, start);
123
if (lineSegment.Offset + endCol < lineSegment.EndOffset)
124
textEditorData.Insert (lineSegment.Offset + endCol, end);
127
textEditorData.MainSelection = new Selection (
128
new DocumentLocation (selection.Anchor.Line, endCol == selection.Anchor.Column ? endCol + start.Length : startCol + 1 + start.Length),
129
new DocumentLocation (selection.Lead.Line, endCol == selection.Anchor.Column ? startCol + 1 + start.Length : endCol + start.Length),
130
Mono.TextEditor.SelectionMode.Block);
131
textEditorData.Document.CommitMultipleLineUpdate (textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine);
133
int anchorOffset = textEditorData.MainSelection.GetAnchorOffset (textEditorData);
134
int leadOffset = textEditorData.MainSelection.GetLeadOffset (textEditorData);
135
if (leadOffset < anchorOffset) {
136
int tmp = anchorOffset;
137
anchorOffset = leadOffset;
140
textEditorData.Insert (anchorOffset, start);
141
textEditorData.Insert (leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end);
142
textEditorData.SetSelection (anchorOffset + start.Length, leadOffset + start.Length);