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:
Jason Reed
2020-05-04 10:39:59 -04:00
parent 29a5ea121a
commit b0f72ebb56
5 changed files with 64 additions and 8 deletions

View File

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

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

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