1 | // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.IO; |
---|
22 | |
---|
23 | using ICSharpCode.AvalonEdit.Document; |
---|
24 | |
---|
25 | namespace ICSharpCode.AvalonEdit.Indentation.CSharp |
---|
26 | { |
---|
27 | /// <summary> |
---|
28 | /// Interface used for the indentation class to access the document. |
---|
29 | /// </summary> |
---|
30 | public interface IDocumentAccessor |
---|
31 | { |
---|
32 | /// <summary>Gets if the current line is read only (because it is not in the |
---|
33 | /// selected text region)</summary> |
---|
34 | bool IsReadOnly { get; } |
---|
35 | /// <summary>Gets the number of the current line.</summary> |
---|
36 | int LineNumber { get; } |
---|
37 | /// <summary>Gets/Sets the text of the current line.</summary> |
---|
38 | string Text { get; set; } |
---|
39 | /// <summary>Advances to the next line.</summary> |
---|
40 | bool MoveNext(); |
---|
41 | } |
---|
42 | |
---|
43 | #region TextDocumentAccessor |
---|
44 | /// <summary> |
---|
45 | /// Adapter IDocumentAccessor -> TextDocument |
---|
46 | /// </summary> |
---|
47 | public sealed class TextDocumentAccessor : IDocumentAccessor |
---|
48 | { |
---|
49 | readonly TextDocument doc; |
---|
50 | readonly int minLine; |
---|
51 | readonly int maxLine; |
---|
52 | |
---|
53 | /// <summary> |
---|
54 | /// Creates a new TextDocumentAccessor. |
---|
55 | /// </summary> |
---|
56 | public TextDocumentAccessor(TextDocument document) |
---|
57 | { |
---|
58 | if (document == null) |
---|
59 | throw new ArgumentNullException("document"); |
---|
60 | doc = document; |
---|
61 | this.minLine = 1; |
---|
62 | this.maxLine = doc.LineCount; |
---|
63 | } |
---|
64 | |
---|
65 | /// <summary> |
---|
66 | /// Creates a new TextDocumentAccessor that indents only a part of the document. |
---|
67 | /// </summary> |
---|
68 | public TextDocumentAccessor(TextDocument document, int minLine, int maxLine) |
---|
69 | { |
---|
70 | if (document == null) |
---|
71 | throw new ArgumentNullException("document"); |
---|
72 | doc = document; |
---|
73 | this.minLine = minLine; |
---|
74 | this.maxLine = maxLine; |
---|
75 | } |
---|
76 | |
---|
77 | int num; |
---|
78 | string text; |
---|
79 | DocumentLine line; |
---|
80 | |
---|
81 | /// <inheritdoc/> |
---|
82 | public bool IsReadOnly { |
---|
83 | get { |
---|
84 | return num < minLine; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /// <inheritdoc/> |
---|
89 | public int LineNumber { |
---|
90 | get { |
---|
91 | return num; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | bool lineDirty; |
---|
96 | |
---|
97 | /// <inheritdoc/> |
---|
98 | public string Text { |
---|
99 | get { return text; } |
---|
100 | set { |
---|
101 | if (num < minLine) return; |
---|
102 | text = value; |
---|
103 | lineDirty = true; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /// <inheritdoc/> |
---|
108 | public bool MoveNext() |
---|
109 | { |
---|
110 | if (lineDirty) { |
---|
111 | doc.Replace(line, text); |
---|
112 | lineDirty = false; |
---|
113 | } |
---|
114 | ++num; |
---|
115 | if (num > maxLine) return false; |
---|
116 | line = doc.GetLineByNumber(num); |
---|
117 | text = doc.GetText(line); |
---|
118 | return true; |
---|
119 | } |
---|
120 | } |
---|
121 | #endregion |
---|
122 | } |
---|