~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor.Utils/TextBreaker.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// TextBreaker.cs
 
3
//  
 
4
// Author:
 
5
//       IBBoard <dev@ibboard.co.uk>
 
6
// 
 
7
// Copyright (c) 2011 IBBoard
 
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
using System.Collections.Generic;
 
28
 
 
29
namespace Mono.TextEditor.Utils
 
30
{
 
31
        /// <summary>
 
32
        /// A utility class for breaking up the text in TextEditors
 
33
        /// </summary>
 
34
        public class TextBreaker
 
35
        {
 
36
                /// <summary>
 
37
                /// Breaks the lines into words in the form of a list of <see cref="ISegment">ISegments</see>. A 'word' is defined as an identifier (a series of letters, digits or underscores)
 
38
                /// or a single non-identifier character (including white space characters)
 
39
                /// </summary>
 
40
                /// <returns>
 
41
                /// The list of segments representing the 'words' in the lines
 
42
                /// </returns>
 
43
                /// <param name='editor'>
 
44
                /// The text editor to get the words from
 
45
                /// </param>
 
46
                /// <param name='startLine'>
 
47
                /// The first line in the editor's documents to get the words from
 
48
                /// </param>
 
49
                /// <param name='lineCount'>
 
50
                /// The number of lines to get words from
 
51
                /// </param>
 
52
                public static List<ISegment> BreakLinesIntoWords (TextEditor editor, int startLine, int lineCount)
 
53
                {
 
54
                        return BreakLinesIntoWords (editor.Document, startLine, lineCount);
 
55
                }
 
56
 
 
57
                
 
58
                /// <summary>
 
59
                /// Breaks the lines into words in the form of a list of <see cref="ISegment">ISegments</see>. A 'word' is defined as an identifier (a series of letters, digits or underscores)
 
60
                /// or a single non-identifier character (including white space characters)
 
61
                /// </summary>
 
62
                /// <returns>
 
63
                /// The list of segments representing the 'words' in the lines
 
64
                /// </returns>
 
65
                /// <param name='document'>
 
66
                /// The document to get the words from
 
67
                /// </param>
 
68
                /// <param name='startLine'>
 
69
                /// The first line in the documents to get the words from
 
70
                /// </param>
 
71
                /// <param name='lineCount'>
 
72
                /// The number of lines to get words from
 
73
                /// </param>
 
74
                public static List<ISegment> BreakLinesIntoWords (Document document, int startLine, int lineCount)
 
75
                {
 
76
                        var result = new List<ISegment> ();
 
77
                        for (int line = startLine; line < startLine + lineCount; line++) {
 
78
                                var lineSegment = document.GetLine (line);
 
79
                                int offset = lineSegment.Offset;
 
80
                                bool wasIdentifierPart = false;
 
81
                                int lastWordEnd = 0;
 
82
                                for (int i = 0; i < lineSegment.EditableLength; i++) {
 
83
                                        char ch = document.GetCharAt (offset + i);
 
84
                                        bool isIdentifierPart = char.IsLetterOrDigit (ch) || ch == '_';
 
85
                                        if (!isIdentifierPart) {
 
86
                                                if (wasIdentifierPart) {
 
87
                                                        result.Add (new Mono.TextEditor.Segment (offset + lastWordEnd, i - lastWordEnd));
 
88
                                                }
 
89
                                                result.Add (new Mono.TextEditor.Segment (offset + i, 1));
 
90
                                                lastWordEnd = i + 1;
 
91
                                        }
 
92
                                        wasIdentifierPart = isIdentifierPart;
 
93
                                }
 
94
                                
 
95
                                if (lastWordEnd != lineSegment.EditableLength) {
 
96
                                        result.Add (new Mono.TextEditor.Segment (offset + lastWordEnd, lineSegment.EditableLength - lastWordEnd));
 
97
                                }
 
98
                        }
 
99
                        
 
100
                        return result;
 
101
                }
 
102
        }
 
103
}
 
104