@Override protected boolean isUsed(Property property) { PsiFile file = property.getContainingFile(); if (Comparing.equal(file.getName(), FN_GRADLE_WRAPPER_PROPERTIES, SystemInfo.isFileSystemCaseSensitive)) { // Ignore all properties in the gradle wrapper: read by the gradle wrapper .jar code return true; } // The android gradle plugin reads sdk.dir, ndk.dir and android.dir from local.properties if (Comparing.equal(file.getName(), FN_LOCAL_PROPERTIES, SystemInfo.isFileSystemCaseSensitive)) { String name = property.getName(); return SDK_DIR_PROPERTY.equals(name) || "ndk.dir".equals(name) || "android.dir".equals(name); } return false; }
public void testLocalProperties() { VirtualFile vFile = myFixture.copyFileToProject("test.properties", "local.properties"); PsiFile file = PsiManager.getInstance(getProject()).findFile(vFile); assertNotNull(file); PropertiesFile propertiesFile = (PropertiesFile)file; GradleImplicitPropertyUsageProvider provider = new GradleImplicitPropertyUsageProvider(); for (IProperty property : propertiesFile.getProperties()) { Property p = (Property)property; // Only but the property with "unused" in its name are considered used String name = property.getName(); if (name.contains("unused")) { assertFalse(name, provider.isUsed(p)); } else { assertTrue(name, provider.isUsed(p)); } } }
private static Collection<PsiReference> findPropertyReferences(final Property pproperty, final Module module) { final Collection<PsiReference> references = Collections.synchronizedList(new ArrayList<PsiReference>()); ProgressManager.getInstance().runProcessWithProgressSynchronously( new Runnable() { public void run() { ReferencesSearch.search(pproperty).forEach(new Processor<PsiReference>() { public boolean process(final PsiReference psiReference) { PsiMethod method = PsiTreeUtil.getParentOfType(psiReference.getElement(), PsiMethod.class); if (method == null || !AsmCodeGenerator.SETUP_METHOD_NAME.equals(method.getName())) { references.add(psiReference); } return true; } }); } }, UIDesignerBundle.message("edit.text.searching.references"), false, module.getProject() ); return references; }
public void insertOrUpdateTranslation(String key, String value, final PropertiesFile propertiesFile) throws IncorrectOperationException { final IProperty property = propertiesFile.findPropertyByKey(key); if (property != null) { property.setValue(value); return; } if (myOrdered) { if (myAlphaSorted) { propertiesFile.addProperty(key, value); return; } final Pair<IProperty, Integer> propertyAndPosition = findExistedPrevSiblingProperty(key, propertiesFile); propertiesFile.addPropertyAfter(key, value, propertyAndPosition == null ? null : (Property)propertyAndPosition.getFirst()); } else { insertPropertyLast(key, value, propertiesFile); } }
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { if (!(element instanceof IProperty)) return; final Property property = (Property)element; PropertiesFile propertiesFile = property.getPropertiesFile(); Collection<IProperty> others = propertiesFile.findPropertiesByKey(property.getUnescapedKey()); ASTNode keyNode = ((PropertyImpl)property).getKeyNode(); if (others.size() != 1) { Annotation annotation = holder.createErrorAnnotation(keyNode, PropertiesBundle.message("duplicate.property.key.error.message")); annotation.registerFix(PropertiesQuickFixFactory.getInstance().createRemovePropertyFix(property)); } highlightTokens(property, keyNode, holder, new PropertiesHighlighter()); ASTNode valueNode = ((PropertyImpl)property).getValueNode(); if (valueNode != null) { highlightTokens(property, valueNode, holder, new PropertiesValueHighlighter()); } }
@Override public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException { final PsiFile file = element.getContainingFile(); if (!FileModificationService.getInstance().prepareFileForWrite(file)) return; final Property property = PsiTreeUtil.getParentOfType(element, Property.class); LOG.assertTrue(property != null); final int start = property.getTextRange().getStartOffset(); @NonNls final Document doc = PsiDocumentManager.getInstance(project).getDocument(file); LOG.assertTrue(doc != null); final int line = doc.getLineNumber(start); final int lineStart = doc.getLineStartOffset(line); doc.insertString(lineStart, "# suppress inspection \"" + shortName + "\"\n"); }
private void ensurePropertiesLoaded() { if (myPropertiesMap != null) return; final ASTNode[] props = getPropertiesList().getChildren(PropertiesElementTypes.PROPERTIES); MostlySingularMultiMap<String, IProperty> propertiesMap = new MostlySingularMultiMap<String, IProperty>(); List<IProperty> properties = new ArrayList<IProperty>(props.length); for (final ASTNode prop : props) { final Property property = (Property)prop.getPsi(); String key = property.getUnescapedKey(); propertiesMap.add(key, property); properties.add(property); } final boolean isAlphaSorted = PropertiesImplUtil.isAlphaSorted(properties); synchronized (lock) { if (myPropertiesMap != null) return; myProperties = properties; myPropertiesMap = propertiesMap; myAlphaSorted = isAlphaSorted; } }
@Nullable private static Property getPropertyArgument(PsiMethodCallExpression arg) { PsiExpression[] args = arg.getArgumentList().getExpressions(); if (args.length > 0) { PsiReference[] references = args[0].getReferences(); for (PsiReference reference : references) { if (reference instanceof PropertyReference) { ResolveResult[] resolveResults = ((PropertyReference)reference).multiResolve(false); if (resolveResults.length == 1 && resolveResults[0].isValidResult()) { PsiElement element = resolveResults[0].getElement(); if (element instanceof Property) { return (Property) element; } } } } } return null; }
@Override protected boolean isUsed(Property property) { PropertiesFileCndKeyModel cndKeyModel = null; try { cndKeyModel = new PropertiesFileCndKeyModel(property.getKey()); } catch (IllegalArgumentException e) { //Nothing to do } if (cndKeyModel != null) { Project project = property.getProject(); String namespace = cndKeyModel.getNamespace(); String nodeTypeName = cndKeyModel.getNodeTypeName(); if (cndKeyModel.isProperty() || cndKeyModel.isProperty() || cndKeyModel.isChoicelistElement()) { return CndUtil.findProperty(project, namespace, nodeTypeName, cndKeyModel.getPropertyName()) != null; } else { return CndUtil.findNodeType(project, namespace, nodeTypeName) != null; } } return false; }
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { if (!(element instanceof IProperty)) return; final Property property = (Property)element; PropertiesFile propertiesFile = property.getPropertiesFile(); Collection<IProperty> others = propertiesFile.findPropertiesByKey(property.getUnescapedKey()); ASTNode keyNode = ((PropertyImpl)property).getKeyNode(); if (others.size() != 1) { Annotation annotation = holder.createErrorAnnotation(keyNode, PropertiesBundle.message("duplicate.property.key.error.message")); annotation.registerFix(new RemovePropertyFix(property)); } highlightTokens(property, keyNode, holder, new PropertiesHighlighter()); ASTNode valueNode = ((PropertyImpl)property).getValueNode(); if (valueNode != null) { highlightTokens(property, valueNode, holder, new PropertiesValueHighlighter()); } }
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException { final PsiFile file = element.getContainingFile(); if (!FileModificationService.getInstance().prepareFileForWrite(file)) return; final Property property = PsiTreeUtil.getParentOfType(element, Property.class); LOG.assertTrue(property != null); final int start = property.getTextRange().getStartOffset(); @NonNls final Document doc = PsiDocumentManager.getInstance(project).getDocument(file); LOG.assertTrue(doc != null); final int line = doc.getLineNumber(start); final int lineStart = doc.getLineStartOffset(line); doc.insertString(lineStart, "# suppress inspection \"" + shortName + "\"\n"); }
private void ensurePropertiesLoaded() { if (myPropertiesMap != null) return; final ASTNode[] props = getPropertiesList().getChildren(PropertiesElementTypes.PROPERTIES); MostlySingularMultiMap<String, IProperty> propertiesMap = new MostlySingularMultiMap<String, IProperty>(); List<IProperty> properties = new ArrayList<IProperty>(props.length); for (final ASTNode prop : props) { final Property property = (Property)prop.getPsi(); String key = property.getUnescapedKey(); propertiesMap.add(key, property); properties.add(property); } synchronized (lock) { if (myPropertiesMap != null) return; myProperties = properties; myPropertiesMap = propertiesMap; } }
@Override @NotNull public PsiElement addPropertyAfter(@NotNull final Property property, @Nullable final Property anchor) throws IncorrectOperationException { final TreeElement copy = ChangeUtil.copyToElement(property); List<IProperty> properties = getProperties(); ASTNode anchorBefore = anchor == null ? properties.isEmpty() ? null : properties.get(0).getPsiElement().getNode() : anchor.getNode().getTreeNext(); if (anchorBefore != null) { if (anchorBefore.getElementType() == TokenType.WHITE_SPACE) { anchorBefore = anchorBefore.getTreeNext(); } } if (anchorBefore == null && haveToAddNewLine()) { insertLineBreakBefore(null); } getPropertiesList().addChild(copy, anchorBefore); if (anchorBefore != null) { insertLineBreakBefore(anchorBefore); } return copy.getPsi(); }
public void testGradleWrapper() { VirtualFile vFile = myFixture.copyFileToProject("projects/projectWithAppandLib/gradle/wrapper/gradle-wrapper.properties", "gradle/wrapper/gradle-wrapper.properties"); PsiFile file = PsiManager.getInstance(getProject()).findFile(vFile); assertNotNull(file); PropertiesFile propertiesFile = (PropertiesFile)file; GradleImplicitPropertyUsageProvider provider = new GradleImplicitPropertyUsageProvider(); for (IProperty property : propertiesFile.getProperties()) { // All properties are considered used in this file String name = property.getName(); assertTrue(name, provider.isUsed((Property)property)); } }
private void doPropertyUsageTest(final String propertyFileName) { PropertiesFile propFile = (PropertiesFile) myPsiManager.findFile(myTestProjectRoot.findChild(propertyFileName)); assertNotNull(propFile); final Property prop = (Property)propFile.findPropertyByKey("key"); assertNotNull(prop); final Query<PsiReference> query = ReferencesSearch.search(prop); final Collection<PsiReference> result = query.findAll(); assertEquals(1, result.size()); verifyReference(result, 0, "form.form", 960); }
public void testDeletePropertyWhitespaceAround() throws Exception { PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "xxx=yyy\nxxx2=tyrt\nxxx3=ttt\n\n"); final Property property = (Property)propertiesFile.findPropertyByKey("xxx2"); WriteCommandAction.runWriteCommandAction(null, new Runnable() { public void run() { property.delete(); } }); assertEquals("xxx=yyy\nxxx3=ttt\n\n", propertiesFile.getContainingFile().getText()); }
public void testDeletePropertyWhitespaceAhead() throws Exception { PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "xxx=yyy\nxxx2=tyrt\nxxx3=ttt\n\n"); final Property property = (Property)propertiesFile.findPropertyByKey("xxx"); WriteCommandAction.runWriteCommandAction(null, new Runnable(){public void run() { property.delete(); } }); assertEquals("xxx2=tyrt\nxxx3=ttt\n\n", propertiesFile.getText()); }
public void testAddPropertyAfter() throws IncorrectOperationException { final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f"); final Property c = (Property)propertiesFile.findPropertyByKey("c"); WriteCommandAction.runWriteCommandAction(null, new Runnable(){public void run() { propertiesFile.addPropertyAfter(myPropertyToAdd, c); } }); assertEquals("a=b\nc=d\nkkk=vvv\ne=f", propertiesFile.getText()); }
public void testAddPropertyAfterLast() throws IncorrectOperationException { final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f"); final Property p = (Property)propertiesFile.findPropertyByKey("e"); WriteCommandAction.runWriteCommandAction(null, new Runnable(){public void run() { propertiesFile.addPropertyAfter(myPropertyToAdd, p); } }); assertEquals("a=b\nc=d\ne=f\nkkk=vvv", propertiesFile.getText()); }
@NotNull public Collection<StructureViewTreeElement> getChildrenBase() { List<? extends IProperty> properties = getElement().getProperties(); Collection<StructureViewTreeElement> elements = new ArrayList<StructureViewTreeElement>(properties.size()); for (IProperty property : properties) { elements.add(new PropertiesStructureViewElement((Property)property)); } return elements; }
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); Property property = PsiTreeUtil.getParentOfType(element, Property.class, false); if (property == null) return; try { new RemovePropertyFix(property).invoke(project, null, property.getPropertiesFile().getContainingFile()); } catch (IncorrectOperationException e) { LOG.error(e); } }
@Override public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException { if (element instanceof Property) { throw new IncorrectOperationException("Use addProperty() instead"); } return super.add(element); }
@Override protected boolean isUsed(Property property) { if (PsiUtil.isIdeaProject(property.getProject())) { final PsiFile file = property.getContainingFile(); if (file != null && file.getName().equals("registry.properties")) { final String name = property.getName(); return name.endsWith(".description") || name.endsWith(".restartRequired"); } } return false; }
@Override protected boolean isUsed(Property property) { if (DGMUtil.isInDGMFile(property)) { String name = property.getName(); return ArrayUtil.find(DGMUtil.KEYS, name) >= 0; } return false; }
@Nullable private static String getTitleValue(@Nullable PsiExpression arg) { if (arg instanceof PsiLiteralExpression) { Object value = ((PsiLiteralExpression)arg).getValue(); if (value instanceof String) { return (String) value; } } if (arg instanceof PsiMethodCallExpression) { PsiMethod psiMethod = ((PsiMethodCallExpression)arg).resolveMethod(); PsiExpression returnValue = PropertyUtil.getGetterReturnExpression(psiMethod); if (arg == returnValue) { return null; } if (returnValue != null) { return getTitleValue(returnValue); } Property propertyArgument = getPropertyArgument((PsiMethodCallExpression)arg); if (propertyArgument != null) { return propertyArgument.getUnescapedValue(); } } if (arg instanceof PsiReferenceExpression) { PsiElement result = ((PsiReferenceExpression)arg).resolve(); if (result instanceof PsiVariable && ((PsiVariable)result).hasModifierProperty(PsiModifier.FINAL)) { return getTitleValue(((PsiVariable) result).getInitializer()); } } return null; }
public void testDeletePropertyWhitespaceAround() throws Exception { PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "xxx=yyy\nxxx2=tyrt\nxxx3=ttt\n\n"); final Property property = (Property)propertiesFile.findPropertyByKey("xxx2"); ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { property.delete(); } }); assertEquals("xxx=yyy\nxxx3=ttt\n\n", propertiesFile.getContainingFile().getText()); }
public void testDeletePropertyWhitespaceAhead() throws Exception { PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "xxx=yyy\nxxx2=tyrt\nxxx3=ttt\n\n"); final Property property = (Property)propertiesFile.findPropertyByKey("xxx"); ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { property.delete(); } }); assertEquals("xxx2=tyrt\nxxx3=ttt\n\n", propertiesFile.getText()); }
public void testAddPropertyAfter() throws IncorrectOperationException { final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f"); final Property c = (Property)propertiesFile.findPropertyByKey("c"); ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { propertiesFile.addPropertyAfter(myPropertyToAdd, c); } }); assertEquals("a=b\nc=d\nkkk=vvv\ne=f", propertiesFile.getText()); }
public void testAddPropertyAfterLast() throws IncorrectOperationException { final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f"); final Property p = (Property)propertiesFile.findPropertyByKey("e"); ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { propertiesFile.addPropertyAfter(myPropertyToAdd, p); } }); assertEquals("a=b\nc=d\ne=f\nkkk=vvv", propertiesFile.getText()); }
@Nullable private static String getTitleValue(PsiExpression arg) { if (arg instanceof PsiLiteralExpression) { Object value = ((PsiLiteralExpression)arg).getValue(); if (value instanceof String) { return (String) value; } } if (arg instanceof PsiMethodCallExpression) { PsiMethod psiMethod = ((PsiMethodCallExpression)arg).resolveMethod(); PsiExpression returnValue = PropertyUtil.getGetterReturnExpression(psiMethod); if (returnValue != null) { return getTitleValue(returnValue); } Property propertyArgument = getPropertyArgument((PsiMethodCallExpression)arg); if (propertyArgument != null) { return propertyArgument.getUnescapedValue(); } } if (arg instanceof PsiReferenceExpression) { PsiElement result = ((PsiReferenceExpression)arg).resolve(); if (result instanceof PsiVariable && ((PsiVariable)result).hasModifierProperty(PsiModifier.FINAL)) { return getTitleValue(((PsiVariable) result).getInitializer()); } } return null; }
/** * get properties in file * * @return properties */ public List<Property> getPropertiesInFile() { GenericAttributeValue<PsiFile> resource = getProperties().getResource(); if (resource != null) { PsiFile psiFile = resource.getValue(); if (psiFile instanceof PropertiesFile) { PropertiesFile propertiesFile = (PropertiesFile) psiFile; return propertiesFile.getProperties(); } } return Collections.emptyList(); }