Java Code Examples for org.eclipse.jface.text.templates.TemplateVariable#getOffsets()

The following examples show how to use org.eclipse.jface.text.templates.TemplateVariable#getOffsets() . 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: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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();
}
 
Example 2
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 3
Source File: CompilationUnitContextType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void validateVariables(TemplateVariable[] variables) throws TemplateException {
	// check for multiple cursor variables
	for (int i= 0; i < variables.length; i++) {
		TemplateVariable var= variables[i];
		if (var.getType().equals(GlobalTemplateVariables.Cursor.NAME)) {
			if (var.getOffsets().length > 1) {
				throw new TemplateException(JavaTemplateMessages.ContextType_error_multiple_cursor_variables);
			}
		}
	}
}
 
Example 4
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 5
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;
}
 
Example 6
Source File: LangTemplateContextType.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void validateVariables(TemplateVariable[] variables) throws TemplateException {
	// check for multiple cursor variables
	for(int i = 0; i < variables.length; i++) {
		TemplateVariable var = variables[i];
		if(var.getType().equals(GlobalTemplateVariables.Cursor.NAME)) {
			if(var.getOffsets().length > 1) {
				throw new TemplateException(JavaTemplateMessages.ContextType_error_multiple_cursor_variables);
			}
		}
	}
}
 
Example 7
Source File: SourceCodeTemplateContext.java    From xds-ide with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Inserts indentation chars to the string according to the current
 * indentation level.
 * 
 * @param string           string to indent
 * @param indentation      current indentation string (with tabs and spaces)
 * @return	the indented string
 */
private String doIndent(String string, TemplateVariable[] variables, String[] indentations) {
	//int stringLength = string.length();
	//final char lastEolChar = System.getProperty("line.separator").charAt(System.getProperty("line.separator").length()-1); //NON-NLS-1 //$NON-NLS-2$
	StringBuilder sb = new StringBuilder();
	String line;

	//dumpVars4dbg(string, variables);

	int resPos = 0;
	boolean line1 = true; // true for intial empty lenes and for 1st non-empty line 
	for (int pos=0; !(line = nextLine(string, pos)).isEmpty(); ) {
	    int cx;
	    int p = line.indexOf(BaseTemplateCompletionProcessor.INDENT_MAGIC);
	    if (p < 0) {
	        if (multilineSel || !line1) {
	            sb.append(indentations[0]);
                cx = indentations[0].length();
	        } else {
                   sb.append(indentations[1]);
                   cx = indentations[1].length();
	        }
	        sb.append(line);
	    } else {
	        if (p>0) {
	            // it is indent from template before 'line_selection':
	            sb.append(line.substring(0,p));
	            line = line.substring(p);
	            pos +=p;
	            resPos += p;
	            p = 0;
	        }
	        // drop INDENT_MAGIC:
	        cx = -BaseTemplateCompletionProcessor.INDENT_MAGIC.length();
	        sb.append(line.substring(-cx));
	    }
	    // move affected variables offsets:
           //System.out.println(String.format("** move: %d at resPos %d (pos %d)", cx, resPos, pos));
	    for (TemplateVariable v : variables) {
            int[] offsets = v.getOffsets();
            for (int k = 0; k < offsets.length; k++) {
                if (cx > 0) {
                       if (offsets[k] >= resPos) {
                           //System.out.println(String.format("++ %s[%d] += %d", v.getName(), offsets[k], cx));
                           offsets[k] += cx;
                       }
                } else {
                       if (offsets[k] >= resPos - cx) {
                           //System.out.println(String.format("-- %s[%d] += %d", v.getName(), offsets[k], cx));
                           offsets[k] += cx;
                       }
                }
            }
            v.setOffsets(offsets);
	    }
	    pos    += line.length();
	    resPos += line.length()+cx;
	    for (char ch : line.toCharArray()) {
	        if (ch != ' ' && ch != '\t') {
	            line1=false;
	            break;
	        }
	    }
	}
       //dumpVars4dbg(sb.toString(), variables);
	return sb.toString();
}
 
Example 8
Source File: JavaFormatter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private List<TypedPosition> createRangeMarkers(TemplateVariable[] variables, IDocument document) throws MalformedTreeException, BadLocationException {
	Map<ReplaceEdit, String> markerToOriginal= new HashMap<ReplaceEdit, String>();

	MultiTextEdit root= new MultiTextEdit(0, document.getLength());
	List<TextEdit> edits= new ArrayList<TextEdit>();
	boolean hasModifications= false;
	for (int i= 0; i != variables.length; i++) {
		final TemplateVariable variable= variables[i];
		int[] offsets= variable.getOffsets();

		String value= variable.getDefaultValue();
		if (isWhitespaceVariable(value)) {
			// replace whitespace positions with unformattable comments
			String placeholder= COMMENT_START + value + COMMENT_END;
			for (int j= 0; j != offsets.length; j++) {
				ReplaceEdit replace= new ReplaceEdit(offsets[j], value.length(), placeholder);
				root.addChild(replace);
				hasModifications= true;
				markerToOriginal.put(replace, value);
				edits.add(replace);
			}
		} else {
			for (int j= 0; j != offsets.length; j++) {
				RangeMarker marker= new RangeMarker(offsets[j], value.length());
				root.addChild(marker);
				edits.add(marker);
			}
		}
	}

	if (hasModifications) {
		// update the document and convert the replaces to markers
		root.apply(document, TextEdit.UPDATE_REGIONS);
	}

	List<TypedPosition> positions= new ArrayList<TypedPosition>();
	for (Iterator<TextEdit> it= edits.iterator(); it.hasNext();) {
		TextEdit edit= it.next();
		try {
			// abuse TypedPosition to piggy back the original contents of the position
			final TypedPosition pos= new TypedPosition(edit.getOffset(), edit.getLength(), markerToOriginal.get(edit));
			document.addPosition(CATEGORY, pos);
			positions.add(pos);
		} catch (BadPositionCategoryException x) {
			Assert.isTrue(false);
		}
	}

	return positions;
}
 
Example 9
Source File: JavaFormatter.java    From goclipse with Eclipse Public License 1.0 4 votes vote down vote up
private List<TypedPosition> createRangeMarkers(TemplateVariable[] variables, IDocument document) throws MalformedTreeException, BadLocationException {
	Map<ReplaceEdit, String> markerToOriginal= new HashMap<ReplaceEdit, String>();

	MultiTextEdit root= new MultiTextEdit(0, document.getLength());
	List<TextEdit> edits= new ArrayList<TextEdit>();
	boolean hasModifications= false;
	for (int i= 0; i != variables.length; i++) {
		final TemplateVariable variable= variables[i];
		int[] offsets= variable.getOffsets();

		String value= variable.getDefaultValue();
		if (isWhitespaceVariable(value)) {
			// replace whitespace positions with unformattable comments
			String placeholder= COMMENT_START + value + COMMENT_END;
			for (int j= 0; j != offsets.length; j++) {
				ReplaceEdit replace= new ReplaceEdit(offsets[j], value.length(), placeholder);
				root.addChild(replace);
				hasModifications= true;
				markerToOriginal.put(replace, value);
				edits.add(replace);
			}
		} else {
			for (int j= 0; j != offsets.length; j++) {
				RangeMarker marker= new RangeMarker(offsets[j], value.length());
				root.addChild(marker);
				edits.add(marker);
			}
		}
	}

	if (hasModifications) {
		// update the document and convert the replaces to markers
		root.apply(document, TextEdit.UPDATE_REGIONS);
	}

	List<TypedPosition> positions= new ArrayList<TypedPosition>();
	for (Iterator<TextEdit> it= edits.iterator(); it.hasNext();) {
		TextEdit edit= it.next();
		try {
			// abuse TypedPosition to piggy back the original contents of the position
			final TypedPosition pos= new TypedPosition(edit.getOffset(), edit.getLength(), markerToOriginal.get(edit));
			document.addPosition(CATEGORY, pos);
			positions.add(pos);
		} catch (BadPositionCategoryException x) {
			Assert.isTrue(false);
		}
	}

	return positions;
}