use of org.eclipse.jface.text.templates.TemplateBuffer in project xtext-eclipse by eclipse.
the class XbaseTemplateContext method evaluate.
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
IXtextDocument xDocument = xtextDocumentUtil.getXtextDocument(getDocument());
// Ensure clean state before evaluation starts
imports.clear();
TemplateBuffer resolvedContent = super.evaluate(template);
Position position = new Position(getCompletionOffset(), 0);
List<ReplaceRegion> rewrite = createImports(imports, xDocument);
if (rewrite.size() > 0 && !isReadOnly()) {
// Remember the completion offset before performing doc changes
// $NON-NLS-1$
final String category = "__template_position_import_section" + System.currentTimeMillis();
IPositionUpdater updater = new DefaultPositionUpdater(category);
xDocument.addPositionCategory(category);
xDocument.addPositionUpdater(updater);
xDocument.addPosition(position);
try {
replaceConverter.convertToTextEdit(rewrite).apply(xDocument);
// restore CompletionOffset
setCompletionOffset(position.getOffset());
} catch (BadLocationException e) {
throw new TemplateException(e);
} finally {
xDocument.removePosition(position);
xDocument.removePositionUpdater(updater);
try {
xDocument.removePositionCategory(category);
} catch (BadPositionCategoryException e) {
throw new TemplateException(e);
}
}
}
return resolvedContent;
}
use of org.eclipse.jface.text.templates.TemplateBuffer in project KaiZen-OpenAPI-Editor by RepreZen.
the class SwaggerTemplateContext method evaluateForDisplay.
/**
* @since 2.3
*/
public TemplateBuffer evaluateForDisplay(Template template) throws BadLocationException, TemplateException {
if (!canEvaluate(template))
return null;
TemplateTranslator translator = new TemplateTranslator();
TemplateBuffer buffer = translator.translate(template);
getContextType().resolve(buffer, this);
return buffer;
}
use of org.eclipse.jface.text.templates.TemplateBuffer in project titan.EclipsePlug-ins by eclipse.
the class TITANTemplateContext method evaluate.
@Override
public TemplateBuffer evaluate(final Template template) throws BadLocationException, TemplateException {
if (!canEvaluate(template)) {
return null;
}
TemplateTranslator translator = new TemplateTranslator();
TemplateBuffer buffer = translator.translate(template);
getContextType().resolve(buffer, this);
if (isReadOnly()) {
// if it is read only we should not modify it
return buffer;
}
// calculate base indentation prefix
IDocument document = getDocument();
String prefixString = "";
String delimeter = null;
try {
IRegion lineRegion = document.getLineInformationOfOffset(getCompletionOffset());
int firstCharLocation = FirstCharAction.firstVisibleCharLocation(document, lineRegion);
if (firstCharLocation != -1) {
prefixString = document.get(lineRegion.getOffset(), firstCharLocation - lineRegion.getOffset());
}
delimeter = document.getLineDelimiter(document.getLineOfOffset(getCompletionOffset()));
} catch (BadLocationException e) {
ErrorReporter.logExceptionStackTrace(e);
}
TemplateVariable[] variables = buffer.getVariables();
// apply the base indentation prefix to every line but the first
IDocument temporalDocument = new Document(buffer.getString());
MultiTextEdit edit = new MultiTextEdit(0, temporalDocument.getLength());
List<RangeMarker> positions = variablesToPositions(variables);
for (int i = temporalDocument.getNumberOfLines() - 1; i >= 0; i--) {
edit.addChild(new InsertEdit(temporalDocument.getLineOffset(i), prefixString));
}
edit.addChildren(positions.toArray(new TextEdit[positions.size()]));
// replace line delimeters with the ones at the insertion
String delimeterZero = temporalDocument.getLineDelimiter(0);
if (delimeter != null && delimeterZero != null && !delimeter.equals(delimeterZero)) {
FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(temporalDocument);
int startOffset = 0;
IRegion region = adapter.find(startOffset, delimeterZero, true, false, false, false);
while (region != null) {
edit.addChild(new ReplaceEdit(region.getOffset(), region.getLength(), delimeter));
startOffset = region.getOffset() + region.getLength();
region = adapter.find(startOffset, delimeterZero, true, false, false, false);
}
}
edit.apply(temporalDocument, TextEdit.UPDATE_REGIONS);
positionsToVariables(positions, variables);
buffer.setContent(temporalDocument.get(), variables);
return buffer;
}
use of org.eclipse.jface.text.templates.TemplateBuffer in project eclipse.jdt.ls by eclipse.
the class SnippetCompletionProposal method getSnippetContent.
private static String getSnippetContent(SnippetCompletionContext scc, CodeGenerationTemplate templateSetting, boolean snippetStringSupport) throws CoreException {
ICompilationUnit cu = scc.getCompilationUnit();
Template template = templateSetting.createTemplate();
if (template == null) {
return null;
}
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), scc.getRecommendedLineSeprator());
context.setVariable(PACKAGEHEADER, scc.getPackageHeader());
String typeName = JavaCore.removeJavaLikeExtension(cu.getElementName());
List<IType> types = Arrays.asList(cu.getAllTypes());
int postfix = 0;
while (!types.isEmpty() && types.stream().filter(isTypeExists(typeName)).findFirst().isPresent()) {
typeName = "Inner" + JavaCore.removeJavaLikeExtension(cu.getElementName()) + (postfix == 0 ? "" : "_" + postfix);
postfix++;
}
if (postfix > 0 && snippetStringSupport) {
context.setVariable(CodeTemplateContextType.TYPENAME, "${1:" + typeName + "}");
} else {
context.setVariable(CodeTemplateContextType.TYPENAME, typeName);
}
context.setVariable(CURSOR, snippetStringSupport ? "${0}" : "");
// TODO Consider making evaluateTemplate public in StubUtility
TemplateBuffer buffer;
try {
buffer = context.evaluate(template);
} catch (BadLocationException e) {
throw new CoreException(Status.CANCEL_STATUS);
} catch (TemplateException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
if (buffer == null) {
return null;
}
String str = buffer.getString();
if (Strings.containsOnlyWhitespaces(str)) {
return null;
}
return str;
}
use of org.eclipse.jface.text.templates.TemplateBuffer in project erlide_eclipse by erlang.
the class ErlTemplateProposal method getAdditionalProposalInfo.
@Override
public String getAdditionalProposalInfo() {
try {
final TemplateContext context = getContext();
context.setReadOnly(true);
TemplateBuffer templateBuffer;
try {
final Template template = getTemplate();
if (context instanceof ErlangTemplateContext) {
final ErlangTemplateContext etc = (ErlangTemplateContext) context;
templateBuffer = etc.evaluate(template, true);
} else {
templateBuffer = context.evaluate(template);
}
} catch (final TemplateException e) {
return null;
}
return templateBuffer.getString();
} catch (final BadLocationException e) {
return null;
}
}
Aggregations