com.intellij.psi.ElementManipulators Java Examples

The following examples show how to use com.intellij.psi.ElementManipulators. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FluidInjector.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    if (context.getLanguage() == XMLLanguage.INSTANCE) return;
    final Project project = context.getProject();
    if (!FluidIndexUtil.hasFluid(project)) return;

    final PsiElement parent = context.getParent();
    if (context instanceof XmlAttributeValueImpl && parent instanceof XmlAttribute) {
        final int length = context.getTextLength();
        final String name = ((XmlAttribute) parent).getName();

        if (isInjectableAttribute(project, length, name)) {
            registrar
                .startInjecting(FluidLanguage.INSTANCE)
                .addPlace(null, null, (PsiLanguageInjectionHost) context, ElementManipulators.getValueTextRange(context))
                .doneInjecting();
        }
    }
}
 
Example #2
Source File: QuickEditAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
protected Pair<PsiElement, TextRange> getRangePair(final PsiFile file, final Editor editor) {
  final int offset = editor.getCaretModel().getOffset();
  final PsiLanguageInjectionHost host =
          PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiLanguageInjectionHost.class, false);
  if (host == null || ElementManipulators.getManipulator(host) == null) return null;
  final List<Pair<PsiElement, TextRange>> injections = InjectedLanguageManager.getInstance(host.getProject()).getInjectedPsiFiles(host);
  if (injections == null || injections.isEmpty()) return null;
  final int offsetInElement = offset - host.getTextRange().getStartOffset();
  final Pair<PsiElement, TextRange> rangePair = ContainerUtil.find(injections, new Condition<Pair<PsiElement, TextRange>>() {
    @Override
    public boolean value(final Pair<PsiElement, TextRange> pair) {
      return pair.second.containsRange(offsetInElement, offsetInElement);
    }
  });
  if (rangePair != null) {
    final Language language = rangePair.first.getContainingFile().getLanguage();
    final Object action = language.getUserData(EDIT_ACTION_AVAILABLE);
    if (action != null && action.equals(false)) return null;

    myLastLanguageName = language.getDisplayName();
  }
  return rangePair;
}
 
Example #3
Source File: XmlSqlTplMultiHostInjector.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
private void registrarInjecting(Language language, MultiHostRegistrar registrar, List<PsiElement> els, String prefix, String suffix) {
    if (els.size() > 0) {
        registrar.startInjecting(language);
        els.forEach(el -> registrar.addPlace(prefix, suffix, (PsiLanguageInjectionHost) el, ElementManipulators.getValueTextRange(el)));
        registrar.doneInjecting();
    }
}
 
Example #4
Source File: CamelBeanMethodReference.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    //Find all the method with the registered method name on it's class.

    final PsiMethod[] methodsByName = getPsiClass().findMethodsByName(methodNameOnly, true);

    for (PsiMethod psiMethod : methodsByName) {
        psiMethod.setName(newElementName);
    }
    //Rename the Camel DSL bean ref method
    ElementManipulators.getManipulator(getElement()).handleContentChange(getElement(), this.getRangeInElement(), newElementName);
    return getElement();
}
 
Example #5
Source File: AbstractBashFileReference.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@NotNull
private ElementManipulator<AbstractBashCommand<?>> getManipulator() {
    ElementManipulator<AbstractBashCommand<?>> manipulator = ElementManipulators.<AbstractBashCommand<?>>getManipulator(cmd);
    if (manipulator == null) {
        throw new IncorrectOperationException("No element manipulator found for " + cmd);
    }

    return manipulator;
}
 
Example #6
Source File: AbstractFunctionReference.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@NotNull
private ElementManipulator<AbstractBashCommand<?>> getManipulator() {
    ElementManipulator<AbstractBashCommand<?>> manipulator = ElementManipulators.<AbstractBashCommand<?>>getManipulator(cmd);
    if (manipulator == null) {
        throw new IncorrectOperationException("No element manipulator found for " + cmd);
    }
    return manipulator;
}
 
Example #7
Source File: CachingReference.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static <T extends PsiElement> ElementManipulator<T> getManipulator(T currentElement){
  ElementManipulator<T> manipulator = ElementManipulators.getManipulator(currentElement);
  if (manipulator == null) {
    throw new IncorrectOperationException("Manipulator for this element is not defined: " + currentElement + "; " + currentElement.getClass());
  }
  return manipulator;
}
 
Example #8
Source File: PathReferenceProviderBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean createReferences(@Nonnull final PsiElement psiElement, final @Nonnull List<PsiReference> references, final boolean soft) {

  final TextRange range = ElementManipulators.getValueTextRange(psiElement);
  int offset = range.getStartOffset();
  int endOffset = range.getEndOffset();
  final String elementText = psiElement.getText();
  for (DynamicContextProvider provider: Extensions.getExtensions(DynamicContextProvider.EP_NAME)) {
    final int dynamicOffset = provider.getOffset(psiElement, offset, elementText);
    if (dynamicOffset == -1) {
      return false;
    } else if (dynamicOffset != offset) {
      offset = dynamicOffset;
    }
  }

  final int pos = getLastPosOfURL(offset, elementText);
  if (pos != -1 && pos < endOffset) {
    endOffset = pos;
  }
  try {
    final String text = elementText.substring(offset, endOffset);
    return createReferences(psiElement, offset, text, references, soft);
  } catch (StringIndexOutOfBoundsException e) {
    LOG.error("Cannot process string: '" + psiElement.getParent().getParent().getText() + "'", e);
    return false;
  }
}
 
Example #9
Source File: BeanSelfReference.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException {
    ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(myElement);
    return manipulator.handleContentChange(myElement, TextRange.from(1, id.length()), newElementName);
}
 
Example #10
Source File: DirectEndpointReference.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException {
    ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(myElement);
    return manipulator.handleContentChange(myElement, endpoint.getNameTextRange().shiftRight(1), newElementName);
}
 
Example #11
Source File: DirectEndpointStartSelfReference.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException {
    ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(myElement);
    return manipulator.handleContentChange(myElement, endpoint.getNameTextRange().shiftRight(1), newElementName);
}
 
Example #12
Source File: ReferenceSetBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
public ReferenceSetBase(@Nonnull PsiElement element) {
  this(element, ElementManipulators.getOffsetInElement(element));
}
 
Example #13
Source File: ReferenceSetBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
public ReferenceSetBase(@Nonnull PsiElement element, int offset) {
  this(ElementManipulators.getValueText(element), element, offset, DOT_SEPARATOR);
}