From 5d090fa0b8d9d45e2fc5516984587aceb70b1ba5 Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Tue, 20 Sep 2022 15:11:08 +0200 Subject: [PATCH 1/3] Expose `dirname` from `SourceCodePos` This will enable `Spec` to print absolute or project-root relative file paths. --- core/SourceCodePos.savi | 1 + src/savi/compiler/code_gen.cr | 1 + 2 files changed, 2 insertions(+) diff --git a/core/SourceCodePos.savi b/core/SourceCodePos.savi index 98cc99d4..d2f4d404 100644 --- a/core/SourceCodePos.savi +++ b/core/SourceCodePos.savi @@ -2,5 +2,6 @@ // TODO: include more properties from the original Savi::Source::Pos? :var string String: "" :var filename String: "" + :var dirname String: "" :var row USize: 0 :var col USize: 0 diff --git a/src/savi/compiler/code_gen.cr b/src/savi/compiler/code_gen.cr index d663f984..250ddd15 100644 --- a/src/savi/compiler/code_gen.cr +++ b/src/savi/compiler/code_gen.cr @@ -3264,6 +3264,7 @@ class Savi::Compiler::CodeGen global = gen_global_const(@gtypes["SourceCodePosition"], { "string" => gen_string(pos.content), "filename" => gen_string(pos.source.filename), + "dirname" => gen_string(pos.source.dirname), "row" => @isize.const_int(pos.row), "col" => @isize.const_int(pos.col), }) From 5206b3ea8f114558042c96bf7e1ec0634907c708 Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Tue, 20 Sep 2022 16:06:28 +0200 Subject: [PATCH 2/3] Expose `filename_pkgrel`, `pkgname` and `pkgpath` in `SourceCodePos` --- core/SourceCodePos.savi | 4 ++++ src/savi/compiler/code_gen.cr | 13 ++++++++----- src/savi/source.cr | 8 ++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/core/SourceCodePos.savi b/core/SourceCodePos.savi index d2f4d404..4a7f150a 100644 --- a/core/SourceCodePos.savi +++ b/core/SourceCodePos.savi @@ -2,6 +2,10 @@ // TODO: include more properties from the original Savi::Source::Pos? :var string String: "" :var filename String: "" + :: The filepath relative to the package + :var filepath_pkgrel String: "" :var dirname String: "" + :var pkgname String: "" + :var pkgpath String: "" :var row USize: 0 :var col USize: 0 diff --git a/src/savi/compiler/code_gen.cr b/src/savi/compiler/code_gen.cr index 250ddd15..f9e6b9ae 100644 --- a/src/savi/compiler/code_gen.cr +++ b/src/savi/compiler/code_gen.cr @@ -3262,11 +3262,14 @@ class Savi::Compiler::CodeGen def gen_source_code_pos(pos : Source::Pos) @source_code_pos_globals.fetch pos do global = gen_global_const(@gtypes["SourceCodePosition"], { - "string" => gen_string(pos.content), - "filename" => gen_string(pos.source.filename), - "dirname" => gen_string(pos.source.dirname), - "row" => @isize.const_int(pos.row), - "col" => @isize.const_int(pos.col), + "string" => gen_string(pos.content), + "filename"=> gen_string(pos.source.filename), + "dirname" => gen_string(pos.source.dirname), + "pkgname" => gen_string(pos.source.package.name), + "pkgpath" => gen_string(pos.source.package.path), + "row" => @isize.const_int(pos.row), + "col" => @isize.const_int(pos.col), + "filepath_pkgrel" => gen_string(pos.source.filepath_relative_to_package), }) @source_code_pos_globals[pos] = global diff --git a/src/savi/source.cr b/src/savi/source.cr index 2f1119d5..9f223d36 100644 --- a/src/savi/source.cr +++ b/src/savi/source.cr @@ -14,6 +14,14 @@ struct Savi::Source File.join(@dirname, @filename) end + def filepath_relative_to_package + if reldir = Path.new(@dirname).relative_to?(@package.path) + (reldir / @filename).to_s + else + raise "File #{path} not relative to package #{@package.path}" + end + end + def entire_pos Source::Pos.index_range(self, 0, content.bytesize) end From f433cfa890034f9b938de13d437ebb0701bb29ac Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Tue, 20 Sep 2022 20:28:49 +0200 Subject: [PATCH 3/3] Add `filepath_rootpkgrel` to `SourceCodePos` --- core/SourceCodePos.savi | 2 ++ src/savi/compiler/code_gen.cr | 2 ++ 2 files changed, 4 insertions(+) diff --git a/core/SourceCodePos.savi b/core/SourceCodePos.savi index 4a7f150a..cb462f7e 100644 --- a/core/SourceCodePos.savi +++ b/core/SourceCodePos.savi @@ -4,6 +4,8 @@ :var filename String: "" :: The filepath relative to the package :var filepath_pkgrel String: "" + :: The filepath relative to the root package + :var filepath_rootpkgrel String: "" :var dirname String: "" :var pkgname String: "" :var pkgpath String: "" diff --git a/src/savi/compiler/code_gen.cr b/src/savi/compiler/code_gen.cr index f9e6b9ae..0db546cf 100644 --- a/src/savi/compiler/code_gen.cr +++ b/src/savi/compiler/code_gen.cr @@ -3270,6 +3270,8 @@ class Savi::Compiler::CodeGen "row" => @isize.const_int(pos.row), "col" => @isize.const_int(pos.col), "filepath_pkgrel" => gen_string(pos.source.filepath_relative_to_package), + "filepath_rootpkgrel" => gen_string((Path.new(pos.source.dirname).relative_to( + Path.new(@ctx.not_nil!.root_package_link.path)) / pos.source.filename).to_s) }) @source_code_pos_globals[pos] = global