48
48
import org .eclipse .jdt .core .dom .MethodDeclaration ;
49
49
import org .eclipse .jdt .core .dom .Modifier ;
50
50
import org .eclipse .jdt .core .dom .ModuleDeclaration ;
51
+ import org .eclipse .jdt .core .dom .NodeFinder ;
51
52
import org .eclipse .jdt .core .dom .RecordDeclaration ;
52
- import org .eclipse .jdt .core .dom .SimpleName ;
53
53
import org .eclipse .jdt .core .dom .Statement ;
54
54
import org .eclipse .jdt .core .dom .SwitchExpression ;
55
55
import org .eclipse .jdt .core .dom .SwitchStatement ;
56
56
import org .eclipse .jdt .core .dom .TryStatement ;
57
57
import org .eclipse .jdt .core .dom .WhileStatement ;
58
58
59
59
import org .eclipse .jdt .internal .corext .dom .IASTSharedValues ;
60
- import org .eclipse .jdt .internal .corext .refactoring .structure .ASTNodeSearchUtil ;
61
60
62
61
import org .eclipse .jdt .ui .JavaUI ;
63
62
@@ -78,10 +77,8 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
78
77
StyledText textWidget = sourceViewer .getTextWidget ();
79
78
ICompilationUnit unit = null ;
80
79
int textWidgetLineNumber = mapLineNumberToWidget (sourceViewer , lineNumber );
81
- // int originalWidgetLineNumber= textWidgetLineNumber;
82
80
int startIndentation = 0 ;
83
81
String line = textWidget .getLine (textWidgetLineNumber );
84
- System .out .println ("line is " + line ); //$NON-NLS-1$
85
82
try {
86
83
startIndentation = getIndentation (line );
87
84
while (startIndentation == IGNORE_LINE_INDENTATION ) {
@@ -90,7 +87,6 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
90
87
break ;
91
88
}
92
89
line = textWidget .getLine (textWidgetLineNumber );
93
- System .out .println ("replacement line is " + line ); //$NON-NLS-1$
94
90
startIndentation = getIndentation (line );
95
91
}
96
92
} catch (IllegalArgumentException e ) {
@@ -99,10 +95,6 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
99
95
100
96
if (textWidgetLineNumber > 0 ) {
101
97
ITypeRoot typeRoot = getJavaInput (javaEditor );
102
- // System.out.println("line is " + line); //$NON-NLS-1$
103
- // System.out.println("line2 is " + textWidget.getLine(textWidgetLineNumber)); //$NON-NLS-1$
104
- // System.out.println("line number is " + lineNumber); //$NON-NLS-1$
105
- // System.out.println("textWidgetLineNumber is " + textWidgetLineNumber); //$NON-NLS-1$
106
98
107
99
ASTNode node = null ;
108
100
if (typeRoot != null ) {
@@ -112,40 +104,22 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
112
104
unit = typeRoot .getWorkingCopy (workingCopyOwner , null );
113
105
if (unit != null ) {
114
106
CompilationUnit cu = convertICompilationUnitToCompilationUnit (unit );
115
- // int originalPosition= cu.getPosition(originalWidgetLineNumber + 1, 0);
116
- // System.out.println(textWidgetLineNumber + " " + startIndentation); //$NON-NLS-1$
117
- int mapNumber = mapWidgetToLineNumber (sourceViewer , textWidgetLineNumber );
118
- System .out .println ("map number is " + mapNumber ); //$NON-NLS-1$
119
- System .out .println ("start indentation is " + startIndentation ); //$NON-NLS-1$
120
- System .out .println ("line is " + line ); //$NON-NLS-1$
121
- System .out .println ("cu position is " + cu .getPosition (mapNumber +1 ,startIndentation )); //$NON-NLS-1$
122
- node = ASTNodeSearchUtil .getAstNode (cu , cu .getPosition (mapWidgetToLineNumber (sourceViewer , textWidgetLineNumber +1 ), startIndentation ), line .length () - startIndentation );
123
- // System.out.println("node found is " + (node != null ? node.getClass() : node)); //$NON-NLS-1$
107
+ node = getASTNode (cu , textWidgetLineNumber +1 , line );
124
108
while (node == null && textWidgetLineNumber > 0 ) {
125
109
line = textWidget .getLine (--textWidgetLineNumber );
126
- System .out .println ("node null line is " + line ); //$NON-NLS-1$
127
110
startIndentation = getIndentation (line );
128
111
while (startIndentation == IGNORE_LINE_INDENTATION && textWidgetLineNumber > 0 ) {
129
112
line = textWidget .getLine (--textWidgetLineNumber );
130
- System .out .println ("node null replacement line is " + line ); //$NON-NLS-1$
131
113
startIndentation = getIndentation (line );
132
114
}
133
115
if (textWidgetLineNumber > 0 ) {
134
- System .out .println ("line is " + line ); //$NON-NLS-1$
135
- System .out .println ("mapped line is " + mapWidgetToLineNumber (sourceViewer , textWidgetLineNumber +1 )); //$NON-NLS-1$
136
- System .out .println ("start indentation is " + startIndentation ); //$NON-NLS-1$
137
- System .out .println ("line length is " + line .length ()); //$NON-NLS-1$
138
- System .out .println ("position is " + cu .getPosition (textWidgetLineNumber + 1 , startIndentation )); //$NON-NLS-1$
139
116
int position = cu .getPosition (textWidgetLineNumber + 1 , startIndentation );
140
117
if (position >= 0 ) {
141
- node = ASTNodeSearchUtil . getAstNode (cu , cu . getPosition ( textWidgetLineNumber +1 , startIndentation ), line . length () - startIndentation );
118
+ node = getASTNode (cu , textWidgetLineNumber +1 , line );
142
119
}
143
120
}
144
121
}
145
122
if (node != null ) {
146
- // node= node.getParent();
147
- System .out .println ("node is " + node .getClass ()); //$NON-NLS-1$
148
- System .out .println ("node start position is " + node .getStartPosition ()); //$NON-NLS-1$
149
123
boolean addStickyLine = false ;
150
124
int nodeLineNumber = 0 ;
151
125
while (node != null ) {
@@ -156,7 +130,6 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
156
130
case ASTNode .ENUM_DECLARATION :
157
131
addStickyLine = true ;
158
132
ASTNode name = ((AbstractTypeDeclaration )node ).getName ();
159
- System .out .println (((SimpleName )name ).getFullyQualifiedName ());
160
133
nodeLineNumber = cu .getLineNumber (name .getStartPosition ());
161
134
break ;
162
135
case ASTNode .METHOD_DECLARATION :
@@ -184,20 +157,17 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
184
157
IfStatement ifStmt = (IfStatement )node ;
185
158
ASTNode ifExpression = ifStmt .getExpression ();
186
159
nodeLineNumber = cu .getLineNumber (ifExpression .getStartPosition ());
187
- System .out .println ("if stmt is " + textWidget .getLine (nodeLineNumber - 1 )); //$NON-NLS-1$
188
160
Statement elseStmt = ifStmt .getElseStatement ();
189
161
if (elseStmt != null ) {
190
162
int elseLine = cu .getLineNumber (elseStmt .getStartPosition ());
191
163
if (elseLine <= textWidgetLineNumber + 1 ) {
192
164
Pattern p = ELSE_PATTERN ;
193
165
nodeLineNumber = elseLine ;
194
166
String stmtLine = textWidget .getLine (nodeLineNumber - 1 );
195
- System .out .println ("else line is " + stmtLine ); //$NON-NLS-1$
196
167
Matcher m = p .matcher (stmtLine );
197
168
while (!m .find () && nodeLineNumber > 1 ) {
198
169
nodeLineNumber --;
199
170
stmtLine = textWidget .getLine (nodeLineNumber - 1 );
200
- System .out .println ("next else line is " + stmtLine ); //$NON-NLS-1$
201
171
m = p .matcher (stmtLine );
202
172
}
203
173
node = node .getParent ();
@@ -232,7 +202,6 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
232
202
case ASTNode .TRY_STATEMENT :
233
203
case ASTNode .ANONYMOUS_CLASS_DECLARATION :
234
204
addStickyLine = true ;
235
- String checkString = "" ; //$NON-NLS-1$
236
205
ASTNode bodyProperty = null ;
237
206
Pattern pattern = null ;
238
207
if (node .getNodeType () == ASTNode .DO_STATEMENT ) {
@@ -248,7 +217,7 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
248
217
bodyProperty = (ASTNode ) ((AnonymousClassDeclaration )node ).bodyDeclarations ().get (0 );
249
218
pattern = NEW_PATTERN ;
250
219
}
251
- if (bodyProperty != null ) {
220
+ if (bodyProperty != null && pattern != null ) {
252
221
nodeLineNumber = cu .getLineNumber (bodyProperty .getStartPosition ());
253
222
String stmtLine = textWidget .getLine (nodeLineNumber - 1 );
254
223
Matcher m = pattern .matcher (stmtLine );
@@ -269,18 +238,15 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
269
238
break ;
270
239
}
271
240
if (addStickyLine ) {
272
- System .out .println ("adding sticky line" ); //$NON-NLS-1$
273
- System .out .println ("line number is " + mapLineNumberToWidget (sourceViewer , nodeLineNumber - 1 )); //$NON-NLS-1$
274
- stickyLines .addFirst (new StickyLine (mapLineNumberToWidget (sourceViewer , nodeLineNumber - 1 ), sourceViewer ));
241
+ stickyLines .addFirst (new StickyLine (mapWidgetToLineNumber (sourceViewer , nodeLineNumber - 1 ), sourceViewer ));
275
242
}
276
243
if (node .getNodeType () == ASTNode .MODIFIER ) {
277
244
Modifier modifier = (Modifier )node ;
278
245
startIndentation += modifier .getLength ();
279
- node = ASTNodeSearchUtil . getAstNode (cu , cu . getPosition ( textWidgetLineNumber +1 , startIndentation ), line . length () - startIndentation );
246
+ node = getASTNode (cu , textWidgetLineNumber +1 , line );
280
247
} else {
281
248
node = node .getParent ();
282
249
}
283
- System .out .println ("parent node is " + (node != null ? node .getClass () : node )); //$NON-NLS-1$
284
250
}
285
251
}
286
252
}
@@ -295,6 +261,15 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
295
261
return stickyLines ;
296
262
}
297
263
264
+ private ASTNode getASTNode (CompilationUnit cu , int lineNum , String line ) {
265
+ int linePos = cu .getPosition (lineNum , 0 );
266
+ if (linePos >= 0 ) {
267
+ NodeFinder finder = new NodeFinder (cu , linePos , line .length ());
268
+ return finder .getCoveringNode ();
269
+ }
270
+ return null ;
271
+ }
272
+
298
273
public static ITypeRoot getJavaInput (IEditorPart part ) {
299
274
IEditorInput editorInput = part .getEditorInput ();
300
275
if (editorInput != null ) {
0 commit comments