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()) } }