use of com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding in project android by JetBrains.
the class ReferencedValuesGradleEditorComponent method bind.
public void bind(@NotNull Project project, @NotNull List<GradleEditorSourceBinding> sourceBindings) {
myProjectRef = new WeakReference<Project>(project);
ImmutableListMultimap<VirtualFile, GradleEditorSourceBinding> byFile = Multimaps.index(sourceBindings, GROUPER);
List<VirtualFile> orderedFiles = Lists.newArrayList(byFile.keySet());
ContainerUtil.sort(orderedFiles, FILES_COMPARATOR);
for (VirtualFile file : orderedFiles) {
ImmutableList<GradleEditorSourceBinding> list = byFile.get(file);
List<RangeMarker> rangeMarkers = Lists.newArrayList();
for (GradleEditorSourceBinding descriptor : list) {
rangeMarkers.add(descriptor.getRangeMarker());
}
if (!rangeMarkers.isEmpty()) {
ContainerUtil.sort(rangeMarkers, RANGE_COMPARATOR);
String name = getRepresentativeName(project, file);
mySourceBindings.put(name, rangeMarkers);
myFilesByName.put(name, file);
}
}
}
use of com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding in project android by JetBrains.
the class GradleEditorModelUtil method removeEntity.
/**
* Removes given entity from the underlying source file if possible.
* <p/>
* <b>Note:</b> this method tried to preserve code style after removing the entity.
*
* @param entity an entity to remove
* @return <code>null</code> as an indication that given entity was successfully removed; an error message otherwise
*/
@Nullable
public static String removeEntity(@NotNull GradleEditorEntity entity, boolean commit) {
GradleEditorSourceBinding location = entity.getEntityLocation();
RangeMarker marker = location.getRangeMarker();
if (!marker.isValid()) {
return "source mapping is outdated for entity " + entity;
}
Document document = FileDocumentManager.getInstance().getDocument(location.getFile());
if (document == null) {
return "can't find a document for file " + location.getFile();
}
int startLine = document.getLineNumber(marker.getStartOffset());
int endLine = document.getLineNumber(marker.getEndOffset());
CharSequence text = document.getCharsSequence();
String ws = " \t";
int start = CharArrayUtil.shiftBackward(text, document.getLineStartOffset(startLine), marker.getStartOffset() - 1, ws);
int end = CharArrayUtil.shiftForward(text, marker.getEndOffset(), document.getLineEndOffset(endLine), ws);
if (start == document.getLineStartOffset(startLine) && startLine > 0) {
// Remove line feed at the end of the previous line.
start--;
} else if (end == document.getLineEndOffset(endLine) && endLine < document.getLineCount() - 1) {
//Remove trailing line feed.
end++;
}
document.deleteString(start, end);
if (commit) {
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(location.getProject());
psiDocumentManager.commitDocument(document);
}
return null;
}
use of com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding in project android by JetBrains.
the class GradleEditorDependencyParser method parse.
/**
* Tries to build a {@link GradleEditorEntity} assuming that given assignment hold dependency definition
* (like <code>'compile "my-group:my-value:my-version"'</code>).
*
* @param assignment assignment that might hold external dependency definition
* @param context current context
* @return an entity for the dependency extracted from the given assignment (if any);
* <code>null</code> otherwise
*/
@Nullable
public GradleEditorEntity parse(@NotNull Assignment assignment, @NotNull GradleEditorModelParseContext context) {
if (assignment.value == null) {
return null;
}
String dependencyText = assignment.value.value;
if (dependencyText.isEmpty() && assignment.rValueString != null) {
dependencyText = assignment.rValueString;
}
Matcher matcher = EXTERNAL_DEPENDENCY_PATTERN.matcher(dependencyText);
if (!matcher.matches()) {
return null;
}
GradleEditorSourceBinding scopeSourceBinding = buildSourceBinding(assignment.lValueLocation, context.getProject());
if (scopeSourceBinding == null) {
return null;
}
DependencyDimension group = parseDimension(TextRange.create(matcher.start(1), matcher.end(1)), assignment, dependencyText, context);
if (group == null) {
return null;
}
DependencyDimension artifact = parseDimension(TextRange.create(matcher.start(2), matcher.end(2)), assignment, dependencyText, context);
if (artifact == null) {
return null;
}
DependencyDimension version = parseDimension(TextRange.create(matcher.start(3), matcher.end(3)), assignment, dependencyText, context);
if (version == null) {
return null;
}
GradleEditorSourceBinding entityLocation = buildSourceBinding(assignment, context.getProject());
if (entityLocation == null) {
return null;
}
Set<GradleEditorEntityMetaData> metaData = Sets.newHashSet();
metaData.add(StdGradleEditorEntityMetaData.REMOVABLE);
if (context.getTargetFile().equals(assignment.lValueLocation.file)) {
if (!assignment.codeStructure.isEmpty()) {
String s = assignment.codeStructure.get(0);
if (GradleEditorDsl.ALL_PROJECTS_SECTION.equals(s) || GradleEditorDsl.SUB_PROJECT_SECTION.equals(s)) {
metaData.add(StdGradleEditorEntityMetaData.OUTGOING);
}
}
} else {
metaData.add(StdGradleEditorEntityMetaData.INJECTED);
}
final GradleEditorEntityValueManager versionValueManager;
if (group.value.isEmpty() || artifact.value.isEmpty()) {
versionValueManager = GradleEditorEntityValueManager.NO_OP;
} else {
versionValueManager = new LibraryVersionsManager(group.value, artifact.value);
}
return new ExternalDependencyGradleEditorEntity(assignment.lValue.name, Arrays.asList(scopeSourceBinding), group.value, group.sourceBindings, artifact.value, artifact.sourceBindings, version.value, version.sourceBindings, entityLocation, version.declarationValueLocation, versionValueManager, metaData);
}
use of com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding in project android by JetBrains.
the class GradleEditorDependencyParser method parseDimension.
@Nullable
private static DependencyDimension parseDimension(@NotNull TextRange dimensionRange, @NotNull Assignment assignment, @NotNull String dependencyDeclarationString, @NotNull GradleEditorModelParseContext context) {
if (assignment.value == null) {
return null;
}
TextRange dimensionRangeInFile = dimensionRange.shiftRight(assignment.value.location.range.getStartOffset());
List<Variable> dependencies = Lists.newArrayList();
for (Map.Entry<Variable, Location> entry : assignment.dependencies.entries()) {
if (dimensionRangeInFile.contains(entry.getValue().range)) {
dependencies.add(entry.getKey());
}
}
String dimensionValue = dependencyDeclarationString.substring(dimensionRange.getStartOffset(), dimensionRange.getEndOffset());
List<GradleEditorSourceBinding> sourceBindings = Lists.newArrayList();
if (dependencies.isEmpty() || dependencies.size() > 1 || // an actual value definition here.
!GradleEditorModelUtil.isVariable(dimensionValue, dependencies.get(0).name)) {
TextRange rangeToUse = assignment.value.location.range.cutOut(dimensionRange);
if (dimensionValue.startsWith("${") && dimensionValue.endsWith("}") && !"${}".equals(dimensionValue)) {
// There is a possible case that dependency coordinate is defined like "my-group:my-artifact:${var1 + var2}". We want
// to point to the 'var1 + var2' instead of '${var1 + var2}' then.
rangeToUse = TextRange.create(rangeToUse.getStartOffset() + "${".length(), rangeToUse.getEndOffset() - "}".length());
dimensionValue = "";
}
Location dimensionLocation = new Location(assignment.value.location.file, rangeToUse);
GradleEditorSourceBinding sourceBinding = buildSourceBinding(dimensionLocation, context.getProject());
if (sourceBinding == null) {
return null;
}
sourceBindings.add(sourceBinding);
} else {
dimensionValue = "";
}
GradleEditorSourceBinding dimensionLocation = buildSourceBinding(new Location(assignment.lValueLocation.file, dimensionRangeInFile), context.getProject());
if (dimensionLocation == null) {
return null;
}
Set<GradleEditorEntityMetaData> metaData = assignment.lValueLocation.file.equals(context.getTargetFile()) ? Collections.<GradleEditorEntityMetaData>emptySet() : Collections.singleton(StdGradleEditorEntityMetaData.INJECTED);
if (dependencies.isEmpty()) {
return new DependencyDimension(dimensionValue, sourceBindings, dimensionLocation, metaData);
}
GradleEditorModelUtil.EntityInfo entityInfo = GradleEditorModelUtil.collectInfo(dependencies, context, null);
String valueToUse = sourceBindings.isEmpty() ? entityInfo.value : "";
sourceBindings.addAll(entityInfo.sourceBindings);
return new DependencyDimension(valueToUse, sourceBindings, dimensionLocation, metaData);
}
use of com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding in project android by JetBrains.
the class GradleEditorModelUtil method buildSourceBinding.
@Nullable
public static GradleEditorSourceBinding buildSourceBinding(@NotNull Location location, @NotNull Project project) {
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
Document document = fileDocumentManager.getDocument(location.file);
if (document == null) {
LOG.warn(String.format("Can't obtain a document for file %s for processing location '%s'", location.file, location));
return null;
}
RangeMarker rangeMarker = document.createRangeMarker(location.range);
return new GradleEditorSourceBinding(project, location.file, rangeMarker);
}
Aggregations