-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Go to definition in classpath #23
Merged
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
ab517c3
Implement syntactic definition indexer for scala sources
olafurpg 6547cf3
Implement classpath indexer.
olafurpg eebbe39
Integrate ctags with compiler and symbol indexer.
olafurpg 445e332
Implement Go to definition in classpath using Ctags module.
olafurpg 062f8e9
Fix trailing comma and run tests in CI
olafurpg cc537c5
Avoid redundant indexing.
olafurpg 766361a
Update lsp plugin
olafurpg 864821d
Get basic java ctags working.
olafurpg 4b175b9
Refactor ctags into separate package.
olafurpg 6ad69f0
Close ZipInputStream.
olafurpg bf1b6f9
Update readme
olafurpg 420f11d
Improve deduplication of indexing source jars.
olafurpg e64a505
Address review feedback
olafurpg 5c7951a
Clean up a few things
olafurpg bcd3905
Fix compilation errors
olafurpg 01f51b3
Add more detailed explanation why we write jar: contents to disk
olafurpg db1779a
Improve ctags indexing for java
olafurpg 3de1c85
Clean up ctags
olafurpg da8e1d8
Handle java fields
olafurpg dca5a4a
Remove unnecessary code.
olafurpg 2d09433
Update readme to reflect current state
olafurpg a85dbaf
Implement java ctags with qdox instead of javaparser.
olafurpg fe8cfa4
Remove javaparser and fix broken tests from qdox migration.
olafurpg 12a4a64
Properly handle java enums
olafurpg 0b29617
Index JDK sources.
olafurpg d05108d
Address review feedback
olafurpg c2cb7bd
Fix bug related to default java fields being static.
olafurpg 020faaa
Remove PrintStream from Formatter
olafurpg 81b8520
Clean up java ctags a bit.
olafurpg dc03def
Use recommended parameter name
olafurpg ed8e327
Minimize log spam on server startup.
olafurpg fd2035a
Link issue to open files from jars.
olafurpg 50191c1
Remove noise from ctags if shouldIndex filters a lot of paths
olafurpg 3576919
Skip broken and slow tests.
olafurpg 5e9272f
Make JDK test pass regardless of JAVA_HOME filename length.
olafurpg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
238 changes: 238 additions & 0 deletions
238
metaserver/src/main/scala/scala/meta/languageserver/Ctags.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
package scala.meta.languageserver | ||
|
||
import java.io.IOException | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.FileVisitResult | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.SimpleFileVisitor | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import java.util.zip.ZipInputStream | ||
import scala.collection.GenSeq | ||
import scala.collection.parallel.mutable.ParArray | ||
import scala.meta.Defn | ||
import scala.meta.Defn | ||
import scala.meta.Name | ||
import scala.meta.Pat | ||
import scala.meta.Pkg | ||
import scala.meta.Source | ||
import scala.meta.Template | ||
import scala.meta.Term | ||
import scala.meta.Tree | ||
import scala.meta.Type | ||
import scala.meta.parsers.ParseException | ||
import scala.meta.transversers.Traverser | ||
import scala.util.control.NonFatal | ||
import com.typesafe.scalalogging.LazyLogging | ||
import org.langmeta.inputs.Input | ||
import org.langmeta.inputs.Position | ||
import org.langmeta.internal.io.FileIO | ||
import org.langmeta.internal.io.PathIO | ||
import org.langmeta.io.AbsolutePath | ||
import org.langmeta.io.Fragment | ||
import org.langmeta.io.RelativePath | ||
import org.langmeta.semanticdb._ | ||
|
||
/** | ||
* Syntactically build a semanticdb containing only global symbol definition. | ||
* | ||
* The purpose of this module is to provide "Go to definition" from | ||
* project sources to dependency sources without indexing classfiles or | ||
* requiring dependencies to publish semanticdbs alongside their artifacts. | ||
* | ||
* One other use-case for this module is to implement "Workspace symbol provider" | ||
* without any build-tool or compiler integration. Essentially, ctags. | ||
*/ | ||
object Ctags extends LazyLogging { | ||
|
||
/** | ||
* Build an index from a classpath of -sources.jar | ||
* | ||
* @param shouldIndex An optional filter to skip particular relative filenames. | ||
* @param inParallel If true, use parallel collection to index using all | ||
* available CPU power. If false, uses single-threaded | ||
* collection. | ||
* @param callback A callback that is called as soon as a document has been | ||
* indexed. | ||
*/ | ||
def index( | ||
classpath: List[AbsolutePath], | ||
shouldIndex: RelativePath => Boolean = _ => true, | ||
inParallel: Boolean = true | ||
)(callback: Document => Unit): Unit = { | ||
val fragments = allClasspathFragments(classpath, inParallel) | ||
fragments.foreach { fragment => | ||
try { | ||
if (PathIO.extension(fragment.name.toNIO) == "scala" && | ||
shouldIndex(fragment.name)) { | ||
callback(index(fragment)) | ||
} | ||
} catch { | ||
case _: ParseException => // nothing | ||
case NonFatal(e) => | ||
logger.error(e.getMessage, e) | ||
} | ||
} | ||
} | ||
|
||
/** Build a Database for a single source file. */ | ||
def index(filename: String, contents: String): Document = { | ||
val input = Input.VirtualFile(filename, contents) | ||
val tree = { | ||
import scala.meta._ | ||
input.parse[Source].get | ||
} | ||
val traverser = new DefinitionTraverser | ||
val (names, symbols) = traverser.index(tree) | ||
Document( | ||
input = input, | ||
language = "scala212", | ||
names = names, | ||
messages = Nil, | ||
symbols = symbols, | ||
synthetics = Nil | ||
) | ||
} | ||
|
||
def index(fragment: Fragment): Document = { | ||
val filename = fragment.uri.toString | ||
val contents = | ||
new String(FileIO.readAllBytes(fragment.uri), StandardCharsets.UTF_8) | ||
logger.trace(s"Indexing $filename with length ${contents.length}") | ||
index(filename, contents) | ||
} | ||
|
||
private val root = Symbol("_root_.") | ||
private sealed abstract class Next extends Product with Serializable | ||
private case object Stop extends Next | ||
private case object Continue extends Next | ||
private class DefinitionTraverser extends Traverser { | ||
def index(tree: Tree): (List[ResolvedName], List[ResolvedSymbol]) = { | ||
apply(tree) | ||
names.result() -> symbols.result() | ||
} | ||
private val names = List.newBuilder[ResolvedName] | ||
private val symbols = List.newBuilder[ResolvedSymbol] | ||
private var currentOwner: _root_.scala.meta.Symbol = root | ||
override def apply(tree: Tree): Unit = { | ||
val old = currentOwner | ||
val next = tree match { | ||
case t: Source => Continue | ||
case t: Template => Continue | ||
case t: Pkg => pkg(t.ref); Continue | ||
case t: Pkg.Object => term(t.name, PACKAGEOBJECT); Continue | ||
case t: Defn.Class => tpe(t.name, CLASS); Continue | ||
case t: Defn.Trait => tpe(t.name, TRAIT); Continue | ||
case t: Defn.Object => term(t.name, OBJECT); Continue | ||
case t: Defn.Def => term(t.name, DEF); Stop | ||
case Defn.Val(_, Pat.Var(name) :: Nil, _, _) => term(name, DEF); Stop | ||
case Defn.Var(_, Pat.Var(name) :: Nil, _, _) => term(name, DEF); Stop | ||
case _ => Stop | ||
} | ||
next match { | ||
case Continue => super.apply(tree) | ||
case Stop => () // do nothing | ||
} | ||
currentOwner = old | ||
} | ||
def addSignature( | ||
signature: Signature, | ||
definition: Position, | ||
flags: Long | ||
): Unit = { | ||
currentOwner = symbol(signature) | ||
names += ResolvedName( | ||
definition, | ||
currentOwner, | ||
isDefinition = true | ||
) | ||
symbols += ResolvedSymbol( | ||
currentOwner, | ||
Denotation(flags, signature.name, "", Nil) | ||
) | ||
} | ||
def symbol(signature: Signature): Symbol = | ||
Symbol.Global(currentOwner, signature) | ||
def term(name: Term.Name, flags: Long): Unit = | ||
addSignature(Signature.Term(name.value), name.pos, flags) | ||
def tpe(name: Type.Name, flags: Long): Unit = | ||
addSignature(Signature.Type(name.value), name.pos, flags) | ||
def pkg(ref: Term.Ref): Unit = ref match { | ||
case Name(name) => | ||
currentOwner = symbol(Signature.Term(name)) | ||
case Term.Select(qual: Term.Ref, Name(name)) => | ||
pkg(qual) | ||
currentOwner = symbol(Signature.Term(name)) | ||
} | ||
} | ||
|
||
/** Returns all *.scala fragments to index from this classpath | ||
* | ||
* This implementation is copy-pasted from scala.meta.Classpath.deep with | ||
* the following differences: | ||
* | ||
* - We build a parallel array | ||
* - We log errors instead of silently ignoring them | ||
* - We filter out non-scala sources | ||
*/ | ||
private def allClasspathFragments( | ||
classpath: List[AbsolutePath], | ||
inParallel: Boolean | ||
): GenSeq[Fragment] = { | ||
var buf = | ||
if (inParallel) ParArray.newBuilder[Fragment] | ||
else List.newBuilder[Fragment] | ||
classpath.foreach { base => | ||
def exploreJar(base: AbsolutePath): Unit = { | ||
val stream = Files.newInputStream(base.toNIO) | ||
try { | ||
val zip = new ZipInputStream(stream) | ||
var entry = zip.getNextEntry | ||
while (entry != null) { | ||
if (!entry.getName.endsWith("/") && isScala(entry.getName)) { | ||
val name = RelativePath(entry.getName.stripPrefix("/")) | ||
buf += Fragment(base, name) | ||
} | ||
entry = zip.getNextEntry | ||
} | ||
} catch { | ||
case ex: IOException => | ||
logger.error(ex.getMessage, ex) | ||
} finally { | ||
stream.close() | ||
} | ||
} | ||
if (base.isDirectory) { | ||
Files.walkFileTree( | ||
base.toNIO, | ||
new SimpleFileVisitor[Path] { | ||
override def visitFile( | ||
file: Path, | ||
attrs: BasicFileAttributes | ||
): FileVisitResult = { | ||
if (isScala(file)) { | ||
buf += Fragment(base, RelativePath(base.toNIO.relativize(file))) | ||
} | ||
FileVisitResult.CONTINUE | ||
} | ||
} | ||
) | ||
} else if (base.isFile) { | ||
if (base.toString.endsWith(".jar")) { | ||
exploreJar(base) | ||
} else { | ||
sys.error( | ||
s"Obtained non-jar file $base. Expected directory or *.jar file." | ||
) | ||
} | ||
} else { | ||
logger.info(s"Skipping $base") | ||
// Skip | ||
} | ||
} | ||
buf.result() | ||
} | ||
|
||
private def isScala(path: String): Boolean = path.endsWith(".scala") | ||
private def isScala(path: Path): Boolean = PathIO.extension(path) == "scala" | ||
} |
111 changes: 0 additions & 111 deletions
111
metaserver/src/main/scala/scala/meta/languageserver/DefinitionIndexer.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 13-24 import various bits of scala.meta, then here you import all. Why? Is the style no wildcards allowed at top of file?