1 20 package org.apache.directory.ldapstudio.aciitemeditor.sourceeditor; 21 22 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 23 import org.eclipse.jface.text.formatter.IFormattingStrategy; 24 import org.eclipse.jface.text.source.ISourceViewer; 25 26 36 public class ACIFormattingStrategy implements IFormattingStrategy 37 { 38 39 40 public static final String INDENT_STRING = " "; 42 43 public static final String NEWLINE = BrowserCoreConstants.LINE_SEPARATOR; 44 45 46 private ISourceViewer sourceViewer; 47 48 49 54 public ACIFormattingStrategy( ISourceViewer sourceViewer ) 55 { 56 this.sourceViewer = sourceViewer; 57 } 58 59 60 63 public String format( String content, boolean isLineStart, String indentation, int[] positions ) 64 { 65 String oldContent = sourceViewer.getDocument().get(); 66 String newContent = internFormat ( oldContent ); 67 sourceViewer.getDocument().set( newContent ); 68 69 return null; 70 } 71 72 73 76 public void formatterStarts( String initialIndentation ) 77 { 78 } 79 80 81 84 public void formatterStops() 85 { 86 } 87 88 private String internFormat( String content ) 89 { 90 StringBuffer sb = new StringBuffer (); 91 92 boolean newLineStarted = true; 94 95 boolean inQuotedString = false; 97 98 boolean oneLineMode = false; 100 101 int indent = 0; 103 104 int contentLength = content.length(); 105 for (int i=0; i<contentLength; i++) 106 { 107 char c = content.charAt( i ); 108 109 if( c == '"') 111 { 112 inQuotedString = !inQuotedString; 113 } 114 115 if( c == '{' && !inQuotedString ) 116 { 117 oneLineMode = checkInOneLine(i, content); 119 120 if(oneLineMode) 121 { 122 sb.append( c ); 124 newLineStarted = false; 125 } 126 else 127 { 128 if( !newLineStarted ) 130 { 131 sb.append( NEWLINE ); 132 for ( int x = 0; x < indent; x++ ) 133 { 134 sb.append( INDENT_STRING ); 135 } 136 } 137 138 sb.append( c ); 140 141 sb.append( NEWLINE ); 143 newLineStarted = true; 144 indent++; 145 for ( int x = 0; x < indent; x++ ) 146 { 147 sb.append( INDENT_STRING ); 148 } 149 } 150 } 151 else if (c == '}' && !inQuotedString ) 152 { 153 if(oneLineMode) 154 { 155 sb.append( c ); 157 newLineStarted = false; 158 159 oneLineMode = false; 161 } 162 else 163 { 164 indent--; 166 167 if( newLineStarted ) 169 { 170 sb.delete( sb.length()-INDENT_STRING.length(), sb.length() ); 172 } 173 else 174 { 175 sb.append( NEWLINE ); 176 for ( int x = 0; x < indent; x++ ) 177 { 178 sb.append( INDENT_STRING ); 179 } 180 } 181 182 sb.append( c ); 184 185 sb.append( NEWLINE ); 187 newLineStarted = true; 188 for ( int x = 0; x < indent; x++ ) 189 { 190 sb.append( INDENT_STRING ); 191 } 192 } 193 } 194 else if (c == ',' && !inQuotedString ) 195 { 196 if(oneLineMode) 198 { 199 sb.append( c ); 200 newLineStarted = false; 201 } 202 else 203 { 204 sb.append( c ); 205 206 sb.append( NEWLINE ); 207 newLineStarted = true; 208 209 for ( int x = 0; x < indent; x++ ) 210 { 211 sb.append( INDENT_STRING ); 212 } 213 } 214 } 215 else if ( Character.isWhitespace( c ) ) 216 { 217 char c1 = 'A'; 218 if(i+1 < contentLength ) 219 { 220 c1 = content.charAt( i+1 ); 221 } 222 223 if( newLineStarted ) 224 { 225 } 227 else if (c == '\n' || c == '\r' ) 228 { 229 } 231 else if( Character.isWhitespace( c1 ) || c1 == '\n' || c1 == '\r' ) 232 { 233 } 235 else 236 { 237 sb.append( c ); 238 } 239 } 240 241 else 242 { 243 sb.append( c ); 245 newLineStarted = false; 246 } 247 } 248 249 return sb.toString(); 250 } 251 252 253 263 private boolean checkInOneLine( int i, String content ) 264 { 265 boolean inQuote = false; 267 268 int commaCounter = 0; 270 271 int contentLength = content.length(); 272 for ( int k=i+1; k<contentLength; k++) 273 { 274 char c = content.charAt( k ); 275 276 if( c == '"') 278 { 279 inQuote = !inQuote; 280 } 281 282 if( ( c == '{' ) && !inQuote ) 284 { 285 return false; 286 } 287 288 if ( c == '}' && !inQuote ) 290 { 291 return true; 292 } 293 294 if (c == ',' && !inQuote ) 296 { 297 commaCounter++; 298 if(commaCounter > 1) 299 { 300 return false; 301 } 302 } 303 } 304 305 return false; 306 } 307 308 } 309 | Popular Tags |