From 50e4076e5c7a48eff46784ee585056eabbd74f76 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 29 Jul 2010 15:44:31 +0400 Subject: [PATCH] 'power save mode' option (turns off daemon code analyzer) --- .../daemon/PowerSaveHectorProvider.java | 61 +++++++++++++++++++ .../daemon/impl/DaemonCodeAnalyzerImpl.java | 2 + .../daemon/impl/DaemonListeners.java | 8 +++ .../daemon/impl/TrafficLightRenderer.java | 10 ++- .../src/com/intellij/ide/PowerSaveMode.java | 50 +++++++++++++++ .../ide/actions/TogglePowerSaveAction.java | 36 +++++++++++ .../wm/impl/status/TogglePopupHintsPanel.java | 29 +++++++-- .../src/messages/ActionsBundle.properties | 2 + .../src/META-INF/LangExtensions.xml | 5 ++ .../src/idea/LangActions.xml | 6 ++ .../highlighting/DomElementsErrorPanel.java | 2 +- 11 files changed, 202 insertions(+), 9 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/PowerSaveHectorProvider.java create mode 100644 platform/lang-impl/src/com/intellij/ide/PowerSaveMode.java create mode 100644 platform/lang-impl/src/com/intellij/ide/actions/TogglePowerSaveAction.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/PowerSaveHectorProvider.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/PowerSaveHectorProvider.java new file mode 100644 index 000000000000..47ba565f8fa3 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/PowerSaveHectorProvider.java @@ -0,0 +1,61 @@ +/* + * Copyright 2000-2010 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.codeInsight.daemon; + +import com.intellij.ide.PowerSaveMode; +import com.intellij.openapi.editor.HectorComponentPanel; +import com.intellij.openapi.editor.HectorComponentPanelsProvider; +import com.intellij.openapi.options.ConfigurationException; +import com.intellij.psi.PsiFile; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; + +/** + * @author yole + */ +public class PowerSaveHectorProvider implements HectorComponentPanelsProvider { + @Override + public HectorComponentPanel createConfigurable(@NotNull PsiFile file) { + return new HectorComponentPanel() { + private JCheckBox myCheckBox = new JCheckBox("Power Save Mode"); + + @Override + public JComponent createComponent() { + return myCheckBox; + } + + @Override + public boolean isModified() { + return myCheckBox.isSelected() != PowerSaveMode.isEnabled(); + } + + @Override + public void apply() throws ConfigurationException { + PowerSaveMode.setEnabled(myCheckBox.isSelected()); + } + + @Override + public void reset() { + myCheckBox.setSelected(PowerSaveMode.isEnabled()); + } + + @Override + public void disposeUIResources() { + } + }; + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java index 6c14fadc30d7..ee6d27286d48 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java @@ -27,6 +27,7 @@ import com.intellij.codeInsight.daemon.ReferenceImporter; import com.intellij.codeInsight.hint.HintManager; import com.intellij.codeInsight.intention.impl.IntentionHintComponent; import com.intellij.concurrency.Job; +import com.intellij.ide.PowerSaveMode; import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; @@ -586,6 +587,7 @@ public class DaemonCodeAnalyzerImpl extends DaemonCodeAnalyzer implements JDOMEx return new Runnable() { public void run() { if (!myUpdateByTimerEnabled) return; + if (PowerSaveMode.isEnabled()) return; if (myDisposed || myProject.isDisposed()) return; final Collection activeEditors = myDaemonListeners.getSelectedEditors(); if (activeEditors.isEmpty()) return; diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java index aa1bc32786e2..b3561b6bd82c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java @@ -19,6 +19,7 @@ package com.intellij.codeInsight.daemon.impl; import com.intellij.ProjectTopics; import com.intellij.codeHighlighting.Pass; import com.intellij.codeInsight.hint.TooltipController; +import com.intellij.ide.PowerSaveMode; import com.intellij.ide.todo.TodoConfiguration; import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.*; @@ -195,6 +196,13 @@ public class DaemonListeners implements Disposable { } }); + connection.subscribe(PowerSaveMode.TOPIC, new PowerSaveMode.Listener() { + @Override + public void powerSaveStateChanged() { + stopDaemon(true); + } + }); + CommandProcessor.getInstance().addCommandListener(new MyCommandListener(), this); ApplicationManager.getApplication().addApplicationListener(myApplicationListener); EditorColorsManager.getInstance().addEditorColorsListener(myEditorColorsListener); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java index aa603832d71e..29281a4f3042 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java @@ -20,6 +20,7 @@ import com.intellij.codeHighlighting.HighlightDisplayLevel; import com.intellij.codeHighlighting.TextEditorHighlightingPass; import com.intellij.codeInsight.daemon.DaemonBundle; import com.intellij.codeInsight.daemon.impl.analysis.HighlightLevelUtil; +import com.intellij.ide.PowerSaveMode; import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.markup.ErrorStripeRenderer; @@ -109,7 +110,7 @@ public class TrafficLightRenderer implements ErrorStripeRenderer { final SeverityRegistrar severityRegistrar = SeverityRegistrar.getInstance(myProject); status.errorCount = new int[severityRegistrar.getSeveritiesCount()]; status.rootsNumber = roots.length; - fillDaemonCodeAnalyzerErrorsSatus(status, fillErrorsCount); + fillDaemonCodeAnalyzerErrorsStatus(status, fillErrorsCount); List passes = myDaemonCodeAnalyzer.getPassesToShowProgressFor(myFile); ArrayList passStati = new ArrayList(); for (TextEditorHighlightingPass tepass : passes) { @@ -127,7 +128,7 @@ public class TrafficLightRenderer implements ErrorStripeRenderer { return status; } - protected void fillDaemonCodeAnalyzerErrorsSatus(DaemonCodeAnalyzerStatus status, boolean fillErrorsCount) { + protected void fillDaemonCodeAnalyzerErrorsStatus(DaemonCodeAnalyzerStatus status, boolean fillErrorsCount) { final SeverityRegistrar severityRegistrar = SeverityRegistrar.getInstance(myProject); List highlights = DaemonCodeAnalyzerImpl.getHighlights(myDocument, myProject); if (highlights == null) highlights = Arrays.asList(HighlightInfo.EMPTY_ARRAY); @@ -185,6 +186,11 @@ public class TrafficLightRenderer implements ErrorStripeRenderer { if (status == null) return null; @NonNls String text = HTML_HEADER; + if (PowerSaveMode.isEnabled()) { + text += "Code analysis is disabled in power save mode."; + text += HTML_FOOTER; + return text; + } if (status.noHighlightingRoots != null && status.noHighlightingRoots.length == status.rootsNumber) { text += DaemonBundle.message("analysis.hasnot.been.run"); text += HTML_FOOTER; diff --git a/platform/lang-impl/src/com/intellij/ide/PowerSaveMode.java b/platform/lang-impl/src/com/intellij/ide/PowerSaveMode.java new file mode 100644 index 000000000000..5b0d27dd3831 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ide/PowerSaveMode.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2010 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.ide; + +import com.intellij.openapi.components.ServiceManager; +import com.intellij.util.messages.MessageBus; +import com.intellij.util.messages.Topic; + +/** + * @author yole + */ +public class PowerSaveMode { + private boolean myEnabled = false; + private final MessageBus myBus; + + public PowerSaveMode(MessageBus bus) { + myBus = bus; + } + + public static boolean isEnabled() { + return ServiceManager.getService(PowerSaveMode.class).myEnabled; + } + + public static void setEnabled(boolean value) { + final PowerSaveMode instance = ServiceManager.getService(PowerSaveMode.class); + if (instance.myEnabled != value) { + instance.myEnabled = value; + instance.myBus.syncPublisher(TOPIC).powerSaveStateChanged(); + } + } + + public interface Listener { + void powerSaveStateChanged(); + } + + public static Topic TOPIC = Topic.create("PowerSaveMode.Listener", Listener.class); +} diff --git a/platform/lang-impl/src/com/intellij/ide/actions/TogglePowerSaveAction.java b/platform/lang-impl/src/com/intellij/ide/actions/TogglePowerSaveAction.java new file mode 100644 index 000000000000..3040982f253b --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ide/actions/TogglePowerSaveAction.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2010 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.ide.actions; + +import com.intellij.ide.PowerSaveMode; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.ToggleAction; +import com.intellij.openapi.project.DumbAware; + +/** + * @author yole + */ +public class TogglePowerSaveAction extends ToggleAction implements DumbAware { + @Override + public boolean isSelected(AnActionEvent e) { + return PowerSaveMode.isEnabled(); + } + + @Override + public void setSelected(AnActionEvent e, boolean state) { + PowerSaveMode.setEnabled(state); + } +} diff --git a/platform/lang-impl/src/com/intellij/openapi/wm/impl/status/TogglePopupHintsPanel.java b/platform/lang-impl/src/com/intellij/openapi/wm/impl/status/TogglePopupHintsPanel.java index fe8dfa478269..2449862dfd81 100644 --- a/platform/lang-impl/src/com/intellij/openapi/wm/impl/status/TogglePopupHintsPanel.java +++ b/platform/lang-impl/src/com/intellij/openapi/wm/impl/status/TogglePopupHintsPanel.java @@ -20,6 +20,7 @@ import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; import com.intellij.codeInsight.daemon.impl.HectorComponent; import com.intellij.codeInsight.daemon.impl.analysis.HighlightLevelUtil; import com.intellij.ide.DataManager; +import com.intellij.ide.PowerSaveMode; import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileEditor.FileEditorManager; @@ -38,6 +39,7 @@ import com.intellij.psi.PsiManager; import com.intellij.ui.UIBundle; import com.intellij.ui.awt.RelativePoint; import com.intellij.util.Consumer; +import com.intellij.util.messages.MessageBusConnection; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -57,7 +59,8 @@ public class TogglePopupHintsPanel implements StatusBarWidget, StatusBarWidget.I public TogglePopupHintsPanel(@NotNull final Project project) { myCurrentIcon = EMPTY_ICON; - project.getMessageBus().connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() { + final MessageBusConnection connection = project.getMessageBus().connect(); + connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() { @Override public void selectionChanged(FileEditorManagerEvent event) { updateStatus(); @@ -68,6 +71,12 @@ public class TogglePopupHintsPanel implements StatusBarWidget, StatusBarWidget.I updateStatus(); } }); + connection.subscribe(PowerSaveMode.TOPIC, new PowerSaveMode.Listener() { + @Override + public void powerSaveStateChanged() { + updateStatus(); + } + }); } public void dispose() { @@ -130,13 +139,21 @@ public class TogglePopupHintsPanel implements StatusBarWidget, StatusBarWidget.I private void updateStatus(PsiFile file) { if (isStateChangeable(file)) { - if (HighlightLevelUtil.shouldInspect(file)) { + if (PowerSaveMode.isEnabled()) { + myCurrentIcon = EMPTY_ICON; + myToolTipText = "Code analysis is disabled in power save mode. "; + } + else if (HighlightLevelUtil.shouldInspect(file)) { myCurrentIcon = INSPECTIONS_ICON; - myToolTipText = "Current inspection profile: " + InspectionProjectProfileManager.getInstance(file.getProject()).getInspectionProfile().getName() + ". "; - } else if (HighlightLevelUtil.shouldHighlight(file)) { + myToolTipText = "Current inspection profile: " + + InspectionProjectProfileManager.getInstance(file.getProject()).getInspectionProfile().getName() + + ". "; + } + else if (HighlightLevelUtil.shouldHighlight(file)) { myCurrentIcon = SYNTAX_ONLY_ICON; - myToolTipText = "Highlighting level is: Syntax. "; - } else { + myToolTipText = "Highlighting level is: Syntax. "; + } + else { myCurrentIcon = INSPECTIONS_OFF_ICON; myToolTipText = "Inspections are off. "; } diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index bb4a5d8623d7..67b7f3c15e8c 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -1171,3 +1171,5 @@ group.ToolsBasicGroup.description=Tools Basic Group group.ToolbarNewElement.text=Toolbar New Element Group action.NewElementToolbarAction.text=Create New File action.ShowRegistry.text=Registry +action.TogglePowerSave.text=Power Save Mode +action.TogglePowerSave.description=Power save mode disables background code analysis and other background operations diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 49b97ae80a36..3356ef190496 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -128,6 +128,9 @@ + + @@ -568,4 +571,6 @@ + + diff --git a/platform/platform-resources/src/idea/LangActions.xml b/platform/platform-resources/src/idea/LangActions.xml index 077cba2d4153..1e24656bd38f 100644 --- a/platform/platform-resources/src/idea/LangActions.xml +++ b/platform/platform-resources/src/idea/LangActions.xml @@ -70,6 +70,12 @@ + + + + + + diff --git a/xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementsErrorPanel.java b/xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementsErrorPanel.java index 294ef8b0231d..90ad7cf85686 100644 --- a/xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementsErrorPanel.java +++ b/xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementsErrorPanel.java @@ -162,7 +162,7 @@ public class DomElementsErrorPanel extends JPanel implements CommittablePanel, H } @Override - protected void fillDaemonCodeAnalyzerErrorsSatus(DaemonCodeAnalyzerStatus status, boolean fillErrorsCount) { + protected void fillDaemonCodeAnalyzerErrorsStatus(DaemonCodeAnalyzerStatus status, boolean fillErrorsCount) { for (int i = 0; i < status.errorCount.length; i++) { final HighlightSeverity minSeverity = SeverityRegistrar.getInstance(myProject).getSeverityByIndex(i); if (minSeverity == null) {