IDEA-19061 Integrate the Rearranger-plugin into core-IDEA

This commit is contained in:
Denis.Zhdanov
2012-07-17 21:16:45 +04:00
parent 7934a35d76
commit 0c412d0d8c
10 changed files with 435 additions and 0 deletions
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2012 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.rearranger;
import org.jetbrains.annotations.NotNull;
/**
* @author Denis Zhdanov
* @since 7/17/12 8:57 PM
*/
public interface ModifierAwareRearrangerEntry extends RearrangerEntry {
@NotNull
RearrangerModifier getModifier();
}
@@ -0,0 +1,54 @@
package com.intellij.psi.codeStyle.rearranger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
/**
* Represents a processing unit during 'rearrangement' operation. I.e. entry's position can be changed during the processing.
* <p/>
* Example: we can provide a grouping rule for class methods (e.g. getters and setters). Every such method will be represented by
* a {@link RearrangerEntry} then.
* <p/>
* The entries can be organised into hierarchies where every sub-hierarchy is independent
* (see {@link #getParent()}, {@link #getChildren()}).
* <p/>
* Implementations of this interface are not obliged to be thread-safe.
*
* @author Denis Zhdanov
* @since 7/16/12 3:31 PM
*/
public interface RearrangerEntry {
/**
* @return parent entry, e.g. it would be a class entry for a method entry
* @see #getChildren()
*/
@Nullable
RearrangerEntry getParent();
/**
* Entries can be organised into hierarchies, that means that only siblings position can be changed during the rearrangement.
* <p/>
* Example: there are two classes at file and every class has two methods. Rearranger should not swap positions of methods
* from the different classes here.
*
* @return current entry's children. Empty collection if there are no children
* @see #getParent()
*/
@NotNull
Collection<? extends RearrangerEntry> getChildren();
/**
* @return start offset of the current entry (inclusive) within the target document. Rearranger engine uses this information
* to move rearranged entries
*/
int getStartOffset();
/**
* @return end offset of the current entry (exclusive) within the target document. Rearranger engine uses this information
* to move rearranged entries
*/
int getEndOffset();
}
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2012 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.rearranger;
import com.intellij.psi.codeStyle.rearranger.RearrangerEntry;
/**
* Enumerates all possible {@link RearrangerEntry entry} types.
* <p/>
* This enum is expected to be populated every time new entry type is introduced.
*
* @author Denis Zhdanov
* @since 7/17/12 11:14 AM
*/
public enum RearrangerEntryType {
CLASS, FIELD, METHOD
}
@@ -0,0 +1,42 @@
package com.intellij.psi.codeStyle.rearranger;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
/**
* Encapsulates language-specific rearrangement logic.
* <p/>
* Implementations of this interface are expected to be thread-safe.
*
* @author Denis Zhdanov
* @since 7/16/12 3:23 PM
* @param <E> entry type
* @param <R> rule type
*/
public interface RearrangerFacade<E extends RearrangerEntry> {
ExtensionPointName<RearrangerFacade> EP_NAME = ExtensionPointName.create("com.intellij.rearranger.facade");
/**
* Allows to build rearranger-interested data for the given element.
*
* @param root root element which children should be parsed for the rearrangement
* @param range target offsets range to use for filtering given root's children
* @return given root's children that are subject for further rearrangement
*/
@NotNull
Collection<E> parse(@NotNull PsiElement root, @NotNull TextRange range);
/**
* Asks to sort given entries matched against the given rule. Given list defines a default order to be used.
*
* @param entries entries matched against the given rule
* @param rule target rule
*/
void sort(@NotNull List<E> entries, @NotNull RearrangerRule rule);
}
@@ -0,0 +1,29 @@
/*
* Copyright 2000-2012 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.rearranger;
/**
* @author Denis Zhdanov
* @since 7/17/12 8:54 PM
*/
public enum RearrangerModifier {
//region Visibility
PUBLIC, PROTECTED, PRIVATE, PACKAGE_PRIVATE,
//endregion
STATIC, FINAL
}
@@ -0,0 +1,41 @@
/*
* Copyright 2000-2012 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.rearranger;
import org.jetbrains.annotations.NotNull;
/**
* Identifies a strategy which can be used for grouping {@link RearrangerEntry entries}.
* <p/>
* Example: we can define a rule like 'private final non-static fields' or 'public static methods' etc.
* <p/>
* Implementations of this interface are expected to be thread-safe.
*
* @author Denis Zhdanov
* @since 7/17/12 11:07 AM
*/
public interface RearrangerRule {
/**
* Allows to check if given entry is matched by the current rule.
* <p/>
* Example: entry like 'public final field' should be matched by a 'final fields' rule but not matched by a 'private fields' rule.
*
* @param entry entry to check
* @return <code>true</code> if given entry is matched by the current rule; <code>false</code> otherwise
*/
boolean isMatched(@NotNull RearrangerEntry entry);
}
@@ -0,0 +1,32 @@
/*
* Copyright 2000-2012 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.rearranger;
import org.jetbrains.annotations.NotNull;
/**
* Stands for the {@link RearrangerEntry} which provides information about its type.
* <p/>
* Implementations of this interface are not obliged to be thread-safe.
*
* @author Denis Zhdanov
* @since 7/17/12 11:17 AM
*/
public interface TypeAwareRearrangerEntry extends RearrangerEntry {
@NotNull
RearrangerEntryType getType();
}
@@ -0,0 +1,64 @@
/*
* Copyright 2000-2012 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.rearranger.rule;
import com.intellij.psi.codeStyle.rearranger.RearrangerEntry;
import com.intellij.psi.codeStyle.rearranger.RearrangerRule;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Denis Zhdanov
* @since 7/17/12 11:26 AM
*/
public class RearrangerCompositeRule implements RearrangerRule {
@NotNull private final List<RearrangerRule> myRules = new ArrayList<RearrangerRule>();
@NotNull private final Operator myOperator;
public RearrangerCompositeRule(@NotNull Operator operator, @NotNull RearrangerRule ... rules) {
myOperator = operator;
myRules.addAll(Arrays.asList(rules));
}
@Override
public boolean isMatched(@NotNull RearrangerEntry entry) {
for (RearrangerRule rule : myRules) {
boolean matched = rule.isMatched(entry);
if (matched && myOperator == Operator.OR) {
return true;
}
else if (!matched && myOperator == Operator.AND) {
return false;
}
}
return myOperator == Operator.AND;
}
@NotNull
public Operator getOperator() {
return myOperator;
}
public void addRule(@NotNull RearrangerRule rule) {
myRules.add(rule);
}
public enum Operator {AND, OR}
}
@@ -0,0 +1,61 @@
/*
* Copyright 2000-2012 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.rearranger.rule;
import com.intellij.psi.codeStyle.rearranger.RearrangerRule;
import org.jetbrains.annotations.NotNull;
/**
* @author Denis Zhdanov
* @since 7/17/12 11:24 AM
*/
public class RearrangerRuleUtil {
private RearrangerRuleUtil() {
}
@NotNull
public static RearrangerRule or(@NotNull RearrangerRule ... rules) {
return combine(RearrangerCompositeRule.Operator.OR, rules);
}
@NotNull
public static RearrangerRule and(@NotNull RearrangerRule ... rules) {
return combine(RearrangerCompositeRule.Operator.AND, rules);
}
@NotNull
private static RearrangerRule combine(@NotNull RearrangerCompositeRule.Operator operator, @NotNull RearrangerRule... rules) {
RearrangerCompositeRule composite = null;
for (RearrangerRule rule : rules) {
if (rule instanceof RearrangerCompositeRule && ((RearrangerCompositeRule)(rule)).getOperator() == operator) {
composite = (RearrangerCompositeRule)rule;
break;
}
}
if (composite == null) {
return new RearrangerCompositeRule(operator, rules);
}
for (RearrangerRule rule : rules) {
if (rule != composite) {
composite.addRule(rule);
}
}
return composite;
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2000-2012 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.rearranger.rule;
import com.intellij.psi.codeStyle.rearranger.RearrangerEntry;
import com.intellij.psi.codeStyle.rearranger.RearrangerEntryType;
import com.intellij.psi.codeStyle.rearranger.RearrangerRule;
import com.intellij.psi.codeStyle.rearranger.TypeAwareRearrangerEntry;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;
/**
* Filters {@link RearrangerEntry entries} by {@link TypeAwareRearrangerEntry#getType() their types}.
* <p/>
* <b>Note:</b> type-unaware entry will not be matched by the current rule.
* <p/>
* Thread-safe.
*
* @author Denis Zhdanov
* @since 7/17/12 11:19 AM
*/
public class RearrangerTypeRule implements RearrangerRule {
private final Set<RearrangerEntryType> myTypes = EnumSet.noneOf(RearrangerEntryType.class);
public RearrangerTypeRule(@NotNull RearrangerEntryType ... interestedTypes) {
myTypes.addAll(Arrays.asList(interestedTypes));
}
@Override
public boolean isMatched(@NotNull RearrangerEntry entry) {
if (entry instanceof TypeAwareRearrangerEntry) {
return myTypes.contains(((TypeAwareRearrangerEntry)(entry)).getType());
}
return false;
}
}