String blockIndent= getBlockIndent(document, command); String insideBlockIndent= blockIndent == null ? "" : blockIndent + createIndent(1, useSpaces()); int insideBlockIndentSize= calculateDisplayedWidth(insideBlockIndent, getTabWidth()); int previousIndentSize= getIndentSize(document, command); int newIndentSize= insideBlockIndentSize < previousIndentSize ? insideBlockIndentSize : previousIndentSize;
String prefix= baseIndent + document.get(refPos, command.offset - refPos); Document temp= new Document(prefix + command.text);    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) { command.length += endOfWS - selectionEnd; afterContent= document.get(endOfWS, scanner.getPosition() - endOfWS); } } else {  command.length += region.getOffset() + region.getLength() - selectionEnd; afterContent= "x"; } }   temp.replace(temp.getLength(), 0, afterContent);  scanner= new JavaHeuristicScanner(temp); indenter= new JavaIndenter(temp, scanner);  int lines= temp.getNumberOfLines(); for (int l= document.computeNumberOfLines(prefix); l < lines; l++) { IRegion r= temp.getLineInformation(l); lineOffset= r.getOffset();  if (r.getLength() == 0) continue;  String indent= indenter.computeIndentation(lineOffset); if (indent == null) return;  endOfWS= scanner.findNonWhitespaceForward(lineOffset, lineOffset + r.getLength()); if (endOfWS == JavaHeuristicScanner.NOT_FOUND) endOfWS= lineOffset;  int wsLen= endOfWS - lineOffset; temp.replace(lineOffset, wsLen, indent); }
	private void smartPaste(IDocument document, DocumentCommand command) {		String lineDelimiter= getLineDelimiter(document);		try {			String pastedText= command.text;			Assert.isNotNull(pastedText);			Assert.isTrue(pastedText.length() > 1);						// extend selection begin if only whitespaces			int selectionStart= command.offset;			IRegion region= document.getLineInformationOfOffset(selectionStart);			String notSelected= document.get(region.getOffset(), selectionStart - region.getOffset());			String selected= document.get(selectionStart, region.getOffset() + region.getLength() - selectionStart);			if (notSelected.trim().length() == 0 && selected.trim().length() != 0) {				pastedText= notSelected + pastedText;				command.length += notSelected.length();				command.offset= region.getOffset();			}						// choose smaller indent of block and preceeding non-empty line 			String blockIndent= getBlockIndent(document, command);			String insideBlockIndent= blockIndent == null ? "" : blockIndent + createIndent(1, useSpaces()); //$NON-NLS-1$ // add one indent level			int insideBlockIndentSize= calculateDisplayedWidth(insideBlockIndent, getTabWidth());			int previousIndentSize= getIndentSize(document, command);			int newIndentSize= insideBlockIndentSize < previousIndentSize ? insideBlockIndentSize : previousIndentSize;			// indent is different if block starts with '}'							if (startsWithClosingBrace(pastedText)) {				int outsideBlockIndentSize= blockIndent == null ? 0 : calculateDisplayedWidth(blockIndent, getTabWidth());				newIndentSize = outsideBlockIndentSize;							}			// check selection			int offset= command.offset;			int line= document.getLineOfOffset(offset);			int lineOffset= document.getLineOffset(line);			String prefix= document.get(lineOffset, offset - lineOffset);			boolean formatFirstLine= prefix.trim().length() == 0;			String formattedParagraph= format(pastedText, newIndentSize, lineDelimiter, formatFirstLine);			// paste			if (formatFirstLine) {				int end= command.offset + command.length;				command.offset= lineOffset;				command.length= end - command.offset;			}			command.text= formattedParagraph;		} catch (BadLocationException e) {			JavaPlugin.log(e);		}	}