use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoTestFunctionCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
Project project = parameters.getPosition().getProject();
PsiFile file = parameters.getOriginalFile();
PsiDirectory containingDirectory = file.getContainingDirectory();
if (file instanceof GoFile && containingDirectory != null) {
CompletionResultSet resultSet = result.withPrefixMatcher(new CamelHumpMatcher(result.getPrefixMatcher().getPrefix(), false));
Collection<String> allPackageFunctionNames = collectAllFunctionNames(containingDirectory);
Set<String> allTestFunctionNames = collectAllTestNames(allPackageFunctionNames, project, (GoFile) file);
String fileNameWithoutTestPrefix = StringUtil.trimEnd(file.getName(), GoConstants.TEST_SUFFIX_WITH_EXTENSION) + ".go";
GlobalSearchScope packageScope = GoPackageUtil.packageScope(containingDirectory, ((GoFile) file).getCanonicalPackageName());
GlobalSearchScope scope = new GoUtil.ExceptTestsScope(packageScope);
IdFilter idFilter = GoIdFilter.getFilesFilter(scope);
for (String functionName : allPackageFunctionNames) {
GoFunctionIndex.process(functionName, project, scope, idFilter, declaration -> {
addVariants(declaration, functionName, fileNameWithoutTestPrefix, allTestFunctionNames, resultSet);
return false;
});
}
Collection<String> methodKeys = ContainerUtil.newTroveSet();
StubIndex.getInstance().processAllKeys(GoMethodIndex.KEY, new CancellableCollectProcessor<>(methodKeys), scope, idFilter);
for (String key : methodKeys) {
Processor<GoMethodDeclaration> processor = declaration -> {
GoMethodDeclarationStubElementType.calcTypeText(declaration);
String typeText = key.substring(Math.min(key.indexOf('.') + 1, key.length()));
String methodName = declaration.getName();
if (methodName != null) {
if (!declaration.isPublic() || declaration.isBlank()) {
return true;
}
String lookupString = !typeText.isEmpty() ? StringUtil.capitalize(typeText) + "_" + methodName : methodName;
addVariants(declaration, lookupString, fileNameWithoutTestPrefix, allTestFunctionNames, resultSet);
}
return true;
};
GoMethodIndex.process(key, project, scope, idFilter, processor);
}
}
}
use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoFmtCheckinFactory method createHandler.
@Override
@NotNull
public CheckinHandler createHandler(@NotNull CheckinProjectPanel panel, @NotNull CommitContext commitContext) {
return new CheckinHandler() {
@Override
public RefreshableOnComponent getBeforeCheckinConfigurationPanel() {
JCheckBox checkBox = new JCheckBox("Go fmt");
return new RefreshableOnComponent() {
@Override
@NotNull
public JComponent getComponent() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(checkBox, BorderLayout.WEST);
return panel;
}
@Override
public void refresh() {
}
@Override
public void saveState() {
PropertiesComponent.getInstance(panel.getProject()).setValue(GO_FMT, Boolean.toString(checkBox.isSelected()));
}
@Override
public void restoreState() {
checkBox.setSelected(enabled(panel));
}
};
}
@Override
public ReturnResult beforeCheckin(@Nullable CommitExecutor executor, PairConsumer<Object, Object> additionalDataConsumer) {
if (enabled(panel)) {
Ref<Boolean> success = Ref.create(true);
FileDocumentManager.getInstance().saveAllDocuments();
for (PsiFile file : getPsiFiles()) {
VirtualFile virtualFile = file.getVirtualFile();
new GoFmtFileAction().doSomething(virtualFile, ModuleUtilCore.findModuleForPsiElement(file), file.getProject(), "Go fmt", true, result -> {
if (!result)
success.set(false);
});
}
if (!success.get()) {
return showErrorMessage(executor);
}
}
return super.beforeCheckin();
}
@NotNull
private ReturnResult showErrorMessage(@Nullable CommitExecutor executor) {
String[] buttons = new String[] { "&Details...", commitButtonMessage(executor, panel), CommonBundle.getCancelButtonText() };
int answer = Messages.showDialog(panel.getProject(), "<html><body>GoFmt returned non-zero code on some of the files.<br/>" + "Would you like to commit anyway?</body></html>\n", "Go Fmt", null, buttons, 0, 1, UIUtil.getWarningIcon());
if (answer == Messages.OK) {
return ReturnResult.CLOSE_WINDOW;
}
if (answer == Messages.NO) {
return ReturnResult.COMMIT;
}
return ReturnResult.CANCEL;
}
@NotNull
private List<PsiFile> getPsiFiles() {
Collection<VirtualFile> files = panel.getVirtualFiles();
List<PsiFile> psiFiles = ContainerUtil.newArrayList();
PsiManager manager = PsiManager.getInstance(panel.getProject());
for (VirtualFile file : files) {
PsiFile psiFile = manager.findFile(file);
if (psiFile instanceof GoFile) {
psiFiles.add(psiFile);
}
}
return psiFiles;
}
};
}
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;
}
Aggregations