@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 )); }
@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(); }
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(); } }
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(); }  }
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 ); }
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 );  }