use of com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix in project intellij-plugins by JetBrains.
the class ActionScriptGenerateDelegatesHandler method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
final JSClass jsClass = findClass(file, editor);
if (jsClass == null)
return;
Collection<JSField> fields = findCandidateFields(jsClass);
final JSField field;
if (ApplicationManager.getApplication().isUnitTestMode()) {
LOG.assertTrue(fields.size() == 1);
field = fields.iterator().next();
} else {
final MemberChooser<JSNamedElementNode> targetChooser = createMemberChooserDialog(project, jsClass, wrap(fields), false, false, CodeInsightBundle.message("generate.delegate.target.chooser.title"));
targetChooser.show();
if (targetChooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
field = (JSField) targetChooser.getSelectedElements().get(0).getPsiElement();
}
JSType fieldType = field.getType();
if (fieldType == null)
return;
JSClass fieldClass = fieldType.resolveClass();
if (fieldClass == null)
return;
final boolean allowPackageLocal = !JSPsiImplUtils.differentPackageName(StringUtil.getPackageName(fieldClass.getQualifiedName()), StringUtil.getPackageName(jsClass.getQualifiedName()));
// don't add members along with their supers
class MemberDescriptor {
private final String name;
@Nullable
private final JSFunction.FunctionKind kind;
public MemberDescriptor(JSFunction method) {
name = method.getName();
kind = method.getKind();
}
public MemberDescriptor(JSVariable field) {
name = field.getName();
kind = null;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
MemberDescriptor that = (MemberDescriptor) o;
if (kind != that.kind)
return false;
if (!name.equals(that.name))
return false;
return true;
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + (kind != null ? kind.hashCode() : 0);
return result;
}
}
final Map<MemberDescriptor, JSNamedElement> memberCandidates = new HashMap<>();
ResolveProcessor p = new ResolveProcessor(null) {
{
setToProcessHierarchy(true);
}
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
JSClass clazz = JSUtils.getMemberContainingClass(element);
if (clazz == null || JSResolveUtil.isObjectClass(clazz) || clazz == jsClass) {
return true;
}
if (element instanceof JSFunction) {
JSFunction method = (JSFunction) element;
if (memberCandidates.containsKey(method.getName())) {
return true;
}
JSAttributeList attributeList = method.getAttributeList();
if (attributeList.getAccessType() == JSAttributeList.AccessType.PRIVATE || attributeList.getAccessType() == JSAttributeList.AccessType.PROTECTED) {
return true;
}
if (!allowPackageLocal && attributeList.getNamespace() == null && attributeList.getAccessType() == JSAttributeList.AccessType.PACKAGE_LOCAL) {
return true;
}
if (method.getKind() == JSFunction.FunctionKind.CONSTRUCTOR) {
return true;
}
if (attributeList.hasModifier(JSAttributeList.ModifierType.STATIC)) {
return true;
}
if (JSInheritanceUtil.findMethodInClass(method, jsClass, true) != null) {
return true;
}
memberCandidates.put(new MemberDescriptor(method), method);
} else if (element instanceof JSVariable) {
JSVariable f = (JSVariable) element;
if (memberCandidates.containsKey(f.getName())) {
return true;
}
JSAttributeList attributeList = f.getAttributeList();
if (attributeList.getAccessType() == JSAttributeList.AccessType.PRIVATE || attributeList.getAccessType() == JSAttributeList.AccessType.PROTECTED) {
return true;
}
if (!allowPackageLocal && attributeList.getAccessType() == JSAttributeList.AccessType.PACKAGE_LOCAL) {
return true;
}
if (jsClass.findFunctionByName(f.getName()) != null) {
return true;
}
memberCandidates.put(new MemberDescriptor(f), f);
}
return true;
}
};
fieldClass.processDeclarations(p, ResolveState.initial(), fieldClass, fieldClass);
Collection<JSNamedElementNode> selected;
if (ApplicationManager.getApplication().isUnitTestMode()) {
LOG.assertTrue(!memberCandidates.isEmpty());
selected = wrap(memberCandidates.values());
} else {
final MemberChooser<JSNamedElementNode> methodsChooser = createMemberChooserDialog(project, jsClass, wrap(memberCandidates.values()), false, true, CodeInsightBundle.message("generate.delegate.method.chooser.title"));
methodsChooser.show();
if (methodsChooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
selected = methodsChooser.getSelectedElements();
}
BaseCreateMethodsFix fix = new BaseCreateMethodsFix<JSNamedElement>(jsClass) {
final JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix generateGetterFix = new JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix(JSGetterSetterGenerationMode.Getter, jsClass, null, false, field.getName());
final JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix generateSetterFix = new JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix(JSGetterSetterGenerationMode.Setter, jsClass, null, false, field.getName());
@Override
protected void adjustAttributeList(JSAttributeListWrapper attributeListWrapper, JSNamedElement function) {
attributeListWrapper.overrideAccessType(JSAttributeList.AccessType.PUBLIC);
attributeListWrapper.overrideModifier(JSAttributeList.ModifierType.STATIC, field.getAttributeList().hasModifier(JSAttributeList.ModifierType.STATIC));
for (JSAttributeList.ModifierType modifierType : new JSAttributeList.ModifierType[] { JSAttributeList.ModifierType.NATIVE, JSAttributeList.ModifierType.DYNAMIC, JSAttributeList.ModifierType.FINAL, JSAttributeList.ModifierType.OVERRIDE, JSAttributeList.ModifierType.VIRTUAL }) {
attributeListWrapper.overrideModifier(modifierType, false);
}
}
@Override
protected void processElements(Project project, MultiMap<String, String> types, Set<JSNamedElement> elementsToProcess) {
for (JSNamedElement e : elementsToProcess) {
if (e instanceof JSFunction) {
anchor = doAddOneMethod(project, buildFunctionText(e, types), anchor);
} else {
anchor = doAddOneMethod(project, generateGetterFix.buildFunctionText(e, types), anchor);
anchor = doAddOneMethod(project, generateSetterFix.buildFunctionText(e, types), anchor);
}
}
}
@Override
protected String buildFunctionBodyText(final String retType, final JSParameterList parameterList, final JSNamedElement element) {
return OverrideMethodsFix.buildDelegatingText(retType, parameterList, ((JSFunction) element), field.getName(), anchor != null ? anchor : myJsClass);
}
};
doInvoke(project, editor, file, selected, fix);
}
use of com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix in project intellij-plugins by JetBrains.
the class ActionScriptGenerateToStringHandler method createFix.
protected BaseCreateMethodsFix createFix(final JSClass jsClass) {
return new BaseCreateMethodsFix<JSVariable>(jsClass) {
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
evalAnchor(editor, file);
final boolean[] needOverride = new boolean[1];
JSInheritanceUtil.processOverrides(jsClass, new JSOverrideHandler() {
public boolean process(@NotNull List<JSPsiElementBase> elements, final PsiElement scope, final String className) {
needOverride[0] = !"Object".equals(className);
return false;
}
}, "toString", null, myJsClass, true);
@NonNls String functionText = "public " + (needOverride[0] ? "override " : "") + "function toString():String {\nreturn " + (needOverride[0] ? "super.toString()" : "\"" + jsClass.getName());
final String semicolon = JSCodeStyleSettings.getSemicolon(file);
boolean first = true;
final Set<JSVariable> jsVariables = getElementsToProcess();
if (!jsVariables.isEmpty()) {
functionText += needOverride[0] ? " + \"{" : "{";
for (JSVariable var : jsVariables) {
if (!first) {
functionText += " + \",";
}
first = false;
functionText += var.getName() + "=\" + String(" + var.getName() + ")";
}
functionText += "+\"}\"";
} else {
if (!needOverride[0])
functionText += "\"";
}
functionText += semicolon + "\n}";
doAddOneMethod(project, functionText, anchor);
}
};
}
use of com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix in project intellij-plugins by JetBrains.
the class GenerateFlexUnitMethodActionBase method getGenerateHandler.
protected BaseJSGenerateHandler getGenerateHandler() {
return new BaseJSGenerateHandler() {
protected String getTitleKey() {
return null;
}
protected BaseCreateMethodsFix createFix(JSClass clazz) {
return new BaseCreateMethodsFix(clazz) {
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
evalAnchor(editor, file);
final PsiElement addedElement = doAddOneMethod(project, "public function fake():void{}", anchor);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
final TemplateManager manager = TemplateManager.getInstance(project);
final Template template = manager.createTemplate("", "");
template.setToReformat(true);
buildTemplate(template, myJsClass);
final TextRange range = addedElement.getTextRange();
editor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), "");
editor.getCaretModel().moveToOffset(range.getStartOffset());
manager.startTemplate(editor, template);
}
};
}
protected boolean canHaveEmptySelectedElements() {
return true;
}
};
}
Aggregations