4848import org .eclipse .jdt .core .dom .MethodDeclaration ;
4949import org .eclipse .jdt .core .dom .Modifier ;
5050import org .eclipse .jdt .core .dom .ModuleDeclaration ;
51+ import org .eclipse .jdt .core .dom .NodeFinder ;
5152import org .eclipse .jdt .core .dom .RecordDeclaration ;
52- import org .eclipse .jdt .core .dom .SimpleName ;
5353import org .eclipse .jdt .core .dom .Statement ;
5454import org .eclipse .jdt .core .dom .SwitchExpression ;
5555import org .eclipse .jdt .core .dom .SwitchStatement ;
5656import org .eclipse .jdt .core .dom .TryStatement ;
5757import org .eclipse .jdt .core .dom .WhileStatement ;
5858
5959import org .eclipse .jdt .internal .corext .dom .IASTSharedValues ;
60- import org .eclipse .jdt .internal .corext .refactoring .structure .ASTNodeSearchUtil ;
6160
6261import org .eclipse .jdt .ui .JavaUI ;
6362
@@ -78,10 +77,8 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
7877 StyledText textWidget = sourceViewer .getTextWidget ();
7978 ICompilationUnit unit = null ;
8079 int textWidgetLineNumber = mapLineNumberToWidget (sourceViewer , lineNumber );
81- // int originalWidgetLineNumber= textWidgetLineNumber;
8280 int startIndentation = 0 ;
8381 String line = textWidget .getLine (textWidgetLineNumber );
84- System .out .println ("line is " + line ); //$NON-NLS-1$
8582 try {
8683 startIndentation = getIndentation (line );
8784 while (startIndentation == IGNORE_LINE_INDENTATION ) {
@@ -90,7 +87,6 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
9087 break ;
9188 }
9289 line = textWidget .getLine (textWidgetLineNumber );
93- System .out .println ("replacement line is " + line ); //$NON-NLS-1$
9490 startIndentation = getIndentation (line );
9591 }
9692 } catch (IllegalArgumentException e ) {
@@ -99,10 +95,6 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
9995
10096 if (textWidgetLineNumber > 0 ) {
10197 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$
10698
10799 ASTNode node = null ;
108100 if (typeRoot != null ) {
@@ -112,40 +104,22 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
112104 unit = typeRoot .getWorkingCopy (workingCopyOwner , null );
113105 if (unit != null ) {
114106 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 );
124108 while (node == null && textWidgetLineNumber > 0 ) {
125109 line = textWidget .getLine (--textWidgetLineNumber );
126- System .out .println ("node null line is " + line ); //$NON-NLS-1$
127110 startIndentation = getIndentation (line );
128111 while (startIndentation == IGNORE_LINE_INDENTATION && textWidgetLineNumber > 0 ) {
129112 line = textWidget .getLine (--textWidgetLineNumber );
130- System .out .println ("node null replacement line is " + line ); //$NON-NLS-1$
131113 startIndentation = getIndentation (line );
132114 }
133115 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$
139116 int position = cu .getPosition (textWidgetLineNumber + 1 , startIndentation );
140117 if (position >= 0 ) {
141- node = ASTNodeSearchUtil . getAstNode (cu , cu . getPosition ( textWidgetLineNumber +1 , startIndentation ), line . length () - startIndentation );
118+ node = getASTNode (cu , textWidgetLineNumber +1 , line );
142119 }
143120 }
144121 }
145122 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$
149123 boolean addStickyLine = false ;
150124 int nodeLineNumber = 0 ;
151125 while (node != null ) {
@@ -156,7 +130,6 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
156130 case ASTNode .ENUM_DECLARATION :
157131 addStickyLine = true ;
158132 ASTNode name = ((AbstractTypeDeclaration )node ).getName ();
159- System .out .println (((SimpleName )name ).getFullyQualifiedName ());
160133 nodeLineNumber = cu .getLineNumber (name .getStartPosition ());
161134 break ;
162135 case ASTNode .METHOD_DECLARATION :
@@ -184,20 +157,17 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
184157 IfStatement ifStmt = (IfStatement )node ;
185158 ASTNode ifExpression = ifStmt .getExpression ();
186159 nodeLineNumber = cu .getLineNumber (ifExpression .getStartPosition ());
187- System .out .println ("if stmt is " + textWidget .getLine (nodeLineNumber - 1 )); //$NON-NLS-1$
188160 Statement elseStmt = ifStmt .getElseStatement ();
189161 if (elseStmt != null ) {
190162 int elseLine = cu .getLineNumber (elseStmt .getStartPosition ());
191163 if (elseLine <= textWidgetLineNumber + 1 ) {
192164 Pattern p = ELSE_PATTERN ;
193165 nodeLineNumber = elseLine ;
194166 String stmtLine = textWidget .getLine (nodeLineNumber - 1 );
195- System .out .println ("else line is " + stmtLine ); //$NON-NLS-1$
196167 Matcher m = p .matcher (stmtLine );
197168 while (!m .find () && nodeLineNumber > 1 ) {
198169 nodeLineNumber --;
199170 stmtLine = textWidget .getLine (nodeLineNumber - 1 );
200- System .out .println ("next else line is " + stmtLine ); //$NON-NLS-1$
201171 m = p .matcher (stmtLine );
202172 }
203173 node = node .getParent ();
@@ -232,7 +202,6 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
232202 case ASTNode .TRY_STATEMENT :
233203 case ASTNode .ANONYMOUS_CLASS_DECLARATION :
234204 addStickyLine = true ;
235- String checkString = "" ; //$NON-NLS-1$
236205 ASTNode bodyProperty = null ;
237206 Pattern pattern = null ;
238207 if (node .getNodeType () == ASTNode .DO_STATEMENT ) {
@@ -248,7 +217,7 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
248217 bodyProperty = (ASTNode ) ((AnonymousClassDeclaration )node ).bodyDeclarations ().get (0 );
249218 pattern = NEW_PATTERN ;
250219 }
251- if (bodyProperty != null ) {
220+ if (bodyProperty != null && pattern != null ) {
252221 nodeLineNumber = cu .getLineNumber (bodyProperty .getStartPosition ());
253222 String stmtLine = textWidget .getLine (nodeLineNumber - 1 );
254223 Matcher m = pattern .matcher (stmtLine );
@@ -269,18 +238,15 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
269238 break ;
270239 }
271240 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 ));
275242 }
276243 if (node .getNodeType () == ASTNode .MODIFIER ) {
277244 Modifier modifier = (Modifier )node ;
278245 startIndentation += modifier .getLength ();
279- node = ASTNodeSearchUtil . getAstNode (cu , cu . getPosition ( textWidgetLineNumber +1 , startIndentation ), line . length () - startIndentation );
246+ node = getASTNode (cu , textWidgetLineNumber +1 , line );
280247 } else {
281248 node = node .getParent ();
282249 }
283- System .out .println ("parent node is " + (node != null ? node .getClass () : node )); //$NON-NLS-1$
284250 }
285251 }
286252 }
@@ -295,6 +261,15 @@ public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumb
295261 return stickyLines ;
296262 }
297263
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+
298273 public static ITypeRoot getJavaInput (IEditorPart part ) {
299274 IEditorInput editorInput = part .getEditorInput ();
300275 if (editorInput != null ) {
0 commit comments