Skip to content

Commit

Permalink
Search Filter supports Regex and remembers some settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillipus committed Jan 17, 2025
1 parent bff2fd8 commit 3d78119
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
Expand Down Expand Up @@ -41,15 +42,20 @@ public class SearchFilter extends ViewerFilter {
private Set<EClass> fConceptsFilter = new HashSet<>();
private Set<String> fPropertiesFilter = new HashSet<>();
private Set<IProfile> fSpecializationsFilter = new HashSet<>();
private boolean fFilterViews = false;

private boolean fShowAllFolders = false;

private boolean fFilterViews;
private boolean fShowAllFolders;

private boolean fMatchCase;
private boolean fUseRegex;
private Pattern fRegexPattern;

SearchFilter() {
}

void setSearchText(String text) {
fSearchText = text;
createRegexPattern();
}

void reset() {
Expand Down Expand Up @@ -136,7 +142,7 @@ private boolean shouldShowObjectWithName(Object element) {
// Normalise in case of multi-line text
name = StringUtils.normaliseNewLineCharacters(name);

return name.toLowerCase().contains(fSearchText.toLowerCase());
return matchesString(name);
}

return false;
Expand All @@ -145,7 +151,7 @@ private boolean shouldShowObjectWithName(Object element) {
private boolean shouldShowObjectWithDocumentation(Object element) {
if(element instanceof IDocumentable documentable) {
String text = StringUtils.safeString(documentable.getDocumentation());
return text.toLowerCase().contains(fSearchText.toLowerCase());
return matchesString(text);
}

return false;
Expand All @@ -170,13 +176,45 @@ private boolean shouldShowProperty(Object element) {
if(element instanceof IProperties properties) {
for(IProperty property : properties.getProperties()) {
if(fPropertiesFilter.contains(property.getKey())) {
return hasSearchText() ? property.getValue().toLowerCase().contains(fSearchText.toLowerCase()) : true;
return hasSearchText() ? matchesString(property.getValue()) : true;
}
}
}

return false;
}

private boolean matchesString(String str) {
if(str == null || str.length() == 0) {
return false;
}

if(isUseRegex()) {
return matchesRegexString(str);
}

if(isMatchCase()) {
return str.contains(fSearchText);
}

return str.toLowerCase().contains(fSearchText.toLowerCase());
}

private boolean matchesRegexString(String searchString) {
return fRegexPattern != null ? fRegexPattern.matcher(searchString).find() : false;
}

private void createRegexPattern() {
fRegexPattern = null;

if(isUseRegex()) {
try {
fRegexPattern = Pattern.compile(fSearchText, isMatchCase() ? 0 : Pattern.CASE_INSENSITIVE);
}
catch(Exception ex) {
}
}
}

public boolean isFiltering() {
return isFilteringName() || isFilteringDocumentation() || isFilteringConcepts() || isFilteringPropertyKeys() || isFilteringSpecializations() || isFilteringViews();
Expand Down Expand Up @@ -206,7 +244,25 @@ private boolean hasSearchText() {
return fSearchText.length() > 0;
}

void setFilterOnName(boolean set) {
void setMatchCase(boolean set) {
fMatchCase = set;
createRegexPattern();
}

boolean isMatchCase() {
return fMatchCase;
}

void setUseRegex(boolean set) {
fUseRegex = set;
createRegexPattern();
}

boolean isUseRegex() {
return fUseRegex;
}

void setFilterOnName(boolean set) {
fFilterName = set;
}

Expand Down Expand Up @@ -269,5 +325,4 @@ void setFilterViews(boolean set) {
boolean isFilteringViews() {
return fFilterViews;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,28 @@ public void run() {
actionFolders.setChecked(fSearchFilter.isShowAllFolders());
dropDownAction.add(actionFolders);

// Match Case
IAction actionMatchCase = new Action("Match Case", IAction.AS_CHECK_BOX) {
@Override
public void run() {
fSearchFilter.setMatchCase(isChecked());
refreshTree();
}
};
actionMatchCase.setChecked(fSearchFilter.isMatchCase());
dropDownAction.add(actionMatchCase);

// Regex
IAction actionUseRegex = new Action("Match Regular Expression", IAction.AS_CHECK_BOX) {
@Override
public void run() {
fSearchFilter.setUseRegex(isChecked());
refreshTree();
}
};
actionUseRegex.setChecked(fSearchFilter.isUseRegex());
dropDownAction.add(actionUseRegex);

dropDownAction.add(new Separator());

// Reset
Expand Down

0 comments on commit 3d78119

Please sign in to comment.