Skip to content

Commit

Permalink
Adding visibility filter - structure view is able to filter non-publi…
Browse files Browse the repository at this point in the history
…c functions/variables

Closes ligasgr#72
  • Loading branch information
arnostv committed Nov 6, 2013
1 parent 8031c20 commit 0bd8d32
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
@@ -1,3 +1,4 @@
Arnost Valicek <arnost.valicek@gmail.com> - Issue #47 - Structure view should display visibility modifier
Slawomir Wachowski <slawwach@gmail.com> - Issue #51 - Highlighting unused imports
Michal Jedynak <m.jedynak@gmail.com> - Issue #22 - Duplicate declarations code insight
Arnost Valicek <arnost.valicek@gmail.com> - Issue #72 - Structure view should be able to filter-out non-public functions and variables
54 changes: 54 additions & 0 deletions src/main/java/org/intellij/xquery/structure/VisibilityFilter.java
@@ -0,0 +1,54 @@
/*
* Copyright 2013 Grzegorz Ligas <ligasgr@gmail.com> 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;
}

}
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
@@ -0,0 +1,54 @@
/*
* Copyright 2013 Grzegorz Ligas <ligasgr@gmail.com> 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));
}


}

0 comments on commit 0bd8d32

Please sign in to comment.