use of com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager in project intellij by bazelbuild.
the class BuildFileModifierImpl method addRule.
@Override
public boolean addRule(Project project, Label newRule, Kind ruleKind) {
BuildReferenceManager manager = BuildReferenceManager.getInstance(project);
File file = manager.resolvePackage(newRule.blazePackage());
if (file == null) {
return false;
}
LocalFileSystem.getInstance().refreshIoFiles(ImmutableList.of(file));
BuildFile buildFile = manager.resolveBlazePackage(newRule.blazePackage());
if (buildFile == null) {
logger.error("No BUILD file found at location: " + newRule.blazePackage());
return false;
}
buildFile.add(createRule(project, ruleKind, newRule.targetName().toString()));
return true;
}
use of com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager in project intellij by bazelbuild.
the class BlazeBuildSystemServiceTest method testAddDependencyWithBuildTargetPsi.
@Test
public void testAddDependencyWithBuildTargetPsi() throws Exception {
PsiElement buildTargetPsi = mock(PsiElement.class);
PsiFile psiFile = mock(PsiFile.class);
BuildReferenceManager buildReferenceManager = BuildReferenceManager.getInstance(project);
when(buildReferenceManager.resolveLabel(Label.create("//foo:bar"))).thenReturn(buildTargetPsi);
when(buildTargetPsi.getContainingFile()).thenReturn(psiFile);
when(buildTargetPsi.getTextOffset()).thenReturn(1337);
VirtualFile buildFile = VirtualFileSystemProvider.getInstance().getSystem().findFileByPath("/foo/BUILD");
assertThat(buildFile).isNotNull();
when(psiFile.getVirtualFile()).thenReturn(buildFile);
// Doesn't matter.
String dependency = "com.android.foo:bar";
service.addDependency(module, dependency);
ArgumentCaptor<OpenFileDescriptor> descriptorCaptor = ArgumentCaptor.forClass(OpenFileDescriptor.class);
verify(FileEditorManager.getInstance(project)).openTextEditor(descriptorCaptor.capture(), eq(true));
OpenFileDescriptor descriptor = descriptorCaptor.getValue();
assertThat(descriptor.getProject()).isEqualTo(project);
assertThat(descriptor.getFile()).isEqualTo(buildFile);
assertThat(descriptor.getOffset()).isEqualTo(1337);
verifyNoMoreInteractions(FileEditorManager.getInstance(project));
}
use of com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager in project intellij by bazelbuild.
the class ProjectViewLabelReference method resolveFile.
@Nullable
private PsiFileSystemItem resolveFile(String path) {
if (path.startsWith("/") || path.contains(":")) {
return null;
}
BuildReferenceManager manager = BuildReferenceManager.getInstance(getProject());
path = StringUtil.trimStart(path, "-");
File file = manager.resolvePackage(WorkspacePath.createIfValid(path));
if (file == null) {
return null;
}
VirtualFile vf = VirtualFileSystemProvider.getInstance().getSystem().findFileByPath(file.getPath());
if (vf == null) {
return null;
}
PsiManager psiManager = PsiManager.getInstance(getProject());
return vf.isDirectory() ? psiManager.findDirectory(vf) : psiManager.findFile(vf);
}
use of com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager in project intellij by bazelbuild.
the class BlazeGoGotoDeclarationHandler method resolveImportPath.
@Nullable
private static PsiElement resolveImportPath(Project project, String importPath) {
BlazeProjectData projectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
if (projectData == null) {
return null;
}
Map<String, TargetKey> packageMap = BlazeGoRootsProvider.getPackageToTargetMap(project);
if (packageMap == null) {
return null;
}
TargetKey targetKey = packageMap.get(importPath);
if (targetKey == null) {
return null;
}
BuildReferenceManager buildReferenceManager = BuildReferenceManager.getInstance(project);
PsiElement resolvedLabel = buildReferenceManager.resolveLabel(targetKey.label);
if (resolvedLabel != null) {
return resolvedLabel;
}
File blazePackage = WorkspaceHelper.resolveBlazePackage(project, targetKey.label);
return buildReferenceManager.findBuildFile(blazePackage);
}
Aggregations