4848import java .awt .event .MouseAdapter ;
4949import java .awt .event .MouseEvent ;
5050import java .io .IOException ;
51- import java .io .OutputStreamWriter ;
52- import java .io .PrintWriter ;
5351import java .util .Collection ;
5452import java .util .HashMap ;
5553import java .util .List ;
7068import javax .swing .SwingConstants ;
7169import javax .swing .event .ListSelectionEvent ;
7270import javax .swing .event .ListSelectionListener ;
71+ import javax .swing .text .BadLocationException ;
72+ import javax .swing .text .StyledDocument ;
7373import org .netbeans .modules .php .api .phpmodule .PhpModule ;
7474import org .netbeans .modules .php .wordpress .WordPress ;
7575import org .netbeans .modules .php .wordpress .util .Charset ;
7676import org .netbeans .modules .php .wordpress .util .WPFileUtils ;
7777import org .netbeans .modules .php .wordpress .util .WPUtils ;
7878import org .openide .awt .StatusLineElementProvider ;
79+ import org .openide .cookies .EditorCookie ;
7980import org .openide .filesystems .FileChangeAdapter ;
8081import org .openide .filesystems .FileEvent ;
8182import org .openide .filesystems .FileObject ;
83+ import org .openide .text .NbDocument ;
8284import org .openide .util .Exceptions ;
8385import org .openide .util .ImageUtilities ;
8486import org .openide .util .Lookup ;
@@ -98,7 +100,7 @@ public class DebugStatusLineElement implements StatusLineElementProvider {
98100 private static final String DEBUG_TRUE = "true" ; // NOI18N
99101 private static final String DEBUG_FALSE = "false" ; // NOI18N
100102 private static final String WP_DEBUG_FORMAT = "define('WP_DEBUG', %s);" ; // NOI18N
101- private static final String DEBUG_REGEX = "^define\\ ('WP_DEBUG', *(true|false)\\ );$" ; // NOI18N
103+ private static final String DEBUG_REGEX = "^define\\ (\\ s* 'WP_DEBUG',\\ s *(true|false)\\ s* \\ );$" ; // NOI18N
102104 private static final Map <String , String > debugLevel = new HashMap <String , String >();
103105 private static final String WP_CONFIG_PHP = "wp-config.php" ; // NOI18N
104106 private final ImageIcon icon = ImageUtilities .loadImageIcon (WordPress .WP_ICON_16 , true );
@@ -169,27 +171,47 @@ public void valueChanged(ListSelectionEvent e) {
169171 *
170172 * @param debugLv true or false
171173 */
172- private void writeConfig (String debugLv ) {
174+ private void writeConfig (final String debugLv ) {
173175 FileObject config = WPFileUtils .getDirectory (phpModule , WP_CONFIG_PHP );
174176 if (config == null ) {
175177 LOGGER .log (Level .WARNING , "Not found wp-config.php" );
176178 return ;
177179 }
178180 try {
181+ Lookup lookup = config .getLookup ();
182+ EditorCookie ec = lookup .lookup (EditorCookie .class );
183+ if (ec == null ) {
184+ return ;
185+ }
186+ final StyledDocument docment = ec .openDocument ();
187+ if (docment == null ) {
188+ return ;
189+ }
179190 List <String > lines = config .asLines (Charset .UTF8 );
180191 Pattern pattern = Pattern .compile (DEBUG_REGEX );
181- PrintWriter pw = new PrintWriter (new OutputStreamWriter (config .getOutputStream (), Charset .UTF8 ));
182- try {
183- for (String line : lines ) {
184- Matcher matcher = pattern .matcher (line );
185- if (matcher .find ()) {
186- line = String .format (WP_DEBUG_FORMAT , debugLv );
187- }
188- pw .println (line );
192+ int lineNumber = 0 ;
193+ for (String line : lines ) {
194+ Matcher matcher = pattern .matcher (line );
195+ if (matcher .find ()) {
196+ // change
197+ final int startOffset = NbDocument .findLineOffset (docment , lineNumber );
198+ final int removeLength = line .length ();
199+
200+ NbDocument .runAtomic (docment , new Runnable () {
201+ @ Override
202+ public void run () {
203+ try {
204+ docment .remove (startOffset , removeLength );
205+ docment .insertString (startOffset , String .format (WP_DEBUG_FORMAT , debugLv ), null );
206+ } catch (BadLocationException ex ) {
207+ Exceptions .printStackTrace (ex );
208+ }
209+ }
210+ });
189211 }
190- } finally {
191- pw .close ();
212+ lineNumber ++;
192213 }
214+ ec .saveDocument ();
193215 } catch (IOException ex ) {
194216 LOGGER .log (Level .WARNING , null , ex );
195217 }
0 commit comments