1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-30 12:25:35 +02:00

Bug 572755: Sorting for new custom templates

The changes adds sorting for source templates in alphabetic order.
Makes possible to bring to the top the new custom templates provided by
extension points, as the most frequently used ones and to avoid the need
for using the drop down list for the right option.

Change-Id: I931bd2fc08f3e37178a64e4b7908db73af1fbc2c
Signed-off-by: Lidia Popescu <lidia.popescu@windriver.com>
This commit is contained in:
Lidia Popescu 2021-04-11 04:15:58 +03:00 committed by Jonah Graham
parent b6b66b5457
commit 68c67c5a1b
2 changed files with 88 additions and 1 deletions

View file

@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2021 Wind River Systems, Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lidia Popescu (Wind River Systems) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.corext.codemanipulation;
import java.util.Objects;
import org.eclipse.jface.text.templates.Template;
public class CdtTemplate implements Comparable<CdtTemplate> {
private String id;
private Template template;
private String key;
private String name;
/**
* @param id - should be the id from TemplatePersistenceData
* @param template
*/
public CdtTemplate(String id, Template template) {
this.id = id;
this.template = template;
if (id == null) {
this.key = ""; //$NON-NLS-1$
} else {
this.key = id;
}
if (template == null || template.getName() == null) {
this.name = ""; //$NON-NLS-1$
} else {
this.name = template.getName();
}
}
public String getID() {
return id;
}
public Template getTemplate() {
return template;
}
public String getKey() {
return key;
}
public String getName() {
return name;
}
@Override
public int compareTo(CdtTemplate cdtTmp) {
int value = Objects.compare(key, cdtTmp.key, String::compareTo);
if (value == 0) {
return Objects.compare(name, cdtTmp.name, String.CASE_INSENSITIVE_ORDER);
}
return value;
}
}

View file

@ -18,6 +18,7 @@ package org.eclipse.cdt.internal.corext.codemanipulation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -759,13 +760,27 @@ public class StubUtility {
}
templateDatas = projectStore.getTemplateData();
}
List<Template> result = new ArrayList<>();
List<CdtTemplate> cdtResult = new ArrayList<>();
for (int j = 0; j < contentTypes.length; j++) {
for (int i = 0; i < templateDatas.length; i++) {
Template template = templateDatas[i].getTemplate();
if (template == null)
continue;
final String contextTypeId = template.getContextTypeId();
if (FileTemplateContextType.isContextTypeForContentType(contextTypeId, contentTypes[j])) {
result.add(template);
cdtResult.add(new CdtTemplate(templateDatas[i].getId(), templateDatas[i].getTemplate()));
}
}
}
Collections.sort(cdtResult);
for (int j = 0; j < contentTypes.length; j++) {
for (CdtTemplate c : cdtResult) {
if (FileTemplateContextType.isContextTypeForContentType(c.getTemplate().getContextTypeId(),
contentTypes[j])) {
result.add(c.getTemplate());
}
}
}