use of com.intellij.codeInsight.template.macro.VariableOfTypeMacro in project android by JetBrains.
the class AndroidAddStringResourceAction method createJavaResourceReference.
private static void createJavaResourceReference(final Module module, final Editor editor, final PsiFile file, final PsiElement element, final String aPackage, final String resName, final ResourceType resType) {
final boolean extendsContext = getContainingInheritorOf(element, CLASS_CONTEXT) != null;
final boolean extendsFragment = getContainingInheritorOf(element, CLASS_FRAGMENT) != null || getContainingInheritorOf(element, CLASS_V4_FRAGMENT) != null;
final String rJavaFieldName = AndroidResourceUtil.getRJavaFieldName(resName);
final String field = aPackage + ".R." + resType + '.' + rJavaFieldName;
final String methodName = getGetterNameForResourceType(resType, element);
assert methodName != null;
final TemplateImpl template;
final boolean inStaticContext = RefactoringUtil.isInStaticContext(element, null);
final Project project = module.getProject();
if ((extendsContext || extendsFragment) && !inStaticContext) {
if (ResourceType.STRING == resType) {
if (useGetStringMethodForStringRes(element)) {
template = new TemplateImpl("", methodName + '(' + field + ')', "");
} else {
template = new TemplateImpl("", field, "");
}
} else {
template = new TemplateImpl("", "$resources$." + methodName + "(" + field + ")", "");
MacroCallNode node = new MacroCallNode(new MyVarOfTypeExpression("getResources()"));
node.addParameter(new ConstantNode(CLASS_RESOURCES));
template.addVariable("resources", node, new ConstantNode(""), true);
}
} else {
boolean addContextVariable = true;
if (ResourceType.STRING == resType) {
if (useGetStringMethodForStringRes(element)) {
template = new TemplateImpl("", "$context$." + methodName + '(' + field + ')', "");
} else {
template = new TemplateImpl("", field, "");
addContextVariable = false;
}
} else {
template = new TemplateImpl("", "$context$.getResources()." + methodName + "(" + field + ")", "");
}
if (addContextVariable) {
final boolean extendsView = getContainingInheritorOf(element, VIEW_CLASS_NAME) != null;
MacroCallNode node = new MacroCallNode(extendsView && !inStaticContext ? new MyVarOfTypeExpression("getContext()") : new VariableOfTypeMacro());
node.addParameter(new ConstantNode(CLASS_CONTEXT));
template.addVariable("context", node, new ConstantNode(""), true);
}
}
final int offset = element.getTextOffset();
editor.getCaretModel().moveToOffset(offset);
final TextRange elementRange = element.getTextRange();
editor.getDocument().deleteString(elementRange.getStartOffset(), elementRange.getEndOffset());
final RangeMarker marker = editor.getDocument().createRangeMarker(offset, offset);
marker.setGreedyToLeft(true);
marker.setGreedyToRight(true);
//noinspection ConstantConditions
TemplateManager.getInstance(project).startTemplate(editor, template, false, null, new TemplateEditingAdapter() {
@Override
public void waitingForInput(Template template) {
JavaCodeStyleManager.getInstance(project).shortenClassReferences(file, marker.getStartOffset(), marker.getEndOffset());
}
@Override
public void beforeTemplateFinished(TemplateState state, Template template) {
JavaCodeStyleManager.getInstance(project).shortenClassReferences(file, marker.getStartOffset(), marker.getEndOffset());
}
});
}
Aggregations