Skip to content

Commit

Permalink
fix relative path saved in meta position after publishing the plugin …
Browse files Browse the repository at this point in the history
…and running with scala-cli
  • Loading branch information
soronpo committed Aug 27, 2024
1 parent 5d3a14c commit 626204d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
7 changes: 7 additions & 0 deletions internals/src/main/scala/dfhdl/internals/MetaContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ final case class Position(

object Position:
val unknown = Position("", 0, 0, 0, 0)
def fromAbsPath(
fileAbsPath: String,
lineStart: Int,
columnStart: Int,
lineEnd: Int,
columnEnd: Int
): Position = Position(getRelativePath(fileAbsPath), lineStart, columnStart, lineEnd, columnEnd)

trait MetaContext:
def setMeta(
Expand Down
6 changes: 6 additions & 0 deletions internals/src/main/scala/dfhdl/internals/helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,9 @@ lazy val scala_cliIsRunning: Boolean = getShellCommand.exists(_.contains(".scala
// detecting if running in Scastie by checking the PWD
lazy val scastieIsRunning: Boolean =
System.getProperty("user.dir").startsWith("/tmp/scastie")

def getRelativePath(absolutePathStr: String): String =
import java.nio.file.Paths
val absolutePath = Paths.get(absolutePathStr).toAbsolutePath()
val currentDir = Paths.get("").toAbsolutePath()
currentDir.relativize(absolutePath).toString
17 changes: 5 additions & 12 deletions plugin/src/main/scala/plugin/CommonPhase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ abstract class CommonPhase extends PluginPhase:
var metaContextIgnoreAnnotSym: ClassSymbol = uninitialized
var metaContextForwardAnnotSym: ClassSymbol = uninitialized
var metaGenSym: Symbol = uninitialized
var positionCls: ClassSymbol = uninitialized
var positionGenSym: TermSymbol = uninitialized
var contextFunctionSym: Symbol = uninitialized
var hasDFCTpe: TypeRef = uninitialized
var inlineAnnotSym: Symbol = uninitialized
Expand Down Expand Up @@ -328,24 +328,17 @@ abstract class CommonPhase extends PluginPhase:
case _ => None
end ContextArg

def getRelativePath(absolutePathStr: String): String =
import java.nio.file.Paths
val absolutePath = Paths.get(absolutePathStr).toAbsolutePath()
val currentDir = Paths.get(System.getProperty("user.dir")).toAbsolutePath()
currentDir.relativize(absolutePath).toString

extension (srcPos: util.SrcPos)(using Context)
def positionTree: Tree =
if (srcPos.span == util.Spans.NoSpan) ref(requiredMethod("dfhdl.internals.Position.unknown"))
else
val fileNameTree = Literal(Constant(getRelativePath(srcPos.startPos.source.path)))
val fileNameTree = Literal(Constant(srcPos.startPos.source.path))
val lineStartTree = Literal(Constant(srcPos.startPos.line + 1))
val columnStartTree = Literal(Constant(srcPos.startPos.column + 1))
val lineEndTree = Literal(Constant(srcPos.endPos.line + 1))
val columnEndTree = Literal(Constant(srcPos.endPos.column + 1))
New(
positionCls.typeRef,
fileNameTree :: lineStartTree :: columnStartTree :: lineEndTree :: columnEndTree :: Nil
ref(positionGenSym).appliedTo(
fileNameTree, lineStartTree, columnStartTree, lineEndTree, columnEndTree
)
end extension

Expand All @@ -371,7 +364,7 @@ abstract class CommonPhase extends PluginPhase:
metaContextIgnoreAnnotSym = requiredClass("dfhdl.internals.metaContextIgnore")
metaContextForwardAnnotSym = requiredClass("dfhdl.internals.metaContextForward")
metaGenSym = requiredMethod("dfhdl.compiler.ir.Meta.gen")
positionCls = requiredClass("dfhdl.internals.Position")
positionGenSym = requiredMethod("dfhdl.internals.Position.fromAbsPath")
hasDFCTpe = requiredClassRef("dfhdl.core.HasDFC")
inlineAnnotSym = requiredClass("scala.inline")
constModTpe = requiredClassRef("dfhdl.core.ISCONST").appliedTo(ConstantType(Constant(true)))
Expand Down

0 comments on commit 626204d

Please sign in to comment.