本文整理汇总了Java中com.intellij.refactoring.introduceVariable.IntroduceVariableBase类的典型用法代码示例。如果您正苦于以下问题:Java IntroduceVariableBase类的具体用法?Java IntroduceVariableBase怎么用?Java IntroduceVariableBase使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IntroduceVariableBase类属于com.intellij.refactoring.introduceVariable包,在下文中一共展示了IntroduceVariableBase类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: replaceOccurenceWithFieldRef
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
public static PsiElement replaceOccurenceWithFieldRef(PsiExpression occurrence, PsiField newField, PsiClass destinationClass)
throws IncorrectOperationException {
final PsiManager manager = destinationClass.getManager();
final String fieldName = newField.getName();
final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
final PsiElement element = occurrence.getUserData(ElementToWorkOn.PARENT);
final PsiVariable psiVariable = facade.getResolveHelper().resolveAccessibleReferencedVariable(fieldName, element != null ? element : occurrence);
final PsiElementFactory factory = facade.getElementFactory();
if (psiVariable != null && psiVariable.equals(newField)) {
return IntroduceVariableBase.replace(occurrence, factory.createExpressionFromText(fieldName, null), manager.getProject());
}
else {
final PsiReferenceExpression ref = (PsiReferenceExpression)factory.createExpressionFromText("this." + fieldName, null);
if (!occurrence.isValid()) return null;
if (newField.hasModifierProperty(PsiModifier.STATIC)) {
ref.setQualifierExpression(factory.createReferenceExpression(destinationClass));
}
return IntroduceVariableBase.replace(occurrence, ref, manager.getProject());
}
}
示例2: testNameSuggestion
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
public void testNameSuggestion() {
final String expectedTypeName = "Path";
doTest(new MockIntroduceVariableHandler("path", true, false, false, expectedTypeName) {
@Override
public IntroduceVariableSettings getSettings(Project project, Editor editor,
PsiExpression expr, PsiExpression[] occurrences,
TypeSelectorManagerImpl typeSelectorManager,
boolean declareFinalIfAll,
boolean anyAssignmentLHS,
InputValidator validator,
PsiElement anchor, final OccurrencesChooser.ReplaceChoice replaceChoice) {
final PsiType type = typeSelectorManager.getDefaultType();
assertTrue(type.getPresentableText(), type.getPresentableText().equals(expectedTypeName));
assertEquals("path", IntroduceVariableBase.getSuggestedName(type, expr).names[0]);
return super.getSettings(project, editor, expr, occurrences, typeSelectorManager, declareFinalIfAll, anyAssignmentLHS,
validator, anchor, replaceChoice);
}
});
}
示例3: testNameSuggestion
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
public void testNameSuggestion() throws Exception {
final String expectedTypeName = "Path";
doTest(new MockIntroduceVariableHandler("path", true, false, false, expectedTypeName) {
@Override
public IntroduceVariableSettings getSettings(Project project, Editor editor,
PsiExpression expr, PsiExpression[] occurrences,
TypeSelectorManagerImpl typeSelectorManager,
boolean declareFinalIfAll,
boolean anyAssignmentLHS,
InputValidator validator,
PsiElement anchor, final OccurrencesChooser.ReplaceChoice replaceChoice) {
final PsiType type = typeSelectorManager.getDefaultType();
Assert.assertTrue(type.getPresentableText(), type.getPresentableText().equals(expectedTypeName));
Assert.assertEquals("path", IntroduceVariableBase.getSuggestedName(type, expr).names[0]);
return super.getSettings(project, editor, expr, occurrences, typeSelectorManager, declareFinalIfAll, anyAssignmentLHS,
validator, anchor, replaceChoice);
}
});
}
示例4: createAction
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
PerformAction createAction(final String className, final IntroduceVariableBase testMe) {
return new PerformAction() {
@Override
public void performAction(VirtualFile vroot, VirtualFile rootAfter) {
final JavaPsiFacade psiManager = getJavaFacade();
final PsiClass aClass = psiManager.findClass(className, GlobalSearchScope.allScope(myProject));
assertTrue(className + " class not found", aClass != null);
final PsiFile containingFile = aClass.getContainingFile();
final VirtualFile virtualFile = containingFile.getVirtualFile();
assertTrue(virtualFile != null);
final Editor editor = createEditor(virtualFile);
setupCursorAndSelection(editor);
testMe.invoke(myProject, editor, containingFile, null);
FileDocumentManager.getInstance().saveAllDocuments();
}
};
}
示例5: surroundExpression
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
@Override
public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
PsiManager manager = expr.getManager();
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
PsiPrefixExpression prefixExpr = (PsiPrefixExpression)factory.createExpressionFromText("!(a)", null);
prefixExpr = (PsiPrefixExpression)codeStyleManager.reformat(prefixExpr);
((PsiParenthesizedExpression)prefixExpr.getOperand()).getExpression().replace(expr);
expr = (PsiExpression)IntroduceVariableBase.replace(expr, prefixExpr, project);
int offset = expr.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
示例6: surroundExpression
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
@Override
public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
PsiManager manager = expr.getManager();
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
PsiParenthesizedExpression parenthExpr = (PsiParenthesizedExpression)factory.createExpressionFromText("(a)", null);
parenthExpr = (PsiParenthesizedExpression)codeStyleManager.reformat(parenthExpr);
parenthExpr.getExpression().replace(expr);
expr = (PsiExpression)IntroduceVariableBase.replace(expr, parenthExpr, project);
int offset = expr.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
示例7: getElementsToSurround
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
@Override
@NotNull public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
if (expr == null) {
expr = IntroduceVariableBase.getSelectedExpression(file.getProject(), file, startOffset, endOffset);
if (expr == null) {
return PsiElement.EMPTY_ARRAY;
}
}
FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.surroundwith.expression");
return new PsiElement[] {expr};
}
示例8: selectAndPass
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
public static void selectAndPass(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final Pass<PsiElement[]> callback) {
editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
if (!editor.getSelectionModel().hasSelection()) {
final int offset = editor.getCaretModel().getOffset();
final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, offset, true);
if (expressions.isEmpty()) {
editor.getSelectionModel().selectLineAtCaret();
}
else if (expressions.size() == 1) {
callback.pass(new PsiElement[]{expressions.get(0)});
return;
}
else {
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PsiExpression>() {
@Override
public void pass(PsiExpression psiExpression) {
callback.pass(new PsiElement[]{psiExpression});
}
}, new PsiExpressionTrimRenderer.RenderFunction());
return;
}
}
PsiDocumentManager.getInstance(project).commitAllDocuments();
callback.pass(getElements(project, editor, file));
}
示例9: getElements
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
public static PsiElement[] getElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
int startOffset = selectionModel.getSelectionStart();
int endOffset = selectionModel.getSelectionEnd();
PsiElement[] elements;
PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
if (expr != null) {
elements = new PsiElement[]{expr};
}
else {
elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
if (elements.length == 0) {
final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset);
if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) {
final PsiType originalType = RefactoringUtil.getTypeByExpressionWithExpectedType(expression);
if (originalType != null) {
elements = new PsiElement[]{expression};
}
}
}
}
return elements;
}
final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, editor.getCaretModel().getOffset());
return expressions.toArray(new PsiElement[expressions.size()]);
}
示例10: findExpressionOccurrences
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
public PsiExpression[] findExpressionOccurrences() {
if (myMainOccurence instanceof PsiLiteralExpression && !myMainOccurence.isPhysical()) {
final FindManager findManager = FindManager.getInstance(getScope().getProject());
final FindModel findModel = (FindModel)findManager.getFindInFileModel().clone();
findModel.setCaseSensitive(true);
findModel.setRegularExpressions(false);
String value = StringUtil.stripQuotesAroundValue(myMainOccurence.getText());
if (value.length() > 0) {
findModel.setStringToFind(value);
final List<PsiExpression> results = new ArrayList<PsiExpression>();
final PsiFile file = getScope().getContainingFile();
final String text = getScope().getText();
final int offset = getScope().getTextRange().getStartOffset();
FindResult result = findManager.findString(text, 0, findModel);
final Set<PsiLiteralExpression> literals = new HashSet<PsiLiteralExpression>();
while (result.isStringFound()) {
final int startOffset = offset + result.getStartOffset();
final int endOffset = result.getEndOffset();
final PsiLiteralExpression literalExpression =
PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), PsiLiteralExpression.class);
if (literalExpression != null && !literals.contains(literalExpression)) { //enum. occurrences inside string literals
final PsiExpression expression =
IntroduceVariableBase.getSelectedExpression(file.getProject(), file, startOffset, offset + endOffset);
if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) {
results.add(expression);
literals.add(literalExpression);
}
}
result = findManager.findString(text, endOffset, findModel);
}
return results.toArray(new PsiExpression[results.size()]);
}
}
return CodeInsightUtil.findExpressionOccurrences(myScope, myMainOccurence);
}
示例11: createAction
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
PerformAction createAction(final String className, final IntroduceVariableBase testMe) {
return (vroot, rootAfter) -> {
final JavaPsiFacade psiManager = getJavaFacade();
final PsiClass aClass = psiManager.findClass(className, GlobalSearchScope.allScope(myProject));
assertTrue(className + " class not found", aClass != null);
final PsiFile containingFile = aClass.getContainingFile();
final VirtualFile virtualFile = containingFile.getVirtualFile();
assertTrue(virtualFile != null);
final Editor editor = createEditor(virtualFile);
setupCursorAndSelection(editor);
testMe.invoke(myProject, editor, containingFile, null);
FileDocumentManager.getInstance().saveAllDocuments();
};
}
示例12: getExpressions
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
@NotNull
protected List<PsiExpression> getExpressions(@NotNull PsiElement context, @NotNull Document document, final int offset) {
List<PsiExpression> expressions = ContainerUtil.filter(IntroduceVariableBase.collectExpressions(context.getContainingFile(), document,
Math.max(offset - 1, 0), false),
new Condition<PsiExpression>() {
@Override
public boolean value(PsiExpression expression) {
return expression.getTextRange().getEndOffset() == offset;
}
}
);
return ContainerUtil.filter(expressions.isEmpty() ? maybeTopmostExpression(context) : expressions, getTypeCondition());
}
示例13: completeLocalVariableName
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
private static void completeLocalVariableName(Set<LookupElement> set, PrefixMatcher matcher, PsiVariable var, boolean includeOverlapped) {
FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.variable.name");
Project project = var.getProject();
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
final VariableKind variableKind = codeStyleManager.getVariableKind(var);
String propertyName = null;
if (variableKind == VariableKind.PARAMETER) {
final PsiMethod method = PsiTreeUtil.getParentOfType(var, PsiMethod.class);
if (method != null) {
propertyName = PropertyUtil.getPropertyName(method);
}
if (method != null && method.getName().startsWith("with")) {
propertyName = StringUtil.decapitalize(method.getName().substring(4));
}
}
final PsiType type = var.getType();
SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(variableKind, propertyName, null, type, StringUtil.isEmpty(matcher.getPrefix()));
suggestedNameInfo = codeStyleManager.suggestUniqueVariableName(suggestedNameInfo, var, false);
final String[] suggestedNames = suggestedNameInfo.names;
addLookupItems(set, suggestedNameInfo, matcher, project, suggestedNames);
if (!hasStartMatches(set, matcher)) {
if (type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) && matcher.prefixMatches("object")) {
set.add(withInsertHandler(suggestedNameInfo, LookupElementBuilder.create("object")));
}
if (type.equalsToText(CommonClassNames.JAVA_LANG_STRING) && matcher.prefixMatches("string")) {
set.add(withInsertHandler(suggestedNameInfo, LookupElementBuilder.create("string")));
}
}
if (!hasStartMatches(set, matcher) && includeOverlapped) {
addLookupItems(set, null, matcher, project, getOverlappedNameVersions(matcher.getPrefix(), suggestedNames, ""));
}
PsiElement parent = PsiTreeUtil.getParentOfType(var, PsiCodeBlock.class);
if(parent == null) parent = PsiTreeUtil.getParentOfType(var, PsiMethod.class, PsiLambdaExpression.class);
addLookupItems(set, suggestedNameInfo, matcher, project, getUnresolvedReferences(parent, false));
if (var instanceof PsiParameter && parent instanceof PsiMethod) {
addSuggestionsInspiredByFieldNames(set, matcher, var, project, codeStyleManager);
}
PsiExpression initializer = var.getInitializer();
if (initializer != null) {
SuggestedNameInfo initializerSuggestions = IntroduceVariableBase.getSuggestedName(type, initializer);
addLookupItems(set, initializerSuggestions, matcher, project, initializerSuggestions.names);
}
}
示例14: processElementToWorkOn
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
public static void processElementToWorkOn(final Editor editor, final PsiFile file, final String refactoringName, final String helpId, final Project project, final ElementsProcessor<ElementToWorkOn> processor) {
PsiLocalVariable localVar = null;
PsiExpression expr = null;
if (!editor.getSelectionModel().hasSelection()) {
PsiElement element = TargetElementUtil.findTargetElement(editor, TargetElementUtil
.ELEMENT_NAME_ACCEPTED | TargetElementUtil
.REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil
.LOOKUP_ITEM_ACCEPTED);
if (element instanceof PsiLocalVariable) {
localVar = (PsiLocalVariable) element;
PsiElement elementAt = file.findElementAt(editor.getCaretModel().getOffset());
if (elementAt instanceof PsiIdentifier && elementAt.getParent() instanceof PsiReferenceExpression) {
expr = (PsiExpression) elementAt.getParent();
} else {
final PsiReference reference = TargetElementUtil.findReference(editor);
if (reference != null) {
final PsiElement refElement = reference.getElement();
if (refElement instanceof PsiReferenceExpression) {
expr = (PsiReferenceExpression)refElement;
}
}
}
} else {
final PsiLocalVariable variable = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PsiLocalVariable.class);
final int offset = editor.getCaretModel().getOffset();
final PsiElement[] statementsInRange = IntroduceVariableBase.findStatementsAtOffset(editor, file, offset);
if (statementsInRange.length == 1 && IntroduceVariableBase.selectLineAtCaret(offset, statementsInRange)) {
editor.getSelectionModel().selectLineAtCaret();
final ElementToWorkOn elementToWorkOn = getElementToWorkOn(editor, file, refactoringName, helpId, project, localVar, expr);
if (elementToWorkOn == null || elementToWorkOn.getLocalVariable() == null && elementToWorkOn.getExpression() == null || !processor.accept(elementToWorkOn)) {
editor.getSelectionModel().removeSelection();
}
}
if (!editor.getSelectionModel().hasSelection()){
final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, offset);
for (Iterator<PsiExpression> iterator = expressions.iterator(); iterator.hasNext(); ) {
PsiExpression expression = iterator.next();
if (!processor.accept(new ElementToWorkOn(null, expression))) {
iterator.remove();
}
}
if (expressions.isEmpty()) {
editor.getSelectionModel().selectLineAtCaret();
}
else if (!IntroduceVariableBase.isChooserNeeded(expressions)) {
expr = expressions.get(0);
}
else {
final int selection = IntroduceVariableBase.preferredSelection(statementsInRange, expressions);
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PsiExpression>() {
@Override
public void pass(final PsiExpression selectedValue) {
PsiLocalVariable var = null; //replace var if selected expression == var initializer
if (variable != null && variable.getInitializer() == selectedValue) {
var = variable;
}
processor.pass(getElementToWorkOn(editor, file, refactoringName, helpId, project, var, selectedValue));
}
}, new PsiExpressionTrimRenderer.RenderFunction(), "Expressions", selection, ScopeHighlighter.NATURAL_RANGER);
return;
}
}
}
}
processor.pass(getElementToWorkOn(editor, file, refactoringName, helpId, project, localVar, expr));
}
示例15: getElementToWorkOn
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; //导入依赖的package包/类
private static ElementToWorkOn getElementToWorkOn(final Editor editor, final PsiFile file,
final String refactoringName,
final String helpId,
final Project project, PsiLocalVariable localVar, PsiExpression expr) {
int startOffset = 0;
int endOffset = 0;
if (localVar == null && expr == null) {
startOffset = editor.getSelectionModel().getSelectionStart();
endOffset = editor.getSelectionModel().getSelectionEnd();
expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
if (expr == null) {
PsiIdentifier ident = CodeInsightUtil.findElementInRange(file, startOffset, endOffset, PsiIdentifier.class);
if (ident != null) {
localVar = PsiTreeUtil.getParentOfType(ident, PsiLocalVariable.class);
}
}
}
if (expr == null && localVar == null) {
PsiElement[] statements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
if (statements.length == 1 && statements[0] instanceof PsiExpressionStatement) {
expr = ((PsiExpressionStatement)statements[0]).getExpression();
}
else if (statements.length == 1 && statements[0] instanceof PsiDeclarationStatement) {
PsiDeclarationStatement decl = (PsiDeclarationStatement)statements[0];
PsiElement[] declaredElements = decl.getDeclaredElements();
if (declaredElements.length == 1 && declaredElements[0] instanceof PsiLocalVariable) {
localVar = (PsiLocalVariable)declaredElements[0];
}
}
}
if (localVar == null && expr == null) {
expr = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset);
}
if (localVar == null && expr != null) {
final String errorMessage = IntroduceVariableBase.getErrorMessage(expr);
if (errorMessage != null) {
CommonRefactoringUtil.showErrorHint(project, editor, errorMessage, refactoringName, helpId);
return null;
}
}
return new ElementToWorkOn(localVar, expr);
}