use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.
the class GradleDslParser method getExpressionElement.
@Nullable
private static GradleDslExpression getExpressionElement(@NotNull GradleDslElement parentElement, @NotNull GroovyPsiElement psiElement, @NotNull String propertyName, @NotNull GrExpression propertyExpression) {
if (propertyExpression instanceof GrLiteral) {
// ex: compileSdkVersion 23 or compileSdkVersion = "android-23"
return new GradleDslLiteral(parentElement, psiElement, propertyName, (GrLiteral) propertyExpression);
}
if (propertyExpression instanceof GrReferenceExpression) {
// ex: compileSdkVersion SDK_VERSION or sourceCompatibility = VERSION_1_5
return new GradleDslReference(parentElement, psiElement, propertyName, (GrReferenceExpression) propertyExpression);
}
if (propertyExpression instanceof GrMethodCallExpression) {
// ex: compile project("someProject")
GrMethodCallExpression methodCall = (GrMethodCallExpression) propertyExpression;
GrReferenceExpression callReferenceExpression = getChildOfType(methodCall, GrReferenceExpression.class);
if (callReferenceExpression != null) {
String referenceName = callReferenceExpression.getText();
if (!isEmpty(referenceName)) {
GrArgumentList argumentList = methodCall.getArgumentList();
if (argumentList.getAllArguments().length > 0) {
return getMethodCall(parentElement, methodCall, referenceName, argumentList);
} else {
return new GradleDslMethodCall(parentElement, methodCall, referenceName);
}
}
}
}
if (propertyExpression instanceof GrNewExpression) {
GrNewExpression newExpression = (GrNewExpression) propertyExpression;
GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
if (referenceElement != null) {
String objectName = referenceElement.getText();
if (!isEmpty(objectName)) {
GrArgumentList argumentList = newExpression.getArgumentList();
if (argumentList != null) {
if (argumentList.getAllArguments().length > 0) {
return getNewExpression(parentElement, newExpression, objectName, argumentList);
}
}
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.
the class JavaVersionDslElement method getVersionText.
@Nullable
private String getVersionText() {
GradleDslLiteral valueLiteral = null;
if (myInternalVersionElement instanceof GradleDslReference) {
GradleDslReference referenceElement = (GradleDslReference) myInternalVersionElement;
valueLiteral = referenceElement.getValue(GradleDslLiteral.class);
if (valueLiteral == null) {
String resolvedReference = referenceElement.getValue(String.class);
if (resolvedReference != null) {
return resolvedReference;
} else {
return referenceElement.getReferenceText();
}
}
}
if (myInternalVersionElement instanceof GradleDslLiteral) {
valueLiteral = (GradleDslLiteral) myInternalVersionElement;
}
if (valueLiteral != null) {
GrLiteral literal = valueLiteral.getLiteral();
if (literal != null) {
return literal.getText();
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.
the class GradleDslLiteral method create.
@Override
@Nullable
public GroovyPsiElement create() {
if (!(myParent instanceof GradleDslExpressionMap)) {
return super.create();
}
// This is a value in the map element we need to create a named argument for it.
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
setPsiElement(parentPsiElement);
GrLiteral newLiteral = createLiteral();
if (newLiteral == null) {
return null;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(newLiteral.getProject());
GrNamedArgument namedArgument = factory.createNamedArgument(myName, newLiteral);
PsiElement added;
if (parentPsiElement instanceof GrArgumentList) {
added = ((GrArgumentList) parentPsiElement).addNamedArgument(namedArgument);
} else {
added = parentPsiElement.addAfter(namedArgument, parentPsiElement.getLastChild());
}
if (added instanceof GrNamedArgument) {
GrNamedArgument addedNameArgument = (GrNamedArgument) added;
GrLiteral literal = getChildOfType(addedNameArgument, GrLiteral.class);
if (literal != null) {
myExpression = literal;
setModified(false);
return getPsiElement();
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.
the class GradleDslLiteral method apply.
@Override
protected void apply() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement == null) {
return;
}
GrLiteral newLiteral = createLiteral();
if (newLiteral == null) {
return;
}
if (myExpression != null) {
PsiElement replace = myExpression.replace(newLiteral);
if (replace instanceof GrLiteral) {
myExpression = (GrLiteral) replace;
}
} else {
PsiElement added;
if (// Entries in [].
psiElement instanceof GrListOrMap || (psiElement instanceof GrArgumentList && !(psiElement instanceof GrCommandArgumentList))) {
// Method call arguments in ().
// add before ) or ]
added = psiElement.addBefore(newLiteral, psiElement.getLastChild());
} else {
added = psiElement.addAfter(newLiteral, psiElement.getLastChild());
}
if (added instanceof GrLiteral) {
myExpression = (GrLiteral) added;
}
if (myUnsavedConfigBlock != null) {
addConfigBlock();
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-plugins by JetBrains.
the class GrStepDefinitionCreator method createStepDefinition.
@Override
public boolean createStepDefinition(@NotNull GherkinStep step, @NotNull final PsiFile file) {
if (!(file instanceof GroovyFile))
return false;
final Project project = file.getProject();
final VirtualFile vFile = ObjectUtils.assertNotNull(file.getVirtualFile());
final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile);
FileEditorManager.getInstance(project).getAllEditors(vFile);
FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor != null) {
final TemplateManager templateManager = TemplateManager.getInstance(file.getProject());
final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
final Template template = templateManager.getActiveTemplate(editor);
if (templateState != null && template != null) {
templateState.gotoEnd();
}
}
// snippet text
final GrMethodCall element = buildStepDefinitionByStep(step);
GrMethodCall methodCall = (GrMethodCall) ((GroovyFile) file).addStatementBefore(element, null);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(methodCall);
methodCall = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(methodCall);
PsiDocumentManager.getInstance(project).commitAllDocuments();
if (ApplicationManager.getApplication().isUnitTestMode())
return true;
final TemplateBuilderImpl builder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(methodCall);
// regexp str
GrLiteral pattern = GrCucumberUtil.getStepDefinitionPattern(methodCall);
assert pattern != null;
String patternText = pattern.getText();
builder.replaceElement(pattern, new TextRange(1, patternText.length() - 1), patternText.substring(1, patternText.length() - 1));
// block vars
GrClosableBlock closure = methodCall.getClosureArguments()[0];
final GrParameter[] blockVars = closure.getAllParameters();
for (GrParameter var : blockVars) {
PsiElement identifier = var.getNameIdentifierGroovy();
builder.replaceElement(identifier, identifier.getText());
}
TemplateManager manager = TemplateManager.getInstance(project);
final Editor editorToRunTemplate;
if (editor == null) {
editorToRunTemplate = IntentionUtils.positionCursor(project, file, methodCall);
} else {
editorToRunTemplate = editor;
}
Template template = builder.buildTemplate();
TextRange range = methodCall.getTextRange();
editorToRunTemplate.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
editorToRunTemplate.getCaretModel().moveToOffset(range.getStartOffset());
manager.startTemplate(editorToRunTemplate, template, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (brokenOff)
return;
ApplicationManager.getApplication().runWriteAction(() -> {
PsiDocumentManager.getInstance(project).commitDocument(editorToRunTemplate.getDocument());
final int offset = editorToRunTemplate.getCaretModel().getOffset();
GrMethodCall methodCall1 = PsiTreeUtil.findElementOfClassAtOffset(file, offset - 1, GrMethodCall.class, false);
if (methodCall1 != null) {
GrClosableBlock[] closures = methodCall1.getClosureArguments();
if (closures.length == 1) {
GrClosableBlock closure1 = closures[0];
selectBody(closure1, editor);
}
}
});
}
});
return true;
}
Aggregations