CorrectIndentationAction implements ITextAction { @Override public String update( String fullText, int selectionOffset, int selectionLength ) { SelectionRange range = TextUtils.findRegionFromSelection( fullText, selectionOffset, selectionLength ); String before = fullText.substring( 0, range.getOffset()); String after = fullText.substring( range.getOffset() + range.getLength()); String text = fullText.substring( range.getOffset(), range.getOffset() + range.getLength()); int indent = findPreviousIndentation( before ); boolean splitLine = false; StringBuilder sb = new StringBuilder(); List<String> lines = Arrays.asList( text.split( "\\n" )); for( Iterator<String> it = lines.iterator(); it.hasNext(); ) { String line = it.next(); String workLine = TextUtils.removeComments( line ).trim(); if( workLine.startsWith( "}" )) indent --; if( ! Utils.isEmptyOrWhitespaces( line )) { for( int i=0; i<indent; i++ ) sb.append( "\t" ); } sb.append( line.replaceFirst( "^\\s*", "" )); if( it.hasNext() || text.endsWith( "\n" )) sb.append( "\n" ); if( workLine.endsWith( "{" )) { indent ++; } else if( ! splitLine && workLine.endsWith( "\\" )) { indent += LB_INDENT; splitLine = true; } else if( splitLine && workLine.endsWith( ";" )) { indent -= LB_INDENT; splitLine = false; } } this.newCursorPosition = before.length() + sb.length(); sb.insert( 0, before ); sb.append( after ); return sb.toString(); }  @Override int getNewCursorPosition(); @Override String update( String fullText, int selectionOffset, int selectionLength );  }
@Test public void testBasics() { CorrectIndentationAction action = new CorrectIndentationAction(); Assert.assertEquals( "", action.update( "", 0, 1 )); Assert.assertEquals( "", action.update( "", 0, 0 )); Assert.assertEquals( "", action.update( "", 0, -11 )); Assert.assertEquals( "", action.update( "", 0, 10 )); Assert.assertEquals( "whatever", action.update( "whatever", 0, 1 )); Assert.assertEquals( "whatever", action.update( " whatever", 0, 1 )); }
@Test public void testMoreComplexCase_noBracket() { CorrectIndentationAction action = new CorrectIndentationAction(); String p1 = " indented\n"; String p2 = "not indented"; Assert.assertEquals( p1 + " " + p2, action.update( p1 + p2, p1.length(), 1 )); Assert.assertEquals( p1 + " " + p2, action.update( p1 + p2, p1.length(), p2.length())); Assert.assertEquals( p1 + " " + p2, action.update( p1 + p2, p1.length(), -5 )); }
@Test public void testRealFiles() throws Exception { File f = TestUtils.findTestFile( "/textactions/indentation-correct.txt" ); String correct = Utils.readFileContent( f ); f = TestUtils.findTestFile( "/textactions/indentation-incorrect.txt" ); String incorrect = Utils.readFileContent( f ); CorrectIndentationAction action = new CorrectIndentationAction(); Assert.assertEquals( correct, action.update( incorrect, 0, incorrect.length())); Assert.assertEquals( correct, action.update( correct, 0, correct.length())); Assert.assertEquals( correct.substring( 0, 44 ) + incorrect.substring( 45 ), action.update( incorrect, 0, 45 )); Assert.assertEquals( incorrect.substring( 0, 45 ) + correct.substring( 44 ), action.update( incorrect, 45, 5000 )); Assert.assertEquals( incorrect.substring( 0, 65 ) + correct.substring( 64, 64 + 50 ) + incorrect.substring( 117 ), action.update( incorrect, 65, 51 )); }