Java Code Examples for org.eclipse.jface.text.templates.TemplateBuffer#getVariables()

The following examples show how to use org.eclipse.jface.text.templates.TemplateBuffer#getVariables() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SourceCodeTemplateContext.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
/**
    * Shifts the variable offsets according to the current indentation level.
    * 
    * @param buffer                template buffer
    * @param currentIndentation    current indentation level
    * @return  the variable offsets according to the current indentation level
    */
   @SuppressWarnings("unused")
private static TemplateVariable[] indentVariableOffsets(TemplateBuffer buffer, int currentIndentation) {
       String content = buffer.getString();
       TemplateVariable[] variables = buffer.getVariables();
       List<Integer> lineBreaks     = getLinebreaksList(content);
       int line;
       for (int j = 0; j < variables.length; j++) {
           int[] offsets = variables[j].getOffsets();

           for (int k = 0; k < offsets.length; k++) {

               line = getLineOfOffset(offsets[k], lineBreaks);
               
               offsets[k] = offsets[k] + (line * currentIndentation);
           }
           variables[j].setOffsets(offsets);
       }
       
       return variables;
   }
 
Example 2
Source File: SourceCodeTemplateContext.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {

    TemplateTranslator translator= new TemplateTranslator();
       TemplateBuffer buffer= translator.translate(template);
       
       templateBody = buffer.getString(); // required to process multiline 'line_selection' 

       getContextType().resolve(buffer, this);

	getIndentation();

	///* Indents the variables */
	//TemplateVariable[] variables = indentVariableOffsets(buffer, indentation.length());
	TemplateVariable[] variables = buffer.getVariables();
	
	/* Indents the template */
	String formattedTemplate = doIndent(buffer.getString(), variables, indents); 
	
	if (cutTemplateCRLF) {
	    if (formattedTemplate.endsWith("\r\n")) {
	        formattedTemplate = formattedTemplate.substring(0, formattedTemplate.length()-2);
	    } else if (formattedTemplate.endsWith("\n")) {
               formattedTemplate = formattedTemplate.substring(0, formattedTemplate.length()-1);
	    }
	}
	
	buffer.setContent(formattedTemplate, variables);
	
	return buffer;
}
 
Example 3
Source File: SnippetTemplateProposal.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: TemplateProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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();
	}
 
Example 6
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TemplateBuffer evaluate(Template template)
		throws BadLocationException, TemplateException {
	
	TemplateBuffer result = super.evaluate(template);
	
	// After the template buffer has been created we are able to add out of range offsets
	// This is not possible beforehand as it will result in an exception!
	for (TemplateVariable tv : result.getVariables()) {
            
           int[] outOfRangeOffsets = this.getVariableOutOfRangeOffsets(tv);
           
           if (outOfRangeOffsets != null && outOfRangeOffsets.length > 0) {
           	int[] offsets = tv.getOffsets();
           	int[] newOffsets = new int[outOfRangeOffsets.length + offsets.length];
           	
           	System.arraycopy(offsets, 0, newOffsets, 0, offsets.length);

           	for (int i = 0; i < outOfRangeOffsets.length; i++) {
           		newOffsets[i + offsets.length] = outOfRangeOffsets[i]; // - getAffectedSourceRegion().getOffset();
           	}
           	
           	tv.setOffsets(newOffsets);
           }
	}
	
	return result;
}