rework macro loading

allow to register macro from the plugin without
platform modification
This commit is contained in:
Sergey Ignatov
2017-09-06 17:08:41 +03:00
parent 465fac71be
commit fdf6dbcb8c
7 changed files with 59 additions and 105 deletions
@@ -0,0 +1,29 @@
/*
* 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.macro;
import com.intellij.application.options.PathMacrosImpl;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
public class IdePathMacros extends PathMacrosImpl {
@NotNull
@Override
public Set<String> getToolMacroNames() {
return MacroManager.getInstance().getMacroNames();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* 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.
@@ -16,8 +16,8 @@
package com.intellij.ide.macro;
import com.intellij.application.options.PathMacrosImpl;
import com.intellij.ide.DataManager;
import com.intellij.ide.macro.Macro.ExecutionCancelledException;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
@@ -30,17 +30,15 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ConvertingIterator;
import com.intellij.util.containers.Convertor;
import com.intellij.util.containers.HashMap;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
public final class MacroManager {
private final HashMap<String, Macro> myMacrosMap = new HashMap<>();
private final Map<String, Macro> myMacrosMap = new LinkedHashMap<>();
public static MacroManager getInstance() {
return ServiceManager.getService(MacroManager.class);
@@ -106,14 +104,17 @@ public final class MacroManager {
}
private void registerMacro(Macro macro) {
assert PathMacrosImpl.getToolMacroNames().contains(macro.getName()) : "Macro '" + macro.getName() + "' should be registered in PathMacros!";
myMacrosMap.put(macro.getName(), macro);
}
public Collection<Macro> getMacros() {
return myMacrosMap.values();
}
@NotNull
public Set<String> getMacroNames() {
return myMacrosMap.keySet();
}
public void cacheMacrosPreview(DataContext dataContext) {
dataContext = getCorrectContext(dataContext);
@@ -143,14 +144,12 @@ public final class MacroManager {
* Expands all macros that are found in the {@code str}.
*/
@Nullable
public String expandMacrosInString(String str, boolean firstQueueExpand, DataContext dataContext) throws Macro.ExecutionCancelledException {
public String expandMacrosInString(@Nullable String str, boolean firstQueueExpand, DataContext dataContext) throws ExecutionCancelledException {
return expandMacroSet(str, firstQueueExpand, dataContext, getMacros().iterator());
}
@Nullable
private String expandMacroSet(String str,
boolean firstQueueExpand, DataContext dataContext, Iterator<Macro> macros
) throws Macro.ExecutionCancelledException {
private static String expandMacroSet(@Nullable String str, boolean firstQueueExpand, DataContext dataContext, Iterator<Macro> macros) throws ExecutionCancelledException {
if (str == null) return null;
while (macros.hasNext()) {
Macro macro = macros.next();
@@ -198,16 +197,8 @@ public final class MacroManager {
return str;
}
public String expandSilentMarcos(String str, boolean firstQueueExpand, DataContext dataContext) throws Macro.ExecutionCancelledException {
final Convertor<Macro, Macro> convertor = macro -> {
if (macro instanceof PromptingMacro) {
return new Macro.Silent(macro, "");
}
return macro;
};
return expandMacroSet(
str, firstQueueExpand, dataContext, ConvertingIterator.create(getMacros().iterator(), convertor)
);
public String expandSilentMarcos(@Nullable String str, boolean firstQueueExpand, DataContext dataContext) throws ExecutionCancelledException {
Convertor<Macro, Macro> convertor = macro -> macro instanceof PromptingMacro ? new Macro.Silent(macro, "") : macro;
return expandMacroSet(str, firstQueueExpand, dataContext, ConvertingIterator.create(getMacros().iterator(), convertor));
}
}
@@ -200,6 +200,9 @@
<applicationService serviceInterface="com.intellij.psi.util.PsiEditorUtil"
serviceImplementation="com.intellij.psi.util.PsiUtilBase"/>
<applicationService serviceInterface="com.intellij.openapi.application.PathMacros"
serviceImplementation="com.intellij.ide.macro.IdePathMacros"/>
<rearranger.ui implementation="com.intellij.application.options.codeStyle.arrangement.component.DefaultArrangementUiComponentFactory"/>
<projectService serviceInterface="com.intellij.ui.EditorTextFieldProvider"
@@ -202,9 +202,6 @@
<applicationService serviceImplementation="com.intellij.internal.statistic.customUsageCollectors.ui.ShortcutsCollector"/>
<applicationService serviceImplementation="com.intellij.internal.statistic.customUsageCollectors.actions.ActionsCollector"/>
<applicationService serviceInterface="com.intellij.openapi.application.PathMacros"
serviceImplementation="com.intellij.application.options.PathMacrosImpl"/>
<applicationService serviceImplementation="com.intellij.openapi.util.DimensionService"/>
<applicationService serviceInterface="com.intellij.openapi.util.WindowStateService"
serviceImplementation="com.intellij.openapi.util.WindowStateApplicationService"/>
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* 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.
@@ -16,7 +16,6 @@
package com.intellij.application.options;
import com.intellij.openapi.application.PathMacroFilter;
import com.intellij.openapi.application.PathMacros;
import com.intellij.openapi.components.CompositePathMacroFilter;
import com.intellij.openapi.components.PathMacroMap;
import com.intellij.openapi.extensions.ExtensionPointName;
@@ -52,11 +51,11 @@ public class PathMacrosCollector extends PathMacroMap {
@NotNull
public static Set<String> getMacroNames(@NotNull final Element e) {
return getMacroNames(e, new CompositePathMacroFilter(Extensions.getExtensions(MACRO_FILTER_EXTENSION_POINT_NAME)),
PathMacros.getInstance());
PathMacrosImpl.getInstanceEx());
}
@NotNull
public static Set<String> getMacroNames(Element root, @Nullable PathMacroFilter filter, @NotNull PathMacros pathMacros) {
public static Set<String> getMacroNames(Element root, @Nullable PathMacroFilter filter, @NotNull PathMacrosImpl pathMacros) {
final PathMacrosCollector collector = new PathMacrosCollector();
collector.substitute(root, true, false, filter);
Set<String> preResult = collector.myMacroMap.keySet();
@@ -67,7 +66,7 @@ public class PathMacrosCollector extends PathMacroMap {
Set<String> result = new SmartHashSet<>(preResult);
result.removeAll(pathMacros.getSystemMacroNames());
result.removeAll(pathMacros.getLegacyMacroNames());
result.removeAll(PathMacrosImpl.getToolMacroNames());
result.removeAll(pathMacros.getToolMacroNames());
result.removeAll(pathMacros.getIgnoredMacroNames());
return result;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* 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.
@@ -30,10 +30,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.serialization.JpsGlobalLoader;
import org.jetbrains.jps.model.serialization.PathMacroUtil;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;
@State(
@@ -67,69 +64,6 @@ public class PathMacrosImpl extends PathMacros implements PersistentStateCompone
SYSTEM_MACROS.add(PathMacroUtil.USER_HOME_NAME);
}
@SuppressWarnings("SpellCheckingInspection")
private static final Set<String> ourToolsMacros = ContainerUtil.immutableSet(
"ClasspathEntry",
"Classpath",
"ColumnNumber",
"ContentRoot",
"FileClass",
"FileDir",
"FileParentDir",
"FileDirName",
"FileDirPathFromParent",
"FileDirRelativeToProjectRoot",
"/FileDirRelativeToProjectRoot",
"FileDirRelativeToSourcepath",
"/FileDirRelativeToSourcepath",
"FileExt",
"FileFQPackage",
"FileName",
"FileNameWithoutExtension",
"FileNameWithoutAllExtensions",
"FilePackage",
"FilePath",
"UnixSeparators",
"FilePathRelativeToProjectRoot",
"/FilePathRelativeToProjectRoot",
"FilePathRelativeToSourcepath",
"/FilePathRelativeToSourcepath",
"FilePrompt",
"FileRelativeDir",
"/FileRelativeDir",
"FileRelativePath",
"/FileRelativePath",
"FileEncoding",
"JavaDocPath",
"JDKPath",
"LineNumber",
"ModuleFileDir",
"ModuleFilePath",
"ModuleName",
"AffectedModuleNames",
"IsMake",
"ModuleSourcePath",
"ModuleSdkPath",
"OutputPath",
"PhpExecutable",
"ProjectFileDir",
"ProjectFilePath",
"ProjectName",
"Projectpath",
"Prompt",
"SourcepathEntry",
"Sourcepath",
"SHOW_CHANGES",
"ClipboardContent",
"SelectedText",
"SelectionStartLine",
"SelectionEndLine",
"SelectionStartColumn",
"SelectionEndColumn",
"PyInterpreterDirectory",
"ExecutableByFileExt"
);
public PathMacrosImpl() {
}
@@ -148,8 +82,9 @@ public class PathMacrosImpl extends PathMacros implements PersistentStateCompone
}
}
public static Set<String> getToolMacroNames() {
return ourToolsMacros;
@NotNull
public Set<String> getToolMacroNames() {
return Collections.emptySet();
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* 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.
@@ -16,7 +16,7 @@
package com.intellij.openapi.components.impl.stores;
import com.intellij.application.options.PathMacrosCollector;
import com.intellij.openapi.application.PathMacros;
import com.intellij.application.options.PathMacrosImpl;
import com.intellij.openapi.components.CompositePathMacroFilter;
import com.intellij.openapi.components.PathMacroSubstitutor;
import com.intellij.openapi.components.TrackingPathMacroSubstitutor;
@@ -80,7 +80,7 @@ public class FileStorageCoreUtil {
}
((TrackingPathMacroSubstitutor)pathMacroSubstitutor)
.addUnknownMacros(name, PathMacrosCollector.getMacroNames(element, filter, PathMacros.getInstance()));
.addUnknownMacros(name, PathMacrosCollector.getMacroNames(element, filter, PathMacrosImpl.getInstanceEx()));
}
// remove only after "getMacroNames" - some PathMacroFilter requires element name attribute