Search in sources :

Example 21 with RangeMarker

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);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GradleEditorSourceBinding(com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding) RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 22 with RangeMarker

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;
}
Also used : GradleEditorSourceBinding(com.android.tools.idea.gradle.editor.entity.GradleEditorSourceBinding) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with RangeMarker

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;
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with RangeMarker

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;
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with RangeMarker

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;
    }
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker)

Aggregations

RangeMarker (com.intellij.openapi.editor.RangeMarker)111 Document (com.intellij.openapi.editor.Document)33 TextRange (com.intellij.openapi.util.TextRange)20 Project (com.intellij.openapi.project.Project)19 PsiFile (com.intellij.psi.PsiFile)14 PsiElement (com.intellij.psi.PsiElement)13 IncorrectOperationException (com.intellij.util.IncorrectOperationException)13 Editor (com.intellij.openapi.editor.Editor)11 Nullable (org.jetbrains.annotations.Nullable)11 NotNull (org.jetbrains.annotations.NotNull)10 Template (com.intellij.codeInsight.template.Template)8 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)7 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)6 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)6 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)5 THashMap (gnu.trove.THashMap)5 GutterMark (com.intellij.codeInsight.daemon.GutterMark)4 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)4 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4