From 5771e4790eb77b743b8c2933a8c1e645e39df077 Mon Sep 17 00:00:00 2001 From: Nick Rolfe Date: Tue, 10 Nov 2020 16:50:10 +0000 Subject: [PATCH] Replace getEncodedFile with getFileBySourceArchiveName predicate While also making it work with paths for databases created on Windows. --- ql/src/printAst.ql | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ql/src/printAst.ql b/ql/src/printAst.ql index d1fba550195..e02a35d2163 100644 --- a/ql/src/printAst.ql +++ b/ql/src/printAst.ql @@ -20,13 +20,23 @@ external string selectedSourceFile(); * output of `.getFile()` on locatable entities. */ cached -File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name } +File getFileBySourceArchiveName(string name) { + // The name provided for a file in the source archive by the VS Code extension + // has some differences from the absolute path in the database: + // 1. colons are replaced by underscores + // 2. there's a leading slash, even for Windows paths: "C:/foo/bar" -> + // "/C_/foo/bar" + // 3. double slashes in UNC prefixes are replaced with a single slash + // We can handle 2 and 3 together by unconditionally adding a leading slash + // before replacing double slashes. + name = ("/" + result.getAbsolutePath().replaceAll(":", "_")).replaceAll("//", "/") +} /** * Overrides the configuration to print only nodes in the selected source file. */ class Cfg extends PrintAstConfiguration { override predicate shouldPrintNode(AstNode n) { - n.getLocation().getFile() = getEncodedFile(selectedSourceFile()) + n.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile()) } }