From f62183c0b1eb8a9360422fc5eafdaa26915f120e Mon Sep 17 00:00:00 2001 From: "Svetlana.Zemlyanskay" Date: Fri, 25 Apr 2014 21:19:50 +0400 Subject: [PATCH] IDEA-93452 Implement "section" support in rearrange menu: cleanup --- .../ArrangementSectionDetector.java | 28 ++++----- .../RulePriorityAwareSettings.java | 27 --------- .../TextAwareArrangementEntry.java | 10 +++- .../std/StdRulePriorityAwareSettings.java | 60 ------------------- .../AddArrangementSectionRuleAction.java | 6 +- .../match/ArrangementSectionRuleManager.java | 20 +++++-- .../arrangement/engine/ArrangementEngine.java | 6 +- 7 files changed, 41 insertions(+), 116 deletions(-) delete mode 100644 platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/RulePriorityAwareSettings.java delete mode 100644 platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/std/StdRulePriorityAwareSettings.java diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/ArrangementSectionDetector.java b/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/ArrangementSectionDetector.java index 76679d68655f..ee66900785e4 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/ArrangementSectionDetector.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/ArrangementSectionDetector.java @@ -30,6 +30,10 @@ import static com.intellij.psi.codeStyle.arrangement.std.StdArrangementTokens.Se import static com.intellij.psi.codeStyle.arrangement.std.StdArrangementTokens.Section.START_SECTION; /** + * Class that is able to detect arrangement section start/end from comment element. + *

+ * The detection is based on arrangement settings. + * * @author Svetlana.Zemlyanskaya */ public class ArrangementSectionDetector { @@ -51,17 +55,16 @@ public class ArrangementSectionDetector { public void processComment(@NotNull PsiComment comment) { final TextRange range = comment.getTextRange(); final TextRange expandedRange = myDocument == null ? range : ArrangementUtil.expandToLineIfPossible(range, myDocument); - final int startOffset = expandedRange.getStartOffset(); - final int endOffset = expandedRange.getEndOffset(); + final TextRange sectionTextRange = new TextRange(expandedRange.getStartOffset(), expandedRange.getEndOffset()); final String commentText = comment.getText().trim(); final boolean start = isSectionStartComment(mySettings, commentText); if (start) { - mySectionEntryProducer.consume(new ArrangementSectionEntryTemplate(comment, START_SECTION, startOffset, endOffset, commentText)); + mySectionEntryProducer.consume(new ArrangementSectionEntryTemplate(comment, START_SECTION, sectionTextRange, commentText)); myCurrentSection = commentText; } else if (myCurrentSection != null && isSectionEndComment(mySettings, commentText)) { - mySectionEntryProducer.consume(new ArrangementSectionEntryTemplate(comment, END_SECTION, startOffset, endOffset, commentText)); + mySectionEntryProducer.consume(new ArrangementSectionEntryTemplate(comment, END_SECTION, sectionTextRange, commentText)); myCurrentSection = null; } } @@ -87,17 +90,16 @@ public class ArrangementSectionDetector { public static class ArrangementSectionEntryTemplate { private PsiElement myElement; private ArrangementSettingsToken myToken; - private int myStartOffset; - private int myEndOffset; + private TextRange myTextRange; private String myText; public ArrangementSectionEntryTemplate(@NotNull PsiElement element, @NotNull ArrangementSettingsToken token, - int startOffset, int endOffset, String text) { + @NotNull TextRange range, + @NotNull String text) { myElement = element; myToken = token; - myStartOffset = startOffset; - myEndOffset = endOffset; + myTextRange = range; myText = text; } @@ -109,12 +111,8 @@ public class ArrangementSectionDetector { return myToken; } - public int getStartOffset() { - return myStartOffset; - } - - public int getEndOffset() { - return myEndOffset; + public TextRange getTextRange() { + return myTextRange; } public String getText() { diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/RulePriorityAwareSettings.java b/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/RulePriorityAwareSettings.java deleted file mode 100644 index b7de50ece09f..000000000000 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/RulePriorityAwareSettings.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2000-2013 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. - */ -package com.intellij.psi.codeStyle.arrangement; - -/** - * Stands for the {@link ArrangementSettings} which also provide arrangement rules sorted in order of entries matching. - *

- * Example: 'public static' rule would have higher priority then 'public' - * - * @deprecated use {@link ArrangementSettings} instead - * @author Svetlana.Zemlyanskaya - */ -public interface RulePriorityAwareSettings extends ArrangementSettings { -} diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/TextAwareArrangementEntry.java b/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/TextAwareArrangementEntry.java index 35e24323b187..12851ca08cca 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/TextAwareArrangementEntry.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/TextAwareArrangementEntry.java @@ -18,9 +18,15 @@ package com.intellij.psi.codeStyle.arrangement; import org.jetbrains.annotations.Nullable; /** - * @author Svetlana.Zemlyanskaya + * Stands for the {@link ArrangementEntry} which provides information about its text. + * E.g. this entry can be used for section start/end element to match section comments by whole text. + *

+ * Implementations of this interface are not obliged to be thread-safe. + * + * @author Svetlana Zemlyanskaya + * @since 25/04/14 08:17 PM */ -public interface TextAwareArrangementEntry { +public interface TextAwareArrangementEntry extends ArrangementEntry { @Nullable String getText(); } diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/std/StdRulePriorityAwareSettings.java b/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/std/StdRulePriorityAwareSettings.java deleted file mode 100644 index 04021fd4995a..000000000000 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/arrangement/std/StdRulePriorityAwareSettings.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2000-2013 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. - */ -package com.intellij.psi.codeStyle.arrangement.std; - -import com.intellij.psi.codeStyle.arrangement.ArrangementSettings; -import com.intellij.psi.codeStyle.arrangement.ArrangementUtil; -import com.intellij.psi.codeStyle.arrangement.RulePriorityAwareSettings; -import com.intellij.psi.codeStyle.arrangement.group.ArrangementGroupingRule; -import com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule; -import com.intellij.psi.codeStyle.arrangement.match.StdArrangementMatchRule; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; - -/** - * @deprecated use {@link StdArrangementSettings} instead - * @author Svetlana.Zemlyanskaya - */ -public class StdRulePriorityAwareSettings extends StdArrangementSettings implements RulePriorityAwareSettings { - public StdRulePriorityAwareSettings(@NotNull List rules) { - super(wrapMatchRulesIntoSections(rules)); - } - - public StdRulePriorityAwareSettings(@NotNull List groupingRules, - @NotNull List matchRules) { - super(groupingRules, wrapMatchRulesIntoSections(matchRules)); - } - - public StdRulePriorityAwareSettings() { - super(); - } - - @NotNull - @Override - public ArrangementSettings clone() { - return new StdRulePriorityAwareSettings(cloneGroupings(), ArrangementUtil.collectMatchRules(cloneSectionRules())); - } - - private static List wrapMatchRulesIntoSections(@NotNull List matchRules) { - final List sectionRules = new ArrayList(); - for (StdArrangementMatchRule rule : matchRules) { - sectionRules.add(ArrangementSectionRule.create(rule)); - } - return sectionRules; - } -} diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/AddArrangementSectionRuleAction.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/AddArrangementSectionRuleAction.java index 3f64fe4fe3a9..e324ee29e848 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/AddArrangementSectionRuleAction.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/AddArrangementSectionRuleAction.java @@ -47,10 +47,8 @@ public class AddArrangementSectionRuleAction extends AddArrangementRuleAction { @Override protected Object createNewRule(@NotNull ArrangementMatchingRulesControl control) { final ArrangementSectionRuleManager manager = control.getSectionRuleManager(); - if (manager != null) { - return manager.createDefaultSectionRule(); - } - return super.createNewRule(control); + assert manager != null; + return manager.createDefaultSectionRule(); } @Override diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/match/ArrangementSectionRuleManager.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/match/ArrangementSectionRuleManager.java index 0e0b5c0f7092..ca9775af7c34 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/match/ArrangementSectionRuleManager.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/match/ArrangementSectionRuleManager.java @@ -19,6 +19,7 @@ import com.intellij.application.options.codeStyle.arrangement.color.ArrangementC import com.intellij.lang.Commenter; import com.intellij.lang.Language; import com.intellij.lang.LanguageCommenters; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.codeStyle.arrangement.ArrangementUtil; @@ -152,8 +153,8 @@ public class ArrangementSectionRuleManager { @NotNull private String processSectionText(@NotNull String text) { - final String line = myCommenter.getLineCommentPrefix(); - if (line != null && text.startsWith(line)) { + final String lineCommentPrefix = myCommenter.getLineCommentPrefix(); + if (lineCommentPrefix != null && text.startsWith(lineCommentPrefix)) { return text; } @@ -163,7 +164,8 @@ public class ArrangementSectionRuleManager { text.length() >= prefix.length() + suffix.length() && text.startsWith(prefix) && text.endsWith(suffix)) { return text; } - return line != null ? line + text : prefix != null && suffix != null ? prefix + text + suffix : ""; + return lineCommentPrefix != null ? wrapIntoLineComment(lineCommentPrefix, text) : + prefix != null && suffix != null ? wrapIntoBlockComment(prefix, suffix, text) : ""; } @NotNull @@ -171,18 +173,26 @@ public class ArrangementSectionRuleManager { if (myCommenter != null) { final String lineCommentPrefix = myCommenter.getLineCommentPrefix(); if (StringUtil.isNotEmpty(lineCommentPrefix)) { - return lineCommentPrefix; + return wrapIntoLineComment(lineCommentPrefix, ""); } final String prefix = myCommenter.getBlockCommentPrefix(); final String suffix = myCommenter.getBlockCommentSuffix(); if (StringUtil.isNotEmpty(prefix) && StringUtil.isNotEmpty(suffix)) { - return prefix + " " + suffix; + return wrapIntoBlockComment(prefix, suffix, " "); } } return ""; } + private static String wrapIntoBlockComment(@NotNull String prefix, @NotNull String suffix, @NotNull String text) { + return prefix + text + suffix; + } + + private static String wrapIntoLineComment(@NotNull String lineCommentPrefix, @NotNull String text) { + return lineCommentPrefix + text; + } + public static class ArrangementSectionRuleData { private boolean myIsSectionStart; private String myText; diff --git a/platform/lang-impl/src/com/intellij/psi/codeStyle/arrangement/engine/ArrangementEngine.java b/platform/lang-impl/src/com/intellij/psi/codeStyle/arrangement/engine/ArrangementEngine.java index cd12090906c9..2fe6734d15cc 100644 --- a/platform/lang-impl/src/com/intellij/psi/codeStyle/arrangement/engine/ArrangementEngine.java +++ b/platform/lang-impl/src/com/intellij/psi/codeStyle/arrangement/engine/ArrangementEngine.java @@ -394,7 +394,7 @@ public class ArrangementEngine { arranged.addAll(arrange(toArrange, context.sectionRules, context.rulesByPriority, entryToSection)); } - final NewSectionInfo newSectionsInfo = NewSectionInfo.getInstance(arranged, entryToSection); + final NewSectionInfo newSectionsInfo = NewSectionInfo.create(arranged, entryToSection); context.changer.prepare(wrappers, context); // We apply changes from the last position to the first position in order not to bother with offsets shifts. for (int i = arranged.size() - 1; i >= 0; i--) { @@ -423,8 +423,8 @@ public class ArrangementEngine { private final Map mySectionStarts = ContainerUtil.newHashMap(); private final Map mySectionEnds = ContainerUtil.newHashMap(); - private static NewSectionInfo getInstance(@NotNull List arranged, - @NotNull Map entryToSection) { + private static NewSectionInfo create(@NotNull List arranged, + @NotNull Map entryToSection) { final NewSectionInfo info = new NewSectionInfo(); boolean sectionIsOpen = false;