use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method run.
@Nullable
public GrVariable run() {
PsiElement scope = myContext.getScope();
final PsiClass targetClass = scope instanceof GroovyFileBase ? ((GroovyFileBase) scope).getScriptClass() : (PsiClass) scope;
if (targetClass == null)
return null;
final GrVariableDeclaration declaration = insertField(targetClass);
final GrVariable field = declaration.getVariables()[0];
if (mySettings.removeLocalVar()) {
myLocalVariable = GrIntroduceHandlerBase.resolveLocalVar(myContext);
assert myLocalVariable != null : myContext.getExpression() + ", " + myContext.getVar() + ", " + myContext.getStringPart();
}
GrExpression originalInitializer = getInitializer();
myInitializer = originalInitializer == null ? null : (GrExpression) originalInitializer.copy();
List<PsiElement> replaced = processOccurrences(targetClass, field);
switch(mySettings.initializeIn()) {
case CUR_METHOD:
initializeInMethod(field, replaced);
break;
case FIELD_DECLARATION:
field.setInitializerGroovy(myInitializer);
break;
case CONSTRUCTOR:
initializeInConstructor(field, replaced);
break;
case SETUP_METHOD:
initializeInSetup(field, replaced);
break;
}
JavaCodeStyleManager.getInstance(declaration.getProject()).shortenClassReferences(declaration);
if (mySettings.removeLocalVar()) {
GrIntroduceHandlerBase.deleteLocalVar(myLocalVariable);
}
return field;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GrIntroduceValidatorEngine method validateVariableOccurrencesUp.
private void validateVariableOccurrencesUp(PsiElement startElement, MultiMap<PsiElement, String> conflicts, @NotNull String varName, double startOffset) {
PsiElement prevSibling = startElement.getPrevSibling();
while (prevSibling != null) {
if (!(GroovyRefactoringUtil.isAppropriateContainerForIntroduceVariable(prevSibling) && prevSibling.getTextRange().getEndOffset() < startOffset)) {
validateOccurrencesDown(prevSibling, conflicts, varName, startOffset);
}
prevSibling = prevSibling.getPrevSibling();
}
PsiElement parent = startElement.getParent();
// Do not check context out of method, type definition and directories
if (parent == null || parent instanceof GrMethod || parent instanceof GrTypeDefinition || parent instanceof GroovyFileBase || parent instanceof PsiDirectory) {
return;
}
validateVariableOccurrencesUp(parent, conflicts, varName, startOffset);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GroovyExtractChooser method invoke.
public static InitialInfo invoke(Project project, Editor editor, PsiFile file, int start, int end, boolean forceStatements) throws GrRefactoringError {
PsiDocumentManager.getInstance(project).commitAllDocuments();
if (!(file instanceof GroovyFileBase)) {
throw new GrRefactoringError(GroovyRefactoringBundle.message("only.in.groovy.files"));
}
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file)) {
throw new GrRefactoringError(RefactoringBundle.message("readonly.occurences.found"));
}
PsiDocumentManager.getInstance(project).commitAllDocuments();
final StringPartInfo stringPart = StringPartInfo.findStringPart(file, start, end);
if (stringPart != null) {
return new InitialInfo(new VariableInfo[0], new VariableInfo[0], PsiElement.EMPTY_ARRAY, GrStatement.EMPTY_ARRAY, new ArrayList<>(), stringPart, project, null);
}
final SelectionModel selectionModel = editor.getSelectionModel();
if (!forceStatements) {
GrVariable variable = GrIntroduceHandlerBase.findVariable(file, start, end);
if (variable != null) {
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
TextRange range = initializer.getTextRange();
return buildInfo(project, file, range.getStartOffset(), range.getEndOffset(), forceStatements, selectionModel, variable);
}
}
}
return buildInfo(project, file, start, end, forceStatements, selectionModel, null);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class OverridingTester method doTest.
public void doTest() throws Throwable {
final String testFile = getTestName(true) + ".test";
final List<String> strings = TestUtils.readInput(getTestDataPath() + "/" + testFile);
GroovyFileBase psiFile = (GroovyFileBase) myFixture.addFileToProject("foo.groovy", strings.get(0));
StringBuilder buffer = new StringBuilder();
GrTypeDefinition[] grTypeDefinitions = psiFile.getTypeDefinitions();
GrTypeDefinition lastTypeDefinition = psiFile.getTypeDefinitions()[grTypeDefinitions.length - 1];
PsiMethod[] psiMethods = lastTypeDefinition.getMethods();
for (PsiMethod method : psiMethods) {
PsiMethod[] superMethods = findMethod(method);
String[] classes = sortUseContainingClass(superMethods);
for (String classAsString : classes) {
buffer.append(classAsString);
//between different super methods
buffer.append("\n");
}
//between different methods
buffer.append("\n");
}
//between class definitions
buffer.append("\n");
assertEquals(strings.get(1), buffer.toString().trim());
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase in project intellij-community by JetBrains.
the class GrAliasImportIntention method findUsages.
private static List<UsageInfo> findUsages(PsiMember member, GroovyFileBase file) {
LocalSearchScope scope = new LocalSearchScope(file);
final ArrayList<UsageInfo> infos = new ArrayList<>();
final HashSet<Object> usedRefs = ContainerUtil.newHashSet();
final Processor<PsiReference> consumer = reference -> {
if (usedRefs.add(reference)) {
infos.add(new UsageInfo(reference));
}
return true;
};
if (member instanceof PsiMethod) {
MethodReferencesSearch.search((PsiMethod) member, scope, false).forEach(consumer);
} else {
ReferencesSearch.search(member, scope).forEach(consumer);
if (member instanceof PsiField) {
final PsiMethod getter = GroovyPropertyUtils.findGetterForField((PsiField) member);
if (getter != null) {
MethodReferencesSearch.search(getter, scope, false).forEach(consumer);
}
final PsiMethod setter = GroovyPropertyUtils.findSetterForField((PsiField) member);
if (setter != null) {
MethodReferencesSearch.search(setter, scope, false).forEach(consumer);
}
}
}
return infos;
}
Aggregations