@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { // test that all variables are defined Iterator<TemplateVariableResolver> iterator= getContextType().resolvers(); while (iterator.hasNext()) { TemplateVariableResolver var= iterator.next(); if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) { Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$ } } if (!canEvaluate(template)) { return null; } String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter); TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(pattern); getContextType().resolve(buffer, this); return buffer; }
/** * Formats the template buffer. * * @param buffer * @param context * @throws BadLocationException */ public void format(TemplateBuffer buffer, TemplateContext context) throws BadLocationException { try { VariableTracker tracker = new VariableTracker(buffer); IDocument document = tracker.getDocument(); internalFormat(document, context); convertLineDelimiters(document); if (!(context instanceof JavaDocContext) && !isReplacedAreaEmpty(context)) trimStart(document); tracker.updateBuffer(); } catch (MalformedTreeException e) { throw new BadLocationException(); } }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { // test that all variables are defined Iterator<TemplateVariableResolver> iterator = getContextType().resolvers(); while (iterator.hasNext()) { TemplateVariableResolver var = iterator.next(); if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) { Assert.isNotNull( getVariable(var.getType()), "Variable " + var.getType() + "not defined"); // $NON-NLS-1$ //$NON-NLS-2$ } } if (!canEvaluate(template)) return null; String pattern = changeLineDelimiter(template.getPattern(), fLineDelimiter); TemplateTranslator translator = new TemplateTranslator(); TemplateBuffer buffer = translator.translate(pattern); getContextType().resolve(buffer, this); return buffer; }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { TemplateTranslator translator = new TemplateTranslator(); TemplateBuffer buffer = translator.translate(template); getContextType().resolve(buffer, this); // IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore(); boolean useCodeFormatter = true; // prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER); IJavaProject project = getJavaProject(); JavaFormatter formatter = new JavaFormatter( TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project); formatter.format(buffer, this); return buffer; }
@Override public String getAdditionalProposalInfo() { this.getContext().setReadOnly(true); TemplateBuffer templateBuffer; try { templateBuffer= this.getContext().evaluate(this.getTemplate()); } catch (BadLocationException | TemplateException ex) { String message = "<b>Error when resolving variables.</b><br>" + "Nothing will be inserted<br><br>" + escapeHTML(ex.getMessage()); return message; } String info = escapeHTML(this.getTemplate().getDescription()) + "<br><br><b>Inserts:</b><br>" + escapeHTML(templateBuffer.getString()); return info; }
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { if (!canEvaluate(template)) return null; try { this.template = template; TemplateTranslator translator = new SnippetTemplateTranslator(); TemplateBuffer buffer = translator.translate(template); getContextType().resolve(buffer, this); return buffer; } finally { this.template = null; } }
/** * Formats the template buffer. * @param buffer * @param context * @throws BadLocationException */ public void format(TemplateBuffer buffer, TemplateContext context) throws BadLocationException { try { VariableTracker tracker= new VariableTracker(buffer); IDocument document= tracker.getDocument(); internalFormat(document, context); convertLineDelimiters(document); if (!(context instanceof JavaDocContext) && !isReplacedAreaEmpty(context)) trimStart(document); tracker.updateBuffer(); } catch (MalformedTreeException e) { throw new BadLocationException(); } }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { // test that all variables are defined Iterator<TemplateVariableResolver> iterator= getContextType().resolvers(); while (iterator.hasNext()) { TemplateVariableResolver var= iterator.next(); if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) { Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$ } } if (!canEvaluate(template)) return null; String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter); TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(pattern); getContextType().resolve(buffer, this); return buffer; }
/** * Evaluates a 'java' template in the context of a compilation unit * * @param template the template to be evaluated * @param compilationUnit the compilation unit in which to evaluate the template * @param position the position inside the compilation unit for which to evaluate the template * @return the evaluated template * @throws CoreException in case the template is of an unknown context type * @throws BadLocationException in case the position is invalid in the compilation unit * @throws TemplateException in case the evaluation fails */ public static String evaluateTemplate(Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException { TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId()); if (!(contextType instanceof CompilationUnitContextType)) throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null)); IDocument document= new Document(); if (compilationUnit != null && compilationUnit.exists()) document.set(compilationUnit.getSource()); CompilationUnitContext context= ((CompilationUnitContextType) contextType).createContext(document, position, 0, compilationUnit); context.setForceEvaluation(true); TemplateBuffer buffer= context.evaluate(template); if (buffer == null) return null; return buffer.getString(); }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(template); getContextType().resolve(buffer, this); IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore(); boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER); IJavaProject project= getJavaProject(); JavaFormatter formatter= new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project); formatter.format(buffer, this); return buffer; }
private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException { IDocument doc= new Document(buffer.getString()); int nLines= doc.getNumberOfLines(); MultiTextEdit edit= new MultiTextEdit(); HashSet<Integer> removedLines= new HashSet<Integer>(); for (int i= 0; i < variables.length; i++) { TemplateVariable position= findVariable(buffer, variables[i]); // look if Javadoc tags have to be added if (position == null || position.getLength() > 0) { continue; } int[] offsets= position.getOffsets(); for (int k= 0; k < offsets.length; k++) { int line= doc.getLineOfOffset(offsets[k]); IRegion lineInfo= doc.getLineInformation(line); int offset= lineInfo.getOffset(); String str= doc.get(offset, lineInfo.getLength()); if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) { int nextStart= doc.getLineOffset(line + 1); edit.addChild(new DeleteEdit(offset, nextStart - offset)); } } } edit.apply(doc, 0); return doc.get(); }
/** * Returns template string to insert. * * @param context the context to use when rendering the template * @return String to insert or null if none is to be inserted */ public final String getTemplateString(final TemplateContext context) { String templateString = null; if (useTemplateButton.getSelection()) { Template template = getSelectedTemplate(); if (template != null) { try { TemplateBuffer buffer = context.evaluate(template); templateString = buffer.getString(); } catch (BadLocationException | TemplateException e) { final String msg = "Unable to create template for new component"; final IStatus status = new Status(IStatus.WARNING, ForceIdeEditorsPlugin.PLUGIN_ID, msg, e); logger().log(status); } } } return templateString; }
/** * Returns template string to insert. * * @return String to insert or null if none is to be inserted */ String getTemplateString() { String templateString = null; Template template = getSelectedTemplate(); if (template != null) { TemplateContextType contextType = GlassmakerUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CardContextType.CONTEXT_TYPE); IDocument document = new Document(); TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0); try { TemplateBuffer buffer = context.evaluate(template); templateString = buffer.getString(); } catch (Exception e) { GlassmakerUIPlugin.logError("Could not create template for new html", e); } } return templateString; }
/** * Returns template string to insert. * * @return String to insert or null if none is to be inserted */ public String getTemplateString() { String templateString = null; Template template = getSelectedTemplate(); if (template != null) { TemplateContextType contextType=SilverStripePDTPlugin.getDefault().getTemplateContextRegistry().getContextType(this._languageProvider.getTemplateContext()); IDocument document = new Document(); TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0); try { TemplateBuffer buffer = context.evaluate(template); templateString = buffer.getString(); } catch (Exception e) { Logger.log(Logger.WARNING_DEBUG, "Could not create template for new html", e); //$NON-NLS-1$ } } return templateString; }
/** * Formats the template buffer. * @param buffer * @param context * @throws BadLocationException */ public void format(TemplateBuffer buffer, CompilationUnitContext context) throws BadLocationException { try { VariableTracker tracker= new VariableTracker(buffer); IDocument document= tracker.getDocument(); internalFormat(document, context); convertLineDelimiters(document); if (!(context instanceof JavaDocContext) && !isReplacedAreaEmpty(context)) trimStart(document); tracker.updateBuffer(); } catch (MalformedTreeException e) { throw new BadLocationException(); } }
private int getCaretOffset(TemplateBuffer buffer) { TemplateVariable[] variables = buffer.getVariables(); for (int i = 0; i != variables.length; i++) { TemplateVariable variable = variables[i]; if (variable.getType().equals(GlobalTemplateVariables.Cursor.NAME)) return variable.getOffsets()[0]; } return buffer.getString().length(); }
private static TemplateVariable findVariable(TemplateBuffer buffer, String variable) { TemplateVariable[] positions = buffer.getVariables(); for (int i = 0; i < positions.length; i++) { TemplateVariable curr = positions[i]; if (variable.equals(curr.getType())) { return curr; } } return null; }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { if (!canEvaluate(template)) return null; TemplateTranslator translator= createTemplateTranslator(); TemplateBuffer buffer= translator.translate(template); getContextType().resolve(buffer, this); return buffer; }
/** * @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; }
/** * Creates a new tracker. * * @param buffer the buffer to track * @throws MalformedTreeException * @throws BadLocationException */ public VariableTracker(TemplateBuffer buffer) throws MalformedTreeException, BadLocationException { Assert.isLegal(buffer != null); fBuffer = buffer; fDocument = new Document(fBuffer.getString()); installJavaStuff(fDocument); fDocument.addPositionCategory(CATEGORY); fDocument.addPositionUpdater(new ExclusivePositionUpdater(CATEGORY)); fPositions = createRangeMarkers(fBuffer.getVariables(), fDocument); }
/** * Restores any decorated regions and updates the buffer's variable offsets. * * @return the buffer. * @throws MalformedTreeException * @throws BadLocationException */ public TemplateBuffer updateBuffer() throws MalformedTreeException, BadLocationException { checkState(); TemplateVariable[] variables = fBuffer.getVariables(); try { removeRangeMarkers(fPositions, fDocument, variables); } catch (BadPositionCategoryException x) { Assert.isTrue(false); } fBuffer.setContent(fDocument.get(), variables); fDocument = null; return fBuffer; }
/** * Evaluates a 'java' template in the context of a compilation unit * * @param template the template to be evaluated * @param compilationUnit the compilation unit in which to evaluate the template * @param position the position inside the compilation unit for which to evaluate the template * @return the evaluated template * @throws CoreException in case the template is of an unknown context type * @throws BadLocationException in case the position is invalid in the compilation unit * @throws TemplateException in case the evaluation fails */ public static String evaluateTemplate( Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException { TemplateContextType contextType = JavaPlugin.getDefault() .getTemplateContextRegistry() .getContextType(template.getContextTypeId()); if (!(contextType instanceof CompilationUnitContextType)) throw new CoreException( new Status( IStatus.ERROR, JavaPlugin.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null)); IDocument document = new Document(); if (compilationUnit != null && compilationUnit.exists()) document.set(compilationUnit.getSource()); CompilationUnitContext context = ((CompilationUnitContextType) contextType) .createContext(document, position, 0, compilationUnit); context.setForceEvaluation(true); TemplateBuffer buffer = context.evaluate(template); if (buffer == null) return null; return buffer.getString(); }
private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException { IDocument doc = new Document(buffer.getString()); int nLines = doc.getNumberOfLines(); MultiTextEdit edit = new MultiTextEdit(); HashSet<Integer> removedLines = new HashSet<Integer>(); for (int i = 0; i < variables.length; i++) { TemplateVariable position = findVariable(buffer, variables[i]); // look if Javadoc tags have to be added if (position == null || position.getLength() > 0) { continue; } int[] offsets = position.getOffsets(); for (int k = 0; k < offsets.length; k++) { int line = doc.getLineOfOffset(offsets[k]); IRegion lineInfo = doc.getLineInformation(line); int offset = lineInfo.getOffset(); String str = doc.get(offset, lineInfo.getLength()); if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) { int nextStart = doc.getLineOffset(line + 1); edit.addChild(new DeleteEdit(offset, nextStart - offset)); } } } edit.apply(doc, 0); return doc.get(); }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { if (!canEvaluate(template)) return null; TemplateTranslator translator = createTemplateTranslator(); TemplateBuffer buffer = translator.translate(template); getContextType().resolve(buffer, this); return buffer; }
/** * @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; }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { try { return super.evaluate(template); } catch (VariableEvaluationException ex) { throw new TemplateException(ex.getMessage(), ex); } }
/** * Evaluate a snippet by replacing the tab stops with the default values. Note the snippet must have a scope or else * the evaluation is not done * * @param snippet * @param document * @param position * @return */ public static String evaluateSnippet(SnippetElement snippet, IDocument document, Position position) { String expansion = snippet.getExpansion(); Template template = new SnippetTemplate(snippet, expansion); String scope = snippet.getScope(); if (scope != null) { SnippetTemplateContextType contextType = new SnippetTemplateContextType(scope); DocumentSnippetTemplateContext context = new DocumentSnippetTemplateContext(contextType, document, position); try { TemplateBuffer buffer = context.evaluate(template); if (buffer != null) { return buffer.getString(); } } catch (Exception e) { IdeLog.logWarning( CommonEditorPlugin.getDefault(), MessageFormat.format("Error in template {0}. {1}", snippet.getDisplayName(), e.getMessage()), IDebugScopes.PRESENTATION); //$NON-NLS-1$ } } return expansion; }
/** * Creates a new tracker. * * @param buffer the buffer to track * @throws MalformedTreeException * @throws BadLocationException */ public VariableTracker(TemplateBuffer buffer) throws MalformedTreeException, BadLocationException { Assert.isLegal(buffer != null); fBuffer= buffer; fDocument= new Document(fBuffer.getString()); installJavaStuff(fDocument); fDocument.addPositionCategory(CATEGORY); fDocument.addPositionUpdater(new ExclusivePositionUpdater(CATEGORY)); fPositions= createRangeMarkers(fBuffer.getVariables(), fDocument); }
/** * Restores any decorated regions and updates the buffer's variable offsets. * * @return the buffer. * @throws MalformedTreeException * @throws BadLocationException */ public TemplateBuffer updateBuffer() throws MalformedTreeException, BadLocationException { checkState(); TemplateVariable[] variables= fBuffer.getVariables(); try { removeRangeMarkers(fPositions, fDocument, variables); } catch (BadPositionCategoryException x) { Assert.isTrue(false); } fBuffer.setContent(fDocument.get(), variables); fDocument= null; return fBuffer; }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { clear(); if (!canEvaluate(template)) throw new TemplateException(JavaTemplateMessages.Context_error_cannot_evaluate); TemplateTranslator translator= new TemplateTranslator() { @Override protected TemplateVariable createVariable(TemplateVariableType type, String name, int[] offsets) { // TemplateVariableResolver resolver= getContextType().getResolver(type.getName()); // return resolver.createVariable(); MultiVariable variable= new JavaVariable(type, name, offsets); fVariables.put(name, variable); return variable; } }; TemplateBuffer buffer= translator.translate(template); getContextType().resolve(buffer, this); rewriteImports(); IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore(); boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER); IJavaProject project= getJavaProject(); JavaFormatter formatter= new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project); formatter.format(buffer, this); clear(); return buffer; }
private static TemplateVariable findVariable(TemplateBuffer buffer, String variable) { TemplateVariable[] positions= buffer.getVariables(); for (int i= 0; i < positions.length; i++) { TemplateVariable curr= positions[i]; if (variable.equals(curr.getType())) { return curr; } } return null; }
private int getCaretOffset(TemplateBuffer buffer) { TemplateVariable[] variables= buffer.getVariables(); for (int i= 0; i != variables.length; i++) { TemplateVariable variable= variables[i]; if (variable.getType().equals(GlobalTemplateVariables.Cursor.NAME)) return variable.getOffsets()[0]; } return buffer.getString().length(); }