Skip to content

Commit bdde9d7

Browse files
committed
Minor improvement
1 parent 872004b commit bdde9d7

3 files changed

Lines changed: 46 additions & 16 deletions

File tree

nbproject/genfiles.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
build.xml.data.CRC32=c16cf6ec
1+
build.xml.data.CRC32=8818b8f8
22
build.xml.script.CRC32=f6cbff90
33
build.xml.stylesheet.CRC32=a56c6a5b@2.62.1
44
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
55
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6-
nbproject/build-impl.xml.data.CRC32=c16cf6ec
6+
nbproject/build-impl.xml.data.CRC32=8818b8f8
77
nbproject/build-impl.xml.script.CRC32=bfa38a5f
88
nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.62.1

nbproject/project.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@
222222
<specification-version>7.33.1</specification-version>
223223
</run-dependency>
224224
</dependency>
225+
<dependency>
226+
<code-name-base>org.openide.text</code-name-base>
227+
<build-prerequisite/>
228+
<compile-dependency/>
229+
<run-dependency>
230+
<specification-version>6.58.1</specification-version>
231+
</run-dependency>
232+
</dependency>
225233
<dependency>
226234
<code-name-base>org.openide.util</code-name-base>
227235
<build-prerequisite/>

src/org/netbeans/modules/php/wordpress/ui/status/DebugStatusLineElement.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
import java.awt.event.MouseAdapter;
4949
import java.awt.event.MouseEvent;
5050
import java.io.IOException;
51-
import java.io.OutputStreamWriter;
52-
import java.io.PrintWriter;
5351
import java.util.Collection;
5452
import java.util.HashMap;
5553
import java.util.List;
@@ -70,15 +68,19 @@
7068
import javax.swing.SwingConstants;
7169
import javax.swing.event.ListSelectionEvent;
7270
import javax.swing.event.ListSelectionListener;
71+
import javax.swing.text.BadLocationException;
72+
import javax.swing.text.StyledDocument;
7373
import org.netbeans.modules.php.api.phpmodule.PhpModule;
7474
import org.netbeans.modules.php.wordpress.WordPress;
7575
import org.netbeans.modules.php.wordpress.util.Charset;
7676
import org.netbeans.modules.php.wordpress.util.WPFileUtils;
7777
import org.netbeans.modules.php.wordpress.util.WPUtils;
7878
import org.openide.awt.StatusLineElementProvider;
79+
import org.openide.cookies.EditorCookie;
7980
import org.openide.filesystems.FileChangeAdapter;
8081
import org.openide.filesystems.FileEvent;
8182
import org.openide.filesystems.FileObject;
83+
import org.openide.text.NbDocument;
8284
import org.openide.util.Exceptions;
8385
import org.openide.util.ImageUtilities;
8486
import 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

Comments
 (0)