use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.
the class AntTasksProvider method getAntObjects.
private static Map<String, Class> getAntObjects(final GroovyFile groovyFile) {
final Project project = groovyFile.getProject();
final Module module = ModuleUtilCore.findModuleForPsiElement(groovyFile);
Set<VirtualFile> jars = new HashSet<>();
if (module != null) {
ContainerUtil.addAll(jars, OrderEnumerator.orderEntries(module).getAllLibrariesAndSdkClassesRoots());
}
if (groovyFile.isScript() && GroovyScriptUtil.getScriptType(groovyFile) instanceof GantScriptType) {
jars.addAll(GantScriptType.additionalScopeFiles(groovyFile));
}
final ArrayList<URL> urls = new ArrayList<>();
for (VirtualFile jar : jars) {
VirtualFile localFile = PathUtil.getLocalFile(jar);
if (localFile.getFileSystem() instanceof LocalFileSystem) {
urls.add(VfsUtilCore.convertToURL(localFile.getUrl()));
}
}
AntClassLoader loader;
synchronized (ourLock) {
final Map<List<URL>, AntClassLoader> map = CachedValuesManager.getManager(project).getParameterizedCachedValue(project, KEY, PROVIDER, false, project);
loader = map.get(urls);
if (loader == null) {
map.put(urls, loader = new AntClassLoader(urls));
}
}
return loader.getAntObjects();
}
use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method performRefactoring.
private static void performRefactoring(final PsiElement element, final GrParametersOwner owner, final Collection<PsiElement> occurrences, final boolean createNewFirstParam, @Nullable final String mapParamName, final boolean specifyMapType) {
final GrParameter param = getAppropriateParameter(element);
assert param != null;
final String paramName = param.getName();
final String mapName = createNewFirstParam ? mapParamName : getFirstParameter(owner).getName();
final Project project = element.getProject();
final Runnable runnable = () -> {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final GrParameterList list = owner.getParameterList();
assert list != null;
final int index = list.getParameterNumber(param);
if (!createNewFirstParam && index <= 0) {
// bad undo
return;
}
//final List<GrCall> calls = getCallOccurrences(occurrences);
try {
for (PsiElement occurrence : occurrences) {
GrReferenceExpression refExpr = null;
GroovyResolveResult resolveResult = null;
boolean isExplicitGetterCall = false;
if (occurrence instanceof GrReferenceExpression) {
final PsiElement parent = occurrence.getParent();
if (parent instanceof GrCall) {
refExpr = (GrReferenceExpression) occurrence;
resolveResult = refExpr.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyGetter(((PsiMethod) resolved)) && //check for explicit getter call
((PsiMethod) resolved).getName().equals(refExpr.getReferenceName())) {
isExplicitGetterCall = true;
}
} else if (parent instanceof GrReferenceExpression) {
resolveResult = ((GrReferenceExpression) parent).advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
refExpr = (GrReferenceExpression) parent;
}
}
}
if (refExpr == null)
continue;
final GrClosureSignature signature = generateSignature(owner, refExpr);
if (signature == null)
continue;
GrCall call;
if (isExplicitGetterCall) {
PsiElement parent = refExpr.getParent();
LOG.assertTrue(parent instanceof GrCall);
parent = parent.getParent();
if (parent instanceof GrReferenceExpression && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
parent = parent.getParent();
}
if (parent instanceof GrCall) {
call = (GrCall) parent;
} else {
continue;
}
} else {
call = (GrCall) refExpr.getParent();
}
if (resolveResult.isInvokedOnProperty()) {
final PsiElement parent = call.getParent();
if (parent instanceof GrCall) {
call = (GrCall) parent;
} else if (parent instanceof GrReferenceExpression && parent.getParent() instanceof GrCall) {
final PsiElement resolved = ((GrReferenceExpression) parent).resolve();
if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
call = (GrCall) parent.getParent();
} else {
continue;
}
}
}
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
if (argInfos == null)
continue;
final GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = argInfos[index];
final GrNamedArgument namedArg;
if (argInfo.isMultiArg) {
if (argInfo.args.isEmpty())
continue;
String arg = "[" + StringUtil.join(ContainerUtil.map(argInfo.args, element1 -> element1.getText()), ", ") + "]";
for (PsiElement psiElement : argInfo.args) {
psiElement.delete();
}
namedArg = factory.createNamedArgument(paramName, factory.createExpressionFromText(arg));
} else {
if (argInfo.args.isEmpty())
continue;
final PsiElement argument = argInfo.args.iterator().next();
assert argument instanceof GrExpression;
namedArg = factory.createNamedArgument(paramName, (GrExpression) argument);
argument.delete();
}
call.addNamedArgument(namedArg);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
//Replace of occurrences of old parameter in closure/method
final Collection<PsiReference> references = ReferencesSearch.search(param).findAll();
for (PsiReference ref : references) {
final PsiElement elt = ref.getElement();
if (elt instanceof GrReferenceExpression) {
GrReferenceExpression expr = (GrReferenceExpression) elt;
final GrExpression newExpr = factory.createExpressionFromText(mapName + "." + paramName);
expr.replaceWithExpression(newExpr, true);
}
}
//Add new map parameter to closure/method if it's necessary
if (createNewFirstParam) {
try {
final GrParameter newParam = factory.createParameter(mapName, specifyMapType ? MAP_TYPE_TEXT : "", null);
list.addAfter(newParam, null);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
//Eliminate obsolete parameter from parameter list
param.delete();
};
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(runnable), REFACTORING_NAME, null);
}
use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.
the class AfterNewClassInsertHandler method generateAnonymousBody.
@Nullable
private static Runnable generateAnonymousBody(final Editor editor, final PsiFile file) {
final Project project = file.getProject();
PsiDocumentManager.getInstance(project).commitAllDocuments();
int offset = editor.getCaretModel().getOffset();
PsiElement element = file.findElementAt(offset);
if (element == null)
return null;
PsiElement parent = element.getParent().getParent();
if (!(parent instanceof PsiAnonymousClass))
return null;
return ConstructorInsertHandler.genAnonymousBodyFor((PsiAnonymousClass) parent, editor, file, project);
}
use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.
the class GroovyMethodSignatureInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
if (!(item.getObject() instanceof PsiMethod)) {
return;
}
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
final Editor editor = context.getEditor();
final PsiMethod method = (PsiMethod) item.getObject();
final PsiParameter[] parameters = method.getParameterList().getParameters();
final StringBuilder buffer = new StringBuilder();
final CharSequence chars = editor.getDocument().getCharsSequence();
int endOffset = editor.getCaretModel().getOffset();
final Project project = context.getProject();
int afterSharp = CharArrayUtil.shiftBackwardUntil(chars, endOffset - 1, "#") + 1;
int signatureOffset = afterSharp;
PsiElement element = context.getFile().findElementAt(signatureOffset - 1);
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(element.getProject());
PsiDocTag tag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class);
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
final PsiDocTagValue value = tag.getValueElement();
endOffset = value.getTextRange().getEndOffset();
}
editor.getDocument().deleteString(afterSharp, endOffset);
editor.getCaretModel().moveToOffset(signatureOffset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
buffer.append(method.getName()).append("(");
final int afterParenth = afterSharp + buffer.length();
for (int i = 0; i < parameters.length; i++) {
final PsiType type = TypeConversionUtil.erasure(parameters[i].getType());
buffer.append(type.getCanonicalText());
if (i < parameters.length - 1) {
buffer.append(",");
if (styleSettings.SPACE_AFTER_COMMA)
buffer.append(" ");
}
}
buffer.append(")");
if (!(tag instanceof PsiInlineDocTag)) {
buffer.append(" ");
} else {
final int currentOffset = editor.getCaretModel().getOffset();
if (chars.charAt(currentOffset) == '}') {
afterSharp++;
} else {
buffer.append("} ");
}
}
String insertString = buffer.toString();
EditorModificationUtil.insertStringAtCaret(editor, insertString);
editor.getCaretModel().moveToOffset(afterSharp + buffer.length());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
shortenReferences(project, editor, context, afterParenth);
}
use of com.intellij.openapi.project.Project in project intellij-community by JetBrains.
the class GantRunner method ensureRunnerConfigured.
@Override
public void ensureRunnerConfigured(@NotNull GroovyScriptRunConfiguration configuration) throws RuntimeConfigurationException {
Project project = configuration.getProject();
if (GantUtils.getSDKInstallPath(configuration.getModule(), project).isEmpty()) {
RuntimeConfigurationException e = new RuntimeConfigurationException("Gant is not configured");
e.setQuickFix(() -> ShowSettingsUtil.getInstance().editConfigurable(project, new GantConfigurable(project)));
throw e;
}
}
Aggregations