-
Notifications
You must be signed in to change notification settings - Fork 317
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
Conversation
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.
|
||
object InputEnrichments { | ||
implicit class XtensionInputOffset(val input: Input) extends AnyVal { | ||
def fromIndexRange(range: i.Range): Position = { |
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.
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 { |
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.
micro-optimization, but this could be a SAM
def empty: Formatter = (code, _, _) => code
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.
also maybe noop
instead of empty
?
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.
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) |
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.
isn't indexJDK = true
already the default?
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.
It is, removed!
|
||
def updated(symbol: String)(f: SymbolIndex => SymbolIndex): Unit = { | ||
val value = symbols.getOrElseUpdate(symbol, newValue(symbol)) | ||
value.getAndUpdate(new UnaryOperator[SymbolIndex] { |
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.
This too can be a SAM, I think.
I haven't tried, but this may actually be as simple as:
value.getAndUpdate(f)
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.
I can make the SAM conversion happen to the call to updated
, however that makes IJ unhappy even if it compiles 😢
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] { |
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.
def index: Traversable[SymbolIndex] = f => symbols.values.foreach(s => f(s.get))
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.
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 { |
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.
Mega? 😄
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.
Didn't know what to call it, but it's mega good 😄
|
||
"fallback" - { | ||
"<<User>>(...)" - { | ||
val term = indexer.findSymbol(path, 3, 17).get |
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.
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")
}
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.
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.
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 { |
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.
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) |
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.
It is, removed!
|
||
} | ||
|
||
object SymbolIndexer { | ||
val emptyDocument: Document = Document(Input.None, "", Nil, Nil, Nil, Nil) | ||
def empty(cwd: AbsolutePath): SymbolIndexer = apply( | ||
new Notifications { |
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.
Converted to SAM!
new AtomicReference(SymbolIndex(symbol = symbol)) | ||
} | ||
|
||
def index: Traversable[SymbolIndex] = new Traversable[SymbolIndex] { |
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.
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] { |
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.
I can make the SAM conversion happen to the call to updated
, however that makes IJ unhappy even if it compiles 😢
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 { |
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.
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) |
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.
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 |
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.
} | ||
} | ||
|
||
"bijection" - { |
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.
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.
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. |
I must not do 1k loc PRs 😅 |
- missing docstrings - consistent blank lines - up-to-date contributing guide
Let's merge this PR as is and I am happy to address bugs and regressions in a followup PR |
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 classloadingNotifications
: make it possible to programmatically load a SymbolIndex without an lsp server.Formatter.empty
: formatter that doesn't do anythingDocumentStore
: abstract key/value store forURI -> 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 newSymbolIndex
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 inUserTest.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.