From f80e0d5c13e735367a19fa19d5cef4a4948d34e6 Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Fri, 18 Aug 2017 21:12:56 +0200 Subject: [PATCH] smart select/unselect api --- .../openapi/ide/SmartSelectProvider.java | 38 +++++++++++++++ .../com/intellij/ide/actions/SmartSelect.java | 48 +++++++++++++++++++ .../intellij/ide/actions/SmartUnSelect.java | 32 +++++++++++++ .../src/META-INF/PlatformExtensionPoints.xml | 1 + .../src/idea/PlatformActions.xml | 2 + 5 files changed, 121 insertions(+) create mode 100644 platform/platform-api/src/com/intellij/openapi/ide/SmartSelectProvider.java create mode 100644 platform/platform-impl/src/com/intellij/ide/actions/SmartSelect.java create mode 100644 platform/platform-impl/src/com/intellij/ide/actions/SmartUnSelect.java diff --git a/platform/platform-api/src/com/intellij/openapi/ide/SmartSelectProvider.java b/platform/platform-api/src/com/intellij/openapi/ide/SmartSelectProvider.java new file mode 100644 index 000000000000..52c23f4f2e11 --- /dev/null +++ b/platform/platform-api/src/com/intellij/openapi/ide/SmartSelectProvider.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2017 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.openapi.ide; + +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.extensions.ExtensionPointName; +import org.jetbrains.annotations.Nullable; + +/** + * @author Konstantin Bulenkov + */ +public interface SmartSelectProvider { + ExtensionPointName EP = ExtensionPointName.create("smartSelectProvider"); + + void increaseSelection(T source); + + void decreaseSelection(T source); + + default boolean isEnabled(DataContext context) { + return getSource(context) != null; + } + + @Nullable + T getSource(DataContext context); +} diff --git a/platform/platform-impl/src/com/intellij/ide/actions/SmartSelect.java b/platform/platform-impl/src/com/intellij/ide/actions/SmartSelect.java new file mode 100644 index 000000000000..540ab9f43e4a --- /dev/null +++ b/platform/platform-impl/src/com/intellij/ide/actions/SmartSelect.java @@ -0,0 +1,48 @@ +/* + * Copyright 2000-2017 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.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.ide.SmartSelectProvider; +import com.intellij.openapi.project.DumbAwareAction; + +/** + * @author Konstantin Bulenkov + */ +public class SmartSelect extends DumbAwareAction { + @Override + public void actionPerformed(AnActionEvent e) { + SmartSelectProvider provider = getProvider(e.getDataContext()); + assert provider != null; + //noinspection unchecked + provider.increaseSelection(provider.getSource(e.getDataContext())); + } + + @Override + public void update(AnActionEvent e) { + e.getPresentation().setEnabled(getProvider(e.getDataContext()) != null); + } + + public SmartSelectProvider getProvider(DataContext context) { + for (SmartSelectProvider provider : SmartSelectProvider.EP.getExtensions()) { + if (provider.isEnabled(context)) { + return provider; + } + } + return null; + } +} diff --git a/platform/platform-impl/src/com/intellij/ide/actions/SmartUnSelect.java b/platform/platform-impl/src/com/intellij/ide/actions/SmartUnSelect.java new file mode 100644 index 000000000000..45dafd589051 --- /dev/null +++ b/platform/platform-impl/src/com/intellij/ide/actions/SmartUnSelect.java @@ -0,0 +1,32 @@ +/* + * Copyright 2000-2017 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.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.ide.SmartSelectProvider; + +/** + * @author Konstantin Bulenkov + */ +public class SmartUnSelect extends SmartSelect { + @Override + public void actionPerformed(AnActionEvent e) { + SmartSelectProvider provider = getProvider(e.getDataContext()); + assert provider != null; + //noinspection unchecked + provider.decreaseSelection(provider.getSource(e.getDataContext())); + } +} diff --git a/platform/platform-resources/src/META-INF/PlatformExtensionPoints.xml b/platform/platform-resources/src/META-INF/PlatformExtensionPoints.xml index 8e4b64654c5b..6ca6f8ce55db 100644 --- a/platform/platform-resources/src/META-INF/PlatformExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/PlatformExtensionPoints.xml @@ -290,6 +290,7 @@ + diff --git a/platform/platform-resources/src/idea/PlatformActions.xml b/platform/platform-resources/src/idea/PlatformActions.xml index 2cc152682607..549abfe8fb61 100644 --- a/platform/platform-resources/src/idea/PlatformActions.xml +++ b/platform/platform-resources/src/idea/PlatformActions.xml @@ -57,6 +57,8 @@ > + +