Search in sources :

Example 1 with Variable

use of com.intellij.codeInsight.template.impl.Variable in project intellij-community by JetBrains.

the class LiveTemplateBuilder method buildTemplate.

@NotNull
public TemplateImpl buildTemplate() {
    List<Variable> variables = getListWithLimit(myVariables);
    if (!findVarOccurence(TemplateImpl.END)) {
        if (myLastEndVarName == null) {
            for (Variable variable : variables) {
                if (isEndVariable(variable.getName())) {
                    myLastEndVarName = variable.getName();
                    break;
                }
            }
        }
        if (myLastEndVarName != null) {
            int endOffset = -1;
            if (myAddEndVariableAtTheEndOfTemplate) {
                endOffset = myText.length();
            } else {
                Iterator<VarOccurence> it = myVariableOccurrences.iterator();
                while (it.hasNext()) {
                    VarOccurence occurence = it.next();
                    if (occurence.myName.equals(myLastEndVarName)) {
                        endOffset = occurence.myOffset;
                        break;
                    }
                }
                if (endOffset >= 0) {
                    for (Iterator<Variable> it1 = variables.iterator(); it1.hasNext(); ) {
                        Variable variable = it1.next();
                        if (myLastEndVarName.equals(variable.getName()) && variable.isAlwaysStopAt()) {
                            it.remove();
                            it1.remove();
                        }
                    }
                }
            }
            if (endOffset >= 0) {
                myVariableOccurrences.add(new VarOccurence(TemplateImpl.END, endOffset));
            }
        }
    }
    TemplateImpl template = new TemplateImpl("", "");
    for (Variable variable : variables) {
        template.addVariable(variable.getName(), variable.getExpressionString(), variable.getDefaultValueString(), variable.isAlwaysStopAt());
    }
    List<VarOccurence> variableOccurrences = getListWithLimit(myVariableOccurrences);
    Collections.sort(variableOccurrences, (o1, o2) -> {
        if (o1.myOffset < o2.myOffset) {
            return -1;
        }
        if (o1.myOffset > o2.myOffset) {
            return 1;
        }
        return 0;
    });
    int last = 0;
    for (VarOccurence occurence : variableOccurrences) {
        template.addTextSegment(myText.substring(last, occurence.myOffset));
        template.addVariableSegment(occurence.myName);
        last = occurence.myOffset;
    }
    template.addTextSegment(myText.substring(last));
    template.setToReformat(myIsToReformat);
    return template;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) Variable(com.intellij.codeInsight.template.impl.Variable) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Variable

use of com.intellij.codeInsight.template.impl.Variable in project intellij-community by JetBrains.

the class SearchDialog method filterOutUnusedVariableConstraints.

private void filterOutUnusedVariableConstraints(Configuration configuration) {
    final List<Variable> variables = getVariablesFromListeners();
    final List<String> variableNames = new ArrayList<>();
    for (Variable variable : variables) {
        variableNames.add(variable.getName());
    }
    variableNames.add(Configuration.CONTEXT_VAR_NAME);
    configuration.getMatchOptions().retainVariableConstraints(variableNames);
}
Also used : Variable(com.intellij.codeInsight.template.impl.Variable)

Example 3 with Variable

use of com.intellij.codeInsight.template.impl.Variable in project intellij-community by JetBrains.

the class ReplaceDialog method getVariablesFromListeners.

@Override
protected List<Variable> getVariablesFromListeners() {
    List<Variable> vars = getVarsFrom(replaceCriteriaEdit);
    List<Variable> searchVars = super.getVariablesFromListeners();
    Map<String, Variable> varsMap = new LinkedHashMap<>(searchVars.size());
    for (Variable var : searchVars) varsMap.put(var.getName(), var);
    for (Variable var : vars) {
        if (!varsMap.containsKey(var.getName())) {
            String newVarName = var.getName() + ReplaceConfiguration.REPLACEMENT_VARIABLE_SUFFIX;
            varsMap.put(newVarName, new Variable(newVarName, null, null, false, false));
        }
    }
    return new ArrayList<>(varsMap.values());
}
Also used : Variable(com.intellij.codeInsight.template.impl.Variable) ArrayList(java.util.ArrayList) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Example 4 with Variable

use of com.intellij.codeInsight.template.impl.Variable in project intellij-community by JetBrains.

the class LiveTemplateBuilder method insertTemplate.

public int insertTemplate(int offset, TemplateImpl template, Map<String, String> predefinedVarValues) {
    myIsToReformat = myText.length() > 0 || template.isToReformat();
    removeEndVarAtOffset(offset);
    String text = template.getTemplateText();
    insertText(offset, text, false);
    Map<String, String> newVarNames = new HashMap<>();
    Set<String> oldVarNames = new HashSet<>();
    for (int i = 0; i < template.getVariableCount(); i++) {
        String varName = template.getVariableNameAt(i);
        oldVarNames.add(varName);
    }
    for (int i = 0; i < template.getVariableCount(); i++) {
        String varName = template.getVariableNameAt(i);
        if (!TemplateImpl.INTERNAL_VARS_SET.contains(varName)) {
            if (predefinedVarValues != null && predefinedVarValues.containsKey(varName)) {
                continue;
            }
            String newVarName;
            if (myVarNames.contains(varName)) {
                oldVarNames.remove(varName);
                newVarName = generateUniqueVarName(oldVarNames, isEndVariable(varName));
                newVarNames.put(varName, newVarName);
                if (varName.equals(myLastEndVarName)) {
                    myLastEndVarName = newVarName;
                }
            } else {
                newVarName = varName;
            }
            Variable var = new Variable(newVarName, template.getExpressionStringAt(i), template.getDefaultValueStringAt(i), template.isAlwaysStopAt(i));
            if (mySegmentLimit >= 0 && myVariables.size() >= mySegmentLimit) {
                if (mySegmentLimit > 0) {
                    LOGGER.warn("Template with more than " + mySegmentLimit + " segments had been build. Text: " + myText);
                }
                break;
            }
            myVariables.add(var);
            myVarNames.add(newVarName);
        }
    }
    int end = -1;
    for (int i = 0; i < template.getSegmentsCount(); i++) {
        String segmentName = template.getSegmentName(i);
        int localOffset = template.getSegmentOffset(i);
        if (TemplateImpl.END.equals(segmentName)) {
            end = offset + localOffset;
        } else {
            if (predefinedVarValues != null && predefinedVarValues.containsKey(segmentName)) {
                String value = predefinedVarValues.get(segmentName);
                insertText(offset + localOffset, value, false);
                offset += value.length();
                continue;
            }
            if (newVarNames.containsKey(segmentName)) {
                segmentName = newVarNames.get(segmentName);
            }
            myVariableOccurrences.add(new VarOccurence(segmentName, offset + localOffset));
        }
    }
    int endOffset = end >= 0 ? end : offset + text.length();
    if (endOffset > 0 && endOffset != offset + text.length() && endOffset < myText.length() && !hasVarAtOffset(endOffset)) {
        myLastEndVarName = generateUniqueVarName(myVarNames, true);
        myVariables.add(new Variable(myLastEndVarName, "", "", true));
        myVarNames.add(myLastEndVarName);
        myVariableOccurrences.add(new VarOccurence(myLastEndVarName, endOffset));
    }
    return endOffset;
}
Also used : Variable(com.intellij.codeInsight.template.impl.Variable) HashMap(com.intellij.util.containers.HashMap) HashSet(com.intellij.util.containers.HashSet)

Example 5 with Variable

use of com.intellij.codeInsight.template.impl.Variable in project intellij-community by JetBrains.

the class LiveTemplateBuilder method insertText.

public void insertText(int offset, String text, boolean disableEndVariable) {
    if (disableEndVariable) {
        String varName = null;
        for (VarOccurence occurence : myVariableOccurrences) {
            if (!isEndVariable(occurence.myName)) {
                continue;
            }
            if (occurence.myOffset == offset) {
                varName = occurence.myName;
                break;
            }
        }
        if (varName != null) {
            for (Variable variable : myVariables) {
                if (varName.equals(variable.getName())) {
                    variable.setAlwaysStopAt(false);
                    variable.setDefaultValueString("\"\"");
                    break;
                }
            }
        }
    }
    int delta = text.length();
    for (VarOccurence occurence : myVariableOccurrences) {
        if (occurence.myOffset > offset || !disableEndVariable && occurence.myOffset == offset) {
            occurence.myOffset += delta;
        }
    }
    myText.insert(offset, text);
    updateMarkers(offset, text);
}
Also used : Variable(com.intellij.codeInsight.template.impl.Variable)

Aggregations

Variable (com.intellij.codeInsight.template.impl.Variable)6 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)1 HashMap (com.intellij.util.containers.HashMap)1 HashSet (com.intellij.util.containers.HashSet)1 LinkedHashMap (com.intellij.util.containers.hash.LinkedHashMap)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1