use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method initializeInMethod.
void initializeInMethod(@NotNull GrVariable field, @NotNull List<PsiElement> replaced) {
final PsiElement _scope = myContext.getScope();
final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass) _scope).getContainingFile() : _scope;
final PsiElement place = replaced.get(0);
final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
GrStatementOwner container = member instanceof GrMethod ? ((GrMethod) member).getBlock() : member instanceof GrClassInitializer ? ((GrClassInitializer) member).getBlock() : place.getContainingFile() instanceof GroovyFile ? ((GroovyFile) place.getContainingFile()) : null;
assert container != null;
final PsiElement anchor;
if (mySettings.removeLocalVar()) {
GrVariable variable = myLocalVariable;
anchor = PsiTreeUtil.getParentOfType(variable, GrStatement.class);
} else {
anchor = GrIntroduceHandlerBase.findAnchor(replaced.toArray(new PsiElement[replaced.size()]), container);
GrIntroduceHandlerBase.assertStatement(anchor, myContext.getScope());
}
initializeInMethodInner(field, container, (GrStatement) anchor, replaced.get(0));
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method insertField.
@NotNull
protected GrVariableDeclaration insertField(@NotNull PsiClass targetClass) {
GrVariableDeclaration declaration = createField(targetClass);
if (targetClass instanceof GrEnumTypeDefinition) {
final GrEnumConstantList enumConstants = ((GrEnumTypeDefinition) targetClass).getEnumConstantList();
return (GrVariableDeclaration) targetClass.addAfter(declaration, enumConstants);
}
if (targetClass instanceof GrTypeDefinition) {
PsiElement anchor = getAnchorForDeclaration((GrTypeDefinition) targetClass);
return (GrVariableDeclaration) targetClass.addAfter(declaration, anchor);
} else {
assert targetClass instanceof GroovyScriptClass;
final GroovyFile file = ((GroovyScriptClass) targetClass).getContainingFile();
PsiElement[] elements = file.getMethods();
if (elements.length == 0)
elements = file.getStatements();
final PsiElement anchor = ArrayUtil.getFirstElement(elements);
return (GrVariableDeclaration) file.addBefore(declaration, anchor);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GrIntroduceConstantDialog method initializeTargetClassEditor.
private void initializeTargetClassEditor() {
myTargetClassEditor = new ReferenceEditorComboWithBrowseButton(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TreeClassChooser chooser = TreeClassChooserFactory.getInstance(myContext.getProject()).createWithInnerClassesScopeChooser(RefactoringBundle.message("choose.destination.class"), GlobalSearchScope.projectScope(myContext.getProject()), new ClassFilter() {
@Override
public boolean isAccepted(PsiClass aClass) {
return aClass.getParent() instanceof GroovyFile || aClass.hasModifierProperty(PsiModifier.STATIC);
}
}, null);
if (myTargetClass != null) {
chooser.selectDirectory(myTargetClass.getContainingFile().getContainingDirectory());
}
chooser.showDialog();
PsiClass aClass = chooser.getSelected();
if (aClass != null) {
myTargetClassEditor.setText(aClass.getQualifiedName());
}
}
}, "", myContext.getProject(), true, RECENTS_KEY);
myTargetClassPanel.setLayout(new BorderLayout());
myTargetClassPanel.add(myTargetClassLabel, BorderLayout.NORTH);
myTargetClassPanel.add(myTargetClassEditor, BorderLayout.CENTER);
Set<String> possibleClassNames = new LinkedHashSet<>();
for (final PsiElement occurrence : myContext.getOccurrences()) {
final PsiClass parentClass = getParentClass(occurrence);
if (parentClass != null && parentClass.getQualifiedName() != null) {
possibleClassNames.add(parentClass.getQualifiedName());
}
}
for (String possibleClassName : possibleClassNames) {
myTargetClassEditor.prependItem(possibleClassName);
}
if (myDefaultTargetClass != null) {
myTargetClassEditor.prependItem(myDefaultTargetClass.getQualifiedName());
}
myTargetClassEditor.getChildComponent().addDocumentListener(new DocumentAdapter() {
@Override
public void documentChanged(DocumentEvent e) {
targetClassChanged();
updateOkStatus();
// enableEnumDependant(introduceEnumConstant());
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyImportOptimizerRefactoringHelper method performOperation.
@Override
public void performOperation(final Project project, final Set<GroovyFile> files) {
final ProgressManager progressManager = ProgressManager.getInstance();
final Map<GroovyFile, Pair<List<GrImportStatement>, Set<GrImportStatement>>> redundants = new HashMap<>();
final Runnable findUnusedImports = () -> {
final ProgressIndicator progressIndicator = progressManager.getProgressIndicator();
final int total = files.size();
int i = 0;
for (final GroovyFile file : files) {
if (!file.isValid())
continue;
final VirtualFile virtualFile = file.getVirtualFile();
if (!ProjectRootManager.getInstance(project).getFileIndex().isInSource(virtualFile)) {
continue;
}
if (progressIndicator != null) {
progressIndicator.setText2(virtualFile.getPresentableUrl());
progressIndicator.setFraction((double) i++ / total);
}
ApplicationManager.getApplication().runReadAction(() -> {
final Set<GrImportStatement> usedImports = GroovyImportUtil.findUsedImports(file);
final List<GrImportStatement> validImports = PsiUtil.getValidImportStatements(file);
redundants.put(file, Pair.create(validImports, usedImports));
});
}
};
if (!progressManager.runProcessWithProgressSynchronously(findUnusedImports, "Optimizing imports (Groovy) ... ", false, project)) {
return;
}
WriteAction.run(() -> {
for (GroovyFile groovyFile : redundants.keySet()) {
if (!groovyFile.isValid())
continue;
final Pair<List<GrImportStatement>, Set<GrImportStatement>> pair = redundants.get(groovyFile);
final List<GrImportStatement> validImports = pair.getFirst();
final Set<GrImportStatement> usedImports = pair.getSecond();
for (GrImportStatement importStatement : validImports) {
if (!usedImports.contains(importStatement)) {
groovyFile.removeImport(importStatement);
}
}
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyRefactoringSupportProvider method isMemberInplaceRenameAvailable.
@Override
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement element, @Nullable PsiElement context) {
if (context == null || context.getContainingFile() instanceof GroovyFile)
return false;
PsiElement parent = context.getParent();
//don't try to inplace rename aliased imported references
if (parent instanceof GrReferenceElement) {
GroovyResolveResult result = ((GrReferenceElement) parent).advancedResolve();
PsiElement fileResolveContext = result.getCurrentFileResolveContext();
if (fileResolveContext instanceof GrImportStatement && ((GrImportStatement) fileResolveContext).isAliasedImport()) {
return false;
}
}
return element instanceof GrMember;
}
Aggregations