Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/org/javacomp/completion/CompletionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static Builder builder() {
* the cached completion.
*/
boolean isIncrementalCompletion(Path filePath, int line, int column, String prefix) {
if(prefix.length()==0){
return false;
}
if (!getFilePath().equals(filePath)) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/javacomp/completion/Completor.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public CompletionResult getCompletionResult(
Optional<PositionContext> positionContext =
PositionContext.createForPosition(moduleManager, filePath, line, contextColumn);

logger.fine("filePath: %s, line: %s, contextColumn: %s", filePath, line, contextColumn );

if (!positionContext.isPresent()) {
return CompletionResult.builder()
.setCompletionCandidates(ImmutableList.of())
Expand All @@ -79,8 +81,10 @@ public CompletionResult getCompletionResult(
ContentWithLineMap contentWithLineMap =
ContentWithLineMap.create(positionContext.get().getFileScope(), fileManager, filePath);
String prefix = contentWithLineMap.extractCompletionPrefix(line, column);
logger.fine("prefix:%s", prefix);
// TODO: limit the number of the candidates.
if (cachedCompletion.isIncrementalCompletion(filePath, line, column, prefix)) {
logger.fine("using cache");
return getCompletionCandidatesFromCache(line, column, prefix);
} else {
cachedCompletion =
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/javacomp/completion/ContentWithLineMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract class ContentWithLineMap {
/** Gets the content before cursor position (line, column) as prefix for completion. */
String extractCompletionPrefix(int line, int column) {
int position = LineMapUtil.getPositionFromZeroBasedLineAndColumn(getLineMap(), line, column);
logger.fine("column:%s, position:%s", column, position);
if (position < 0) {
logger.warning(
"Position of (%s, %s): %s is negative when getting completion prefix for file %s",
Expand All @@ -44,6 +45,7 @@ String extractCompletionPrefix(int line, int column) {
while (start >= 0 && Character.isJavaIdentifierPart(getContent().charAt(start))) {
start--;
}
logger.fine("start:%s", start);
return getContent().subSequence(start + 1, position).toString();
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/javacomp/file/FileWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ synchronized void shutdown() {
}

synchronized void notifyFileChange(Path path, WatchEvent.Kind<?> eventKind) {
logger.fine("path: %s", path);
if (PathUtils.shouldIgnorePath(path, projectRoot, ignorePathMatchers)) {
return;
}
Expand Down Expand Up @@ -179,15 +180,18 @@ private void handleWatchEvent(Path dir, WatchEvent<Path> event) {
Path fullPath = dir.resolve(event.context());

if (PathUtils.shouldIgnorePath(fullPath, projectRoot, ignorePathMatchers)) {
logger.fine("%s ignored", fullPath);
return;
}

if (fileSnapshotPaths.contains(fullPath)) {
logger.fine("%s is on fileSnapshotPaths", fullPath);
// The file is managed by file snapshots. Ignore file system events.
return;
}

if (Files.isDirectory(fullPath)) {
logger.fine("%s is a directory", fullPath);
handleDirectoryEvent(fullPath, eventKind);
return;
}
Expand All @@ -196,6 +200,7 @@ private void handleWatchEvent(Path dir, WatchEvent<Path> event) {
}

private void handleDirectoryEvent(Path path, WatchEvent.Kind<?> eventKind) {
logger.fine("path:%s", path);
if (eventKind == StandardWatchEventKinds.ENTRY_CREATE) {
// New directory created, watch it.
watchNewDirectory(path);
Expand Down
51 changes: 36 additions & 15 deletions src/main/java/org/javacomp/logging/JLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public class JLogger {
private static volatile boolean hasFileHandler = false;
private final Logger javaLogger;

static {
// remove default logging handlers, we only support a FileHandler set through setLogFile
Logger rootLogger = Logger.getLogger("");
for(Handler handler : rootLogger.getHandlers()) {
rootLogger.removeHandler(handler);
}
}

public static synchronized void setLogFile(String filePath) {
Logger rootLogger = Logger.getLogger("");

Expand All @@ -38,9 +46,6 @@ public static synchronized void setLogFile(String filePath) {
public static synchronized void setLogLevel(Level level) {
Logger rootLogger = Logger.getLogger("");
rootLogger.setLevel(level);
for (Handler handler : rootLogger.getHandlers()) {
handler.setLevel(level);
}
}

private JLogger(String enclosingClassName) {
Expand All @@ -50,14 +55,14 @@ private JLogger(String enclosingClassName) {
/** Creates a {@link JLogger} and sets its tag to the name of the class that calls it. */
public static JLogger createForEnclosingClass() {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
// The top of the stack trace is this method. The one belows it is the caller class.
// The top of the stack trace is this method. The one below it is the caller class.
String enclosingClassName = stackTrace[1].getClassName();
return new JLogger(enclosingClassName);
}

/** Logs a message at severe level. */
public void severe(String msg) {
log(Level.SEVERE, msg, null /* thrown */);
log(Level.SEVERE, msg);
}

/**
Expand All @@ -67,7 +72,7 @@ public void severe(String msg) {
* @param args arguments to be filled into {@code msgfmt}
*/
public void severe(String msgfmt, Object... args) {
log(Level.SEVERE, String.format(msgfmt, args), null /* thrown */);
log(Level.SEVERE, msgfmt, args);
}

/**
Expand All @@ -78,12 +83,12 @@ public void severe(String msgfmt, Object... args) {
* @param args arguments to be filled into {@code msgfmt}
*/
public void severe(Throwable thrown, String msgfmt, Object... args) {
log(Level.SEVERE, String.format(msgfmt, args), thrown);
log(Level.SEVERE, thrown, msgfmt, args);
}

/** Logs a message at warning level. */
public void warning(String msg) {
log(Level.WARNING, msg, null /* thrown */);
log(Level.WARNING, msg);
}

/**
Expand All @@ -93,7 +98,7 @@ public void warning(String msg) {
* @param args arguments to be filled into {@code msgfmt}
*/
public void warning(String msgfmt, Object... args) {
log(Level.WARNING, String.format(msgfmt, args), null /* thrown */);
log(Level.WARNING, msgfmt, args);
}

/**
Expand All @@ -105,12 +110,12 @@ public void warning(String msgfmt, Object... args) {
* @param args arguments to be filled into {@code msgfmt}
*/
public void warning(Throwable thrown, String msgfmt, Object... args) {
log(Level.WARNING, String.format(msgfmt, args), thrown);
log(Level.WARNING, thrown, msgfmt, args);
}

/** Logs a message at info level. */
public void info(String msg) {
log(Level.INFO, msg, null /* thrown */);
log(Level.INFO, msg);
}

/**
Expand All @@ -120,12 +125,12 @@ public void info(String msg) {
* @param args arguments to be filled into {@code msgfmt}
*/
public void info(String msgfmt, Object... args) {
log(Level.INFO, String.format(msgfmt, args), null /* thrown */);
log(Level.INFO, msgfmt, args);
}

/** Logs a message at fine level. */
public void fine(String msg) {
log(Level.FINE, msg, null /* thrown */);
log(Level.FINE, msg);
}

/**
Expand All @@ -135,10 +140,26 @@ public void fine(String msg) {
* @param args arguments to be filled into {@code msgfmt}
*/
public void fine(String msgfmt, Object... args) {
log(Level.FINE, String.format(msgfmt, args), null /* thrown */);
log(Level.FINE, msgfmt, args);
}

public void log(Level level, String msgFormat, Object... msgArgs) {
log(level, null, msgFormat, msgArgs);
}

private void log(Level level, String msg, @Nullable Throwable thrown) {
public void log(Level level, String msg) {
log(level, null, msg);
}

public void log(Level level, @Nullable Throwable thrown, String msgFormat, Object... msgArgs) {
if(!javaLogger.isLoggable(level))
return;
log(level, thrown, String.format(msgFormat, msgArgs));
}

public void log(Level level, @Nullable Throwable thrown, String msg) {
if(!javaLogger.isLoggable(level))
return;
LogRecord logRecord = new LogRecord(level, msg);
if (thrown != null) {
logRecord.setThrown(thrown);
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/javacomp/model/PackageScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.javacomp.logging.JLogger;

/** Scope of sub packages and files in a package. */
public class PackageScope implements EntityScope {
// Map of simple names -> subPackages.
static JLogger logger= JLogger.createForEnclosingClass();

// Map of simple names -> subPackages.
private final Multimap<String, PackageEntity> subPackages;
private final Set<FileScope> files;

Expand Down Expand Up @@ -53,11 +56,13 @@ public void removePackage(PackageEntity entity) {
}

public void addFile(FileScope fileScope) {
files.add(fileScope);
if(!files.add(fileScope))
logger.warning("couldn't add fileScope:%s", fileScope);
}

public void removeFile(FileScope fileScope) {
files.remove(fileScope);
if(!files.remove(fileScope))
logger.warning("couln't remove fileScope:%s", fileScope);
}

/** @return whether the package has sub packages or files. */
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/javacomp/parser/AstScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public Void visitMethod(MethodTree node, EntityScope currentScope) {
methodEntity.setParameters(parameterListBuilder.build());

// TODO: distinguish between static and non-static methods.
logger.fine("fileScope:%s, methodEntity:%s", fileScope, methodEntity);
currentScope.addEntity(methodEntity);
List<String> previousQualifiers = this.currentQualifiers;
// No entity defined inside method scope is qualified.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/javacomp/parser/LineMapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public final class LineMapUtil {
private LineMapUtil() {}

public static int getPositionFromZeroBasedLineAndColumn(LineMap lineMap, int line, int column) {
// LineMap accepts 1-based line and column numbers.
return (int) lineMap.getPosition(line + 1, column + 1);
// LineMap accepts 1-based line.
return (int)lineMap.getStartPosition(line+1)+column;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ public synchronized void addOrUpdateFile(Path path, boolean fixContentForParsing
}

private void addOrUpdateFile(Module module, Path path, boolean fixContentForParsing) {
logger.fine("path:%s", path);
try {
Optional<FileScope> fileScope = parser.parseSourceFile(path, fixContentForParsing);
logger.fine("fileScope:%s", fileScope);
if (fileScope.isPresent()) {
module.addOrReplaceFileScope(fileScope.get());
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/javacomp/project/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public synchronized void loadTypeIndexFile(String typeIndexFile) {
private synchronized void addOrUpdateFile(Path filePath) {
// Only fix content for files that are under completion.
boolean fixContentForParsing = lastCompletedFile != null && lastCompletedFile.equals(filePath);
logger.fine("fixContentForParsing:%s", fixContentForParsing);
moduleManager.addOrUpdateFile(filePath, fixContentForParsing);
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/javacomp/reference/DefinitionSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public List<? extends Entity> getDefinitionEntities(
PositionContext.createForPosition(moduleManager, filePath, line, column);

if (!positionContext.isPresent()) {
logger.fine("positionContext not present");
return ImmutableList.of();
}

Expand All @@ -86,8 +87,10 @@ List<? extends Entity> getDefinitionEntities(Module module, PositionContext posi
if (leafTree instanceof LiteralTree) {
// LiteralTree is also an ExpressionTree. We don't want to show type definitions for literal
// constants.
logger.fine("leafTree is a LiteralTree");
return ImmutableList.of();
} else if (leafTree instanceof ExpressionTree) {
logger.fine("leafTree is a ExpressionTree");
Set<Entity.Kind> allowedKinds = ALLOWED_ENTITY_KINDS;
if (treeIsMethodName(leafTree, parentTree)) {
// parentTree is the method we need to solve.
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/javacomp/server/JavaComp.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,18 @@ public synchronized void initialize(
if (initializeOptions != null) {
mergeOptions(options, initializeOptions);
}

logger.info("Initializing project: %s", projectRootUri);
logger.info(
"Options:\n logPath: %s\n logLevel: %s\n" + " ignorePaths: %s\n typeIndexFiles: %s",
options.logPath, options.logLevel, options.ignorePaths, options.typeIndexFiles);

if (options.logPath != null) {
JLogger.setLogFile(options.logPath);
}
if (options.logLevel != null) {
JLogger.setLogLevel(options.getLogLevel());
}
logger.info("Initializing project: %s", projectRootUri);
logger.info(
"Options:\n logPath: %s\n logLevel: %s\n" + " ignorePaths: %s\n typeIndexFiles: %s",
options.logPath, options.logLevel, options.ignorePaths, options.typeIndexFiles);

if (options.ignorePaths != null) {
ignorePaths = options.getIgnorePaths();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import org.javacomp.logging.JLogger;
import org.javacomp.model.Entity;
import org.javacomp.model.EntityScope;
import org.javacomp.model.FileScope;
Expand All @@ -22,6 +23,7 @@
* https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#textDocument_definition
*/
public class DefinitionHandler extends RequestHandler<TextDocumentPositionParams> {
private static final JLogger logger=JLogger.createForEnclosingClass();
private final Server server;

public DefinitionHandler(Server server) {
Expand All @@ -43,6 +45,7 @@ public List<Location> handleRequest(Request<TextDocumentPositionParams> request)
return definitions.stream()
.map(
entity -> {
logger.fine("getting location for %s", entity);
com.google.common.collect.Range<Integer> range = entity.getSymbolRange();
EntityScope scope = entity.getScope();
while (!(scope instanceof FileScope) && scope.getParentScope().isPresent()) {
Expand All @@ -58,6 +61,7 @@ public List<Location> handleRequest(Request<TextDocumentPositionParams> request)
// If the file scope is not created from a source code (e.g. it's created from
// a type index JSON file or class file), there is no souce code that defines the
// symbol.
logger.fine("%s is on a non SOURCE_COCE fileScope");
return null;
}
if (!fileScope.getLineMap().isPresent()) {
Expand Down