Merge pull request #3405 from jcreedcmu/jcreed/jump-to-def-python

Python: Refactor definitions query, add queries for ide search
This commit is contained in:
Taus
2020-05-07 12:51:35 +02:00
committed by GitHub
5 changed files with 67 additions and 8 deletions

View File

@@ -490,3 +490,25 @@ class NiceLocationExpr extends @py_expr {
)
}
}
/**
* Gets the definition (of kind `kind`) for the expression `use`, if one can be found.
*/
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 }

View File

@@ -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

View File

@@ -0,0 +1,19 @@
/**
* @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, string f
where defn = definitionOf(use, kind)
and use.hasLocationInfo(f, _, _, _, _)
and getEncodedFile(selectedSourceFile()).getAbsolutePath() = f
select use, defn, kind

View 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

View File

@@ -2,3 +2,8 @@
- qlpack: codeql-python
- apply: lgtm-selectors.yml
from: codeql-suite-helpers
# These are only for IDE use.
- exclude:
tags contain:
- ide-contextual-queries/local-definitions
- ide-contextual-queries/local-references