Skip to content

Commit 430860c

Browse files
committed
Fix #96
1 parent 7c26ee2 commit 430860c

File tree

3 files changed

+175
-154
lines changed

3 files changed

+175
-154
lines changed

src/main/java/org/netbeans/modules/python/debugger/PythonDebugger.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public static void startDebugger(Project owner, DataObject dob, boolean singleFi
118118

119119
io = IOProvider.getDefault().getIO(String.format("%s (%s)",
120120
Bundle.CTL_SessionName(), sessionName), false);
121+
io.setInputVisible(true);
121122
io.select();
122123

123124
pdbClient = new PdbClient(process, /*loggingPtyProcessTtyConnector,*/ io);

src/main/java/org/netbeans/modules/python/debugger/pdb/PdbClient.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openide.text.Annotatable;
2828
import org.openide.text.Line;
2929
import org.openide.util.Exceptions;
30+
import org.openide.util.RequestProcessor;
3031
import org.openide.windows.IOColorLines;
3132
import org.openide.windows.InputOutput;
3233

@@ -45,12 +46,17 @@ public class PdbClient {
4546
InputOutput io;
4647
private final BufferedWriter writer;
4748
private final BufferedReader reader;
49+
private final BufferedReader ioReader;
4850

4951
public PdbClient(Process process, InputOutput io) {
5052
this.process = process;
5153
this.io = io;
5254
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
5355
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
56+
ioReader = new BufferedReader(this.io.getIn());
57+
RequestProcessor.getDefault().post(() -> {
58+
handleIoInput();
59+
});
5460
}
5561

5662
public Process getProcess() {
@@ -215,4 +221,18 @@ public boolean isSuspended() {
215221
return isStopped;
216222
}
217223

224+
private void handleIoInput() {
225+
try {
226+
try (ioReader) {
227+
String line;
228+
while ((line = ioReader.readLine()) != null) {
229+
// Process each line of input as needed
230+
writeToStream(line);
231+
}
232+
}
233+
} catch (IOException e) {
234+
e.printStackTrace();
235+
}
236+
}
237+
218238
}
Lines changed: 154 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,154 @@
1-
package org.netbeans.modules.python.indexing;
2-
3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.net.URISyntaxException;
6-
import java.nio.file.Paths;
7-
import java.util.ArrayList;
8-
import java.util.Collections;
9-
import java.util.List;
10-
import java.util.logging.Level;
11-
import java.util.logging.Logger;
12-
import java.util.regex.MatchResult;
13-
import java.util.regex.Pattern;
14-
import javax.swing.text.BadLocationException;
15-
import javax.swing.text.StyledDocument;
16-
import org.apache.commons.lang3.StringUtils;
17-
import org.netbeans.api.project.FileOwnerQuery;
18-
import org.netbeans.api.project.Project;
19-
import org.netbeans.modules.parsing.spi.indexing.Context;
20-
import org.netbeans.modules.parsing.spi.indexing.CustomIndexer;
21-
import org.netbeans.modules.parsing.spi.indexing.ErrorsCache;
22-
import org.netbeans.modules.parsing.spi.indexing.Indexable;
23-
import org.netbeans.modules.parsing.spi.indexing.support.IndexDocument;
24-
import org.netbeans.modules.parsing.spi.indexing.support.IndexingSupport;
25-
import org.netbeans.modules.python.PythonUtility;
26-
import org.netbeans.modules.python.tasklist.PythonDiagCollector;
27-
import org.openide.cookies.EditorCookie;
28-
import org.openide.filesystems.FileObject;
29-
import org.openide.filesystems.FileUtil;
30-
import org.openide.util.Exceptions;
31-
32-
/**
33-
*
34-
* @author albilu
35-
*/
36-
public class PythonCustomIndexer extends CustomIndexer {
37-
38-
public static final Logger LOG = Logger.getLogger(PythonCustomIndexer.class.getName());
39-
static Pattern PYTHON_CLASS = Pattern.compile(".*\\nclass\\s+([^\\(|:]+).*:");
40-
static Pattern PYTHON_METHOD = Pattern.compile(".*def\\s+([^\\(|:]+).*:");
41-
public static final String TYPE_FIELD = "classes";
42-
public static final String SYMBOLS_FIELD = "methods";
43-
44-
@Override
45-
protected void index(Iterable<? extends Indexable> files, Context context) {
46-
long startTime = System.currentTimeMillis();
47-
int cnt = 0;
48-
if (context.isCancelled()) {
49-
LOG.fine("Indexer cancelled");
50-
return;
51-
}
52-
FileObject root = context.getRoot();
53-
Project owner = root != null ? FileOwnerQuery.getOwner(root) : null;
54-
boolean pyProject = owner != null && owner.getClass()
55-
.getName().equals("org.netbeans.modules.python.project.PythonProject");
56-
boolean pyLibPath = root != null && (root.getName().endsWith("Lib") || (root.getParent() != null
57-
&& root.getParent().getName().equals("lib")));
58-
if (pyProject || pyLibPath) {
59-
try {
60-
IndexingSupport is = IndexingSupport.getInstance(context);
61-
for (Indexable file : files) {
62-
File toFile = Paths.get(file.getURL().toURI()).toFile();
63-
FileObject fo = FileUtil.toFileObject(toFile);
64-
if (fo.getExt().equals("py")) {
65-
boolean notSkipped = !StringUtils.containsAny(toFile.toPath().toString(),
66-
PythonUtility.EXCLUDED_DIRS);
67-
if (pyProject && notSkipped) {
68-
String content = getContent(fo);
69-
setErrors(fo, context, file);
70-
setClasses(content, is, file);
71-
setMethods(content, is, file);
72-
cnt++;
73-
}
74-
if (pyLibPath && notSkipped) {
75-
String content = getContent(fo);
76-
setClasses(content, is, file);
77-
//Not relevant to index python methods
78-
//setMethods(content, is, file);
79-
cnt++;
80-
81-
}
82-
}
83-
}
84-
} catch (BadLocationException | URISyntaxException | IOException ex) {
85-
Exceptions.printStackTrace(ex);
86-
}
87-
}
88-
long endTime = System.currentTimeMillis();
89-
if (LOG.isLoggable(Level.INFO)) {
90-
LOG.log(Level.INFO, "Processed {0} files for {1} in {2}ms.", new Object[]{
91-
cnt, root != null ? root.getPath() : "null", endTime - startTime});
92-
}
93-
}
94-
95-
private String getContent(FileObject fo) throws BadLocationException, IOException {
96-
EditorCookie lookup = fo.getLookup().lookup(EditorCookie.class);
97-
StyledDocument openDocument = lookup.openDocument();
98-
return openDocument.getText(0, openDocument.getEndPosition().getOffset());
99-
}
100-
101-
private void setClasses(String content, IndexingSupport is, Indexable file) throws IOException, BadLocationException {
102-
103-
IndexDocument createDocument = is.createDocument(file);
104-
PYTHON_CLASS.matcher(content).results()
105-
.forEach((match) -> {
106-
StringBuilder stringBuilder = new StringBuilder();
107-
stringBuilder.append(match.group(1));
108-
stringBuilder.append("#");
109-
stringBuilder.append(match.start(1));
110-
createDocument.addPair(TYPE_FIELD, stringBuilder.toString(), true, true);
111-
});
112-
113-
is.addDocument(createDocument);
114-
}
115-
116-
private void setErrors(FileObject fo, Context context, Indexable file) {
117-
List errors = new ArrayList<>();
118-
boolean hasErrors = PythonDiagCollector.hasErrors(fo);
119-
PythonDefaultError pythonDefaultError = new PythonDefaultError(
120-
"PARSING", "badging_error", "badging_error", fo, 0, 0, org.netbeans.modules.csl.api.Severity.ERROR);
121-
122-
errors.add(pythonDefaultError);
123-
ErrorsCache
124-
.setErrors(context.getRootURI(), file,
125-
hasErrors ? errors : Collections.emptyList(),
126-
new PythonErrorConvertor());
127-
}
128-
129-
private void setMethods(String content, IndexingSupport is, Indexable file) throws IOException, BadLocationException {
130-
IndexDocument createDocument = is.createDocument(file);
131-
PYTHON_METHOD.matcher(content).results()
132-
.forEach((methodMatch) -> {
133-
String className = " ";
134-
int startOffset = methodMatch.start(1);
135-
MatchResult classGroup = PYTHON_CLASS.matcher(content).results()
136-
.filter((classMatch) -> classMatch.end(1) < startOffset)
137-
.reduce((first, second) -> second)
138-
.orElse(null);
139-
if (classGroup != null) {
140-
className = classGroup.group(1);
141-
}
142-
143-
StringBuilder stringBuilder = new StringBuilder();
144-
stringBuilder.append(methodMatch.group(1));
145-
stringBuilder.append("#");
146-
stringBuilder.append(methodMatch.start(1));
147-
stringBuilder.append("#");
148-
stringBuilder.append(className);
149-
createDocument.addPair(SYMBOLS_FIELD, stringBuilder.toString(), true, true);
150-
});
151-
152-
is.addDocument(createDocument);
153-
}
154-
}
1+
package org.netbeans.modules.python.indexing;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URISyntaxException;
6+
import java.nio.file.Paths;
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.logging.Level;
11+
import java.util.logging.Logger;
12+
import java.util.regex.MatchResult;
13+
import java.util.regex.Pattern;
14+
import javax.swing.text.BadLocationException;
15+
import javax.swing.text.StyledDocument;
16+
import org.apache.commons.lang3.StringUtils;
17+
import org.netbeans.api.project.FileOwnerQuery;
18+
import org.netbeans.api.project.Project;
19+
import org.netbeans.modules.parsing.spi.indexing.Context;
20+
import org.netbeans.modules.parsing.spi.indexing.CustomIndexer;
21+
import org.netbeans.modules.parsing.spi.indexing.ErrorsCache;
22+
import org.netbeans.modules.parsing.spi.indexing.Indexable;
23+
import org.netbeans.modules.parsing.spi.indexing.support.IndexDocument;
24+
import org.netbeans.modules.parsing.spi.indexing.support.IndexingSupport;
25+
import org.netbeans.modules.python.PythonUtility;
26+
import org.netbeans.modules.python.tasklist.PythonDiagCollector;
27+
import org.openide.cookies.EditorCookie;
28+
import org.openide.filesystems.FileObject;
29+
import org.openide.filesystems.FileUtil;
30+
import org.openide.util.Exceptions;
31+
32+
/**
33+
*
34+
* @author albilu
35+
*/
36+
public class PythonCustomIndexer extends CustomIndexer {
37+
38+
public static final Logger LOG = Logger.getLogger(PythonCustomIndexer.class.getName());
39+
static Pattern PYTHON_CLASS = Pattern.compile(".*\\nclass\\s+([^\\(|:]+).*:");
40+
static Pattern PYTHON_METHOD = Pattern.compile(".*def\\s+([^\\(|:]+).*:");
41+
public static final String TYPE_FIELD = "classes";
42+
public static final String SYMBOLS_FIELD = "methods";
43+
44+
@Override
45+
protected void index(Iterable<? extends Indexable> files, Context context) {
46+
long startTime = System.currentTimeMillis();
47+
int cnt = 0;
48+
if (context.isCancelled()) {
49+
LOG.fine("Indexer cancelled");
50+
return;
51+
}
52+
FileObject root = context.getRoot();
53+
Project owner = root != null ? FileOwnerQuery.getOwner(root) : null;
54+
boolean pyProject = owner != null && owner.getClass()
55+
.getName().equals("org.netbeans.modules.python.project.PythonProject");
56+
boolean pyLibPath = root != null && (root.getName().endsWith("Lib") || (root.getParent() != null
57+
&& root.getParent().getName().equals("lib")));
58+
if (pyProject || pyLibPath) {
59+
try {
60+
IndexingSupport is = IndexingSupport.getInstance(context);
61+
for (Indexable file : files) {
62+
File toFile = Paths.get(file.getURL().toURI()).toFile();
63+
FileObject fo = FileUtil.toFileObject(toFile);
64+
if (fo != null && fo.getExt().equals("py")) {
65+
boolean notSkipped = !StringUtils.containsAny(toFile.toPath().toString(),
66+
PythonUtility.EXCLUDED_DIRS);
67+
if (pyProject && notSkipped) {
68+
String content = getContent(fo);
69+
setErrors(fo, context, file);
70+
setClasses(content, is, file);
71+
setMethods(content, is, file);
72+
cnt++;
73+
}
74+
if (pyLibPath && notSkipped) {
75+
String content = getContent(fo);
76+
setClasses(content, is, file);
77+
//Not relevant to index python methods
78+
//setMethods(content, is, file);
79+
cnt++;
80+
81+
}
82+
}
83+
}
84+
} catch (BadLocationException | URISyntaxException | IOException ex) {
85+
Exceptions.printStackTrace(ex);
86+
}
87+
}
88+
long endTime = System.currentTimeMillis();
89+
if (LOG.isLoggable(Level.INFO)) {
90+
LOG.log(Level.INFO, "Processed {0} files for {1} in {2}ms.", new Object[]{
91+
cnt, root != null ? root.getPath() : "null", endTime - startTime});
92+
}
93+
}
94+
95+
private String getContent(FileObject fo) throws BadLocationException, IOException {
96+
EditorCookie lookup = fo.getLookup().lookup(EditorCookie.class);
97+
StyledDocument openDocument = lookup.openDocument();
98+
return openDocument.getText(0, openDocument.getEndPosition().getOffset());
99+
}
100+
101+
private void setClasses(String content, IndexingSupport is, Indexable file) throws IOException, BadLocationException {
102+
103+
IndexDocument createDocument = is.createDocument(file);
104+
PYTHON_CLASS.matcher(content).results()
105+
.forEach((match) -> {
106+
StringBuilder stringBuilder = new StringBuilder();
107+
stringBuilder.append(match.group(1));
108+
stringBuilder.append("#");
109+
stringBuilder.append(match.start(1));
110+
createDocument.addPair(TYPE_FIELD, stringBuilder.toString(), true, true);
111+
});
112+
113+
is.addDocument(createDocument);
114+
}
115+
116+
private void setErrors(FileObject fo, Context context, Indexable file) {
117+
List errors = new ArrayList<>();
118+
boolean hasErrors = PythonDiagCollector.hasErrors(fo);
119+
PythonDefaultError pythonDefaultError = new PythonDefaultError(
120+
"PARSING", "badging_error", "badging_error", fo, 0, 0, org.netbeans.modules.csl.api.Severity.ERROR);
121+
122+
errors.add(pythonDefaultError);
123+
ErrorsCache
124+
.setErrors(context.getRootURI(), file,
125+
hasErrors ? errors : Collections.emptyList(),
126+
new PythonErrorConvertor());
127+
}
128+
129+
private void setMethods(String content, IndexingSupport is, Indexable file) throws IOException, BadLocationException {
130+
IndexDocument createDocument = is.createDocument(file);
131+
PYTHON_METHOD.matcher(content).results()
132+
.forEach((methodMatch) -> {
133+
String className = " ";
134+
int startOffset = methodMatch.start(1);
135+
MatchResult classGroup = PYTHON_CLASS.matcher(content).results()
136+
.filter((classMatch) -> classMatch.end(1) < startOffset)
137+
.reduce((first, second) -> second)
138+
.orElse(null);
139+
if (classGroup != null) {
140+
className = classGroup.group(1);
141+
}
142+
143+
StringBuilder stringBuilder = new StringBuilder();
144+
stringBuilder.append(methodMatch.group(1));
145+
stringBuilder.append("#");
146+
stringBuilder.append(methodMatch.start(1));
147+
stringBuilder.append("#");
148+
stringBuilder.append(className);
149+
createDocument.addPair(SYMBOLS_FIELD, stringBuilder.toString(), true, true);
150+
});
151+
152+
is.addDocument(createDocument);
153+
}
154+
}

0 commit comments

Comments
 (0)