use of org.eclipse.jface.text.templates.Template in project che by eclipse.
the class StubUtility method getMethodBodyContent.
/*
* Don't use this method directly, use CodeGeneration.
*/
public static String getMethodBodyContent(boolean isConstructor, IJavaProject project, String destTypeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
String templateName = isConstructor ? CodeTemplateContextType.CONSTRUCTORSTUB_ID : CodeTemplateContextType.METHODSTUB_ID;
Template template = getCodeTemplate(templateName, project);
if (template == null) {
return bodyStatement;
}
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
context.setVariable(CodeTemplateContextType.BODY_STATEMENT, bodyStatement);
String str = evaluateTemplate(context, template, new String[] { CodeTemplateContextType.BODY_STATEMENT });
if (str == null && !Strings.containsOnlyWhitespaces(bodyStatement)) {
return bodyStatement;
}
return str;
}
use of org.eclipse.jface.text.templates.Template in project dbeaver by serge-rider.
the class SQLCompletionProcessor method makeTemplateProposals.
@NotNull
private ICompletionProposal[] makeTemplateProposals(ITextViewer viewer, SQLCompletionAnalyzer.CompletionRequest request) {
String wordPart = request.wordPart.toLowerCase();
final List<SQLTemplateCompletionProposal> templateProposals = new ArrayList<>();
// Templates
for (Template template : editor.getTemplatesPage().getTemplateStore().getTemplates()) {
if (template.getName().toLowerCase().startsWith(wordPart)) {
templateProposals.add(new SQLTemplateCompletionProposal(template, new SQLContext(SQLTemplatesRegistry.getInstance().getTemplateContextRegistry().getContextType(template.getContextTypeId()), viewer.getDocument(), new Position(request.wordDetector.getStartOffset(), request.wordDetector.getLength()), editor), new Region(request.documentOffset, 0), null));
}
}
Collections.sort(templateProposals, new Comparator<SQLTemplateCompletionProposal>() {
@Override
public int compare(SQLTemplateCompletionProposal o1, SQLTemplateCompletionProposal o2) {
return o1.getDisplayString().compareTo(o2.getDisplayString());
}
});
return templateProposals.toArray(new ICompletionProposal[templateProposals.size()]);
}
use of org.eclipse.jface.text.templates.Template in project che by eclipse.
the class StubUtility method getCompilationUnitContent.
public static String getCompilationUnitContent(ICompilationUnit cu, String packDecl, String fileComment, String typeComment, String typeContent, String lineDelimiter) throws CoreException {
Template template = getCodeTemplate(CodeTemplateContextType.NEWTYPE_ID, cu.getJavaProject());
if (template == null) {
return null;
}
IJavaProject project = cu.getJavaProject();
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
context.setCompilationUnitVariables(cu);
context.setVariable(CodeTemplateContextType.PACKAGE_DECLARATION, packDecl);
//$NON-NLS-1$
context.setVariable(CodeTemplateContextType.TYPE_COMMENT, typeComment != null ? typeComment : "");
//$NON-NLS-1$
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : "");
context.setVariable(CodeTemplateContextType.TYPE_DECLARATION, typeContent);
context.setVariable(CodeTemplateContextType.TYPENAME, JavaCore.removeJavaLikeExtension(cu.getElementName()));
String[] fullLine = { CodeTemplateContextType.PACKAGE_DECLARATION, CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT };
return evaluateTemplate(context, template, fullLine);
}
use of org.eclipse.jface.text.templates.Template in project dbeaver by dbeaver.
the class SQLCompletionProcessor method makeTemplateProposals.
@NotNull
private ICompletionProposal[] makeTemplateProposals(ITextViewer viewer, SQLCompletionAnalyzer.CompletionRequest request) {
String wordPart = request.wordPart.toLowerCase();
final List<SQLTemplateCompletionProposal> templateProposals = new ArrayList<>();
// Templates
for (Template template : editor.getTemplatesPage().getTemplateStore().getTemplates()) {
if (template.getName().toLowerCase().startsWith(wordPart)) {
templateProposals.add(new SQLTemplateCompletionProposal(template, new SQLContext(SQLTemplatesRegistry.getInstance().getTemplateContextRegistry().getContextType(template.getContextTypeId()), viewer.getDocument(), new Position(request.wordDetector.getStartOffset(), request.wordDetector.getLength()), editor), new Region(request.documentOffset, 0), null));
}
}
Collections.sort(templateProposals, new Comparator<SQLTemplateCompletionProposal>() {
@Override
public int compare(SQLTemplateCompletionProposal o1, SQLTemplateCompletionProposal o2) {
return o1.getDisplayString().compareTo(o2.getDisplayString());
}
});
return templateProposals.toArray(new ICompletionProposal[templateProposals.size()]);
}
use of org.eclipse.jface.text.templates.Template in project eclipse.platform.text by eclipse.
the class AbstractTemplatesPage method initializeDND.
/**
* Initializes drag and drop the template items
*/
private void initializeDND() {
DragSourceAdapter dragListener = new DragSourceAdapter() {
@Override
public void dragStart(DragSourceEvent event) {
if (getSelectedTemplates().length == 0) {
event.doit = false;
}
}
@Override
public void dragSetData(DragSourceEvent event) {
if (TemplatesTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = getSelectedTemplates();
}
}
};
fTreeViewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { TemplatesTransfer.getInstance() }, dragListener);
DropTargetAdapter dropListener = new DropTargetAdapter() {
Transfer textTransfer = TextTransfer.getInstance();
Transfer templateTransfer = TemplatesTransfer.getInstance();
@Override
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
@Override
public void dragOperationChanged(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
@Override
public void dragOver(DropTargetEvent event) {
event.feedback |= DND.FEEDBACK_SCROLL;
if (event.item == null) {
event.detail = DND.DROP_NONE;
return;
}
int index = 0;
boolean isTemplateTransfer = false;
while (index < event.dataTypes.length) {
if (textTransfer.isSupportedType(event.dataTypes[index])) {
break;
}
if (templateTransfer.isSupportedType(event.dataTypes[index])) {
isTemplateTransfer = true;
break;
}
index++;
}
if (index < event.dataTypes.length) {
event.currentDataType = event.dataTypes[index];
if (event.detail == DND.DROP_DEFAULT || !isTemplateTransfer)
event.detail = DND.DROP_COPY;
return;
}
}
@Override
public void drop(DropTargetEvent event) {
if (event.item == null)
return;
Object object = ((TreeItem) event.item).getData();
final String contextId;
if (object instanceof TemplateContextType)
contextId = ((TemplateContextType) object).getId();
else
contextId = ((TemplatePersistenceData) object).getTemplate().getContextTypeId();
if (textTransfer.isSupportedType(event.currentDataType)) {
// $NON-NLS-1$ //$NON-NLS-2$
String text = ((String) event.data).replaceAll("\\$", "\\$\\$");
final Template template = new Template(createTemplateName(), TemplatesMessages.TemplatesPage_paste_description, contextId, text, true);
getShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
addTemplate(template);
}
});
return;
}
if (templateTransfer.isSupportedType(event.currentDataType)) {
final TemplatePersistenceData[] templates = (TemplatePersistenceData[]) event.data;
final int dropType = event.detail;
getShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
if (dropType == DND.DROP_COPY)
copyTemplates(templates, contextId);
else
moveTemplates(templates, contextId);
}
});
}
}
};
Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(), TemplatesTransfer.getInstance() };
fTreeViewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers, dropListener);
}
Aggregations