~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor/ISelectionSurroundingProvider.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ISelectionSurroundingProvider.cs
 
3
//
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
 
8
//
 
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:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
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
 
25
// THE SOFTWARE.
 
26
using System;
 
27
 
 
28
namespace Mono.TextEditor
 
29
{
 
30
        /// <summary>
 
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.
 
33
        /// </summary>
 
34
        public interface ISelectionSurroundingProvider  
 
35
        {
 
36
                /// <summary>
 
37
                /// Gets the selection surroundings for a given unicode key.
 
38
                /// </summary>
 
39
                /// <returns>
 
40
                /// true, if the key is valid for a surrounding action.
 
41
                /// </returns>
 
42
                /// <param name='unicodeKey'>
 
43
                /// The key to handle.
 
44
                /// </param>
 
45
                /// <param name='start'>
 
46
                /// The start of the surrounding
 
47
                /// </param>
 
48
                /// <param name='end'>
 
49
                /// The end of the surrounding
 
50
                /// </param>
 
51
                bool GetSelectionSurroundings (TextEditorData textEditorData, uint unicodeKey, out string start, out string end);
 
52
 
 
53
                void HandleSpecialSelectionKey (TextEditorData textEditorData, uint unicodeKey);
 
54
        }
 
55
 
 
56
        /// <summary>
 
57
        /// Null selection surrounding provider. Basically turns off that feature.
 
58
        /// </summary>
 
59
        public sealed class NullSelectionSurroundingProvider : ISelectionSurroundingProvider
 
60
        {
 
61
                public bool GetSelectionSurroundings (TextEditorData textEditorData, uint unicodeKey, out string start, out string end)
 
62
                {
 
63
                        start = end = "";
 
64
                        return false;
 
65
                }
 
66
 
 
67
                public void HandleSpecialSelectionKey (TextEditorData textEditorData, uint unicodeKey)
 
68
                {
 
69
                        throw new NotSupportedException ();
 
70
                }
 
71
        }
 
72
 
 
73
        /// <summary>
 
74
        /// Default selection surrounding provider.
 
75
        /// </summary>
 
76
        public class DefaultSelectionSurroundingProvider : ISelectionSurroundingProvider
 
77
        {
 
78
                public virtual bool GetSelectionSurroundings (TextEditorData textEditorData, uint unicodeKey, out string start, out string end)
 
79
                {
 
80
                        switch ((char)unicodeKey) {
 
81
                        case '"':
 
82
                                start = end = "\"";
 
83
                                return true;
 
84
                        case '\'':
 
85
                                start = end = "'";
 
86
                                return true;
 
87
                        case '(':
 
88
                                start = "(";
 
89
                                end = ")";
 
90
                                return true;
 
91
                        case '<':
 
92
                                start = "<";
 
93
                                end = ">";
 
94
                                return true;
 
95
                        case '[':
 
96
                                start = "[";
 
97
                                end = "]";
 
98
                                return true;
 
99
                        case '{':
 
100
                                start = "{";
 
101
                                end = "}";
 
102
                                return true;
 
103
                        default:
 
104
                                start = end = "";
 
105
                                return false;
 
106
                        }
 
107
                }
 
108
        
 
109
                public virtual void HandleSpecialSelectionKey (TextEditorData textEditorData,uint unicodeKey)
 
110
                {
 
111
                        string start, end;
 
112
                        GetSelectionSurroundings (textEditorData, unicodeKey, out start, out end);
 
113
                        
 
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);
 
120
                                        
 
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);
 
125
                                }
 
126
                                
 
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);
 
132
                        } else {
 
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;
 
138
                                        leadOffset = tmp;
 
139
                                }
 
140
                                textEditorData.Insert (anchorOffset, start);
 
141
                                textEditorData.Insert (leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end);
 
142
                                textEditorData.SetSelection (anchorOffset + start.Length, leadOffset + start.Length);
 
143
                        }
 
144
                }
 
145
        }
 
146
}
 
147