Skip to content
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

Migrate to new symbol index schema #37

Merged
merged 34 commits into from
Nov 20, 2017
Merged

Conversation

olafurpg
Copy link
Member

@olafurpg olafurpg commented Nov 18, 2017

This PR migrates to the persistable indexing schema explained in #35. It does not yet take advantage of the fact that we can persist the symbol index and load it on startup, we leave that for another PR. The major improvement is related to testing.

Testing

While doing the refactoring, I took the liberty to create a proper test fixture to spin up the server, and programmatically invoke "go to definition" requests. Tests are now written using utest. This makes it possible to assert key features including workspace semanticdb indexing and classpath ctags indexing (for both java and scala sources). Setting up the test fixture required making it easy to stub out several services and selectively disable certain features:

  • ServerConfig: bag of options to disable jdk indexing, classpath indexing or scalafmt classloading
  • Notifications: make it possible to programmatically load a SymbolIndex without an lsp server.
  • Formatter.empty: formatter that doesn't do anything
  • DocumentStore: abstract key/value store for URI -> Document, default implementation is in-memory but should be easy to create alternative store that persists to disk (e.g., sqlite).
  • SymbolIndexerMap: in-memory store over the new SymbolIndex protobuf message. Has methods to incrementally register definitions and references. Should come up with a better name.

While setting up the test infrastructure, I moved the test-workspace build into the main build so that edits in UserTest.scala trigger semanticdb recompilation and tests in metaserver to re-run.

Thanks to Monix TestScheduler, we have fine grained control over the execution context and can manually trigger all the indexing mechanism that normally runs asynchronously to run synchronously before initializing our tests.

@olafurpg olafurpg changed the title Migrate to new symbol index schema [WIP] Migrate to new symbol index schema Nov 18, 2017
Copy link
Member

@gabro gabro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive work @olafurpg. I think the refactor is very worth it and it's also a step towards #6.

I would rather see this merged sooner than later, since it's already a bit long to review and incremental changes are going to be much easier to review.

I left some non-blocking comments :-)


object InputEnrichments {
implicit class XtensionInputOffset(val input: Input) extends AnyVal {
def fromIndexRange(range: i.Range): Position = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe toPosition for consistency?

@@ -10,6 +10,13 @@ abstract class Formatter {
def format(code: String, configFile: String, filename: String): String
}
object Formatter extends LazyLogging {
def empty: Formatter = new Formatter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

micro-optimization, but this could be a SAM

def empty: Formatter = (code, _, _) => code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also maybe noop instead of empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to noop, realized there is actually another method to format using the default config so I added that one instead of converting to SAM. To double check it worked as expected I added FormatterTest

@@ -15,6 +15,7 @@ object Main extends LazyLogging {
val out = new PrintStream(new FileOutputStream(logPath))
val err = new PrintStream(new FileOutputStream(logPath))
val cwd = AbsolutePath(workspace)
val config = ServerConfig(cwd, indexJDK = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't indexJDK = true already the default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, removed!


def updated(symbol: String)(f: SymbolIndex => SymbolIndex): Unit = {
val value = symbols.getOrElseUpdate(symbol, newValue(symbol))
value.getAndUpdate(new UnaryOperator[SymbolIndex] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too can be a SAM, I think.

I haven't tried, but this may actually be as simple as:

value.getAndUpdate(f)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make the SAM conversion happen to the call to updated, however that makes IJ unhappy even if it compiles 😢
screen shot 2017-11-19 at 13 17 40
Problem seems to be caused by the type parameter.

I think my reluctance to use SAM is caused by fairly bad IJ support (and need to cross-build against 2.11 in other projects). I haven't yet made the switch to LSP+vscode/vim

new AtomicReference(SymbolIndex(symbol = symbol))
}

def index: Traversable[SymbolIndex] = new Traversable[SymbolIndex] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def index: Traversable[SymbolIndex] = f => symbols.values.foreach(s => f(s.get))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreach signature is polymorphic [U](f: SymbolIndex => U) which makes SAM conversion fail 😢

* - pretty multiline string diffing
* - FunSuite-style test("name") { => fun }
*/
class MegaSuite(implicit filename: sourcecode.File) extends TestSuite {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mega? 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know what to call it, but it's mega good 😄


"fallback" - {
"<<User>>(...)" - {
val term = indexer.findSymbol(path, 3, 17).get
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this fail in case a symbol is not found?

Maybe it's worth to also check that term is defined?

val term = indexer.findSymbol(path, 3, 17)
assert(term.isDefined)
assertNoDiff(term.get.symbol, "_root_.a.User#")
assert(term.get.definition.isDefined)

Or even a common utility as:

def assertSymbolFound(path: AbsolutePath, line: Int, column: Int, expected: String) = {
  val term = indexer.findSymbol(path, line, column)
  assert(term.isDefined, s"Symbol not found at $path:$line:$column")
  assertNoDiff(term.get.symbol, expected)
  assert(term.get.definition.isDefined, s"Definition not found for term $term")
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your style! Great idea, much cleaner now 💯

screen shot 2017-11-19 at 13 35 08

Now just need to figure out how to replace those pesky line+column magic numbers with something more readable

@gabro gabro mentioned this pull request Nov 19, 2017
@olafurpg olafurpg mentioned this pull request Nov 19, 2017
Copy link
Member Author

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review @gabro I admit this PR is way too big, I should have started with the test fixture before migrating to the new schema. Let me try and complete the bullets before merging this, since currently the PR is removing features without any additional improvements.

@@ -10,6 +10,13 @@ abstract class Formatter {
def format(code: String, configFile: String, filename: String): String
}
object Formatter extends LazyLogging {
def empty: Formatter = new Formatter {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to noop, realized there is actually another method to format using the default config so I added that one instead of converting to SAM. To double check it worked as expected I added FormatterTest

@@ -15,6 +15,7 @@ object Main extends LazyLogging {
val out = new PrintStream(new FileOutputStream(logPath))
val err = new PrintStream(new FileOutputStream(logPath))
val cwd = AbsolutePath(workspace)
val config = ServerConfig(cwd, indexJDK = true)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, removed!


}

object SymbolIndexer {
val emptyDocument: Document = Document(Input.None, "", Nil, Nil, Nil, Nil)
def empty(cwd: AbsolutePath): SymbolIndexer = apply(
new Notifications {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted to SAM!

new AtomicReference(SymbolIndex(symbol = symbol))
}

def index: Traversable[SymbolIndex] = new Traversable[SymbolIndex] {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreach signature is polymorphic [U](f: SymbolIndex => U) which makes SAM conversion fail 😢


def updated(symbol: String)(f: SymbolIndex => SymbolIndex): Unit = {
val value = symbols.getOrElseUpdate(symbol, newValue(symbol))
value.getAndUpdate(new UnaryOperator[SymbolIndex] {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make the SAM conversion happen to the call to updated, however that makes IJ unhappy even if it compiles 😢
screen shot 2017-11-19 at 13 17 40
Problem seems to be caused by the type parameter.

I think my reluctance to use SAM is caused by fairly bad IJ support (and need to cross-build against 2.11 in other projects). I haven't yet made the switch to LSP+vscode/vim

* - pretty multiline string diffing
* - FunSuite-style test("name") { => fun }
*/
class MegaSuite(implicit filename: sourcecode.File) extends TestSuite {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know what to call it, but it's mega good 😄

def check(filename: String, original: String, expected: String): Unit = {
test(filename) {
val obtained = Database(Ctags.index(filename, original) :: Nil)
// println(obtained)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These commented printlns are intentionally left behind, since they're useful when updating the snapshot tests


"fallback" - {
"<<User>>(...)" - {
val term = indexer.findSymbol(path, 3, 17).get
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your style! Great idea, much cleaner now 💯

screen shot 2017-11-19 at 13 35 08

Now just need to figure out how to replace those pesky line+column magic numbers with something more readable

}
}

"bijection" - {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved all of the tricky logic here to an InverseSymbolIndexer utility in main, making this test a lot smaller.

It compiles, but lots of ???.
I didn't like how
- the expected output was in a separate file
- it was too easy to create huge snapshot files
I think the ideal snapshot tests should be small and you should be able
to see the expected output alongside the input. This should
be possible by using the same trick as Jane Street does with "expect
tests", where the test framework creates a sibling source file
`X.check`, which is identical to the original test suite `X.scala`
except with the multiline strings fixed. If you are happy with the
expected output in the check file, you can overwrite the original file
with the check file.
- Move everything to _root_.tests package
- Remove relics of snapshot testing
- Configure logback to not spam in tests
- Print out when tests are ignored
This test asserts that we can reconstruct the database from symbol
index. This commit removes the test-workspace build in favor of merging
it into the main build.
- `test-workspace` now compiles before running `metaserver/test`.
- updated contributing guide to reflect this
Previously, I manually created the SymbolIndexer. However, this made it
difficult to be sure the tests were testing the actual server. Now, we
use the server directly instead.
To speed up the edit/test/debug, use ServerConfig to disable several
features like indexing the dependnecy classpath, JDK, or classload
scalafmt.
Added a test suite to validate it works as expected, and realized that

- we didn't expose the method to format without a config file
- the format(String, String, String) signature was unnecessarily
  weakly typed.
- SAM types ftw
- no implicit cwd: AbsolutePath, make it explicit
The IJ formatter will sometimes randomly reorder statements, even if
that completely breaks a ton of code.
This separation from interface and implementation can make it easier for
us to experiment with different approaches.
We were ignoring everything in the scala.meta.languageserver package.
Previously, exceptions would get lost (not appear in logs) when the server
crashed. Now we at least log them so we can debug the problem.
This is the best we got until scalameta#36 gets fixed. I had to adapt the
implementation to accommodate the new URI based indexing scheme.
@olafurpg olafurpg changed the title [WIP] Migrate to new symbol index schema Migrate to new symbol index schema Nov 19, 2017
@olafurpg
Copy link
Member Author

OK, I've rebased on top of master and made the functionality on par with what we have in master. Jump to definition works for project sources and classpath entries.

I will leave it for another PR to actually persist the index on shutdown and load it on startup.

@olafurpg
Copy link
Member Author

I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs
I must not do 1k loc PRs

😅

- missing docstrings
- consistent blank lines
- up-to-date contributing guide
@olafurpg
Copy link
Member Author

Let's merge this PR as is and I am happy to address bugs and regressions in a followup PR

@olafurpg olafurpg merged commit 984e42d into scalameta:master Nov 20, 2017
@olafurpg olafurpg deleted the snapshot branch November 20, 2017 19:56
This was referenced Nov 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants