mirror of
https://github.com/github/codeql.git
synced 2025-12-18 01:33:15 +01:00
Python: Refactor definitions query, add queries for ide search
This enables jump-to-definition and find-references in the VS Code extension, for python source archives.
This commit is contained in:
@@ -490,3 +490,24 @@ class NiceLocationExpr extends @py_expr {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an element, of kind `kind`, that element `e` uses, if any.
|
||||
*/
|
||||
cached
|
||||
Definition definitionOf(NiceLocationExpr use, string kind) {
|
||||
exists(string f, int l |
|
||||
result = getUniqueDefinition(use) and
|
||||
kind = "Definition" and
|
||||
use.hasLocationInfo(f, l, _, _, _) and
|
||||
// Ignore if the definition is on the same line as the use
|
||||
not result.getLocation().hasLocationInfo(f, l, _, _, _))
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }
|
||||
|
||||
@@ -8,11 +8,6 @@
|
||||
import python
|
||||
import DefinitionTracking
|
||||
|
||||
from NiceLocationExpr use, Definition defn, string kind, string f, int l
|
||||
where
|
||||
defn = getUniqueDefinition(use) and
|
||||
kind = "Definition" and
|
||||
use.hasLocationInfo(f, l, _, _, _) and
|
||||
// Ignore if the definition is on the same line as the use
|
||||
not defn.getLocation().hasLocationInfo(f, l, _, _, _)
|
||||
select use, defn, kind
|
||||
from NiceLocationExpr use, Definition defn, string kind
|
||||
where defn = definitionOf(use, kind)
|
||||
select use, defn, kind
|
||||
18
python/ql/src/analysis/LocalDefinitions.ql
Normal file
18
python/ql/src/analysis/LocalDefinitions.ql
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @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 python/ide-jump-to-definition
|
||||
* @tags ide-contextual-queries/local-definitions
|
||||
*/
|
||||
|
||||
import python
|
||||
import DefinitionTracking
|
||||
|
||||
external string selectedSourceFile();
|
||||
|
||||
from NiceLocationExpr use, Definition defn, string kind
|
||||
where defn = definitionOf(use, kind)
|
||||
and use.(Expr).getLocation().getFile() = getEncodedFile(selectedSourceFile())
|
||||
select use, defn, kind
|
||||
18
python/ql/src/analysis/LocalReferences.ql
Normal file
18
python/ql/src/analysis/LocalReferences.ql
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @name Find-references links
|
||||
* @description Generates use-definition pairs that provide the data
|
||||
* for find-references in the code viewer.
|
||||
* @kind definitions
|
||||
* @id python/ide-find-references
|
||||
* @tags ide-contextual-queries/local-references
|
||||
*/
|
||||
|
||||
import python
|
||||
import DefinitionTracking
|
||||
|
||||
external string selectedSourceFile();
|
||||
|
||||
from NiceLocationExpr use, Definition defn, string kind
|
||||
where defn = definitionOf(use, kind)
|
||||
and defn.getLocation().getFile() = getEncodedFile(selectedSourceFile())
|
||||
select use, defn, kind
|
||||
Reference in New Issue
Block a user