diff --git a/CONTRIBUTORS b/CONTRIBUTORS index a53127f7..79d0e23d 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1,3 +1,4 @@ Arnost Valicek - Issue #47 - Structure view should display visibility modifier Slawomir Wachowski - Issue #51 - Highlighting unused imports Michal Jedynak - Issue #22 - Duplicate declarations code insight +Arnost Valicek - Issue #72 - Structure view should be able to filter-out non-public functions and variables diff --git a/src/main/java/org/intellij/xquery/structure/VisibilityFilter.java b/src/main/java/org/intellij/xquery/structure/VisibilityFilter.java new file mode 100644 index 00000000..552111db --- /dev/null +++ b/src/main/java/org/intellij/xquery/structure/VisibilityFilter.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013 Grzegorz Ligas and other contributors (see the CONTRIBUTORS file). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.intellij.xquery.structure; + +import com.intellij.ide.util.treeView.smartTree.ActionPresentation; +import com.intellij.ide.util.treeView.smartTree.ActionPresentationData; +import com.intellij.ide.util.treeView.smartTree.Filter; +import com.intellij.ide.util.treeView.smartTree.TreeElement; +import com.intellij.util.PlatformIcons; +import org.jetbrains.annotations.NotNull; + +public class VisibilityFilter implements Filter { + + @NotNull + @Override + public ActionPresentation getPresentation() { + return new ActionPresentationData("Show non-public", null, PlatformIcons.PRIVATE_ICON); + } + + @NotNull + @Override + public String getName() { + return "SHOW_NON_PUBLIC_FILTER"; + } + + @Override + public boolean isVisible(TreeElement element) { + if (element instanceof XQueryStructureViewElement) { + return ((XQueryStructureViewElement) element).isPublic(); + } else { + return true; + } + } + + @Override + public boolean isReverted() { + return true; + } + +} diff --git a/src/main/java/org/intellij/xquery/structure/XQueryStructureViewElement.java b/src/main/java/org/intellij/xquery/structure/XQueryStructureViewElement.java index c08b4215..eab12b18 100644 --- a/src/main/java/org/intellij/xquery/structure/XQueryStructureViewElement.java +++ b/src/main/java/org/intellij/xquery/structure/XQueryStructureViewElement.java @@ -25,6 +25,7 @@ import org.intellij.xquery.psi.XQueryFile; import org.intellij.xquery.psi.XQueryFunctionDecl; import org.intellij.xquery.psi.XQueryVarDecl; +import org.intellij.xquery.psi.impl.XQueryPsiImplUtil; import java.util.ArrayList; import java.util.List; @@ -94,4 +95,12 @@ public TreeElement[] getChildren() { return EMPTY_ARRAY; } } + + public boolean isPublic() { + if (element instanceof XQueryVarDecl) + return XQueryPsiImplUtil.variableIsPublic((XQueryVarDecl) element); + if (element instanceof XQueryFunctionDecl) + return XQueryPsiImplUtil.functionIsPublic((XQueryFunctionDecl) element); + return true; + } } diff --git a/src/main/java/org/intellij/xquery/structure/XQueryStructureViewModel.java b/src/main/java/org/intellij/xquery/structure/XQueryStructureViewModel.java index 6ec5cff6..69346c17 100644 --- a/src/main/java/org/intellij/xquery/structure/XQueryStructureViewModel.java +++ b/src/main/java/org/intellij/xquery/structure/XQueryStructureViewModel.java @@ -19,6 +19,7 @@ import com.intellij.ide.structureView.StructureViewModel; import com.intellij.ide.structureView.StructureViewModelBase; import com.intellij.ide.structureView.StructureViewTreeElement; +import com.intellij.ide.util.treeView.smartTree.Filter; import com.intellij.ide.util.treeView.smartTree.Sorter; import com.intellij.psi.PsiFile; import org.intellij.xquery.psi.XQueryFile; @@ -40,6 +41,12 @@ public Sorter[] getSorters() { return new Sorter[]{Sorter.ALPHA_SORTER}; } + @org.jetbrains.annotations.NotNull + @Override + public Filter[] getFilters() { + return new Filter[]{new VisibilityFilter()}; + } + @Override public boolean isAlwaysShowsPlus(StructureViewTreeElement element) { return false; diff --git a/src/test/java/org/intellij/xquery/structure/VisibilityFilterTest.java b/src/test/java/org/intellij/xquery/structure/VisibilityFilterTest.java new file mode 100644 index 00000000..b3408621 --- /dev/null +++ b/src/test/java/org/intellij/xquery/structure/VisibilityFilterTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013 Grzegorz Ligas and other contributors (see the CONTRIBUTORS file). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.intellij.xquery.structure; + +import com.intellij.ide.util.treeView.smartTree.Filter; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; + +public class VisibilityFilterTest { + Filter filter = new VisibilityFilter(); + + @Test + public void nonPublicElemensShouldBeDisplaydWhenButtonIsPressed() { + assertThat(filter.isReverted(), is(true)); + } + + @Test + public void privateStructureViewElemensIsNotVisible() { + XQueryStructureViewElement element = mock(XQueryStructureViewElement.class); + given(element.isPublic()).willReturn(false); + + final boolean visible = filter.isVisible(element); + assertThat(visible, is(false)); + } + + @Test + public void publicStructureViewElemensIsVisible() { + XQueryStructureViewElement element = mock(XQueryStructureViewElement.class); + given(element.isPublic()).willReturn(true); + + final boolean visible = filter.isVisible(element); + assertThat(visible, is(true)); + } + + +}