Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,35 @@ public enum CompilerRange {
* Map the compiler (point + 1) position to SemanticDB start and use (point + symbol name length +
* 1) for the SemanticDB end position.
*/
FROM_POINT_TO_SYMBOL_NAME_PLUS_ONE;
FROM_POINT_TO_SYMBOL_NAME_PLUS_ONE,

/**
* Use text search to find the start of the symbol name and use (found start + symbol name length)
* for the SemanticDB end position;
*/
FROM_TEXT_SEARCH,

/**
* Use text search to find the start of the symbol name, using the point position as the starting
* search offset and using (found start + symbol name length) for the SemanticDB end position;
*/
FROM_POINT_WITH_TEXT_SEARCH;

public boolean isFromPoint() {
switch (this) {
case FROM_POINT_TO_SYMBOL_NAME:
case FROM_POINT_TO_SYMBOL_NAME_PLUS_ONE:
case FROM_POINT_WITH_TEXT_SEARCH:
return true;
default:
return false;
}
}

public boolean isFromTextSearch() {
switch (this) {
case FROM_TEXT_SEARCH:
case FROM_POINT_WITH_TEXT_SEARCH:
return true;
default:
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.sourcegraph.semanticdb_javac;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.LineMap;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;

import javax.lang.model.element.Element;
import javax.lang.model.element.Name;
import java.io.IOException;
import java.util.HashMap;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.sourcegraph.semanticdb_javac.Debugging.pprint;

public class RangeFinder {
public static Optional<Semanticdb.Range> findRange(
TreePath path,
Trees trees,
CompilationUnitTree root,
Element element,
int startPos,
String source) {
LineMap lineMap = root.getLineMap();
Name name = element.getSimpleName();
if (name.contentEquals("<init>")) name = element.getEnclosingElement().getSimpleName();

int endPos = (int) trees.getSourcePositions().getEndPosition(root, path.getLeaf());
// false for anonymous classes
if (name.length() != 0) {
startPos = findNameIn(name, startPos, source);
endPos = startPos + name.length();
}

if (endPos == -1 || startPos == -1) {
return Optional.empty();
}

Semanticdb.Range range =
Semanticdb.Range.newBuilder()
.setStartLine((int) lineMap.getLineNumber(startPos) - 1)
.setStartCharacter((int) lineMap.getColumnNumber(startPos) - 1)
.setEndLine((int) lineMap.getLineNumber(endPos) - 1)
.setEndCharacter((int) lineMap.getColumnNumber(endPos) - 1)
.build();
return Optional.of(range);
}

private static int findNameIn(CharSequence name, int start, String source) {
if (source.equals("")) return -1;

int offset = source.indexOf(" " + name, start);
if (offset > -1) {
return offset + 1;
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.sourcegraph.semanticdb_javac;

import com.sun.source.tree.*;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.*;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.Position;
import com.sourcegraph.semanticdb_javac.Semanticdb.SymbolOccurrence.Role;

import javax.lang.model.element.Element;
import javax.lang.model.util.Elements;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -28,9 +28,11 @@ public class SemanticdbVisitor extends TreePathScanner<Void, Void> {
private final JavacTask task;
private final TaskEvent event;
private final SemanticdbOptions options;
private final Trees trees;
private final EndPosTable endPosTable;
private final ArrayList<Semanticdb.SymbolOccurrence> occurrences;
private final ArrayList<Semanticdb.SymbolInformation> symbolInfos;
private String source;

public SemanticdbVisitor(
JavacTask task, GlobalSymbolsCache globals, TaskEvent event, SemanticdbOptions options) {
Expand All @@ -39,13 +41,15 @@ public SemanticdbVisitor(
this.locals = new LocalSymbolsCache(); // Fresh cache per compilation unit.
this.event = event;
this.options = options;
this.trees = Trees.instance(task);
if (event.getCompilationUnit() instanceof JCTree.JCCompilationUnit) {
this.endPosTable = ((JCTree.JCCompilationUnit) event.getCompilationUnit()).endPositions;
} else {
this.endPosTable = new EmptyEndPosTable();
}
this.occurrences = new ArrayList<>();
this.symbolInfos = new ArrayList<>();
this.source = semanticdbText();
}

public Semanticdb.TextDocument buildTextDocument(CompilationUnitTree tree) {
Expand All @@ -56,7 +60,7 @@ public Semanticdb.TextDocument buildTextDocument(CompilationUnitTree tree) {
.setSchema(Semanticdb.Schema.SEMANTICDB4)
.setLanguage(Semanticdb.Language.JAVA)
.setUri(semanticdbUri())
.setText(semanticdbText())
.setText(options.includeText ? this.source : "")
.setMd5(semanticdbMd5())
.addAllOccurrences(occurrences)
.addAllSymbols(symbolInfos)
Expand Down Expand Up @@ -94,7 +98,8 @@ private void emitSymbolInformation(Symbol sym) {
public Void visitClass(ClassTree node, Void unused) {
if (node instanceof JCTree.JCClassDecl) {
JCTree.JCClassDecl cls = (JCTree.JCClassDecl) node;
emitSymbolOccurrence(cls.sym, cls, Role.DEFINITION, CompilerRange.FROM_POINT_TO_SYMBOL_NAME);
emitSymbolOccurrence(
cls.sym, cls, Role.DEFINITION, CompilerRange.FROM_POINT_WITH_TEXT_SEARCH);
}
return super.visitClass(node, unused);
}
Expand All @@ -103,8 +108,11 @@ public Void visitClass(ClassTree node, Void unused) {
public Void visitMethod(MethodTree node, Void unused) {
if (node instanceof JCTree.JCMethodDecl) {
JCTree.JCMethodDecl meth = (JCTree.JCMethodDecl) node;
emitSymbolOccurrence(
meth.sym, meth, Role.DEFINITION, CompilerRange.FROM_POINT_TO_SYMBOL_NAME);
CompilerRange range = CompilerRange.FROM_POINT_TO_SYMBOL_NAME;
if ((meth.sym.flags() & Flags.GENERATEDCONSTR) != 0L) {
range = CompilerRange.FROM_TEXT_SEARCH;
}
emitSymbolOccurrence(meth.sym, meth, Role.DEFINITION, range);
}
return super.visitMethod(node, unused);
}
Expand Down Expand Up @@ -177,7 +185,11 @@ private Optional<Semanticdb.Range> semanticdbRange(
start = pos.getStartPosition();
end = pos.getEndPosition(endPosTable);
}
if (start != Position.NOPOS && end != Position.NOPOS && end > start) {

if (kind.isFromTextSearch() && sym.name.length() > 0) {
return RangeFinder.findRange(
getCurrentPath(), trees, getCurrentPath().getCompilationUnit(), sym, start, this.source);
} else if (start != Position.NOPOS && end != Position.NOPOS && end > start) {
LineMap lineMap = event.getCompilationUnit().getLineMap();
Semanticdb.Range range =
Semanticdb.Range.newBuilder()
Expand Down Expand Up @@ -214,12 +226,13 @@ private Optional<Semanticdb.SymbolOccurrence> semanticdbOccurrence(
}

private String semanticdbText() {
if (!options.includeText) return "";
if (source != null) return source;
try {
return event.getSourceFile().getCharContent(true).toString();
source = event.getSourceFile().getCharContent(true).toString();
} catch (IOException e) {
return "";
source = "";
}
return source;
}

private String semanticdbMd5() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
// ^^^^^^^^^^^^^^^ reference java/lang/annotation/RetentionPolicy#
// ^^^^^ reference java/lang/annotation/RetentionPolicy#CLASS.
public @interface AfterPropsSet {
// ^^^^^^^^^^^^^ definition com/airbnb/epoxy/AfterPropsSet#
// ^^^^^^^^^^^^^ definition com/airbnb/epoxy/AfterPropsSet#
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* See https://github.com/airbnb/epoxy/wiki/Epoxy-Controller#asynchronous-support
*/
public abstract class AsyncEpoxyController extends EpoxyController {
// ^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyController#
// ^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyController#
// ^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyController#

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
* Also adds support for canceling an in progress diff, and makes everything thread safe.
*/
class AsyncEpoxyDiffer {
//^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#

interface ResultCallback {
//^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#ResultCallback#
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#ResultCallback#
void onResult(@NonNull DiffResult result);
// ^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#ResultCallback#onResult().
// ^^^^^^^ reference androidx/annotation/NonNull#
Expand Down Expand Up @@ -322,7 +322,6 @@ public void submitList(@Nullable final List<? extends EpoxyModel<?>> newList) {
// ^^^^^^^^^^^^^^^^ reference local13 6:5
// ^^^^^^^^ reference java/lang/Runnable#
// ^^^^^^^^ reference java/lang/Runnable#
// ^ definition local13 1:4
@Override
// ^^^^^^^^ reference java/lang/Override#
public void run() {
Expand Down Expand Up @@ -371,7 +370,6 @@ private void onRunCompleted(
// ^^^^^^^^^^^^^^^^ reference local20 8:5
// ^^^^^^^^ reference java/lang/Runnable#
// ^^^^^^^^ reference java/lang/Runnable#
// ^ definition local20 1:4
@Override
// ^^^^^^^^ reference java/lang/Override#
public void run() {
Expand Down Expand Up @@ -449,8 +447,8 @@ private synchronized boolean tryLatchList(@Nullable List<? extends EpoxyModel<?>
* generation number is synced with the list state at the time it was created.
*/
private static class GenerationTracker {
// ^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#GenerationTracker#`<init>`().
// ^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#GenerationTracker#
// ^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#GenerationTracker#
// ^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#GenerationTracker#`<init>`().

// Max generation of currently scheduled runnable
private volatile int maxScheduledGeneration;
Expand Down Expand Up @@ -507,7 +505,7 @@ synchronized boolean finishGeneration(int runGeneration) {
}

private static class DiffCallback extends DiffUtil.Callback {
// ^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#DiffCallback#
// ^^^^^^^^^^^^ definition com/airbnb/epoxy/AsyncEpoxyDiffer#DiffCallback#
// ^^^^^^^^ reference DiffUtil/
// ^^^^^^^^ reference DiffUtil/Callback#

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
// ^^^^^^^^^^^^^^^ reference java/lang/annotation/RetentionPolicy#
// ^^^^^ reference java/lang/annotation/RetentionPolicy#CLASS.
public @interface AutoModel {
// ^^^^^^^^^ definition com/airbnb/epoxy/AutoModel#
// ^^^^^^^^^ definition com/airbnb/epoxy/AutoModel#

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
// ^^^^^^^^^^^^ reference androidx/recyclerview/widget/RecyclerView#

public abstract class BaseEpoxyAdapter
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BaseEpoxyAdapter#
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BaseEpoxyAdapter#
extends RecyclerView.Adapter<EpoxyViewHolder>
// ^^^^^^^^^^^^ reference RecyclerView/
// ^^^^^^^ reference RecyclerView/Adapter#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// ^^^^ reference android/view/View#

interface BaseEpoxyTouchCallback<T extends EpoxyModel> {
//^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BaseEpoxyTouchCallback#
// ^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BaseEpoxyTouchCallback#
// ^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyModel#

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
@SuppressWarnings("WeakerAccess")
//^^^^^^^^^^^^^^^ reference java/lang/SuppressWarnings#
public class BoundViewHolders implements Iterable<EpoxyViewHolder> {
// ^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#`<init>`().
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#`<init>`().
// ^^^^^^^^ reference java/lang/Iterable#
// ^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyViewHolder#
private final LongSparseArray<EpoxyViewHolder> holders = new LongSparseArray<>();
Expand Down Expand Up @@ -104,8 +104,8 @@ public EpoxyViewHolder getHolderForModel(EpoxyModel<?> model) {
}

private class HolderIterator implements Iterator<EpoxyViewHolder> {
// ^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#HolderIterator#`<init>`().
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#HolderIterator#
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#HolderIterator#
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#HolderIterator#`<init>`().
// ^^^^^^^^ reference java/util/Iterator#
// ^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyViewHolder#
private int position = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
// ^^^^^^^^^^^^^^^ reference java/lang/annotation/RetentionPolicy#
// ^^^^^ reference java/lang/annotation/RetentionPolicy#CLASS.
public @interface CallbackProp {
// ^^^^^^^^^^^^ definition com/airbnb/epoxy/CallbackProp#
// ^^^^^^^^^^^^ definition com/airbnb/epoxy/CallbackProp#
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
// ^^^^ reference com/airbnb/epoxy/ModelView#Size#
// ^^^^^^^^^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/ModelView#Size#MATCH_WIDTH_WRAP_HEIGHT.
public class Carousel extends EpoxyRecyclerView {
// ^^^^^^^^ definition com/airbnb/epoxy/Carousel#
// ^^^^^^^^ definition com/airbnb/epoxy/Carousel#
// ^^^^^^^^^^^^^^^^^ reference _root_/
public static final int NO_VALUE_SET = -1;
// ^^^^^^^^^^^^ definition com/airbnb/epoxy/Carousel#NO_VALUE_SET.
Expand All @@ -122,7 +122,6 @@ public class Carousel extends EpoxyRecyclerView {
// ^^^^^^^^^^^^^^^^^^^^^^^^^ reference local1 7:7
// ^^^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/Carousel#SnapHelperFactory#
// ^^^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/Carousel#SnapHelperFactory#
// ^ definition local1 2:3

@Override
// ^^^^^^^^ reference java/lang/Override#
Expand Down Expand Up @@ -768,7 +767,7 @@ public void setPadding(@Nullable Padding padding) {
* @see #setPadding(Padding)
*/
public static class Padding {
// ^^^^^^^ definition com/airbnb/epoxy/Carousel#Padding#
// ^^^^^^^ definition com/airbnb/epoxy/Carousel#Padding#
public final int left;
// ^^^^ definition com/airbnb/epoxy/Carousel#Padding#left.
public final int top;
Expand All @@ -784,8 +783,8 @@ public static class Padding {
// ^^^^^^^^^^^ definition com/airbnb/epoxy/Carousel#Padding#paddingType.

enum PaddingType {
// ^^^^^^ definition com/airbnb/epoxy/Carousel#Padding#PaddingType#`<init>`().
// ^^^^^^^^^^^ definition com/airbnb/epoxy/Carousel#Padding#PaddingType#
// ^^^^^^^^^^^ definition com/airbnb/epoxy/Carousel#Padding#PaddingType#
// ^^^^^^^^^^^ definition com/airbnb/epoxy/Carousel#Padding#PaddingType#`<init>`().
PX,
// ^^ definition com/airbnb/epoxy/Carousel#Padding#PaddingType#PX.
DP,
Expand Down Expand Up @@ -1151,8 +1150,8 @@ public void clear() {

/** Provide a SnapHelper implementation you want to use with a Carousel. */
public abstract static class SnapHelperFactory {
// ^^^^^^ definition com/airbnb/epoxy/Carousel#SnapHelperFactory#`<init>`().
// ^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/Carousel#SnapHelperFactory#
// ^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/Carousel#SnapHelperFactory#
// ^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/Carousel#SnapHelperFactory#`<init>`().
/**
* Create and return a new instance of a {@link androidx.recyclerview.widget.SnapHelper} for use
* with a Carousel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* annotation processor.
*/
public abstract class ControllerHelper<T extends EpoxyController> {
// ^^^^^^ definition com/airbnb/epoxy/ControllerHelper#`<init>`().
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ControllerHelper#
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ControllerHelper#
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ControllerHelper#`<init>`().
// ^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyController#
public abstract void resetAutoModels();
// ^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ControllerHelper#resetAutoModels().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* be returned.
*/
class ControllerHelperLookup {
//^^^^^^ definition com/airbnb/epoxy/ControllerHelperLookup#`<init>`().
//^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ControllerHelperLookup#
// ^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ControllerHelperLookup#
// ^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ControllerHelperLookup#`<init>`().
private static final String GENERATED_HELPER_CLASS_SUFFIX = "_EpoxyHelper";
// ^^^^^^ reference java/lang/String#
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ControllerHelperLookup#GENERATED_HELPER_CLASS_SUFFIX.
Expand Down
Loading