FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/java/src/processing/mode/java/debug/LineID.java at patch-1 · Stefterv/processing · GitHub
Stefterv
processing
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
java
/
src
/
processing
/
mode
/
java
/
debug
/
LineID.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
277 lines (233 loc) · 7.6 KB
Breadcrumbs
processing
/
java
/
src
/
processing
/
mode
/
java
/
debug
/
LineID.java
Copy path
File metadata and controls
277 lines (233 loc) · 7.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-16 The Processing Foundation
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package
processing
.
mode
.
java
.
debug
;
import
java
.
util
.
HashSet
;
import
java
.
util
.
Set
;
import
javax
.
swing
.
event
.
DocumentEvent
;
import
javax
.
swing
.
event
.
DocumentListener
;
import
javax
.
swing
.
text
.
BadLocationException
;
import
javax
.
swing
.
text
.
Document
;
import
javax
.
swing
.
text
.
Element
;
import
javax
.
swing
.
text
.
Position
;
import
processing
.
app
.
Messages
;
/**
* Describes an ID for a code line. Comprised of a file name and a (0-based)
* line number. Can track changes to the line number due to text editing by
* attaching a {@link Document}. Registered {@link LineListener}s are notified
* of changes to the line number.
*/
public
class
LineID
implements
DocumentListener
{
protected
String
fileName
;
// the filename
protected
int
lineIdx
;
// the line number, 0-based
protected
Document
doc
;
// the Document to use for line number tracking
protected
Position
pos
;
// the Position acquired during line number tracking
protected
Set
<
LineHighlight
>
listeners
=
new
HashSet
<
LineHighlight
>();
// listeners for line number changes
public
LineID
(
String
fileName
,
int
lineIdx
) {
this
.
fileName
=
fileName
;
this
.
lineIdx
=
lineIdx
;
}
/**
* Get the file name of this line.
*
* @return the file name
*/
public
String
fileName
() {
return
fileName
;
}
/**
* Get the (0-based) line number of this line.
*
* @return the line index (i.e. line number, starting at 0)
*/
public
synchronized
int
lineIdx
() {
return
lineIdx
;
}
@
Override
public
int
hashCode
() {
return
toString
().
hashCode
();
}
/**
* Test whether this {@link LineID} is equal to another object. Two
* {@link LineID}'s are equal when both their fileName and lineNo are equal.
*
* @param obj the object to test for equality
* @return {@code true} if equal
*/
@
Override
public
boolean
equals
(
Object
obj
) {
if
(
obj
==
null
) {
return
false
;
}
if
(
getClass
() !=
obj
.
getClass
()) {
return
false
;
}
final
LineID
other
= (
LineID
)
obj
;
if
((
this
.
fileName
==
null
) ? (
other
.
fileName
!=
null
) : !
this
.
fileName
.
equals
(
other
.
fileName
)) {
return
false
;
}
if
(
this
.
lineIdx
!=
other
.
lineIdx
) {
return
false
;
}
return
true
;
}
/**
* Output a string representation in the form fileName:lineIdx+1. Note this
* uses a 1-based line number as is customary for human-readable line
* numbers.
*
* @return the string representation of this line ID
*/
@
Override
public
String
toString
() {
return
fileName
+
":"
+ (
lineIdx
+
1
);
}
/**
* Attach a {@link Document} to enable line number tracking when editing.
* The position to track is before the first non-whitespace character on the
* line. Edits happening before that position will cause the line number to
* update accordingly. Multiple {@link #startTracking} calls will replace
* the tracked document. Whoever wants a tracked line should track it and
* add itself as listener if necessary.
* ({@link LineHighlight}, {@link LineBreakpoint})
*
* @param doc the {@link Document} to use for line number tracking
*/
public
synchronized
void
startTracking
(
Document
doc
) {
//System.out.println("tracking: " + this);
if
(
doc
==
null
) {
return
;
// null arg
}
if
(
doc
==
this
.
doc
) {
return
;
// already tracking that doc
}
try
{
Element
line
=
doc
.
getDefaultRootElement
().
getElement
(
lineIdx
);
if
(
line
==
null
) {
return
;
// line doesn't exist
}
String
lineText
=
doc
.
getText
(
line
.
getStartOffset
(),
line
.
getEndOffset
() -
line
.
getStartOffset
());
// set tracking position at (=before) first non-white space character on line,
// or, if the line consists of entirely white spaces, just before the newline
// character
pos
=
doc
.
createPosition
(
line
.
getStartOffset
() +
nonWhiteSpaceOffset
(
lineText
));
this
.
doc
=
doc
;
doc
.
addDocumentListener
(
this
);
}
catch
(
BadLocationException
ex
) {
Messages
.
loge
(
null
,
ex
);
pos
=
null
;
this
.
doc
=
null
;
}
}
/**
* Notify this {@link LineID} that it is no longer in use. Will stop
* position tracking. Call this when this {@link LineID} is no longer
* needed.
*/
public
synchronized
void
stopTracking
() {
if
(
doc
!=
null
) {
doc
.
removeDocumentListener
(
this
);
doc
=
null
;
}
}
/**
* Update the tracked position. Will notify listeners if line number has
* changed.
*/
protected
synchronized
void
updatePosition
() {
if
(
doc
!=
null
&&
pos
!=
null
) {
// track position
int
offset
=
pos
.
getOffset
();
int
oldLineIdx
=
lineIdx
;
lineIdx
=
doc
.
getDefaultRootElement
().
getElementIndex
(
offset
);
// offset to lineNo
if
(
lineIdx
!=
oldLineIdx
) {
for
(
LineHighlight
l
:
listeners
) {
if
(
l
!=
null
) {
l
.
lineChanged
(
this
,
oldLineIdx
,
lineIdx
);
}
else
{
listeners
.
remove
(
l
);
// remove null listener
}
}
}
}
}
/**
* Add listener to be notified when the line number changes.
*
* @param l the listener to add
*/
public
void
addListener
(
LineHighlight
l
) {
listeners
.
add
(
l
);
}
/**
* Remove a listener for line number changes.
*
* @param l the listener to remove
*/
public
void
removeListener
(
LineHighlight
l
) {
listeners
.
remove
(
l
);
}
/**
* Calculate the offset of the first non-whitespace character in a string.
* @param str the string to examine
* @return offset of first non-whitespace character in str
*/
protected
static
int
nonWhiteSpaceOffset
(
String
str
) {
for
(
int
i
=
0
;
i
<
str
.
length
();
i
++) {
if
(!
Character
.
isWhitespace
(
str
.
charAt
(
i
))) {
return
i
;
}
}
// If we've reached here, that implies the line consists of purely white
// space. So return at a position just after the whitespace (i.e.,
// just before the newline).
//
// The " - 1" part resolves issue #3552
return
str
.
length
() -
1
;
}
/**
* Called when the {@link Document} registered using {@link #startTracking}
* is edited. This happens when text is inserted or removed.
*/
protected
void
editEvent
(
DocumentEvent
de
) {
//System.out.println("document edit @ " + de.getOffset());
if
(
de
.
getOffset
() <=
pos
.
getOffset
()) {
updatePosition
();
//System.out.println("updating, new line no: " + lineNo);
}
}
/**
* {@link DocumentListener} callback. Called when text is inserted.
*/
@
Override
public
void
insertUpdate
(
DocumentEvent
de
) {
editEvent
(
de
);
}
/**
* {@link DocumentListener} callback. Called when text is removed.
*/
@
Override
public
void
removeUpdate
(
DocumentEvent
de
) {
editEvent
(
de
);
}
/**
* {@link DocumentListener} callback. Called when attributes are changed.
* Not used.
*/
@
Override
public
void
changedUpdate
(
DocumentEvent
de
) {
// not needed.
}
}
Back
|
FazBrowse Home
|
New Git URL