From e9d05e1bce35a3605700a6c06134c170680d3311 Mon Sep 17 00:00:00 2001 From: "Denis.Zhdanov" Date: Thu, 30 Aug 2012 10:32:02 +0400 Subject: [PATCH] IDEA-19061 Integrate the Rearranger-plugin into core-IDE 'Rearrange' action is provided --- .../action/AddArrangementRuleAction.java | 3 +- .../action/RearrangeCodeAction.java | 87 +++++++++++++++++++ .../arrangement/engine/ArrangementEngine.java | 33 ++++--- .../src/messages/ActionsBundle.properties | 2 + .../src/idea/Keymap_Default.xml | 3 + .../src/idea/LangActions.xml | 1 + 6 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/RearrangeCodeAction.java diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/AddArrangementRuleAction.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/AddArrangementRuleAction.java index 93470446ef3f..6b62e3e2f372 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/AddArrangementRuleAction.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/AddArrangementRuleAction.java @@ -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")); diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/RearrangeCodeAction.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/RearrangeCodeAction.java new file mode 100644 index 000000000000..7c7d16ae7e43 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/arrangement/action/RearrangeCodeAction.java @@ -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 ranges = new ArrayList(); + 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); + } + } +} 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 9072ca1c8b8a..6d1ef77ff3b5 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 @@ -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 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> rulesRef = new Ref>(); + List arrangementRules = settings.getCommonSettings(file.getLanguage()).getArrangementRules(); + if (arrangementRules.isEmpty() && rearranger instanceof ArrangementStandardSettingsAware) { + List defaultRules = ((ArrangementStandardSettingsAware)rearranger).getDefaultRules(); + if (defaultRules != null) { + arrangementRules = defaultRules; + } + } + if (arrangementRules.isEmpty()) { + return; + } + else { + rulesRef.set(arrangementRules); + } + final Collection 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 arrangementRules, + @NotNull List arrangementRules, @NotNull Collection 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 rules, - @NotNull List entries, + private static void doArrange(@NotNull List rules, + @NotNull List entries, @NotNull Document document) { List arranged = new ArrayList(); diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index cbfc54bf7e28..a6e457b6e768 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -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 diff --git a/platform/platform-resources/src/idea/Keymap_Default.xml b/platform/platform-resources/src/idea/Keymap_Default.xml index b713cd608aa2..6c55c58c08b8 100644 --- a/platform/platform-resources/src/idea/Keymap_Default.xml +++ b/platform/platform-resources/src/idea/Keymap_Default.xml @@ -85,6 +85,9 @@ + + + diff --git a/platform/platform-resources/src/idea/LangActions.xml b/platform/platform-resources/src/idea/LangActions.xml index b868fed93bf5..f7b95ad65248 100644 --- a/platform/platform-resources/src/idea/LangActions.xml +++ b/platform/platform-resources/src/idea/LangActions.xml @@ -241,6 +241,7 @@ +