use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrHighlightUtil method collectReassignedNames.
private static Set<String> collectReassignedNames(PsiElement scope) {
final Set<String> result = ContainerUtil.newHashSet();
PsiTreeUtil.processElements(scope, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
if (!(element instanceof GrReferenceExpression) || !((GrReferenceExpression) element).isQualified()) {
return true;
}
GrReferenceExpression ref = (GrReferenceExpression) element;
if (isWriteAccess(ref)) {
String varName = ref.getReferenceName();
if (!result.contains(varName)) {
PsiElement target = ref.resolve();
if (target instanceof GrVariable && ((GrVariable) target).getInitializerGroovy() != null || target instanceof GrParameter) {
result.add(varName);
}
}
}
return true;
}
});
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrInplaceVariableIntroducer method getComponent.
@Override
protected JComponent getComponent() {
myCanBeFinalCb = new NonFocusableCheckBox("Declare final");
myCanBeFinalCb.setSelected(false);
myCanBeFinalCb.setMnemonic('f');
final GrFinalListener finalListener = new GrFinalListener(myEditor);
myCanBeFinalCb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new WriteCommandAction(myProject, getCommandName(), getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
final GrVariable variable = getVariable();
if (variable != null) {
finalListener.perform(myCanBeFinalCb.isSelected(), variable);
}
}
}.execute();
}
});
final JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(null);
if (myCanBeFinalCb != null) {
panel.add(myCanBeFinalCb, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
}
panel.add(Box.createVerticalBox(), new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
return panel;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrInplaceVariableIntroducer method getInitialSettingsForInplace.
@Nullable
@Override
protected GroovyIntroduceVariableSettings getInitialSettingsForInplace(@NotNull final GrIntroduceContext context, @NotNull final OccurrencesChooser.ReplaceChoice choice, final String[] names) {
return new GroovyIntroduceVariableSettings() {
private final CanonicalTypes.Type myType;
{
GrExpression expression = context.getExpression();
StringPartInfo stringPart = context.getStringPart();
GrVariable var = context.getVar();
PsiType type = expression != null ? expression.getType() : var != null ? var.getType() : stringPart != null ? stringPart.getLiteral().getType() : null;
myType = type != null && !PsiType.NULL.equals(type) ? CanonicalTypes.createTypeWrapper(type) : null;
}
@Override
public boolean isDeclareFinal() {
return myCanBeFinalCb != null ? myCanBeFinalCb.isSelected() : false;
}
@Nullable
@Override
public String getName() {
return names[0];
}
@Override
public boolean replaceAllOccurrences() {
return choice == OccurrencesChooser.ReplaceChoice.ALL;
}
@Nullable
@Override
public PsiType getSelectedType() {
return myType != null ? myType.getType(context.getPlace()) : null;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class UnusedDefInspection method check.
@Override
protected void check(@NotNull final GrControlFlowOwner owner, @NotNull final ProblemsHolder problemsHolder) {
final Instruction[] flow = owner.getControlFlow();
final ReachingDefinitionsDfaInstance dfaInstance = new ReachingDefinitionsDfaInstance(flow);
final ReachingDefinitionsSemilattice lattice = new ReachingDefinitionsSemilattice();
final DFAEngine<DefinitionMap> engine = new DFAEngine<>(flow, dfaInstance, lattice);
final List<DefinitionMap> dfaResult = engine.performDFAWithTimeout();
if (dfaResult == null) {
return;
}
final TIntHashSet unusedDefs = new TIntHashSet();
for (Instruction instruction : flow) {
if (instruction instanceof ReadWriteVariableInstruction && ((ReadWriteVariableInstruction) instruction).isWrite()) {
unusedDefs.add(instruction.num());
}
}
for (int i = 0; i < dfaResult.size(); i++) {
final Instruction instruction = flow[i];
if (instruction instanceof ReadWriteVariableInstruction) {
final ReadWriteVariableInstruction varInst = (ReadWriteVariableInstruction) instruction;
if (!varInst.isWrite()) {
final String varName = varInst.getVariableName();
DefinitionMap e = dfaResult.get(i);
e.forEachValue(new TObjectProcedure<TIntHashSet>() {
@Override
public boolean execute(TIntHashSet reaching) {
reaching.forEach(new TIntProcedure() {
@Override
public boolean execute(int defNum) {
final String defName = ((ReadWriteVariableInstruction) flow[defNum]).getVariableName();
if (varName.equals(defName)) {
unusedDefs.remove(defNum);
}
return true;
}
});
return true;
}
});
}
}
}
final Set<PsiElement> checked = ContainerUtil.newHashSet();
unusedDefs.forEach(new TIntProcedure() {
@Override
public boolean execute(int num) {
final ReadWriteVariableInstruction instruction = (ReadWriteVariableInstruction) flow[num];
final PsiElement element = instruction.getElement();
process(element, checked, problemsHolder, GroovyInspectionBundle.message("unused.assignment.tooltip"));
return true;
}
});
owner.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitVariable(@NotNull GrVariable variable) {
if (checked.contains(variable) || variable.getInitializerGroovy() != null)
return;
if (ReferencesSearch.search(variable, variable.getUseScope()).findFirst() == null) {
process(variable, checked, problemsHolder, GroovyInspectionBundle.message("unused.variable"));
}
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class PsiImplUtil method removeVariable.
public static void removeVariable(GrVariable variable) {
final GrVariableDeclaration varDecl = (GrVariableDeclaration) variable.getParent();
final List<GrVariable> variables = Arrays.asList(varDecl.getVariables());
if (!variables.contains(variable)) {
throw new IllegalArgumentException();
}
final PsiElement parent = varDecl.getParent();
final ASTNode owner = parent.getNode();
if (variables.size() == 1 && owner != null) {
PsiElement next = varDecl.getNextSibling();
//noinspection ConstantConditions
while (next != null && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI) {
PsiElement tmpNext = next.getNextSibling();
//noinspection ConstantConditions
next.delete();
next = tmpNext;
}
removeNewLineAfter(varDecl);
varDecl.delete();
return;
}
variable.delete();
}
Aggregations