Skip to content

Added logic to avoid losing range information#771

Merged
lihaoyi merged 3 commits intocom-lihaoyi:masterfrom
Baccata:transform-range-positions
Mar 18, 2018
Merged

Added logic to avoid losing range information#771
lihaoyi merged 3 commits intocom-lihaoyi:masterfrom
Baccata:transform-range-positions

Conversation

@Baccata
Copy link
Copy Markdown
Contributor

@Baccata Baccata commented Mar 15, 2018

  • Adds logic to shift the point in range positions (it does not shift start / end because of some internal validation that crashes the compilation)
  • Shares the source file trimming between nodes for optimisation

Rationale :

When compiling ammonite script with YrangePos enabled, the transformation performed in the AmmonitePlugin was discarding the "range" nature of the position. This allows to conserve range information, which improves interoperability with some compiler plugins.

* Adds logic to shift the point in range positions (it does not shift
start / end because of some internal validation)
* Shares the source file trimming between nodes for optimisation
import scala.reflect.internal.util._

private val trimmedSource = new BatchSourceFile(g.currentSource.file,
g.currentSource.content.drop(topWrapperLen))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is an optimisation to avoid re-trimming the source for every node on the tree

@Baccata Baccata changed the title Added logic to avoid losing range information [WIP] Added logic to avoid losing range information Mar 15, 2018
@Baccata Baccata changed the title [WIP] Added logic to avoid losing range information Added logic to avoid losing range information Mar 15, 2018
new TransparentPosition(trimmedSource, s.start, s.point - topWrapperLen, s.end)
case s: RangePosition if s.start > topWrapperLen =>
new RangePosition(trimmedSource, s.start, s.point - topWrapperLen, s.end)
case s: OffsetPosition if s.start > topWrapperLen =>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

s.start == s.point in this case

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't the startIn of the RangePosition also be s.start - topWrapperLen? and the endIn be s.end - topWrapperLen?

Copy link
Copy Markdown
Contributor Author

@Baccata Baccata Mar 16, 2018

Choose a reason for hiding this comment

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

Ideally it would be the case, but the compiler performs some later validation on the trees to make sure the ranges aren't overlapping, which throws when I try what your suggest. I'm not quite sure why, it could have to do with multi-phase scripts.

With the current change, the behaviour of ammonite remains the same whilst allowing external tools (semanticdb) to work around this issue by adding post-processing logic that would look up the position of the symbol matching the filename (thanks for that btw 😄) and applying the appropriate shift (outside of the scalac's kingdome)

I can open a ticket for further investigation and reference it in a TODO in this piece of code, if that is acceptable

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is a ticket to tone down position validation errors in the compiler scala/scala-dev#390 The validation is required for Scala IDE to efficiently find enclosing trees given a position. semanticdb does not require the validated properties to hold so the errors are unnecessary.

Copy link
Copy Markdown
Member

@lihaoyi lihaoyi Mar 18, 2018

Choose a reason for hiding this comment

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

Sure that sounds good to me; @Baccata if you could put your explanation here as a comment in the code that's all I ask before merging

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done ! fb8ed68

@Baccata
Copy link
Copy Markdown
Contributor Author

Baccata commented Mar 15, 2018

Not sure I'll ever finish shaving the yak, but my hope is to get ammonite and semanticdb-scalac compatible with each other, eventually allowing some tools written for standard Scala to work with ammonite scripts (maybe even LSP support while we're at it ^^)

Added tests to check that the ammonite plugin shifting the point in
RangePosition (rather than upcasting them to OffsetPositions) does not
temper with the behaviour of Ammonite
Baccata added a commit to Baccata/Ammonite that referenced this pull request Mar 18, 2018
Added a comment to explain why `start` and `end` values aren't
shifted in Range positions.

com-lihaoyi#771 (comment)
Added a comment to explain why `start` and `end` values aren't
shifted in Range positions.

com-lihaoyi#771 (comment)
@Baccata Baccata force-pushed the transform-range-positions branch from af2c9e0 to fb8ed68 Compare March 18, 2018 12:04
@lihaoyi
Copy link
Copy Markdown
Member

lihaoyi commented Mar 18, 2018

Looks good to me, thanks!

@lihaoyi lihaoyi merged commit ac754ee into com-lihaoyi:master Mar 18, 2018
@Baccata Baccata deleted the transform-range-positions branch March 18, 2018 15:19
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.

3 participants