use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.
the class KeywordArgumentReference method resolve.
/**
* Find the referenced function. If it has a keyword parameter with matching name, return that.
* Otherwise if it has a **kwargs param, return that. Else return the function itself.
*/
@Nullable
@Override
public PsiElement resolve() {
String keyword = myElement.getName();
if (keyword == null) {
return null;
}
FunctionStatement function = resolveFunction();
if (function == null) {
return null;
}
Parameter.StarStar kwargsParameter = null;
for (Parameter param : function.getParameters()) {
if (param instanceof Parameter.StarStar) {
kwargsParameter = (Parameter.StarStar) param;
continue;
}
if (keyword.equals(param.getName())) {
return param;
}
}
if (kwargsParameter != null) {
return kwargsParameter;
}
return null;
}
use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.
the class BuildDocumentationProvider method buildDocs.
private static String buildDocs(DocStringOwner element) {
StringBuilder docs = new StringBuilder();
describeFile(element.getContainingFile(), docs, !(element instanceof BuildFile));
if (element instanceof FunctionStatement) {
describeFunction((FunctionStatement) element, docs);
}
StringLiteral docString = element.getDocString();
if (docString != null) {
docs.append(DocStringFormatter.formatDocString(docString, element));
}
return wrapDocInHtml(docs.toString());
}
use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.
the class FindParameterUsagesTest method testNonLocalReferences.
@Test
public void testNonLocalReferences() {
BuildFile foo = createBuildFile(new WorkspacePath("java/com/google/build_defs.bzl"), "def function(arg1, arg2)");
BuildFile bar = createBuildFile(new WorkspacePath("java/com/google/other/BUILD"), "load(\"//java/com/google:build_defs.bzl\", \"function\")", "function(arg1 = 1, arg2 = \"name\", extra = x)");
FunctionStatement fn = foo.findChildByClass(FunctionStatement.class);
ParameterList params = fn.getParameterList();
PsiReference[] references = FindUsages.findAllReferences(params.findParameterByName("arg1"));
assertThat(references).hasLength(1);
assertThat(references[0].getElement().getContainingFile()).isEqualTo(bar);
references = FindUsages.findAllReferences(params.findParameterByName("arg2"));
assertThat(references).hasLength(1);
assertThat(references[0].getElement().getContainingFile()).isEqualTo(bar);
}
use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.
the class FunctionStatementUsagesTest method testFuncallReference.
@Test
public void testFuncallReference() {
BuildFile extFile = createBuildFile(new WorkspacePath("java/com/google/tools/build_defs.bzl"), "def function(name, deps)");
BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "load(", "\"//java/com/google/tools:build_defs.bzl\",", "\"function\"", ")", "function(name = \"name\", deps = []");
FunctionStatement function = extFile.firstChildOfClass(FunctionStatement.class);
FuncallExpression funcall = buildFile.firstChildOfClass(FuncallExpression.class);
PsiReference[] references = FindUsages.findAllReferences(function);
assertThat(references).hasLength(2);
assertThat(references[1].getElement()).isEqualTo(funcall);
}
use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.
the class FunctionStatementUsagesTest method testLoadedFunctionReferences.
@Test
public void testLoadedFunctionReferences() {
BuildFile extFile = createBuildFile(new WorkspacePath("java/com/google/build_defs.bzl"), "def function(name, deps)");
BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "load(", "\"//java/com/google:build_defs.bzl\",", "\"function\"", ")");
FunctionStatement funcDef = extFile.findChildByClass(FunctionStatement.class);
LoadStatement load = buildFile.firstChildOfClass(LoadStatement.class);
PsiReference[] references = FindUsages.findAllReferences(funcDef);
assertThat(references).hasLength(1);
PsiElement ref = references[0].getElement();
assertThat(ref).isInstanceOf(StringLiteral.class);
assertThat(ref.getParent().getParent()).isEqualTo(load);
}
Aggregations