FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/java/src/processing/mode/java/pdex/Rename.java at master · sunsure/processing · GitHub
sunsure
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
java
/
src
/
processing
/
mode
/
java
/
pdex
/
Rename.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
335 lines (280 loc) · 11.1 KB
Breadcrumbs
processing
/
java
/
src
/
processing
/
mode
/
java
/
pdex
/
Rename.java
Copy path
File metadata and controls
335 lines (280 loc) · 11.1 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package
processing
.
mode
.
java
.
pdex
;
import
java
.
awt
.
Dimension
;
import
java
.
awt
.
EventQueue
;
import
java
.
awt
.
event
.
ActionEvent
;
import
java
.
awt
.
event
.
ActionListener
;
import
java
.
awt
.
event
.
ComponentAdapter
;
import
java
.
awt
.
event
.
ComponentEvent
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
Collections
;
import
java
.
util
.
Comparator
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
stream
.
Collectors
;
import
javax
.
swing
.
Box
;
import
javax
.
swing
.
JButton
;
import
javax
.
swing
.
JDialog
;
import
javax
.
swing
.
JLabel
;
import
javax
.
swing
.
JMenuItem
;
import
javax
.
swing
.
JOptionPane
;
import
javax
.
swing
.
JRootPane
;
import
javax
.
swing
.
JTextField
;
import
javax
.
swing
.
WindowConstants
;
import
javax
.
swing
.
text
.
BadLocationException
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
ASTNode
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
CompilationUnit
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
IBinding
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
IMethodBinding
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
ITypeBinding
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
SimpleName
;
import
processing
.
app
.
Language
;
import
processing
.
app
.
Platform
;
import
processing
.
app
.
Sketch
;
import
processing
.
app
.
SketchCode
;
import
processing
.
app
.
syntax
.
SyntaxDocument
;
import
processing
.
app
.
ui
.
EditorStatus
;
import
processing
.
app
.
ui
.
Toolkit
;
import
processing
.
mode
.
java
.
JavaEditor
;
import
processing
.
mode
.
java
.
pdex
.
PreprocessedSketch
.
SketchInterval
;
import
static
processing
.
mode
.
java
.
pdex
.
ASTUtils
.
findAllOccurrences
;
import
static
processing
.
mode
.
java
.
pdex
.
ASTUtils
.
getSimpleNameAt
;
import
static
processing
.
mode
.
java
.
pdex
.
ASTUtils
.
resolveBinding
;
class
Rename
{
final
JavaEditor
editor
;
final
PreprocessingService
pps
;
final
ShowUsage
showUsage
;
final
JDialog
window
;
final
JTextField
textField
;
final
JLabel
oldNameLabel
;
IBinding
binding
;
PreprocessedSketch
ps
;
Rename
(
JavaEditor
editor
,
PreprocessingService
pps
,
ShowUsage
showUsage
) {
this
.
editor
=
editor
;
this
.
pps
=
pps
;
this
.
showUsage
=
showUsage
;
// Add rename option
JMenuItem
renameItem
=
new
JMenuItem
(
Language
.
text
(
"editor.popup.rename"
));
renameItem
.
addActionListener
(
e
->
handleRename
());
editor
.
getTextArea
().
getRightClickPopup
().
add
(
renameItem
);
window
=
new
JDialog
(
editor
);
JRootPane
rootPane
=
window
.
getRootPane
();
window
.
setTitle
(
"Rename"
);
window
.
setDefaultCloseOperation
(
WindowConstants
.
HIDE_ON_CLOSE
);
Toolkit
.
registerWindowCloseKeys
(
rootPane
,
new
ActionListener
() {
@
Override
public
void
actionPerformed
(
ActionEvent
e
) {
window
.
setVisible
(
false
);
}
});
Toolkit
.
setIcon
(
window
);
window
.
setModal
(
true
);
window
.
setResizable
(
false
);
window
.
addComponentListener
(
new
ComponentAdapter
() {
@
Override
public
void
componentHidden
(
ComponentEvent
e
) {
binding
=
null
;
ps
=
null
;
}
});
//window.setSize(Toolkit.zoom(250, 130));
//window.setLayout(new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));
Box
windowBox
=
Box
.
createVerticalBox
();
Toolkit
.
setBorder
(
windowBox
);
final
int
GAP
=
Toolkit
.
zoom
(
5
);
{
// old name
Box
oldBox
=
Box
.
createHorizontalBox
();
oldNameLabel
=
new
JLabel
(
"Current Name: "
);
oldBox
.
add
(
oldNameLabel
);
//oldBox.add(Box.createHorizontalStrut(10));
oldBox
.
add
(
Box
.
createHorizontalGlue
());
windowBox
.
add
(
oldBox
);
windowBox
.
add
(
Box
.
createVerticalStrut
(
GAP
));
}
{
// new name
Box
newBox
=
Box
.
createHorizontalBox
();
JLabel
newNameLabel
=
new
JLabel
(
"New Name: "
);
newBox
.
add
(
newNameLabel
);
newBox
.
add
(
textField
=
new
JTextField
(
20
));
newBox
.
add
(
Box
.
createHorizontalGlue
());
windowBox
.
add
(
newBox
);
windowBox
.
add
(
Box
.
createVerticalStrut
(
GAP
*
2
));
}
{
// button panel
JButton
showUsageButton
=
new
JButton
(
"Show Usage"
);
showUsageButton
.
addActionListener
(
e
-> {
showUsage
.
findUsageAndUpdateTree
(
ps
,
binding
);
window
.
setVisible
(
false
);
});
JButton
renameButton
=
new
JButton
(
"Rename"
);
renameButton
.
addActionListener
(
e
-> {
final
String
newName
=
textField
.
getText
().
trim
();
if
(!
newName
.
isEmpty
()) {
if
(
newName
.
length
() >=
1
&&
newName
.
chars
().
limit
(
1
).
allMatch
(
Character
::
isUnicodeIdentifierStart
) &&
newName
.
chars
().
skip
(
1
).
allMatch
(
Character
::
isUnicodeIdentifierPart
)) {
rename
(
ps
,
binding
,
newName
);
window
.
setVisible
(
false
);
}
else
{
String
msg
=
String
.
format
(
"'%s' is not a valid name"
,
newName
);
JOptionPane
.
showMessageDialog
(
editor
,
msg
,
"Naming is Hard"
,
JOptionPane
.
PLAIN_MESSAGE
);
}
}
});
rootPane
.
setDefaultButton
(
renameButton
);
//JPanel panelBottom = new JPanel();
//panelBottom.setLayout(new BoxLayout(panelBottom, BoxLayout.X_AXIS));
Box
buttonBox
=
Box
.
createHorizontalBox
();
//Toolkit.setBorder(panelBottom, 5, 5, 5, 5);
buttonBox
.
add
(
Box
.
createHorizontalGlue
());
buttonBox
.
add
(
showUsageButton
);
if
(!
Platform
.
isMacOS
()) {
buttonBox
.
add
(
Box
.
createHorizontalStrut
(
GAP
));
}
buttonBox
.
add
(
renameButton
);
buttonBox
.
add
(
Box
.
createHorizontalGlue
());
Dimension
showDim
=
showUsageButton
.
getPreferredSize
();
Dimension
renameDim
=
renameButton
.
getPreferredSize
();
final
int
niceSize
=
Math
.
max
(
showDim
.
width
,
renameDim
.
width
) +
GAP
;
final
Dimension
buttonDim
=
new
Dimension
(
niceSize
,
showDim
.
height
);
showUsageButton
.
setPreferredSize
(
buttonDim
);
renameButton
.
setPreferredSize
(
buttonDim
);
windowBox
.
add
(
buttonBox
);
//window.add(panelBottom);
}
window
.
add
(
windowBox
);
window
.
pack
();
//window.setMinimumSize(window.getSize());
}
// Thread: EDT
void
handleRename
() {
int
startOffset
=
editor
.
getSelectionStart
();
int
stopOffset
=
editor
.
getSelectionStop
();
int
tabIndex
=
editor
.
getSketch
().
getCurrentCodeIndex
();
pps
.
whenDoneBlocking
(
ps
->
handleRename
(
ps
,
tabIndex
,
startOffset
,
stopOffset
));
}
// Thread: worker
void
handleRename
(
PreprocessedSketch
ps
,
int
tabIndex
,
int
startTabOffset
,
int
stopTabOffset
) {
if
(
ps
.
hasSyntaxErrors
) {
editor
.
statusMessage
(
"Cannot rename until syntax errors are fixed"
,
EditorStatus
.
WARNING
);
return
;
}
ASTNode
root
=
ps
.
compilationUnit
;
// Map offsets
int
startJavaOffset
=
ps
.
tabOffsetToJavaOffset
(
tabIndex
,
startTabOffset
);
int
stopJavaOffset
=
ps
.
tabOffsetToJavaOffset
(
tabIndex
,
stopTabOffset
);
// Find the node
SimpleName
name
=
getSimpleNameAt
(
root
,
startJavaOffset
,
stopJavaOffset
);
if
(
name
==
null
) {
editor
.
statusMessage
(
"Highlight the class/function/variable name first"
,
EditorStatus
.
NOTICE
);
return
;
}
// Find binding
IBinding
binding
=
resolveBinding
(
name
);
if
(
binding
==
null
) {
editor
.
statusMessage
(
name
.
getIdentifier
() +
" isn't defined in this sketch, "
+
"so it cannot be renamed"
,
EditorStatus
.
ERROR
);
return
;
}
ASTNode
decl
=
ps
.
compilationUnit
.
findDeclaringNode
(
binding
.
getKey
());
if
(
decl
==
null
) {
editor
.
statusMessage
(
name
.
getIdentifier
() +
" isn't defined in this sketch, "
+
"so it cannot be renamed"
,
EditorStatus
.
ERROR
);
return
;
}
// Display the rename dialog
EventQueue
.
invokeLater
(() -> {
if
(!
window
.
isVisible
()) {
this
.
ps
=
ps
;
this
.
binding
=
binding
;
oldNameLabel
.
setText
(
"Current name: "
+
binding
.
getName
());
textField
.
setText
(
binding
.
getName
());
textField
.
requestFocus
();
textField
.
selectAll
();
int
x
=
editor
.
getX
() + (
editor
.
getWidth
() -
window
.
getWidth
()) /
2
;
int
y
=
editor
.
getY
() + (
editor
.
getHeight
() -
window
.
getHeight
()) /
2
;
window
.
setLocation
(
x
,
y
);
window
.
setVisible
(
true
);
window
.
toFront
();
}
});
}
// Thread: EDT (we can't allow user to mess with sketch while renaming)
void
rename
(
PreprocessedSketch
ps
,
IBinding
binding
,
String
newName
) {
CompilationUnit
root
=
ps
.
compilationUnit
;
// Renaming constructor should rename class
if
(
binding
.
getKind
() ==
IBinding
.
METHOD
) {
IMethodBinding
method
= (
IMethodBinding
)
binding
;
if
(
method
.
isConstructor
()) {
binding
=
method
.
getDeclaringClass
();
}
}
ASTNode
decl
=
root
.
findDeclaringNode
(
binding
.
getKey
());
if
(
decl
==
null
)
return
;
showUsage
.
hide
();
List
<
SimpleName
>
occurrences
=
new
ArrayList
<>();
occurrences
.
addAll
(
findAllOccurrences
(
root
,
binding
.
getKey
()));
// Renaming class should rename all constructors
if
(
binding
.
getKind
() ==
IBinding
.
TYPE
) {
ITypeBinding
type
= (
ITypeBinding
)
binding
;
//type = type.getErasure();
IMethodBinding
[]
methods
=
type
.
getDeclaredMethods
();
Arrays
.
stream
(
methods
)
.
filter
(
IMethodBinding
::
isConstructor
)
.
flatMap
(
c
->
findAllOccurrences
(
root
,
c
.
getKey
()).
stream
())
.
forEach
(
occurrences
::
add
);
}
Map
<
Integer
,
List
<
SketchInterval
>>
mappedNodes
=
occurrences
.
stream
()
.
map
(
ps
::
mapJavaToSketch
)
.
filter
(
ps
::
inRange
)
.
collect
(
Collectors
.
groupingBy
(
interval
->
interval
.
tabIndex
));
Sketch
sketch
=
ps
.
sketch
;
editor
.
startCompoundEdit
();
mappedNodes
.
entrySet
().
forEach
(
entry
-> {
int
tabIndex
=
entry
.
getKey
();
SketchCode
sketchCode
=
sketch
.
getCode
(
tabIndex
);
SyntaxDocument
document
= (
SyntaxDocument
)
sketchCode
.
getDocument
();
List
<
SketchInterval
>
nodes
=
entry
.
getValue
();
nodes
.
stream
()
// Replace from the end so all unprocess offsets stay valid
.
sorted
(
Comparator
.
comparing
((
SketchInterval
si
) ->
si
.
startTabOffset
).
reversed
())
.
forEach
(
si
-> {
// Make sure offsets are in bounds
int
documentLength
=
document
.
getLength
();
if
(
si
.
startTabOffset
>=
0
&&
si
.
startTabOffset
<=
documentLength
&&
si
.
stopTabOffset
>=
0
&&
si
.
stopTabOffset
<=
documentLength
) {
// Replace the code
int
length
=
si
.
stopTabOffset
-
si
.
startTabOffset
;
try
{
document
.
remove
(
si
.
startTabOffset
,
length
);
document
.
insertString
(
si
.
startTabOffset
,
newName
,
null
);
}
catch
(
BadLocationException
e
) {
/* Whatever */
}
}
});
try
{
sketchCode
.
setProgram
(
document
.
getText
(
0
,
document
.
getLength
()));
}
catch
(
BadLocationException
e
) {
/* Whatever */
}
sketchCode
.
setModified
(
true
);
});
editor
.
stopCompoundEdit
();
editor
.
repaintHeader
();
int
currentTabIndex
=
sketch
.
getCurrentCodeIndex
();
final
int
currentOffset
=
editor
.
getCaretOffset
();
int
precedingIntervals
=
(
int
)
mappedNodes
.
getOrDefault
(
currentTabIndex
,
Collections
.
emptyList
())
.
stream
()
.
filter
(
interval
->
interval
.
stopTabOffset
<
currentOffset
)
.
count
();
int
intervalLengthDiff
=
newName
.
length
() -
binding
.
getName
().
length
();
int
offsetDiff
=
precedingIntervals
*
intervalLengthDiff
;
editor
.
getTextArea
().
setCaretPosition
(
currentOffset
+
offsetDiff
);
}
void
dispose
() {
if
(
window
!=
null
) {
window
.
dispose
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL