5
// IBBoard <dev@ibboard.co.uk>
7
// Copyright (c) 2011 IBBoard
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
27
using System.Collections.Generic;
29
namespace Mono.TextEditor.Utils
32
/// A utility class for breaking up the text in TextEditors
34
public class TextBreaker
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)
41
/// The list of segments representing the 'words' in the lines
43
/// <param name='editor'>
44
/// The text editor to get the words from
46
/// <param name='startLine'>
47
/// The first line in the editor's documents to get the words from
49
/// <param name='lineCount'>
50
/// The number of lines to get words from
52
public static List<ISegment> BreakLinesIntoWords (TextEditor editor, int startLine, int lineCount)
54
return BreakLinesIntoWords (editor.Document, startLine, lineCount);
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)
63
/// The list of segments representing the 'words' in the lines
65
/// <param name='document'>
66
/// The document to get the words from
68
/// <param name='startLine'>
69
/// The first line in the documents to get the words from
71
/// <param name='lineCount'>
72
/// The number of lines to get words from
74
public static List<ISegment> BreakLinesIntoWords (Document document, int startLine, int lineCount)
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;
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));
89
result.Add (new Mono.TextEditor.Segment (offset + i, 1));
92
wasIdentifierPart = isIdentifierPart;
95
if (lastWordEnd != lineSegment.EditableLength) {
96
result.Add (new Mono.TextEditor.Segment (offset + lastWordEnd, lineSegment.EditableLength - lastWordEnd));