Search in sources :

Example 1 with BuildReferenceManager

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;
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) BuildReferenceManager(com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager) File(java.io.File) BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)

Example 2 with BuildReferenceManager

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));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) BuildReferenceManager(com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager) PsiElement(com.intellij.psi.PsiElement) Test(org.junit.Test)

Example 3 with BuildReferenceManager

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) BuildReferenceManager(com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 4 with BuildReferenceManager

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);
}
Also used : BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) BuildReferenceManager(com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager) GoFile(com.goide.psi.GoFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

Aggregations

BuildReferenceManager (com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 File (java.io.File)3 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 Nullable (javax.annotation.Nullable)2 GoFile (com.goide.psi.GoFile)1 TargetKey (com.google.idea.blaze.base.ideinfo.TargetKey)1 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)1 MockVirtualFile (com.intellij.mock.MockVirtualFile)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PsiManager (com.intellij.psi.PsiManager)1 Test (org.junit.Test)1