1 19 package org.netbeans.modules.retouche.editor; 20 21 import java.io.IOException ; 22 import java.io.Writer ; 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.Document ; 25 import javax.swing.text.JTextComponent ; 26 import org.netbeans.api.gsf.FormattingPreferences; 27 import org.netbeans.api.gsf.FormattingPreferences; 28 import org.netbeans.editor.BaseDocument; 29 import org.netbeans.editor.SettingsChangeEvent; 30 import org.netbeans.editor.SettingsDefaults; 31 import org.netbeans.editor.SettingsNames; 32 import org.netbeans.editor.SettingsUtil; 33 import org.netbeans.editor.Syntax; 34 import org.netbeans.editor.TokenItem; 35 import org.netbeans.editor.TokenItem; 36 import org.netbeans.editor.Utilities; 37 import org.netbeans.editor.ext.AbstractFormatLayer; 38 import org.netbeans.editor.ext.ExtFormatter; 39 import org.netbeans.editor.ext.FormatSupport; 40 import org.netbeans.editor.ext.FormatTokenPosition; 41 import org.netbeans.editor.ext.FormatWriter; 42 import org.netbeans.modules.gsf.GsfEditorKitFactory; 43 import org.netbeans.modules.gsf.Language; 44 import org.openide.filesystems.FileObject; 45 import org.openide.loaders.DataObject; 46 47 48 54 public class GsfFormatter extends ExtFormatter { 55 private Language language; 56 private Document doc; 57 private boolean inited; 58 private boolean customSpacesPerTab; 59 private int spacesPerTab; 60 61 62 private int formatStartOffset; 108 private int formatEndOffset; 109 110 public GsfFormatter(Class kitClass, Language language, Document doc) { 111 super(kitClass); 112 assert doc != null; 113 this.language = language; 114 this.doc = doc; 115 } 116 117 protected boolean acceptSyntax(Syntax syntax) { 118 return true; 120 } 121 122 @Override 123 public int[] getReformatBlock(JTextComponent target, String typedText) { 124 int[] ret = null; 125 126 BaseDocument doc = Utilities.getDocument(target); 129 int dotPos = target.getCaret().getDot(); 130 131 if (doc != null) { 132 135 if ("e".equals(typedText)) { 137 try { 138 int fnw = Utilities.getRowFirstNonWhite(doc, dotPos); 139 140 if ((fnw >= 0) && ((fnw + 4) == dotPos) && 141 "else".equals(doc.getText(fnw, 4)) ) { 143 ret = new int[] { fnw, fnw + 4 }; 144 } 145 } catch (BadLocationException e) { 146 } 147 } else if (":".equals(typedText)) { 149 try { 150 int fnw = Utilities.getRowFirstNonWhite(doc, dotPos); 151 152 if ((fnw >= 0) && ((fnw + 4) <= doc.getLength()) && 153 "case".equals(doc.getText(fnw, 4)) ) { 155 ret = new int[] { fnw, fnw + 4 }; 156 } else { 157 if (((fnw >= 0) & ((fnw + 7) <= doc.getLength())) && 158 "default".equals(doc.getText(fnw, 7)) ) { 160 ret = new int[] { fnw, fnw + 7 }; 161 } 162 } 163 } catch (BadLocationException e) { 164 } 165 } else { 166 ret = super.getReformatBlock(target, typedText); 167 } 168 } 169 170 return ret; 171 } 172 173 protected void initFormatLayers() { 174 addFormatLayer(new GsfLayer()); 176 } 177 178 public FormatSupport createFormatSupport(FormatWriter fw) { 179 assert doc != null; 180 181 return new GsfFormatSupport(fw, language, doc); 182 } 183 184 @Override 185 public Writer reformat(BaseDocument doc, int startOffset, int endOffset, boolean indentOnly) 186 throws BadLocationException , IOException { 187 this.formatStartOffset = formatStartOffset; 188 this.formatEndOffset = formatEndOffset; 189 190 return super.reformat(doc, startOffset, endOffset, indentOnly); 191 } 192 193 public void settingsChange(SettingsChangeEvent evt) { 194 String settingName = (evt != null) ? evt.getSettingName() : null; 195 if (!inited || settingName == null || SettingsNames.SPACES_PER_TAB.equals(settingName)) { 196 if (!customSpacesPerTab) { 197 int deflt = SettingsDefaults.defaultSpacesPerTab; 198 if (language.getFormatter() != null) { 199 deflt = language.getFormatter().indentSize(); 200 } 201 int spacesPerTab = SettingsUtil.getInteger(GsfEditorKitFactory.GsfEditorKit.class, SettingsNames.SPACES_PER_TAB, 202 deflt); 203 setSpacesPerTab(spacesPerTab); 204 } 205 } 206 207 inited = true; 208 209 super.settingsChange(evt); 210 } 211 212 215 public int getSpacesPerTab() { 216 if (!customSpacesPerTab && !inited) { 217 settingsChange(null); 218 } 219 220 return spacesPerTab; 221 } 222 223 public void setSpacesPerTab(int spacesPerTab) { 224 customSpacesPerTab = true; 225 this.spacesPerTab = spacesPerTab; 226 super.setSpacesPerTab(spacesPerTab); 227 } 228 229 public static class GenericFormattingPreferences extends FormattingPreferences { 230 private int indentSize; 231 232 public GenericFormattingPreferences(int indentSize) { 233 this.indentSize = indentSize; 234 } 235 236 public int getIndentation() { 237 return indentSize; 238 } 239 } 240 241 public class GsfLayer extends AbstractFormatLayer { 242 public GsfLayer() { 243 super("gsf-layer"); } 245 246 protected FormatSupport createFormatSupport(FormatWriter fw) { 247 assert GsfFormatter.this.doc != null; 248 249 return new GsfFormatSupport(fw, language, GsfFormatter.this.doc); 250 } 251 252 private FileObject getFileObject(Document doc) { 253 DataObject od = (DataObject)doc.getProperty(Document.StreamDescriptionProperty); 254 255 return (od != null) ? od.getPrimaryFile() : null; 256 } 257 258 public void format(FormatWriter fw) { 259 try { 260 if (language.getFormatter() != null) { 261 if (fw.isIndentOnly()) { 263 int indentSize = language.getFormatter().indentSize(); 264 FormattingPreferences preferences = new GenericFormattingPreferences(indentSize); 265 GsfFormatSupport jfs = (GsfFormatSupport)createFormatSupport(fw); 266 int offset = fw.getOffset(); 267 int indent = 268 language.getFormatter().getLineIndent(doc, offset, preferences); 269 FormatTokenPosition pos = jfs.getFormatStartPosition(); 270 jfs.changeLineIndent(pos, indent); 271 } else { 272 final int startOffset = formatStartOffset; 275 final int endOffset = formatEndOffset == 0 ? doc.getLength() : formatEndOffset; 276 277 FormattingPreferences preferences = new GenericFormattingPreferences(language.getFormatter().indentSize()); 278 language.getFormatter().reindent(doc, startOffset, endOffset, null, preferences); 279 280 return; 281 282 284 } 314 } 315 316 GsfFormatSupport jfs = (GsfFormatSupport)createFormatSupport(fw); 319 320 FormatTokenPosition pos = jfs.getFormatStartPosition(); 321 322 if (jfs.isIndentOnly()) { jfs.indentLine(pos); 324 } else { 326 while (pos != null) { 327 jfs.indentLine(pos); 329 330 formatLine(jfs, pos); 332 333 FormatTokenPosition pos2 = jfs.findLineEnd(pos); 335 336 if ((pos2 == null) || (pos2.getToken() == null)) { 337 break; } 339 340 pos = jfs.getNextPosition(pos2, javax.swing.text.Position.Bias.Forward); 341 342 if (pos == pos2) { 343 break; } 345 346 if ((pos == null) || (pos.getToken() == null)) { 347 break; } 349 350 FormatTokenPosition fnw = jfs.findLineFirstNonWhitespace(pos); 351 352 if (fnw != null) { 353 pos = fnw; 354 } else { pos = jfs.findLineStart(pos); 356 } 357 } 358 } 359 } catch (IllegalStateException e) { 360 } 361 } 362 363 protected void formatLine(GsfFormatSupport jfs, FormatTokenPosition pos) { 364 TokenItem token = jfs.findLineStart(pos).getToken(); 365 366 while (token != null) { 367 token = token.getNext(); 369 } 370 } 371 } 372 } 373 | Popular Tags |