/** * Computes current element's parent context. * * @return contexts collection */ @NotNull @Override public Collection<PsiFileSystemItem> computeDefaultContexts() { PsiFile containingFile = getElement().getContainingFile(); PsiDirectory containingDirectory = containingFile.getParent(); return containingDirectory != null ? Collections.singletonList(containingDirectory) : super.computeDefaultContexts(); }
@Nullable private PsiDirectory getDirectory() { PsiElement originalElement = CompletionUtil.getOriginalElement(getElement()); PsiFile file = originalElement != null ? originalElement.getContainingFile() : getElement().getContainingFile(); return file.getParent(); } }
public void testDirectory() { PsiFile file = myFixture.configureByText("a.go", "package main"); doTestProducedConfigurations(file.getParent()); }
public void testDirectory() { PsiFile file = myFixture.addFileToProject("import/path/a.go", "package main; func main(){}"); doTestProducedConfigurations(file.getParent()); }
@NotNull public static List<String> getImportPathVariantsToImport(@NotNull String packageName, @NotNull PsiElement context) { PsiFile contextFile = context.getContainingFile(); Set<String> imported = contextFile instanceof GoFile ? ((GoFile)contextFile).getImportedPackagesMap().keySet() : Collections.emptySet(); Project project = context.getProject(); PsiDirectory parentDirectory = contextFile != null ? contextFile.getParent() : null; String testTargetPackage = GoTestFinder.getTestTargetPackage(contextFile); Module module = contextFile != null ? ModuleUtilCore.findModuleForPsiElement(contextFile) : null; boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(module); GlobalSearchScope scope = GoUtil.goPathResolveScope(context); Collection<GoFile> packages = StubIndex.getElements(GoPackagesIndex.KEY, packageName, project, scope, GoFile.class); return sorted(skipNulls(map2Set( packages, file -> { if (parentDirectory != null && parentDirectory.isEquivalentTo(file.getParent())) { if (testTargetPackage == null || !testTargetPackage.equals(file.getPackageName())) { return null; } } if (!GoPsiImplUtil.canBeAutoImported(file, false, module)) { return null; } String importPath = file.getImportPath(vendoringEnabled); return !imported.contains(importPath) ? importPath : null; } )), new MyImportsComparator(context, vendoringEnabled)); }
PsiDirectory parent = originalFile.getParent();
private boolean processUnqualifiedResolve(@NotNull GoFile file, @NotNull GoScopeProcessor processor, @NotNull ResolveState state) { if (getIdentifier().textMatches("_")) return processor.execute(myElement, state); PsiElement parent = myElement.getParent(); if (parent instanceof GoSelectorExpr) { boolean result = processSelector((GoSelectorExpr)parent, processor, state, myElement); if (processor.isCompletion()) return result; if (!result || prevDot(myElement)) return false; } PsiElement grandPa = parent.getParent(); if (grandPa instanceof GoSelectorExpr && !processSelector((GoSelectorExpr)grandPa, processor, state, parent)) return false; if (prevDot(parent)) return false; if (!processBlock(processor, state, true)) return false; if (!processReceiver(processor, state, true)) return false; if (!processImports(file, processor, state, myElement)) return false; if (!processFileEntities(file, processor, state, true)) return false; if (!processDirectory(file.getOriginalFile().getParent(), file, file.getPackageName(), processor, state, true)) return false; return processBuiltin(processor, state, myElement); }
@Override public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) { PsiElement position = parameters.getPosition(); PsiFile file = parameters.getOriginalFile(); ASTNode node = position.getNode(); if (file instanceof GoFile && position.getParent() instanceof GoPackageClause && node.getElementType() == GoTypes.IDENTIFIER) { boolean isTestFile = GoTestFinder.isTestFile(file); PsiDirectory directory = file.getParent(); String currentPackageName = ((GoFile)file).getPackageName(); Collection<String> packagesInDirectory = GoPackageUtil.getAllPackagesInDirectory(directory, null, true); for (String packageName : packagesInDirectory) { if (!packageName.equals(currentPackageName)) { result.addElement(packageLookup(packageName, GoCompletionUtil.PACKAGE_PRIORITY - 1)); } if (isTestFile) { result.addElement(packageLookup(packageName + GoConstants.TEST_SUFFIX, GoCompletionUtil.PACKAGE_PRIORITY)); } } if (directory != null && ContainerUtil.filter(directory.getFiles(), Conditions.instanceOf(GoFile.class)).size() == 1) { String packageFromDirectory = GoPsiImplUtil.getLocalPackageName(directory.getName()); if (!packageFromDirectory.isEmpty()) { result.addElement(packageLookup(packageFromDirectory, GoCompletionUtil.PACKAGE_PRIORITY - 1)); } } result.addElement(packageLookup(GoConstants.MAIN, GoCompletionUtil.PACKAGE_PRIORITY - 2)); } super.fillCompletionVariants(parameters, result); }
private boolean processUnqualifiedResolve(@NotNull GoFile file, @NotNull GoScopeProcessor processor, @NotNull ResolveState state, boolean localResolve) { GoScopeProcessorBase delegate = createDelegate(processor); ResolveUtil.treeWalkUp(myElement, delegate); Collection<? extends GoNamedElement> result = delegate.getVariants(); if (!processNamedElements(processor, state, result, localResolve)) return false; if (!processFileEntities(file, processor, state, localResolve)) return false; PsiDirectory dir = file.getOriginalFile().getParent(); if (!processDirectory(dir, file, file.getPackageName(), processor, state, true)) return false; if (PsiTreeUtil.getParentOfType(getElement(), GoReceiver.class) != null) return true; if (!processImports(file, processor, state, myElement)) return false; if (!processBuiltin(processor, state, myElement)) return false; if (getIdentifier().textMatches(GoConstants.NIL) && PsiTreeUtil.getParentOfType(myElement, GoTypeCaseClause.class) != null) { GoType type = PsiTreeUtil.getParentOfType(myElement, GoType.class); if (FormatterUtil.getPrevious(type != null ? type.getNode() : null, GoTypes.CASE) == null) return true; GoFile builtinFile = GoSdkUtil.findBuiltinFile(myElement); if (builtinFile == null) return false; GoVarDefinition nil = ContainerUtil.find(builtinFile.getVars(), v -> GoConstants.NIL.equals(v.getName())); if (nil != null && !processor.execute(nil, state)) return false; } return true; }
final Pattern pattern = Glob.createPattern(current, entry.getSyntax()); if (pattern != null) { PsiDirectory parent = getElement().getContainingFile().getParent(); final VirtualFile root = isOuterFile ? contextVirtualFile : ((parent != null) ? parent.getVirtualFile() : null);
public void testSameRunApplicationConfigurationOnDirectory() { PsiFile file = myFixture.addFileToProject("import/path/a.go", "package main; func main(){}"); PsiDirectory directory = file.getParent(); assertNotNull(directory); ConfigurationContext configurationContext = new ConfigurationContext(directory); GoApplicationRunConfigurationProducer producer = new GoApplicationRunConfigurationProducer(); GoApplicationConfiguration runConfiguration = createRunAppPackageConfiguration("import/path"); assertTrue(producer.isConfigurationFromContext(runConfiguration, configurationContext)); runConfiguration = createRunAppPackageConfiguration("import/path/other"); assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext)); }
private File relativePathToModuleContentRoots(final @NotNull PsiFile file, final @NotNull Module module, final @NotNull File baseTmpDir) { if (file.getParent() != null) { final String parentUrl = file.getParent().getVirtualFile().getUrl(); for (String moduleSourceRoot : ModuleRootManager.getInstance(module).getContentRootUrls()) { if (parentUrl.startsWith(moduleSourceRoot)) { return new File(baseTmpDir.getAbsolutePath() + parentUrl.substring(moduleSourceRoot.length())); } } } return null; }
private File relativePathToProjectRoot(final @NotNull PsiFile file, final @NotNull File baseTmpDir) { if (file.getParent() != null) { final String baseDirUrl = file.getProject().getBaseDir().getUrl(); final String parentUrl = file.getParent().getVirtualFile().getUrl(); if (parentUrl.startsWith(baseDirUrl)) { return new File(baseTmpDir.getAbsolutePath() + parentUrl.substring(baseDirUrl.length())); } } return null; }
public void testImportPackageWithoutBuildableSource() { PsiFile file = myFixture.addFileToProject("withSources/a.go", "package withSources"); myFixture.addFileToProject("withIgnoredFiles/a.go", "package documentation"); myFixture.addFileToProject("withIgnoredFiles/.b.go", "package withIgnoredFiles"); myFixture.addFileToProject("withIgnoredFiles/_b.go", "package withIgnoredFiles"); WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> { //noinspection ConstantConditions file.getParent().getParent().createSubdirectory("withoutSources"); }); myFixture.configureByText("a.go", "package pack\n" + "import `withSources`\n" + "import <error descr=\"'/src/withIgnoredFiles' has no buildable Go source files\">`withIgnoredFiles`</error>\n" + "import <error descr=\"'/src/withoutSources' has no buildable Go source files\">`withoutSources`</error>\n" + "import <error descr=\"'/src/withoutSources' has no buildable Go source files\">_ `without<caret>Sources`</error>\n" + "import `unresolved`\n"); myFixture.checkHighlighting(); applySingleQuickFix(GoDeleteImportQuickFix.QUICK_FIX_NAME); myFixture.checkResult("package pack\n" + "import `withSources`\n" + "import `withIgnoredFiles`\n" + "import `withoutSources`\n" + "import `unresolved`\n"); }
ContainerUtil.addAll(psiElements, RouteHelper.getMethodsOnControllerShortcut(psiElement.getProject(), content)); PsiDirectory parent = psiElement.getContainingFile().getParent(); if(parent != null) { VirtualFile relativeFile = VfsUtil.findRelativeFile(parent.getVirtualFile(), content.replace("\\", "/").split("/"));
public void testSameConfigurationOnDirectory() { PsiFile file = myFixture.configureByText("a.go", "package main"); PsiDirectory directory = file.getParent(); assertNotNull(directory); ConfigurationContext configurationContext = new ConfigurationContext(directory); GotestRunConfigurationProducer producer = new GotestRunConfigurationProducer(); GoTestRunConfiguration runConfiguration = createDirectoryConfiguration(GotestFramework.INSTANCE, directory.getVirtualFile().getPath(), directory.getVirtualFile().getPath()); assertTrue(producer.isConfigurationFromContext(runConfiguration, configurationContext)); runConfiguration = createDirectoryConfiguration(GotestFramework.INSTANCE, directory.getVirtualFile().getPath() + "_vl", directory.getVirtualFile().getPath()); assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext)); runConfiguration = createDirectoryConfiguration(GobenchFramework.INSTANCE, directory.getVirtualFile().getPath(), directory.getVirtualFile().getPath()); assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext)); runConfiguration = createDirectoryConfiguration(GotestFramework.INSTANCE, directory.getVirtualFile().getPath(), directory.getVirtualFile().getPath() + "_vl"); assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext)); }
/** * Looks for an ANTLR grammar file named {@code <baseName>}.g4 next to the given {@code sibling} file. */ private static PsiFile findTokenVocab(String baseName, PsiFile sibling) { PsiDirectory parentDirectory = sibling.getParent(); if (parentDirectory != null) { PsiFile candidate = parentDirectory.findFile(baseName + ".g4"); if (candidate instanceof ANTLRv4FileRoot) { return candidate; } } return null; } }
@NotNull @Override public Collection<PsiFileSystemItem> computeDefaultContexts() { String path = getPathString(); PsiFile containingFile = getContainingFile(); if (StringUtil.startsWith(path, ".") && containingFile != null && containingFile.getParent() != null) { return Collections.singletonList(containingFile.getParent()); } return super.computeDefaultContexts(); } }.getAllReferences()));
@Nullable private String findBaseDir(CompletionParameters parameters, String usedPrefix) { if (homePrefixes.contains(usedPrefix)) { return BashFiles.userHomeDir(); } PsiDirectory file = parameters.getOriginalFile().getParent(); return file != null ? file.getVirtualFile().getPath() : null; }