|
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