This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 181703
Collapse All | Expand All

(-)editor.codetemplates/src/org/netbeans/lib/editor/codetemplates/CodeTemplateInsertHandler.java (-1 / +37 lines)
Lines 44-52 Link Here
44
44
45
package org.netbeans.lib.editor.codetemplates;
45
package org.netbeans.lib.editor.codetemplates;
46
46
47
import static java.lang.Integer.MAX_VALUE;
47
import java.util.ArrayList;
48
import java.util.ArrayList;
48
import java.util.Collection;
49
import java.util.Collection;
49
import java.util.Collections;
50
import java.util.Collections;
51
import java.util.Comparator;
50
import java.util.List;
52
import java.util.List;
51
import java.util.logging.Level;
53
import java.util.logging.Level;
52
import java.util.logging.LogRecord;
54
import java.util.logging.LogRecord;
Lines 335-342 Link Here
335
            }
337
            }
336
            
338
            
337
            TextRegion<?> caretTextRegion = null;
339
            TextRegion<?> caretTextRegion = null;
340
            
341
            List<CodeTemplateParameter> prioritizedMasterParameters = prioritizeParameters(masterParameters);
342
            
338
            // Go through all master parameters and create region infos for them
343
            // Go through all master parameters and create region infos for them
339
            for (CodeTemplateParameter master : masterParameters) {
344
            for (CodeTemplateParameter master : prioritizedMasterParameters) {
340
                CodeTemplateParameterImpl masterImpl = CodeTemplateParameterImpl.get(master);
345
                CodeTemplateParameterImpl masterImpl = CodeTemplateParameterImpl.get(master);
341
                if (CodeTemplateParameter.CURSOR_PARAMETER_NAME.equals(master.getName())) {
346
                if (CodeTemplateParameter.CURSOR_PARAMETER_NAME.equals(master.getName())) {
342
                    // Add explicit ${cursor} as last into text sync group to jump to it by TAB as last param
347
                    // Add explicit ${cursor} as last into text sync group to jump to it by TAB as last param
Lines 587-593 Link Here
587
        }
592
        }
588
        return sb.toString();
593
        return sb.toString();
589
    }
594
    }
595
    /**
596
     * #181703 - Allow prioritizing parameters in a code-template.
597
     * Package private for testing
598
     */
599
    static List<CodeTemplateParameter> prioritizeParameters(List<CodeTemplateParameter> params) {
590
600
601
        List<CodeTemplateParameter> result = new ArrayList<>(params);
602
        Collections.sort(result, new Comparator<CodeTemplateParameter>() {
603
            @Override
604
            public int compare(CodeTemplateParameter p1, CodeTemplateParameter p2) {
605
                return getPrio(p1) - getPrio(p2);
606
            }
607
608
            private int getPrio(CodeTemplateParameter templateParam) throws NumberFormatException {
609
                if (null == templateParam) {
610
                    return MAX_VALUE;
611
                }
612
                String value = templateParam.getHints().get(CodeTemplateParameter.ORDERING_HINT_NAME);
613
                if (null != value) {
614
                    try {
615
                        return Integer.parseInt(value);
616
                    } catch (NumberFormatException e) {
617
                        // ignore
618
                        return MAX_VALUE;
619
                    }
620
                }
621
                return MAX_VALUE;
622
            }
623
        });
624
        return result;
625
    }
626
591
    private static final class TemplateInsertUndoEdit extends AbstractUndoableEdit {
627
    private static final class TemplateInsertUndoEdit extends AbstractUndoableEdit {
592
        
628
        
593
        private Document doc;
629
        private Document doc;
(-)editor.codetemplates/src/org/netbeans/lib/editor/codetemplates/spi/CodeTemplateParameter.java (-1 / +18 lines)
Lines 112-118 Link Here
112
     */
112
     */
113
    public static final String EDITABLE_HINT_NAME = "editable"; // NOI18N
113
    public static final String EDITABLE_HINT_NAME = "editable"; // NOI18N
114
    
114
    
115
    
115
    /**
116
     * The ordering attribute defines the sequence in which placeholders are
117
     * completed. Use 0 for the first element, 1 for the second... Placeholders
118
     * without ordering information will be placed after the last placeholder
119
     * with ordering information.
120
     *
121
     * <p>
122
     * Example of ordering of parameters :
123
     * <pre>
124
     * // paramC comes first, then paramB, then paramA
125
     * ${paramA} ${paramB ordering=2} ${paramC ordering=1}
126
     * </pre>
127
     * </p>
128
     * https://netbeans.org/bugzilla/show_bug.cgi?id=181703
129
     * @since 1.42.0
130
     */
131
    public static final String ORDERING_HINT_NAME = "ordering"; // NOI18N
132
116
    private final CodeTemplateParameterImpl impl;
133
    private final CodeTemplateParameterImpl impl;
117
    
134
    
118
    CodeTemplateParameter(CodeTemplateParameterImpl impl) {
135
    CodeTemplateParameter(CodeTemplateParameterImpl impl) {
(-)editor.codetemplates/test/unit/src/org/netbeans/lib/editor/codetemplates/CodeTemplateInsertHandlerTest.java (+67 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2016 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2016 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.lib.editor.codetemplates;
43
44
import java.util.Arrays;
45
import java.util.List;
46
import org.junit.Test;
47
import static org.junit.Assert.*;
48
import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateParameter;
49
50
/**
51
 *
52
 * @author markiewb
53
 */
54
public class CodeTemplateInsertHandlerTest {
55
56
    @Test
57
    public void testPrioritizeParameters() {
58
        CodeTemplateSpiPackageAccessor get = CodeTemplateSpiPackageAccessor.get();
59
        CodeTemplateParameter paramA = get.createParameter(new CodeTemplateParameterImpl(null, "${paramA}", 0));
60
        CodeTemplateParameter paramB = get.createParameter(new CodeTemplateParameterImpl(null, "${paramB ordering=\"2\"}", 0));
61
        CodeTemplateParameter paramC = get.createParameter(new CodeTemplateParameterImpl(null, "${paramC ordering=\"1\"}", 0));
62
63
        List<CodeTemplateParameter> prioritizeParameters = CodeTemplateInsertHandler.prioritizeParameters(Arrays.asList(paramA, paramB, paramC));
64
        assertEquals(Arrays.asList(paramC, paramB, paramA), prioritizeParameters);
65
    }
66
67
}
(-)groovy.editor/src/org/netbeans/modules/groovy/editor/resources/DefaultAbbrevs.xml (-1 / +1 lines)
Lines 132-138 Link Here
132
    </codetemplate>
132
    </codetemplate>
133
    <codetemplate abbreviation="fore" contexts="BLOCK,CASE">
133
    <codetemplate abbreviation="fore" contexts="BLOCK,CASE">
134
        <code>
134
        <code>
135
<![CDATA[for (${TYPE iterableElementType default="Object" editable=false} ${ELEM newVarName default="elem"} : ${ITER iterable default="col"}) {
135
<![CDATA[for (${TYPE iterableElementType default="Object" editable=false} ${ELEM newVarName default="elem" ordering="2"} : ${ITER iterable default="col" ordering="1"}) {
136
   ${selection line}${cursor}
136
   ${selection line}${cursor}
137
}
137
}
138
]]>
138
]]>
(-)java.editor/src/org/netbeans/modules/java/editor/resources/DefaultAbbrevs.xml (-1 / +1 lines)
Lines 176-182 Link Here
176
    </codetemplate>
176
    </codetemplate>
177
    <codetemplate abbreviation="fore" contexts="BLOCK,CASE,LABELED_STATEMENT,DO_WHILE_LOOP,ENHANCED_FOR_LOOP,FOR_LOOP,IF,WHILE_LOOP,LAMBDA_EXPRESSION">
177
    <codetemplate abbreviation="fore" contexts="BLOCK,CASE,LABELED_STATEMENT,DO_WHILE_LOOP,ENHANCED_FOR_LOOP,FOR_LOOP,IF,WHILE_LOOP,LAMBDA_EXPRESSION">
178
        <code>
178
        <code>
179
<![CDATA[for (${TYPE iterableElementType default="Object" editable=false} ${ELEM newVarName default="elem"} : ${ITER iterable default="col"}) {
179
<![CDATA[for (${TYPE iterableElementType default="Object" editable=false} ${ELEM newVarName default="elem" ordering="2"} : ${ITER iterable default="col" ordering="1"}) {
180
${selection}${cursor}
180
${selection}${cursor}
181
}
181
}
182
]]>
182
]]>

Return to bug 181703