temp.replace(lineOffset, wsLen, indent);
temp.replace(lineOffset, wsLen, indent.toString());
	private static void smartPaste(IDocument document, DocumentCommand command) {		try {			JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);			JavaIndenter indenter= new JavaIndenter(document, scanner);			int offset= command.offset;						// reference position to get the indent from			int refPos= indenter.findReferencePosition(offset);			if (refPos == JavaHeuristicScanner.NOT_FOUND)				return;			IRegion region= document.getLineInformationOfOffset(refPos);			int lineOffset= region.getOffset();						// get the base indentation from the reference position			int endOfWS= scanner.findNonWhitespaceForwardInAnyPartition(lineOffset, JavaHeuristicScanner.UNBOUND);			if (endOfWS == JavaHeuristicScanner.NOT_FOUND)				endOfWS= lineOffset;			String baseIndent= document.get(lineOffset, endOfWS - lineOffset);			if (refPos == JavaHeuristicScanner.NOT_FOUND)				return;			// eat any WS before the insertion to the beginning of the line 			region= document.getLineInformationOfOffset(offset);			String notSelected= document.get(region.getOffset(), offset - region.getOffset());			if (notSelected.trim().length() == 0) {				command.length += notSelected.length();				command.offset= region.getOffset();			}						// prefix: the part we need for formatting but won't paste			String prefix= baseIndent + document.get(refPos, command.offset - refPos);			Document temp= new Document(prefix + command.text);						// eat any WS after the insertion, up to the delimiter, if the pasted text ends with			// a newline (with optional WS on the new line).			region= temp.getLineInformation(temp.getNumberOfLines() - 1);			boolean eatAfterSpace= temp.get(region.getOffset(), region.getLength()).trim().length() == 0;			String afterContent= new String();			if (eatAfterSpace) {				int selectionEnd= command.offset + command.length;				region= document.getLineInformationOfOffset(selectionEnd);				endOfWS= scanner.findNonWhitespaceForwardInAnyPartition(selectionEnd, region.getOffset() + region.getLength());				if (endOfWS != JavaHeuristicScanner.NOT_FOUND) {					int token= scanner.nextToken(selectionEnd, region.getOffset() + region.getLength());					if (token != Symbols.TokenEOF && token != Symbols.TokenCASE) { // doesn't work nicely with case						command.length += endOfWS - selectionEnd;						afterContent= document.get(endOfWS, scanner.getPosition() - endOfWS);					}				} else {					// simulate something to get the indentation					command.length += region.getOffset() + region.getLength() - selectionEnd;					afterContent= "x"; //$NON-NLS-1$				}			}						// add context behind the insertion			temp.replace(temp.getLength(), 0, afterContent);						scanner= new JavaHeuristicScanner(temp);			indenter= new JavaIndenter(temp, scanner);			String[] types= new String[] {									   IJavaPartitions.JAVA_DOC,									   		IJavaPartitions.JAVA_MULTI_LINE_COMMENT,									   		IJavaPartitions.JAVA_SINGLE_LINE_COMMENT,									   		IJavaPartitions.JAVA_STRING,									   		IJavaPartitions.JAVA_CHARACTER,									   		IDocument.DEFAULT_CONTENT_TYPE			};			DefaultPartitioner partitioner= new DefaultPartitioner(new FastJavaPartitionScanner(), types);			partitioner.connect(temp);			temp.setDocumentPartitioner(IJavaPartitions.JAVA_PARTITIONING, partitioner);						int lines= temp.getNumberOfLines();			for (int l= document.computeNumberOfLines(prefix); l < lines; l++) { // we don't change the number of lines while adding indents				IRegion r= temp.getLineInformation(l);				lineOffset= r.getOffset();				int lineLength= r.getLength();								if (lineLength == 0) // don't format empty lines					continue;								if (lineOffset < temp.getLength() - 1) {					// special single line comment handling: if comment starts at the start of the line, leave it alone.					String s= temp.get(lineOffset, 2);					if ("//".equals(s)) { //$NON-NLS-1$						continue;					}				}								String indent= indenter.computeIndentation(lineOffset);				if (indent == null) // bail out					return;								// add single character for *-ed multiline comments				ITypedRegion partition= temp.getPartition(IJavaPartitions.JAVA_PARTITIONING, lineOffset);				String type= partition.getType();				if (type.equals(IJavaPartitions.JAVA_DOC) || type.equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT))					if (partition.getOffset() != lineOffset) // not for first line						indent += ' ';									endOfWS= scanner.findNonWhitespaceForwardInAnyPartition(lineOffset, lineOffset + lineLength);				if (endOfWS == JavaHeuristicScanner.NOT_FOUND)					endOfWS= lineOffset + lineLength;								int wsLen= Math.max(endOfWS - lineOffset, 0);				temp.replace(lineOffset, wsLen, indent);			}						temp.setDocumentPartitioner(IJavaPartitions.JAVA_PARTITIONING, null);			partitioner.disconnect();			command.text= temp.get(prefix.length(), temp.getLength() - prefix.length() - afterContent.length());					} catch (BadLocationException e) {			JavaPlugin.log(e);		} catch (BadPartitioningException e) {			JavaPlugin.log(e);		}	}