From 67123829cd3e242cf299fc5c0ce2a17ed896f19c Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 12 May 2018 22:28:46 +0200 Subject: [PATCH] SSR: put existing templates in language folder --- .../plugin/ui/ExistingTemplatesComponent.java | 75 ++++++++----------- .../source/messages/SSRBundle.properties | 20 ++--- 2 files changed, 40 insertions(+), 55 deletions(-) diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/ExistingTemplatesComponent.java b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/ExistingTemplatesComponent.java index c593b9ef9eca..58d2d4b1d6af 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/ExistingTemplatesComponent.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/ExistingTemplatesComponent.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * 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. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.structuralsearch.plugin.ui; import com.intellij.CommonBundle; @@ -26,6 +12,7 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.structuralsearch.MatchVariableConstraint; import com.intellij.structuralsearch.SSRBundle; import com.intellij.structuralsearch.StructuralSearchUtil; @@ -45,10 +32,11 @@ import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; -import java.util.ArrayList; -import java.util.List; +import java.util.regex.Pattern; public class ExistingTemplatesComponent { + private static final Pattern SPLIT = Pattern.compile("(? nodesToExpand = new ArrayList<>(); - - final List predefined = StructuralSearchUtil.getPredefinedTemplates(); - for (final Configuration info : predefined) { - final DefaultMutableTreeNode node = new DefaultMutableTreeNode(info); - - if (lastCategory == null || !lastCategory.equals(info.getCategory())) { - if (info.getCategory().length() > 0) { - root.add(parent = new DefaultMutableTreeNode(info.getCategory())); - nodesToExpand.add(parent); - lastCategory = info.getCategory(); - } - else { - root.add(node); - continue; - } - } - - parent.add(node); - } - - userTemplatesNode = new DefaultMutableTreeNode(SSRBundle.message("user.defined.category")); - root.add(userTemplatesNode); - - for (final DefaultMutableTreeNode nodeToExpand : nodesToExpand) { - patternTree.expandPath(new TreePath(new Object[]{root, nodeToExpand})); + for (Configuration info : StructuralSearchUtil.getPredefinedTemplates()) { + getOrCreateCategoryNode(root, SPLIT.split(info.getCategory())).add(new DefaultMutableTreeNode(info, false)); } + root.add(userTemplatesNode = new DefaultMutableTreeNode(SSRBundle.message("user.defined.category"))); + TreeUtil.expandAll(patternTree); final TreeExpander treeExpander = new DefaultTreeExpander(patternTree); final CommonActionsManager actionManager = CommonActionsManager.getInstance(); panel = ToolbarDecorator.createDecorator(patternTree) @@ -179,6 +143,27 @@ public class ExistingTemplatesComponent { configureSelectTemplateAction(historyList); } + @NotNull + private static DefaultMutableTreeNode getOrCreateCategoryNode(@NotNull DefaultMutableTreeNode root, String[] path) { + DefaultMutableTreeNode result = root; + outer: + for (String step : path) { + step = StringUtil.replace(step, "//", "/"); + DefaultMutableTreeNode child = (result.getChildCount() == 0) ? null : (DefaultMutableTreeNode)result.getLastChild(); + while (child != null) { + if (step.equals(child.getUserObject())) { + result = child; + continue outer; + } + else child = child.getPreviousSibling(); + } + final DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(step); + result.add(newNode); + result = newNode; + } + return result; + } + public void selectConfiguration(String name) { final DefaultMutableTreeNode root = (DefaultMutableTreeNode)patternTreeModel.getRoot(); final int count = root.getChildCount(); diff --git a/platform/structuralsearch/source/messages/SSRBundle.properties b/platform/structuralsearch/source/messages/SSRBundle.properties index dba506ca236d..18cb08358573 100644 --- a/platform/structuralsearch/source/messages/SSRBundle.properties +++ b/platform/structuralsearch/source/messages/SSRBundle.properties @@ -62,16 +62,16 @@ do.replace.all.button=Replace &All replace.selected.button=&Replace Selected # predefined configuration categories -expressions.category=expressions -user.defined.category=user defined -xml_html.category=xml/html -generics.category=generics -misc.category=miscellaneous -metadata.category=comments, javadoc and metadata -class.category=class-based -operators.category=operators -j2ee.category=j2ee -interesting.category=interesting +expressions.category=Java/Expressions +user.defined.category=User Defined +xml_html.category=XML//HTML +generics.category=Java/Generics +misc.category=Java/Miscellaneous +metadata.category=Java/Comments, Javadoc and Metadata +class.category=Java/Class-based +operators.category=Java/Operators +j2ee.category=Java/J2EE +interesting.category=Java/Interesting used.templates=Used templates: # predefined configuration names