diff --git a/ql/src/codeql/IDE.qll b/ql/src/codeql/IDE.qll new file mode 100644 index 00000000000..0e58b1d878b --- /dev/null +++ b/ql/src/codeql/IDE.qll @@ -0,0 +1,19 @@ +private import codeql.files.FileSystem + +/** + * Returns an appropriately encoded version of a filename `name` + * passed by the VS Code extension in order to coincide with the + * output of `.getFile()` on locatable entities. + */ +cached +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("//", "/") +} diff --git a/ql/src/localDefinitions.ql b/ql/src/localDefinitions.ql new file mode 100644 index 00000000000..3054a6e4ca0 --- /dev/null +++ b/ql/src/localDefinitions.ql @@ -0,0 +1,20 @@ +/** + * @name Jump-to-definition links + * @description Generates use-definition pairs that provide the data + * for jump-to-definition in the code viewer. + * @kind definitions + * @id ruby/ide-jump-to-definition + * @tags ide-contextual-queries/local-definitions + */ + +import codeql.IDE +import codeql_ruby.AST + +external string selectedSourceFile(); + +from AstNode e, Variable def, string kind +where + e = def.getAnAccess() and + kind = "local variable" and + e.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile()) +select e, def, kind diff --git a/ql/src/localReferences.ql b/ql/src/localReferences.ql new file mode 100644 index 00000000000..42a7168ace6 --- /dev/null +++ b/ql/src/localReferences.ql @@ -0,0 +1,21 @@ +/** + * @name Find-references links + * @description Generates use-definition pairs that provide the data + * for find-references in the code viewer. + * @kind definitions + * @id ruby/ide-find-references + * @tags ide-contextual-queries/local-references + */ + +import codeql.IDE +import codeql_ruby.AST +import codeql_ruby.ast.Variable + +external string selectedSourceFile(); + +from AstNode e, Variable def, string kind +where + e = def.getAnAccess() and + kind = "local variable" and + def.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile()) +select e, def, kind diff --git a/ql/src/printAst.ql b/ql/src/printAst.ql index 92898a25228..c69ca543f6b 100644 --- a/ql/src/printAst.ql +++ b/ql/src/printAst.ql @@ -8,31 +8,13 @@ */ import codeql_ruby.printAst -import codeql.files.FileSystem +import codeql.IDE /** * The source file to generate an AST from. */ external string selectedSourceFile(); -/** - * Returns an appropriately encoded version of a filename `name` - * passed by the VS Code extension in order to coincide with the - * output of `.getFile()` on locatable entities. - */ -cached -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. */