mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
revert IDEA-145973 Same-named template macros for different languages
This commit is contained in:
@@ -19,7 +19,7 @@ public class MacroParserTest extends LightIdeaTestCase {
|
||||
Expression e = MacroParser.parse(" variableOfType( \"java.util.Collection\" ) ");
|
||||
assertTrue(e instanceof MacroCallNode);
|
||||
MacroCallNode n = (MacroCallNode) e;
|
||||
assertTrue(n.getMacro(new TemplateContextType[0]) instanceof VariableOfTypeMacro);
|
||||
assertTrue(n.getMacro() instanceof VariableOfTypeMacro);
|
||||
Expression[] parameters = n.getParameters();
|
||||
assertEquals(1, parameters.length);
|
||||
assertTrue(parameters [0] instanceof ConstantNode);
|
||||
@@ -46,7 +46,7 @@ public class MacroParserTest extends LightIdeaTestCase {
|
||||
Expression e = MacroParser.parse("variableOfType(\"A\", \"B\")");
|
||||
assertTrue(e instanceof MacroCallNode);
|
||||
MacroCallNode n = (MacroCallNode) e;
|
||||
assertTrue(n.getMacro(new TemplateContextType[0]) instanceof VariableOfTypeMacro);
|
||||
assertTrue(n.getMacro() instanceof VariableOfTypeMacro);
|
||||
Expression[] parameters = n.getParameters();
|
||||
assertEquals(2, parameters.length);
|
||||
assertTrue(parameters [0] instanceof ConstantNode);
|
||||
|
||||
@@ -21,7 +21,6 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface ExpressionContext {
|
||||
@@ -37,11 +36,5 @@ public interface ExpressionContext {
|
||||
<T> T getProperty(Key<T> key);
|
||||
@Nullable
|
||||
PsiElement getPsiElementAtStartOffset();
|
||||
|
||||
/**
|
||||
* @return all template context types matching the template invocation place
|
||||
*/
|
||||
@NotNull
|
||||
TemplateContextType[] getCompatibleContexts();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,49 +21,39 @@ import com.intellij.codeInsight.template.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class MacroCallNode extends Expression {
|
||||
private final List<Macro> myMacros;
|
||||
private final Macro myMacro;
|
||||
private final ArrayList<Expression> myParameters = new ArrayList<>();
|
||||
|
||||
public MacroCallNode(@NotNull Macro macro) {
|
||||
this(Collections.singletonList(macro));
|
||||
}
|
||||
|
||||
public MacroCallNode(List<Macro> macros) {
|
||||
myMacros = macros;
|
||||
assert macros.size() > 0;
|
||||
myMacro = macro;
|
||||
}
|
||||
|
||||
public void addParameter(Expression node) {
|
||||
myParameters.add(node);
|
||||
}
|
||||
|
||||
public Macro getMacro(TemplateContextType[] context) {
|
||||
Predicate<Macro> isAcceptableInContext = macro -> Arrays.stream(context).anyMatch(macro::isAcceptableInContext);
|
||||
return myMacros.stream().filter(isAcceptableInContext).findFirst().orElse(myMacros.get(0));
|
||||
public Macro getMacro() {
|
||||
return myMacro;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result calculateResult(ExpressionContext context) {
|
||||
Expression[] parameters = myParameters.toArray(new Expression[myParameters.size()]);
|
||||
return getMacro(context.getCompatibleContexts()).calculateResult(parameters, context);
|
||||
return getMacro().calculateResult(parameters, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result calculateQuickResult(ExpressionContext context) {
|
||||
Expression[] parameters = myParameters.toArray(new Expression[myParameters.size()]);
|
||||
return getMacro(context.getCompatibleContexts()).calculateQuickResult(parameters, context);
|
||||
return getMacro().calculateQuickResult(parameters, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookupElement[] calculateLookupItems(ExpressionContext context) {
|
||||
Expression[] parameters = myParameters.toArray(new Expression[myParameters.size()]);
|
||||
return getMacro(context.getCompatibleContexts()).calculateLookupItems(parameters, context);
|
||||
return getMacro().calculateLookupItems(parameters, context);
|
||||
}
|
||||
|
||||
public Expression[] getParameters() {
|
||||
|
||||
@@ -84,7 +84,7 @@ public class MacroParser {
|
||||
}
|
||||
|
||||
advance(lexer);
|
||||
MacroCallNode macroCallNode = new MacroCallNode(macros);
|
||||
MacroCallNode macroCallNode = new MacroCallNode(macros.get(0));
|
||||
if (lexer.getTokenType() == null) {
|
||||
return macroCallNode;
|
||||
}
|
||||
|
||||
@@ -122,7 +122,6 @@ public class TemplateManagerImpl extends TemplateManager implements Disposable {
|
||||
|
||||
private TemplateState initTemplateState(@NotNull Editor editor) {
|
||||
clearTemplateState(editor);
|
||||
PsiDocumentManager.getInstance(myProject).commitDocument(editor.getDocument());
|
||||
TemplateState state = new TemplateState(myProject, editor);
|
||||
Disposer.register(this, state);
|
||||
editor.putUserData(TEMPLATE_STATE_KEY, state);
|
||||
|
||||
@@ -99,19 +99,11 @@ public class TemplateState implements Disposable {
|
||||
@Nullable private PairProcessor<String, String> myProcessor;
|
||||
private boolean mySelectionCalculated = false;
|
||||
private boolean myStarted;
|
||||
private final TemplateContextType[] myCompatibleContexts;
|
||||
|
||||
TemplateState(@NotNull Project project, @NotNull final Editor editor) {
|
||||
myProject = project;
|
||||
myEditor = editor;
|
||||
myDocument = myEditor.getDocument();
|
||||
|
||||
PsiFile file = getPsiFile();
|
||||
if (file != null) {
|
||||
myCompatibleContexts = TemplateManagerImpl.getApplicableContextTypes(file, editor.getCaretModel().getOffset()).toArray(new TemplateContextType[0]);
|
||||
} else {
|
||||
myCompatibleContexts = new TemplateContextType[0];
|
||||
}
|
||||
}
|
||||
|
||||
private void initListeners() {
|
||||
@@ -935,12 +927,6 @@ public class TemplateState implements Disposable {
|
||||
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
|
||||
return file == null ? null : file.findElementAt(offset);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public TemplateContextType[] getCompatibleContexts() {
|
||||
return myCompatibleContexts;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1085,7 +1071,7 @@ public class TemplateState implements Disposable {
|
||||
Expression e = myTemplate.getExpressionAt(j);
|
||||
@NonNls String marker = "a";
|
||||
if (e instanceof MacroCallNode) {
|
||||
marker = ((MacroCallNode)e).getMacro(myCompatibleContexts).getDefaultValue();
|
||||
marker = ((MacroCallNode)e).getMacro().getDefaultValue();
|
||||
}
|
||||
replaceString(marker, mySegments.getSegmentStart(i), mySegments.getSegmentEnd(i), i);
|
||||
indices.add(i);
|
||||
|
||||
Reference in New Issue
Block a user