Sfall-ScriptEditor

Форк
0
199 строк · 8.5 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Drawing;
6

7
using ICSharpCode.TextEditor;
8
using ICSharpCode.TextEditor.Document;
9

10
using ScriptEditor.TextEditorUI;
11

12
namespace ScriptEditor.TextEditorUtilities
13
{
14
    internal static class MessageStructure
15
    {
16
        const int Open = 0, Close = 1;
17
        static char[] Curves = new char[] { '{', '}' };
18

19
        public static List<Error> CheckStructure(TextAreaControl TAC, string file)
20
        {
21
            TAC.Document.MarkerStrategy.RemoveAll(delegate(TextMarker tm) { return true; });
22
            
23
            int openCurve = 0, closeCurve = 0;
24
            
25
            int[] openOffsetCurve = new int[2];
26
            int closeOffsetCurve = 0;
27

28
            int offsetError = -1;
29
            int offsetOpen = -1;
30
            int openCount = 0;
31
            
32
            //bool errorOpenCurve = false, errorCloseCurve = false;
33
            bool only_once = false, new_line = false;
34

35
            List<int> numbersLine = new List<int>();
36

37
            List<int> warning_comment = new List<int>();
38
            List<Error> error_number = new List<Error>();
39
            List<Error> warning = new List<Error>();
40
            List<Error> report = new List<Error>();
41

42
            char[] chars = new char[] { '.', ',', '!', '?'};
43

44
            int len = TAC.Document.TextContent.Length;
45
            for (int offset = 0; offset < len; offset++)
46
	        {
47
                char ch = TAC.Document.TextContent[offset];
48
                if (ch == '\r' || ch == '\n') {
49
                    //Check whitespace on new line
50
                    if (!new_line && openCurve == 1 && closeCurve == 0 && openCount == 3) {
51
                        int i = -1;
52
                        bool ws = false;
53
                        do {
54
                            ws = (TAC.Document.TextContent[offset + i] == ' ');
55
                            i++;
56
                        } while (!ws && i < 3);
57
                        if (!ws)
58
                            warning.Add(new Error(ErrorType.Message, "New line the whitespace character is missing.", null, offset - 1));
59
                    }
60
                    new_line = true;
61
                    only_once = false;
62
                    continue;
63
                }
64

65
                if (ch == Curves[Open]) {
66
                    openOffsetCurve[1] = openOffsetCurve[0];
67
                    openOffsetCurve[0] = offset;
68
                    openCurve++;
69
                    if (closeCurve > 0)
70
                        closeCurve--;
71

72
                    openCount++;
73
                    if (openCount == 1) //записываем offset кавычки в первой группе
74
                        offsetOpen = offset + 1;
75

76
                } else if (ch == Curves[Close]) {
77
                    closeOffsetCurve = offset;
78
                    closeCurve++;
79
                    openCurve--;
80

81
                    if (openCount == 1) { //проверка корректности номера строки в первой группе кавычек
82
                        int ln = offset - offsetOpen;
83
                        string str = TAC.Document.GetText(offsetOpen, ln);
84
                        
85
                        if (str.Trim().Length > 0) {
86
                            int number;
87
                            if (int.TryParse(str, out number) && number > 0) {
88
                                if (numbersLine.Contains(number))
89
                                    error_number.Add(new Error(
90
                                                     ErrorType.Warning, "Duplicate message line number: " + number, null, offsetOpen, ln));
91
                                else
92
                                    numbersLine.Add(number);
93
                            } else
94
                                error_number.Add(new Error(
95
                                                 ErrorType.Error, "Invalid line number of the message.", null, offsetOpen, ln));
96
                        } else
97
                            error_number.Add(new Error(
98
                                                 ErrorType.Error, "Missing line number of the message.", null, offsetOpen, ln));
99

100
                    } else if (openCount == 3)
101
                        openCount = 0;
102
                }
103
     
104
                if (closeCurve > 1) {
105
                    //errorOpenCurve = true;
106
                    offsetError = closeOffsetCurve;
107
                    break;
108
                }
109
                if (openCurve > 1) {
110
                    //errorCloseCurve = true;
111
                    offsetError = openOffsetCurve[1];
112
                    break;
113
                } else if (openCurve < 0) {
114
                    //errorCloseCurve = true;
115
                    offsetError = closeOffsetCurve;
116
                    break;
117
                }
118

119
                //Check whitespace on punctuation
120
                if (openCurve == 1 && closeCurve == 0 && Utilities.IsAnyChar(ch, chars)) {
121
                    char chCheck = TAC.Document.TextContent[offset + 1];
122
                    if (char.IsLetter(chCheck))
123
                        warning.Add(new Error(ErrorType.Message, "The whitespace character is missing.", null, offset));
124

125
                    if (char.IsWhiteSpace(chCheck) && TAC.Document.TextContent[offset - 1] != chars[0] 
126
                        && TAC.Document.TextContent[offset] != chars[1] && char.IsLower(TAC.Document.TextContent, offset + 2))
127
                        warning.Add(new Error(ErrorType.Message, "The sentence begins with a small letter instead of the capital letter.", null, offset + 2));
128
                }
129

130
                // Comment check characters
131
                if (openCurve == 0 && closeCurve == 1) {
132
                    if (!only_once && !char.IsWhiteSpace(ch) && ch != Curves[Open] && ch != Curves[Close]) {
133
                        only_once = true;
134
                        if (ch != '#')
135
                            warning_comment.Add(offset);
136
                    }
137
                } else
138
                    only_once = false;
139

140
                new_line = false;
141
            }
142
            
143
            #region Build reporting error
144
            foreach (Error error in error_number) 
145
            {
146
                TextLocation tLoc =  TAC.Document.OffsetToPosition(error.line);
147
                if (error.column == 0) {
148
                    error.column = 2;
149
                    error.line--;
150
                }
151
                TextMarker tm = new TextMarker(error.line, error.column, TextMarkerType.WaveLine, Color.Red);
152
                tm.ToolTip = error.message;
153
                TAC.Document.MarkerStrategy.AddMarker(tm);
154

155
                report.Add(new Error(error.type, error.message, file, tLoc.Line + 1, tLoc.Column));
156
                if (report.Count == 1)
157
                    TAC.Caret.Position = tLoc;
158
            }
159

160
            if (offsetError > -1) {
161
                TextLocation tLoc =  TAC.Document.OffsetToPosition(offsetError);
162
                TAC.Caret.Position = tLoc;
163
                LineSegment ls = TAC.Document.GetLineSegment(tLoc.Line);
164
                TextMarker tm = new TextMarker(ls.Offset, ls.Length, TextMarkerType.WaveLine, Color.Red);
165
                tm.ToolTip = "Wrong structure of the paired curves brackets.";
166
                TAC.Document.MarkerStrategy.AddMarker(tm);
167

168
                report.Add(new Error(ErrorType.Error, tm.ToolTip, file, tLoc.Line + 1, 0));
169
            }
170

171
            foreach (Error error in warning) 
172
            {
173
                TextMarker tm = new TextMarker(error.line - 1, 3, TextMarkerType.WaveLine, Color.Blue);
174
                tm.ToolTip = error.message; //"The white space character is missing.";
175
                TAC.Document.MarkerStrategy.AddMarker(tm);
176

177
                TextLocation tl = TAC.Document.OffsetToPosition(error.line);
178
                report.Add(new Error(error.type, error.message, file, tl.Line + 1, tl.Column));
179
            }
180

181
            foreach (int offset in warning_comment) 
182
            {
183
                TextLocation tLoc =  TAC.Document.OffsetToPosition(offset);
184
                LineSegment ls = TAC.Document.GetLineSegment(tLoc.Line);
185
                int length = offset - ls.Offset;
186
                length = ls.Length - length;
187
                TextMarker tm = new TextMarker(offset, length, TextMarkerType.Underlined, Color.ForestGreen);
188
                tm.ToolTip = "Missing '#' character of comment?";
189
                TAC.Document.MarkerStrategy.AddMarker(tm);
190

191
                report.Add(new Error(ErrorType.None, tm.ToolTip, file, tLoc.Line + 1, tLoc.Column));
192
            }
193
            #endregion
194

195
            TAC.Refresh();
196
            return report;
197
        }
198
    }
199
}
200

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.