Skip to content

Commit

Permalink
[Javac] basic support for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Nov 28, 2023
1 parent 63926c5 commit de8bb2d
Showing 1 changed file with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,36 @@
package org.eclipse.jdt.core.dom;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

import com.sun.tools.javac.comp.Todo;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.parser.JavadocTokenizer;
import com.sun.tools.javac.parser.Scanner;
import com.sun.tools.javac.parser.ScannerFactory;
import com.sun.tools.javac.parser.Tokens.Comment;
import com.sun.tools.javac.parser.Tokens.TokenKind;
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.util.Context;
Expand Down Expand Up @@ -120,7 +126,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws java.io.
ast.setDefaultNodeFlag(ASTNode.ORIGINAL);
JavacConverter converter = new JavacConverter(ast, javacCompilationUnit);
CompilationUnit res = converter.convertCompilationUnit(javacCompilationUnit);
buildCommentsTable();
buildCommentsTable(res, context, fileObject, converter, compilerOptions);
//
ast.setOriginalModificationCount(ast.modificationCount()); // "un-dirty" AST so Rewrite can process it
ast.setDefaultNodeFlag(savedDefaultNodeFlag);
Expand Down Expand Up @@ -181,40 +187,37 @@ protected Comment processComment(int pos, int endPos, CommentStyle style) {
}
}

/**
* Currently no-op as we don't have a case to both building
* comment table yet, but code in here is viable.
*/
private void buildCommentsTable() {
// try {
// char[] content = fileObject.getCharContent(false).toString().toCharArray();
// ScannerFactory scannerFactory = ScannerFactory.instance(context);
// JavadocTokenizerFeedingComments commentTokenizer = new JavadocTokenizerFeedingComments(scannerFactory, content, converter);
// Scanner javacScanner = new Scanner(scannerFactory, commentTokenizer) {
// // subclass just to access constructor
// // TODO DefaultCommentMapper.this.scanner.linePtr == -1?
// };
// do { // consume all tokens to populate comments
// javacScanner.nextToken();
// } while (javacScanner.token() != null && javacScanner.token().kind != TokenKind.EOF);
// commentTokenizer.comments.forEach(comment -> comment.setAlternateRoot(res));
// res.setCommentTable(commentTokenizer.comments.toArray(org.eclipse.jdt.core.dom.Comment[]::new));
// org.eclipse.jdt.internal.compiler.parser.Scanner ecjScanner = new ASTConverter(compilerOptions, false, null).scanner;
// ecjScanner.recordLineSeparator = true;
// ecjScanner.skipComments = false;
// try {
// ecjScanner.setSource(content);
// do {
// ecjScanner.getNextToken();
// } while (!ecjScanner.atEnd());
// } catch (InvalidInputException ex) {
// JavaCore.getPlugin().getLog().log(Status.error(ex.getMessage(), ex));
// }
// // need to scan with ecjScanner first to populate some line indexes used by the CommentMapper
// // on longer-term, implementing an alternative comment mapper based on javac scanner might be best
// res.initCommentMapper(ecjScanner);
// } catch (IOException ex) {
// throw new RuntimeException(ex);
// }
private void buildCommentsTable(CompilationUnit res, Context context, FileObject fileObject, JavacConverter converter, Map<String, String> compilerOptions) {
try {
char[] content = fileObject.getCharContent(false).toString().toCharArray();
ScannerFactory scannerFactory = ScannerFactory.instance(context);
JavadocTokenizerFeedingComments commentTokenizer = new JavadocTokenizerFeedingComments(scannerFactory, content, converter);
Scanner javacScanner = new Scanner(scannerFactory, commentTokenizer) {
// subclass just to access constructor
// TODO DefaultCommentMapper.this.scanner.linePtr == -1?
};
do { // consume all tokens to populate comments
javacScanner.nextToken();
} while (javacScanner.token() != null && javacScanner.token().kind != TokenKind.EOF);
commentTokenizer.comments.forEach(comment -> comment.setAlternateRoot(res));
res.setCommentTable(commentTokenizer.comments.toArray(org.eclipse.jdt.core.dom.Comment[]::new));
org.eclipse.jdt.internal.compiler.parser.Scanner ecjScanner = new ASTConverter(compilerOptions, false, null).scanner;
ecjScanner.recordLineSeparator = true;
ecjScanner.skipComments = false;
try {
ecjScanner.setSource(content);
do {
ecjScanner.getNextToken();
} while (!ecjScanner.atEnd());
} catch (InvalidInputException ex) {
JavaCore.getPlugin().getLog().log(Status.error(ex.getMessage(), ex));
}
// need to scan with ecjScanner first to populate some line indexes used by the CommentMapper
// on longer-term, implementing an alternative comment mapper based on javac scanner might be best
res.initCommentMapper(ecjScanner);
res.setCommentTable(commentTokenizer.comments.toArray(org.eclipse.jdt.core.dom.Comment[]::new)); // TODO only javadoc comments are in; need to add regular comments
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}

0 comments on commit de8bb2d

Please sign in to comment.