use of com.intellij.openapi.editor.RangeMarker 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.intellij.openapi.editor.RangeMarker 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.intellij.openapi.editor.RangeMarker in project android by JetBrains.
the class AbstractSimpleGradleEditorEntity method changeValue.
/**
* Tries to apply given value to the current entity and {@link #getDefinitionValueSourceBindings() backing files}.
* <p/>
* Main success scenario here is to show UI for config properties manipulations and flush user-defined values via this method.
*
* @param newValue new value to use
* @return null as an indication that given value has been successfully applied; an error message otherwise
*/
@Nullable
public String changeValue(@NotNull String newValue) {
if (newValue.equals(getCurrentValue())) {
return null;
}
List<GradleEditorSourceBinding> sourceBindings = getDefinitionValueSourceBindings();
if (sourceBindings.size() != 1) {
return String.format("Can't apply version '%s' to the entity '%s'. Reason: expected the entity to hold only one source binding " + "but it has %d (%s)", newValue, this, sourceBindings.size(), sourceBindings);
}
GradleEditorSourceBinding binding = sourceBindings.get(0);
RangeMarker rangeMarker = binding.getRangeMarker();
if (!rangeMarker.isValid()) {
return String.format("Can't apply version '%s' to the entity '%s'. Reason: source file binding is incorrect", newValue, this);
}
myCurrentValue = newValue;
rangeMarker.getDocument().replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), newValue);
return null;
}
use of com.intellij.openapi.editor.RangeMarker in project android by JetBrains.
the class ExternalDependencyGradleEditorEntity method changeVersion.
/**
* Tries to apply given version to the current entity and {@link #getVersionSourceBindings() backing files}.
* <p/>
* Main success scenario here is to show UI for config properties manipulations and flush user-defined values via this method.
*
* @param newVersion new value to use
* @return <code>null</code> as an indication that given value has been successfully applied; an error message otherwise
*/
@Nullable
public String changeVersion(@NotNull String newVersion) {
if (newVersion.equals(getVersion())) {
return null;
}
List<GradleEditorSourceBinding> sourceBindings = getVersionSourceBindings();
if (sourceBindings.size() != 1) {
return String.format("Can't apply version '%s' to the entity '%s'. Reason: expected the entity to hold only one version source binding " + "but it has %d (%s)", newVersion, this, sourceBindings.size(), sourceBindings);
}
GradleEditorSourceBinding binding = sourceBindings.get(0);
RangeMarker rangeMarker = binding.getRangeMarker();
if (!rangeMarker.isValid()) {
return String.format("Can't apply version '%s' to the entity '%s'. Reason: source file binding is incorrect", newVersion, this);
}
myVersion = newVersion;
rangeMarker.getDocument().replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), newVersion);
return null;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class OffsetMap method getOffset.
/**
* @param key key
* @return offset An offset registered earlier with this key.
* -1 if offset wasn't registered or became invalidated due to document changes
*/
public int getOffset(OffsetKey key) {
synchronized (myMap) {
final RangeMarker marker = myMap.get(key);
if (marker == null)
throw new IllegalArgumentException("Offset " + key + " is not registered");
if (!marker.isValid()) {
removeOffset(key);
throw new IllegalStateException("Offset " + key + " is invalid: " + marker);
}
final int endOffset = marker.getEndOffset();
if (marker.getStartOffset() != endOffset) {
saveOffset(key, endOffset, false);
}
return endOffset;
}
}
Aggregations