Skip to content

Commit d706d06

Browse files
committed
util: add FileLoc.toFilenameOnly
1 parent 8ac541d commit d706d06

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/util/fan/FileLoc.fan

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ const class FileLoc
114114
return "$file($line,$col)"
115115
}
116116

117+
** Simplify the file path to remove path
118+
@NoDoc FileLoc toFilenameOnly()
119+
{
120+
slash := file.indexr("/") ?: file.indexr("\\")
121+
if (slash == null) return this
122+
return make(file[slash+1..-1], line, col)
123+
}
117124
}
118125

119126
**************************************************************************

src/util/test/FileLocTest.fan

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class FileLocTest : Test
2424
verifyCmd(FileLoc("a", 2, 5), FileLoc("a", 2, 5), 0)
2525
verifyCmd(FileLoc("a", 2, 5), FileLoc("a", 2, 7), -1)
2626
verifyCmd(FileLoc("a", 2, 9), FileLoc("a", 2, 7), 1)
27+
28+
verifyLoc(FileLoc("foo.txt").toFilenameOnly, "foo.txt", 0, 0)
29+
verifyLoc(FileLoc("foo.txt", 1, 2).toFilenameOnly, "foo.txt", 1, 2)
30+
verifyLoc(FileLoc("/path/foo.txt").toFilenameOnly, "foo.txt", 0, 0)
31+
verifyLoc(FileLoc("\\path\\foo.txt", 1, 2).toFilenameOnly, "foo.txt", 1, 2)
32+
verifyLoc(FileLoc("/long/path/here/foo.txt", 2, 3).toFilenameOnly, "foo.txt", 2, 3)
2733
}
2834

2935
Void verifyLoc(FileLoc loc, Str file, Int line, Int col)
@@ -42,4 +48,5 @@ class FileLocTest : Test
4248
verifyEq(a < b, expected < 0)
4349
verifyEq(a > b, expected > 0)
4450
}
45-
}
51+
}
52+

0 commit comments

Comments
 (0)