Java Code Examples for com.intellij.psi.PsiElement#replace()

The following examples show how to use com.intellij.psi.PsiElement#replace() . 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: GroovyDslUtil.java    From ok-gradle with Apache License 2.0 7 votes vote down vote up
static void maybeUpdateName(@NotNull GradleDslElement element) {
  PsiElement oldName = element.getNameElement().getNamedPsiElement();
  String newName = element.getNameElement().getUnsavedName();
  PsiElement newElement;
  if (newName == null || oldName == null) {
    return;
  }
  if (oldName instanceof PsiNamedElement) {
    PsiNamedElement namedElement = (PsiNamedElement)oldName;
    namedElement.setName(newName);
    newElement = namedElement;
  }
  else {
    PsiElement psiElement = createNameElement(element, newName);
    if (psiElement == null) {
      throw new IllegalStateException("Can't create new GrExpression for name element");
    }
    newElement = oldName.replace(psiElement);
  }
  element.getNameElement().commitNameChange(newElement);
}
 
Example 2
Source File: ShaderLabElementColorProvider.java    From consulo-unity3d with Apache License 2.0 6 votes vote down vote up
@RequiredWriteAction
@Override
public void setColorTo(@Nonnull PsiElement element, @Nonnull ColorValue color)
{
	float[] colorComponents = color.toRGB().getFloatValues();

	StringBuilder builder = new StringBuilder();
	builder.append("(");
	for(int i = 0; i < (colorComponents[3] == 1 ? colorComponents.length - 1 : colorComponents.length); i++)
	{
		if(i != 0)
		{
			builder.append(", ");
		}

		builder.append(colorComponents[i]);
	}
	builder.append(")");

	ShaderPropertyValue value = createValue(element.getProject(), builder.toString());

	element.replace(value);
}
 
Example 3
Source File: GroovyDslUtil.java    From ok-gradle with Apache License 2.0 5 votes vote down vote up
static void applyDslLiteralOrReference(@NotNull GradleDslSettableExpression expression) {
  PsiElement psiElement = ensureGroovyPsi(expression.getPsiElement());
  if (psiElement == null) {
    return;
  }

  maybeUpdateName(expression);

  GrExpression newLiteral = extractUnsavedExpression(expression);
  if (newLiteral == null) {
    return;
  }
  PsiElement psiExpression = ensureGroovyPsi(expression.getExpression());
  if (psiExpression != null) {
    PsiElement replace = psiExpression.replace(newLiteral);
    if (replace instanceof GrLiteral || replace instanceof GrReferenceExpression || replace instanceof GrIndexProperty) {
      expression.setExpression(replace);
    }
  }
  else {
    // This element has just been created and will currently look like "propertyName =" or "propertyName ". Here we add the value.
    PsiElement added = psiElement.addAfter(newLiteral, psiElement.getLastChild());
    expression.setExpression(added);

    if (expression.getUnsavedConfigBlock() != null) {
      addConfigBlock(expression);
    }
  }

  expression.reset();
  expression.commit();
}
 
Example 4
Source File: JSGraphQLEndpointImportFileReferencePsiElement.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
	final PsiElement nameIdentifier = getNameIdentifier();
	if(nameIdentifier != null) {
		final LeafElement renamedLeaf = Factory.createSingleLeafElement(JSGraphQLEndpointTokenTypes.STRING_BODY, name, null, getManager());
		final PsiElement renamedPsiElement = SourceTreeToPsiMap.treeElementToPsi(renamedLeaf);
		if (renamedPsiElement != null) {
			nameIdentifier.replace(renamedPsiElement);
		}
	}
	return this;
}
 
Example 5
Source File: ReplaceVarWithParamExpansionQuickfix.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    // Replace this position with the same value, we have to trigger a reparse somehow
    Document document = file.getViewProvider().getDocument();
    if (document != null && document.isWritable()) {
        PsiElement replacement = BashPsiElementFactory.createComposedVar(project, variableName);
        startElement.replace(replacement);
    }
}
 
Example 6
Source File: DoubleBracketsQuickfix.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    BashConditionalCommand conditionalCommand = (BashConditionalCommand) startElement;
    String command = conditionalCommand.getCommandText();
    PsiElement replacement = useDoubleBrackets(project, command);
    startElement.replace(replacement);
}
 
Example 7
Source File: NoNewBaseActionFix.java    From eslint-plugin with MIT License 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement element, @NotNull PsiElement end) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    if (!(parent instanceof JSNewExpression)) return;
    final JSExpressionCodeFragment useStrict = JSElementFactory.createExpressionCodeFragment(project, getNewExp(), parent);
    PsiElement child = useStrict.getFirstChild();
    parent.replace(child);
}
 
Example 8
Source File: CSharpCodeBodyProxyImpl.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void replace(@Nullable PsiElement newElement)
{
	ASTNode lazyNode = myMethodElement.getNode().findChildByType(CSharpElements.METHOD_BODIES);
	if(lazyNode != null)
	{
		if(newElement == null)
		{
			lazyNode.getPsi().delete();
		}
		else
		{
			lazyNode.getPsi().replace(newElement);
		}
	}
	else
	{
		PsiElement element = getElement();

		if(newElement != null)
		{
			if(element == null)
			{
				myMethodElement.add(newElement);
			}
			else
			{
				element.replace(newElement);
			}
		}
		else if(element != null)
		{
			element.delete();
		}
	}
}
 
Example 9
Source File: CSharpRefactoringUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
public static void replaceNameIdentifier(PsiNameIdentifierOwner owner, String newName)
{
	PsiElement nameIdentifier = owner.getNameIdentifier();
	if(!(nameIdentifier instanceof CSharpIdentifier))
	{
		return;
	}

	CSharpIdentifier newIdentifier = CSharpFileFactory.createIdentifier(owner.getProject(), newName);

	nameIdentifier.replace(newIdentifier);
}
 
Example 10
Source File: CSharpParenthesesPostfixTemplate.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void expand(@Nonnull PsiElement context, @Nonnull Editor editor)
{
	DotNetExpression newExpression = CSharpFileFactory.createExpression(context.getProject(), "(" + context.getText() + ")");

	context.replace(newExpression);
}
 
Example 11
Source File: CorrectClassNameCasingYamlLocalQuickFix.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement psiElement1 = descriptor.getPsiElement();
    YAMLKeyValue replacement = YamlPsiElementFactory.createFromText(
            project,
            YAMLKeyValue.class,
            "class: " + replacementFQN
    );

    if (replacement != null && replacement.getValue() != null) {
        psiElement1.replace(replacement.getValue());
    }
}
 
Example 12
Source File: ImportReorderer.java    From needsmoredojo with Apache License 2.0 5 votes vote down vote up
public PsiElement[] reorder(PsiElement source, PsiElement destination)
{
    PsiElement[] results = new PsiElement[2];

    results[0] = destination.replace(source);
    results[1] = source.replace(destination);

    return results;
}
 
Example 13
Source File: GroovyDslWriter.java    From ok-gradle with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement moveDslElement(@NotNull GradleDslElement element) {
  // 1. Get the anchor where we need to move the element to.
  GradleDslElement anchorAfter = element.getAnchor();

  GroovyPsiElement psiElement = ensureGroovyPsi(element.getPsiElement());
  if (psiElement == null) {
    return null;
  }

  PsiElement parentPsiElement = getParentPsi(element);
  if (parentPsiElement == null) {
    return null;
  }

  PsiElement anchor = getPsiElementForAnchor(parentPsiElement, anchorAfter);

  // 2. Create a dummy element that we can move the element to.
  GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
  PsiElement lineTerminator = factory.createLineTerminator(1);
  PsiElement toReplace = parentPsiElement.addAfter(lineTerminator, anchor);

  // 3. Find the element we need to actually replace. The psiElement we have may be a child of what we need.
  PsiElement e = element.getPsiElement();
  while (!(e.getParent() instanceof GroovyFile || e.getParent() instanceof GrClosableBlock)) {
    // Make sure e isn't going to be set to null.
    if (e.getParent() == null) {
      e = element.getPsiElement();
      break;
    }
    e = e.getParent();
  }

  // 4. Copy the old PsiElement tree.
  PsiElement treeCopy = e.copy();

  // 5. Replace what we need to replace.
  PsiElement newTree = toReplace.replace(treeCopy);

  // 6. Delete the original tree.
  e.delete();

  // 7. Set the new PsiElement. Note: The internal state of this element will have invalid elements. It is required to reparse the file
  // to obtain the correct elements.
  element.setPsiElement(newTree);

  return element.getPsiElement();
}
 
Example 14
Source File: ShaderRefactorUtil.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
public static void replaceIdentifier(@Nonnull PsiElement identifier, @Nonnull String newName)
{
	PsiElement newIdentifier = ShaderFileFactory.createSimpleIdentifier(identifier.getProject(), newName);
	identifier.replace(newIdentifier);
}
 
Example 15
Source File: CSharpIntroduceHandler.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Nullable
protected PsiElement replaceExpression(PsiElement expression, PsiElement newExpression, CSharpIntroduceOperation operation)
{
	return expression.replace(newExpression);
}
 
Example 16
Source File: UtilToClassConverter.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
public void cleanupWhiteSpace(PsiElement parent)
{
    String result = "{\n" + parent.getText().substring(1).trim();
    parent.replace(JSUtil.createStatement(parent, result));
}