本文整理汇总了Java中com.intellij.lang.javascript.psi.impl.JSChangeUtil类的典型用法代码示例。如果您正苦于以下问题:Java JSChangeUtil类的具体用法?Java JSChangeUtil怎么用?Java JSChangeUtil使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JSChangeUtil类属于com.intellij.lang.javascript.psi.impl包,在下文中一共展示了JSChangeUtil类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: moveImportToEnd
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
private PsiElement moveImportToEnd(JSArrayLiteralExpression imports, JSParameterList parameters, String module, String parameter, PsiElement lastDefine, PsiElement lastParameter)
{
// TODO move to AMDPsiUtil if we need to reuse this in the future
PsiElement lastChild = imports.getChildren()[imports.getChildren().length-1];
if(lastDefine != null)
{
lastChild = lastDefine;
}
PsiElement element = imports.addAfter(JSChangeUtil.createExpressionFromText(imports.getProject(), String.format("%s", module)).getPsi(), lastChild);
imports.getNode().addLeaf(JSTokenTypes.COMMA, ",", element.getNode());
imports.getNode().addLeaf(JSTokenTypes.WHITE_SPACE, "\n", element.getNode());
PsiElement lastParameterChild = parameters.getChildren()[parameters.getChildren().length-1];
if(lastParameter != null)
{
lastParameterChild = lastParameter;
}
PsiElement parameterElement = parameters.addAfter(JSChangeUtil.createExpressionFromText(imports.getProject(), String.format("%s", parameter)).getPsi(), lastParameterChild);
parameters.getNode().addLeaf(JSTokenTypes.COMMA, ",", parameterElement.getNode());
return element;
}
示例2: getOppositePathSyntaxFromImport
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
/**
* Given a module that is being imported using absolute or relative path syntax, return the module
* import with the other syntax, if possible.
*
* @param define
* @return
*/
public @Nullable PsiElement getOppositePathSyntaxFromImport(PsiElement define)
{
if(define == null)
{
return null;
}
boolean relative = define.getText().charAt(1) == '.';
char quote = define.getText().charAt(0);
String moduleText = define.getText().replaceAll("'", "").replaceAll("\"", "");
String syntax = getPathSyntax(define.getProject(), define.getText(), define.getContainingFile(), !relative);
if(syntax.equals(moduleText))
{
return null; // no point in replacing with the same thing
}
return JSChangeUtil.createExpressionFromText(define.getProject(), quote + syntax + quote).getPsi();
}
示例3: invoke
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException
{
if(!CodeInsightUtilBase.getInstance().prepareFileForWrite(file))
{
return;
}
final ASTNode fromText = JSChangeUtil.createJSTreeFromText(project, "override class A {}");
final JSAttributeList jsAttributeList = myNode.getAttributeList();
final JSAttributeList createdAttrList = ((JSClass) fromText.getPsi()).getAttributeList();
if(jsAttributeList != null)
{
jsAttributeList.add(createdAttrList.getFirstChild());
}
else
{
myNode.addBefore(createdAttrList, myNode.getFirstChild());
}
}
示例4: surroundElements
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
@Override
@Nullable
public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws
IncorrectOperationException
{
ASTNode node = JSChangeUtil.createStatementFromText(project, getStatementTemplate(project, elements[0]));
PsiElement container = elements[0].getParent();
container.getNode().addChild(node, elements[0].getNode());
final ASTNode insertBeforeNode = getInsertBeforeNode(node);
for(int i = 0; i < elements.length; i++)
{
final ASTNode childNode = elements[i].getNode();
final ASTNode childNodeCopy = childNode.copyElement();
container.getNode().removeChild(childNode);
insertBeforeNode.getTreeParent().addChild(childNodeCopy, insertBeforeNode);
}
final CodeStyleManager csManager = CodeStyleManager.getInstance(project);
csManager.reformat(node.getPsi());
return getSurroundSelectionRange(node);
}
示例5: deleteImport
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
private static void deleteImport(final JSImportStatement anImport)
{
if(!anImport.isValid())
{
return;
}
PsiElement nextSibling = anImport.getNextSibling();
if(nextSibling instanceof PsiWhiteSpace)
{
// remove with the following whitespace
String whitespace = nextSibling.getText();
if(whitespace.contains("]]>"))
{
nextSibling.replace(JSChangeUtil.createJSTreeFromText(anImport.getProject(), "]]>").getPsi());
}
// don't remove trailing line break if it is an injection suffix
else if(nextSibling.getNextSibling() == null || nextSibling.getNextSibling().getNode().getElementType() != JSTokenTypes.RBRACE ||
!(nextSibling.getParent() instanceof JSBlockStatement) ||
!ImportUtils.isAnonymousEventHandler((JSBlockStatement) nextSibling.getParent()))
{
nextSibling.delete();
}
}
anImport.delete();
}
示例6: createImportBlock
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
public static String createImportBlock(Project project, Collection<String> fqns)
{
List<String> sorted = new ArrayList<String>(fqns);
Collections.sort(sorted, new Comparator<String>()
{
@Override
public int compare(final String o1, final String o2)
{
return compareImports(o1, o2);
}
});
final String semicolon = JSChangeUtil.getSemicolon(project);
StringBuilder s = new StringBuilder();
for(String fqn : sorted)
{
s.append("import ").append(fqn).append(semicolon);
}
return s.toString();
}
示例7: replaceExpression
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
protected static void replaceExpression(JSExpression expression,
String newExpression)
throws IncorrectOperationException {
final ASTNode fromText = JSChangeUtil.createStatementFromText(expression.getProject(), newExpression + ';');
final PsiElement element = fromText != null ? fromText.getPsi() : null;
final JSExpressionStatement expressionStatement = element instanceof JSExpressionStatement ? (JSExpressionStatement) element:null;
if (expressionStatement == null) {
return;
}
final JSExpression newExp = expressionStatement.getExpression();
final ASTNode newExpressionNode = newExp.getNode();
final ASTNode expressionNode = expression.getNode();
final PsiElement parent = expression.getParent();
final ASTNode parentNode = parent.getNode();
parentNode.replaceChild(expressionNode, newExpressionNode);
// final CodeStyleManager styleManager = manager.getCodeStyleManager();
// styleManager.reformat(newExpressionNode.getPsi());
}
示例8: replaceStatement
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
protected static void replaceStatement(JSStatement statement,
@NonNls String newStatement)
throws IncorrectOperationException {
final ASTNode fromText = JSChangeUtil.createStatementFromText(statement.getProject(), newStatement);
final PsiElement element = fromText != null ? fromText.getPsi() : null;
final JSStatement newStmt = element instanceof JSStatement ? (JSStatement)element:null;
if (newStmt == null) {
return;
}
final PsiElement parent = statement.getParent();
final ASTNode parentNode = parent.getNode();
final ASTNode statementNode = statement.getNode();
final ASTNode newStatementNode = newStmt.getNode();
parentNode.replaceChild(statementNode, newStatementNode);
// final CodeStyleManager styleManager = manager.getCodeStyleManager();
// styleManager.reformat(newStatementNode.getPsi());
}
示例9: replaceElementWithStatement
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
public static JSStatement replaceElementWithStatement(@NotNull JSElement element,
@NonNls @NotNull String statementText)
throws IncorrectOperationException {
final ASTNode newStatementNode = JSChangeUtil.createStatementFromText(
element.getProject(),
statementText);
final ASTNode oldStatementParentNode = element.getNode();
final PsiElement parentNode = element.getParent();
final ASTNode newStatementParentNode = parentNode.getNode();
if (newStatementParentNode == null || oldStatementParentNode == null || newStatementNode == null) {
return null;
}
newStatementParentNode.replaceChild(oldStatementParentNode, newStatementNode);
reformat(parentNode);
return (JSStatement) newStatementNode.getPsi();
}
示例10: replaceStatement
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
public static JSStatement replaceStatement(@NotNull JSStatement statement,
@NonNls @NotNull String text)
throws IncorrectOperationException {
final ASTNode newStatementNode = JSChangeUtil.createStatementFromText(
statement.getProject(),
text);
final ASTNode oldStatementParentNode = statement.getNode();
final PsiElement parentNode = statement.getParent();
final ASTNode newStatementParentNode = parentNode.getNode();
if (newStatementParentNode == null || oldStatementParentNode == null || newStatementNode == null) {
return null;
}
newStatementParentNode.replaceChild(oldStatementParentNode, newStatementNode);
reformat(parentNode);
return (JSStatement) newStatementNode.getPsi();
}
示例11: addStatementBefore
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
public static JSStatement addStatementBefore(@NotNull JSStatement statement,
@NonNls @NotNull String previousStatementText)
throws IncorrectOperationException {
final ASTNode newStatementNode = JSChangeUtil.createStatementFromText(
statement.getProject(),
previousStatementText);
final ASTNode oldStatementParentNode = statement.getNode();
final PsiElement parentNode = statement.getParent();
final ASTNode newStatementParentNode = parentNode.getNode();
if (newStatementParentNode == null || newStatementNode == null) {
return null;
}
newStatementParentNode.addChild(newStatementNode, oldStatementParentNode);
reformat(parentNode);
return (JSStatement) newStatementNode.getPsi();
}
示例12: addStatementAfter
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
public static JSStatement addStatementAfter(@NotNull JSStatement statement,
@NonNls @NotNull String nextStatementText)
throws IncorrectOperationException {
final ASTNode newStatementNode = JSChangeUtil.createStatementFromText(
statement.getProject(),
nextStatementText);
final ASTNode statementNode = statement.getNode();
final ASTNode oldStatementParentNode = ((statementNode == null) ? null : statementNode.getTreeNext());
final PsiElement parentNode = statement.getParent();
final ASTNode newStatementParentNode = parentNode.getNode();
if (newStatementParentNode == null || newStatementNode == null) {
return null;
}
newStatementParentNode.addChild(newStatementNode, oldStatementParentNode);
reformat(parentNode);
return (JSStatement) newStatementNode.getPsi();
}
示例13: getNegated
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
public static JSExpression getNegated(JSExpression condition) {
if (condition instanceof JSPrefixExpression) {
final JSPrefixExpression prefixExp = (JSPrefixExpression) condition;
final JSExpression operand = prefixExp.getExpression();
return ParenthesesUtils.stripParentheses(operand);
} else if (condition instanceof JSBinaryExpression) {
final JSBinaryExpression binaryExpression = (JSBinaryExpression) condition;
final IElementType sign = binaryExpression.getOperationSign();
final JSExpression lhs = binaryExpression.getLOperand();
final JSExpression rhs = binaryExpression.getROperand();
final String negatedSign = ComparisonUtils.getNegatedOperatorText(sign);
final String negatedText = lhs.getText() + negatedSign + rhs.getText();
return (JSExpression) JSChangeUtil.createExpressionFromText(
condition.getProject(),
negatedText);
} else if (condition instanceof JSParenthesizedExpression) {
return getNegated(((JSParenthesizedExpression) condition).getInnerExpression());
}
return condition;
}
示例14: invoke
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
@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 {
JSIndexedPropertyAccessExpression indexed = PsiTreeUtil.getParentOfType(element, JSIndexedPropertyAccessExpression.class);
JSReferenceExpression ref = PsiTreeUtil.findChildOfType(indexed, JSReferenceExpression.class);
JSLiteralExpression literalExpression = (JSLiteralExpression) indexed.getIndexExpression();
String path = StringUtil.stripQuotesAroundValue(literalExpression.getText());
ASTNode dotExp = JSChangeUtil.createStatementFromText(project, ref.getText() + '.' + path);
indexed.replace(dotExp.getPsi());
}
示例15: invoke
import com.intellij.lang.javascript.psi.impl.JSChangeUtil; //导入依赖的package包/类
@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 element = descriptor.getPsiElement();
JSBinaryExpression binary = PsiTreeUtil.getParentOfType(element, JSBinaryExpression.class);
JSBinaryExpression binaryClone = (JSBinaryExpression) binary.copy();
binaryClone.getLOperand().replace(binary.getLOperand().getLastChild());
ASTNode negate = JSChangeUtil.createStatementFromText(project, "!(true)");
JSParenthesizedExpression paren = PsiTreeUtil.getChildOfType(negate.getPsi().getFirstChild(), JSParenthesizedExpression.class);
paren.getInnerExpression().replace(binaryClone);
binary.replace(negate.getPsi());
}