use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoExcludePathLookupActionProvider method fillActions.
@Override
public void fillActions(LookupElement element, Lookup lookup, Consumer<LookupElementAction> consumer) {
PsiElement psiElement = element.getPsiElement();
PsiFile file = psiElement != null && psiElement.isValid() ? psiElement.getContainingFile() : null;
String importPath = file instanceof GoFile ? ((GoFile) file).getImportPath(false) : null;
if (importPath != null) {
Project project = psiElement.getProject();
for (String path : getPaths(importPath)) {
consumer.consume(new ExcludePathAction(project, path));
}
consumer.consume(new EditExcludedAction(project));
}
}
use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoImportPackageQuickFix method getImportPathVariantsToImport.
@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));
}
use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoImportPackageQuickFix method doAutoImportOrShowHint.
public boolean doAutoImportOrShowHint(@NotNull Editor editor, boolean showHint) {
PsiElement element = getStartElement();
if (element == null || !element.isValid())
return false;
PsiReference reference = getReference(element);
if (reference == null || reference.resolve() != null)
return false;
List<String> packagesToImport = getImportPathVariantsToImport(element);
if (packagesToImport.isEmpty()) {
return false;
}
PsiFile file = element.getContainingFile();
String firstPackageToImport = getFirstItem(packagesToImport);
// autoimport on trying to fix
if (packagesToImport.size() == 1) {
if (GoCodeInsightSettings.getInstance().isAddUnambiguousImportsOnTheFly() && !LaterInvocator.isInModalContext() && (ApplicationManager.getApplication().isUnitTestMode() || DaemonListeners.canChangeFileSilently(file))) {
CommandProcessor.getInstance().runUndoTransparentAction(() -> perform(file, firstPackageToImport));
return true;
}
}
// show hint on failed autoimport
if (showHint) {
if (ApplicationManager.getApplication().isUnitTestMode())
return false;
if (HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true))
return false;
if (!GoCodeInsightSettings.getInstance().isShowImportPopup())
return false;
TextRange referenceRange = reference.getRangeInElement().shiftRight(element.getTextRange().getStartOffset());
HintManager.getInstance().showQuestionHint(editor, ShowAutoImportPass.getMessage(packagesToImport.size() > 1, getFirstItem(packagesToImport)), referenceRange.getStartOffset(), referenceRange.getEndOffset(), () -> {
if (file.isValid() && !editor.isDisposed()) {
perform(packagesToImport, file, editor);
}
return true;
});
return true;
}
return false;
}
use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoAutoImportInsertHandler method autoImport.
private static void autoImport(@NotNull InsertionContext context, @NotNull GoNamedElement element) {
PsiFile file = context.getFile();
boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(ModuleUtilCore.findModuleForPsiElement(file));
String importPath = element.getContainingFile().getImportPath(vendoringEnabled);
if (StringUtil.isEmpty(importPath))
return;
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
PsiReference reference = file.findReferenceAt(context.getStartOffset());
if (reference != null) {
PsiElement referenceElement = reference.getElement();
GoImportPackageQuickFix fix = new GoImportPackageQuickFix(referenceElement, importPath);
fix.invoke(context.getProject(), file, context.getEditor(), referenceElement, referenceElement);
}
}
use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoCompletionContributor method fillCompletionVariants.
@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);
}
Aggregations