IDEA-93452 Implement "section" support in rearrange menu: cleanup

This commit is contained in:
Svetlana.Zemlyanskay
2014-04-25 21:20:22 +04:00
parent a4f2f0e73b
commit f62183c0b1
7 changed files with 41 additions and 116 deletions
@@ -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.
* <p/>
* 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() {
@@ -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.
* <p/>
* Example: 'public static' rule would have higher priority then 'public'
*
* @deprecated use {@link ArrangementSettings} instead
* @author Svetlana.Zemlyanskaya
*/
public interface RulePriorityAwareSettings extends ArrangementSettings {
}
@@ -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.
* <p/>
* 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();
}
@@ -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<StdArrangementMatchRule> rules) {
super(wrapMatchRulesIntoSections(rules));
}
public StdRulePriorityAwareSettings(@NotNull List<ArrangementGroupingRule> groupingRules,
@NotNull List<StdArrangementMatchRule> matchRules) {
super(groupingRules, wrapMatchRulesIntoSections(matchRules));
}
public StdRulePriorityAwareSettings() {
super();
}
@NotNull
@Override
public ArrangementSettings clone() {
return new StdRulePriorityAwareSettings(cloneGroupings(), ArrangementUtil.collectMatchRules(cloneSectionRules()));
}
private static List<ArrangementSectionRule> wrapMatchRulesIntoSections(@NotNull List<StdArrangementMatchRule> matchRules) {
final List<ArrangementSectionRule> sectionRules = new ArrayList<ArrangementSectionRule>();
for (StdArrangementMatchRule rule : matchRules) {
sectionRules.add(ArrangementSectionRule.create(rule));
}
return sectionRules;
}
}
@@ -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
@@ -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;
@@ -394,7 +394,7 @@ public class ArrangementEngine {
arranged.addAll(arrange(toArrange, context.sectionRules, context.rulesByPriority, entryToSection));
}
final NewSectionInfo<E> newSectionsInfo = NewSectionInfo.getInstance(arranged, entryToSection);
final NewSectionInfo<E> 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<E, String> mySectionStarts = ContainerUtil.newHashMap();
private final Map<E, String> mySectionEnds = ContainerUtil.newHashMap();
private static <E extends ArrangementEntry> NewSectionInfo getInstance(@NotNull List<E> arranged,
@NotNull Map<E, ArrangementSectionRule> entryToSection) {
private static <E extends ArrangementEntry> NewSectionInfo create(@NotNull List<E> arranged,
@NotNull Map<E, ArrangementSectionRule> entryToSection) {
final NewSectionInfo<E> info = new NewSectionInfo<E>();
boolean sectionIsOpen = false;