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

'Rearrange' action is provided
This commit is contained in:
Denis.Zhdanov
2012-08-30 10:32:02 +04:00
parent 6fb3feed8b
commit e9d05e1bce
6 changed files with 118 additions and 11 deletions
@@ -19,12 +19,13 @@ import com.intellij.application.options.codeStyle.arrangement.ArrangementConstan
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.project.DumbAware;
/**
* @author Denis Zhdanov
* @since 8/24/12 1:54 PM
*/
public class AddArrangementRuleAction extends AnAction {
public class AddArrangementRuleAction extends AnAction implements DumbAware {
public AddArrangementRuleAction() {
getTemplatePresentation().setText(ApplicationBundle.message("arrangement.action.rule.add.text"));
@@ -0,0 +1,87 @@
/*
* 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.application.options.codeStyle.arrangement.action;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine;
import java.util.ArrayList;
import java.util.List;
/**
* Arranges content at the target file(s).
*
* @author Denis Zhdanov
* @since 8/30/12 10:01 AM
*/
public class RearrangeCodeAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());
if (project == null) {
return;
}
final Editor editor = PlatformDataKeys.EDITOR.getData(e.getDataContext());
if (editor == null) {
return;
}
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
Document document = editor.getDocument();
documentManager.commitDocument(document);
PsiFile file = documentManager.getPsiFile(document);
if (file == null) {
return;
}
List<TextRange> ranges = new ArrayList<TextRange>();
SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
ranges.add(TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()));
}
else if (selectionModel.hasBlockSelection()) {
int[] starts = selectionModel.getBlockSelectionStarts();
int[] ends = selectionModel.getBlockSelectionEnds();
for (int i = 0; i < starts.length; i++) {
ranges.add(TextRange.create(starts[i], ends[i]));
}
}
else {
ranges.add(TextRange.create(0, document.getTextLength()));
}
ArrangementEngine engine = ServiceManager.getService(project, ArrangementEngine.class);
try {
engine.arrange(file, ranges);
}
finally {
documentManager.commitDocument(document);
}
}
}
@@ -18,6 +18,7 @@ package com.intellij.psi.codeStyle.arrangement.engine;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
@@ -26,6 +27,8 @@ import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.arrangement.ArrangementEntry;
import com.intellij.psi.codeStyle.arrangement.ArrangementRule;
import com.intellij.psi.codeStyle.arrangement.Rearranger;
import com.intellij.psi.codeStyle.arrangement.StdArrangementRule;
import com.intellij.psi.codeStyle.arrangement.settings.ArrangementStandardSettingsAware;
import com.intellij.util.containers.Stack;
import org.jetbrains.annotations.NotNull;
@@ -50,17 +53,27 @@ public class ArrangementEngine {
return;
}
CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings();
final List<ArrangementRule> arrangementRules = settings.getCommonSettings(file.getLanguage()).getArrangementRules();
if (arrangementRules.isEmpty()) {
return;
}
Rearranger<?> rearranger = Rearranger.EXTENSION.forLanguage(file.getLanguage());
if (rearranger == null) {
return;
}
CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings();
final Ref<List<? extends ArrangementRule>> rulesRef = new Ref<List<? extends ArrangementRule>>();
List<? extends ArrangementRule> arrangementRules = settings.getCommonSettings(file.getLanguage()).getArrangementRules();
if (arrangementRules.isEmpty() && rearranger instanceof ArrangementStandardSettingsAware) {
List<StdArrangementRule> defaultRules = ((ArrangementStandardSettingsAware)rearranger).getDefaultRules();
if (defaultRules != null) {
arrangementRules = defaultRules;
}
}
if (arrangementRules.isEmpty()) {
return;
}
else {
rulesRef.set(arrangementRules);
}
final Collection<? extends ArrangementEntry> entriesToProcess = rearranger.parse(file, document, ranges);
final DocumentEx documentEx;
if (document instanceof DocumentEx && !((DocumentEx)document).isInBulkUpdate()) {
@@ -77,7 +90,7 @@ public class ArrangementEngine {
documentEx.setInBulkUpdate(true);
}
try {
doArrange(document, arrangementRules, entriesToProcess);
doArrange(document, rulesRef.get(), entriesToProcess);
}
finally {
if (documentEx != null) {
@@ -89,7 +102,7 @@ public class ArrangementEngine {
}
private static void doArrange(@NotNull final Document document,
@NotNull List<ArrangementRule> arrangementRules,
@NotNull List<? extends ArrangementRule> arrangementRules,
@NotNull Collection<? extends ArrangementEntry> entriesToProcess)
{
// The general idea is to process entries bottom-up where every processed group belongs to the same parent. We may not bother
@@ -174,8 +187,8 @@ public class ArrangementEngine {
}
}
private static void doArrange(@NotNull List<ArrangementRule> rules,
@NotNull List<ArrangementEntry> entries,
private static void doArrange(@NotNull List<? extends ArrangementRule> rules,
@NotNull List<? extends ArrangementEntry> entries,
@NotNull Document document)
{
List<ArrangementEntry> arranged = new ArrayList<ArrangementEntry>();
@@ -478,6 +478,8 @@ action.AutoIndentLines.text=_Auto-Indent Lines
action.AutoIndentLines.description=Indent current line or selected block according to the code style settings
action.OptimizeImports.text=Optimi_ze Imports...
action.OptimizeImports.description=Remove unused imports and reorder/reorganize imports
action.RearrangeCode.text=Rearrange code
action.RearrangeCode.description=Rearrange code
action.MoveStatementDown.text=Move Statement Do_wn
action.MoveStatementDown.description=Move selected statements one line down
action.MoveStatementUp.text=Move Statement _Up
@@ -85,6 +85,9 @@
<action id="ReformatCode">
<keyboard-shortcut first-keystroke="control alt L"/>
</action>
<action id="RearrangeCode">
<keyboard-shortcut first-keystroke="control alt R"/>
</action>
<action id="Generate">
<keyboard-shortcut first-keystroke="alt INSERT"/>
</action>
@@ -241,6 +241,7 @@
<action id="ReformatCode" class="com.intellij.codeInsight.actions.ReformatCodeAction"/>
<action id="AutoIndentLines" class="com.intellij.codeInsight.generation.actions.AutoIndentLinesAction"/>
<action id="OptimizeImports" class="com.intellij.codeInsight.actions.OptimizeImportsAction"/>
<action id="RearrangeCode" class="com.intellij.application.options.codeStyle.arrangement.action.RearrangeCodeAction"/>
</group>
<separator/>