forked from KDE/kdiff3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentParser.cpp
More file actions
180 lines (160 loc) · 5.57 KB
/
CommentParser.cpp
File metadata and controls
180 lines (160 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* KDiff3 - Text Diff And Merge Tool
*
* SPDX-FileCopyrightText: 2019-2020 Michael Reeves reeves.87@gmail.com
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "CommentParser.h"
#include "TypeUtils.h"
#include <algorithm>
#include <QChar>
#include <QRegularExpression>
#include <QString>
void DefaultCommentParser::processChar(const QString &line, const QChar &inChar)
{
if(!bIsEscaped)
{
switch(inChar.unicode())
{
case '\\':
if(bInString)
bIsEscaped = true;
break;
case '\'':
case '"':
if(!inComment())
{
if(!bInString)
{
bInString = true;
mStartChar = inChar;
}
else if(mStartChar == inChar)
{
bInString = false;
}
}
break;
case '/':
if(bInString)
break;
if(!inComment() && mLastChar == '/')
{
mCommentType = singleLine;
mIsPureComment = mIsCommentOrWhite = line.startsWith(u8"//");
lastComment.startOffset = offset - 1;
if(lastComment.startOffset != 0) //whitespace at begining
{
mIsPureComment = false;
}
}
else if(mLastChar == '*' && mCommentType == multiLine)
{
//ending multi-line comment
mCommentType = none;
lastComment.endOffset = offset + 1; //include last char in offset
comments.push_back(lastComment);
if(!isFirstLine)
mIsPureComment = mIsCommentOrWhite = line.endsWith(u8"*/") ? true : mIsCommentOrWhite;
}
break;
case '*':
if(bInString)
break;
if(mLastChar == '/' && !inComment())
{
mCommentType = multiLine;
mIsPureComment = mIsCommentOrWhite = line.startsWith(u8"/*") ? true : mIsCommentOrWhite;
isFirstLine = true;
lastComment.startOffset = offset - 1;
if(lastComment.startOffset != 0) //whitespace at begining
{
mIsPureComment = false;
}
}
break;
case '\n':
if(mCommentType == singleLine)
{
mCommentType = none;
lastComment.endOffset = offset;
comments.push_back(lastComment);
}
if(mCommentType == multiLine && !isFirstLine)
{
mIsPureComment = mIsCommentOrWhite = true;
}
if(lastComment.startOffset > 0 && lastComment.endOffset == 0)
{
lastComment.endOffset = offset;
comments.push_back(lastComment);
}
isFirstLine = false;
break;
default:
if(inComment())
{
break;
}
mIsPureComment = mIsCommentOrWhite = false;
break;
}
mLastChar = inChar;
}
else
{
bIsEscaped = false;
mLastChar = QChar();
}
++offset;
};
/*
Find comments if any and set its pure comment flag if it has nothing but whitespace and comments.
*/
void DefaultCommentParser::processLine(const QString &line)
{
static const QRegularExpression nonWhiteRegexp = QRegularExpression("[\\S]", QRegularExpression::UseUnicodePropertiesOption);
static const QRegularExpression tailRegexp = QRegularExpression("\\s+$", QRegularExpression::UseUnicodePropertiesOption);
offset = line.indexOf(nonWhiteRegexp);
const QtNumberType trailIndex = line.lastIndexOf(tailRegexp);
lastComment.startOffset = lastComment.endOffset = 0; //reset these for each line
comments.clear();
//remove trailing and ending spaces.
const QString trimmedLine = line.trimmed();
for(const QChar &c : trimmedLine)
{
processChar(trimmedLine, c);
}
/*
Line has trailing space after multi-line comment ended.
*/
if(trailIndex != -1 && !inComment())
{
mIsPureComment = false;
}
//mIsPureComment = mIsPureComment && offset == 0;
processChar(trimmedLine, '\n');
}
/*
Modifies the input data, and replaces comments with whitespace when the line contains other data too.
*/
void DefaultCommentParser::removeComment(QString &line)
{
if(isSkipable() || lastComment.startOffset == lastComment.endOffset) return;
for(const CommentRange &range : comments)
{
/*
assert isn't useful during auto testing as it causes the QTest not to show the actual line that
the test failed on.
*/
#ifndef AUTOTEST
assert(range.endOffset <= line.length() && range.startOffset <= line.length());
assert(range.endOffset >= range.startOffset);
#else
if(range.endOffset > line.length() && range.startOffset > line.length()) return;
if(range.endOffset < range.startOffset) return;
#endif
QtSizeType size = range.endOffset - range.startOffset;
line.replace(range.startOffset, size, QString(" ").repeated(size));
}
}