use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.
the class GradleSettingsFile method addModule.
/**
* Adds a reference to the module to the settings file, if there is not already one. The module path must be colon separated, with a
* leading colon, e.g. ":project:subproject". Must be run inside a write action.
*
* If the file does not match the default module location, this method will override the location.
*/
public void addModule(@NotNull String modulePath, @NotNull File location) {
checkInitialized();
commitDocumentChanges();
for (GrMethodCall includeStatement : getMethodCalls(myGroovyFile, INCLUDE_METHOD)) {
for (GrLiteral lit : getLiteralArguments(includeStatement)) {
if (modulePath.equals(lit.getValue())) {
return;
}
}
}
GrMethodCall includeStatement = getMethodCall(myGroovyFile, INCLUDE_METHOD);
if (includeStatement != null) {
GrArgumentList argList = includeStatement.getArgumentList();
GrLiteral literal = GroovyPsiElementFactory.getInstance(myProject).createLiteralFromValue(modulePath);
argList.addAfter(literal, argList.getLastChild());
} else {
GrStatement statement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(INCLUDE_METHOD + " '" + modulePath + "'");
myGroovyFile.add(statement);
}
// We get location relative to this file parent
VirtualFile parent = getFile().getParent();
File defaultLocation = GradleUtil.getModuleDefaultPath(parent, modulePath);
if (!FileUtil.filesEqual(defaultLocation, location)) {
final String path;
File parentFile = VfsUtilCore.virtualToIoFile(parent);
if (FileUtil.isAncestor(parentFile, location, true)) {
path = PathUtil.toSystemIndependentName(FileUtil.getRelativePath(parentFile, location));
} else {
path = PathUtil.toSystemIndependentName(location.getAbsolutePath());
}
String locationAssignment = String.format(CUSTOM_LOCATION_FORMAT, modulePath, path);
GrStatement locationStatement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(locationAssignment);
myGroovyFile.add(locationStatement);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.
the class GradleSettingsFile method removeModule.
/**
* Removes the reference to the module from the settings file, if present. The module path must be colon separated, with a
* leading colon, e.g. ":project:subproject". Must be run inside a write action.
*/
public void removeModule(@NotNull String modulePath) {
checkInitialized();
commitDocumentChanges();
boolean removedAnyIncludes = false;
for (GrMethodCall includeStatement : getMethodCalls(myGroovyFile, INCLUDE_METHOD)) {
for (GrLiteral lit : getLiteralArguments(includeStatement)) {
if (modulePath.equals(lit.getValue())) {
lit.delete();
removedAnyIncludes = true;
if (getArguments(includeStatement).length == 0) {
includeStatement.delete();
// If this happens we will fall through both for loops before we get into iteration trouble. We want to keep iterating in
// case the module is added more than once (via hand-editing of the file).
}
}
}
}
if (removedAnyIncludes) {
for (Pair<String, GrAssignmentExpression> pair : getAllProjectLocationStatements()) {
if (modulePath.equals(pair.first)) {
pair.second.delete();
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.
the class GradleDslLiteral method getValue.
@Override
@Nullable
public Object getValue() {
if (myUnsavedValue != null) {
return myUnsavedValue;
}
if (myExpression == null) {
return null;
}
Object value = ((GrLiteral) myExpression).getValue();
if (value != null) {
return value;
}
if (myExpression instanceof GrString) {
// String literal with variables. ex: compileSdkVersion = "$ANDROID-${VERSION}"
String literalText = myExpression.getText();
if (isQuotedString(literalText)) {
literalText = unquoteString(literalText);
}
List<GradleResolvedVariable> resolvedVariables = Lists.newArrayList();
GrStringInjection[] injections = ((GrString) myExpression).getInjections();
for (GrStringInjection injection : injections) {
String variableName = null;
GrClosableBlock closableBlock = injection.getClosableBlock();
if (closableBlock != null) {
String blockText = closableBlock.getText();
variableName = blockText.substring(1, blockText.length() - 1);
} else {
GrExpression expression = injection.getExpression();
if (expression != null) {
variableName = expression.getText();
}
}
if (!isEmpty(variableName)) {
GradleDslExpression resolvedExpression = resolveReference(variableName, GradleDslExpression.class);
if (resolvedExpression != null) {
Object resolvedValue = resolvedExpression.getValue();
if (resolvedValue != null) {
resolvedVariables.add(new GradleResolvedVariable(variableName, resolvedValue, resolvedExpression));
literalText = literalText.replace(injection.getText(), resolvedValue.toString());
}
}
}
}
setResolvedVariables(resolvedVariables);
return literalText;
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.
the class GradleDslLiteral method createLiteral.
@Nullable
private GrLiteral createLiteral() {
if (myUnsavedValue == null) {
return null;
}
CharSequence unsavedValueText = null;
if (myUnsavedValue instanceof String) {
unsavedValueText = GrStringUtil.getLiteralTextByValue((String) myUnsavedValue);
} else if (myUnsavedValue instanceof Integer || myUnsavedValue instanceof Boolean) {
unsavedValueText = myUnsavedValue.toString();
}
myUnsavedValue = null;
if (unsavedValueText == null) {
return null;
}
GroovyPsiElementFactory factory = getPsiElementFactory();
if (factory == null) {
return null;
}
GrExpression newExpression = factory.createExpressionFromText(unsavedValueText);
if (!(newExpression instanceof GrLiteral)) {
return null;
}
return (GrLiteral) newExpression;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-plugins by JetBrains.
the class GrCucumberStepDeclarationSearcher method findDeclarationsAt.
@Override
public void findDeclarationsAt(@NotNull PsiElement element, int offsetInElement, Consumer<PomTarget> consumer) {
PsiLanguageInjectionHost host = InjectedLanguageManager.getInstance(element.getProject()).getInjectionHost(element);
if (host != null) {
element = host;
}
if (element.getParent() instanceof GrLiteral) {
element = element.getParent();
}
if (element instanceof GrLiteral) {
//~literal
final PsiElement parent = element.getParent();
if (parent != null) {
//(~literal)
final PsiElement pparent = parent.getParent();
if (pparent != null) {
//When(~literal)
final PsiElement ppparent = pparent.getParent();
if (ppparent instanceof GrMethodCall && GrCucumberUtil.isStepDefinition(ppparent)) {
final GrMethodCall methodCall = (GrMethodCall) ppparent;
consumer.consume(GrStepDefinition.getStepDefinition(methodCall));
}
}
}
}
}
Aggregations