public int compareTo(AlternativeLocation alternative) { PythonAltLocation alt = (PythonAltLocation) alternative; // The preferred item should be chosen if (isPreferred) { return -1; } else if (alt.isPreferred) { return 1; } // Can't both be so no else == check if (order != alt.order) { return order - alt.order; } // Nodoced items last if (element.isNoDoc() != alt.element.isNoDoc()) { return element.isNoDoc() ? 1 : -1; } // Documented items on top if (element.isDocumented() != alt.element.isDocumented()) { return element.isDocumented() ? -1 : 1; } // TODO: Sort by classes? String thisClz = element.getClz() != null ? element.getClz() : ""; String thatClz = alt.element.getClz() != null ? alt.element.getClz() : ""; int cmp = thisClz.compareTo(thatClz); if (cmp != 0) { return cmp; } // TODO: Sort by gem? // Sort by containing clz - just do fqn here? String thisIn = element.getIn() != null ? element.getIn() : ""; String thatIn = alt.element.getIn() != null ? alt.element.getIn() : ""; cmp = thisIn.compareTo(thatIn); if (cmp != 0) { return cmp; } // Sort by file String thisFile = element.getFileObject() != null ? element.getFileObject().getNameExt() : ""; String thatFile = alt.element.getFileObject() != null ? alt.element.getFileObject().getNameExt() : ""; cmp = thisFile.compareTo(thatFile); return cmp; }
public String getDisplayHtml(HtmlFormatter formatter) { formatter.setMaxLength(120); if (cachedDisplayItem == null) { formatter.reset(); boolean nodoc = element.isNoDoc(); boolean documented = element.isDocumented(); if (isPreferred) { formatter.emphasis(true); } else if (nodoc) { formatter.deprecated(true); } if (element instanceof IndexedMethod) { // if (element.getFqn() != null) { // formatter.appendText(element.getFqn()); // formatter.appendText("."); // } formatter.appendText(element.getName()); IndexedMethod method = (IndexedMethod) element; String[] parameters = method.getParams(); if ((parameters != null) && (parameters.length > 0)) { formatter.appendText("("); // NOI18N boolean first = true; for (String parameter : parameters) { if (first) { first = false; } else { formatter.appendText(", "); // NOI18N } formatter.parameters(true); formatter.appendText(parameter); formatter.parameters(false); } formatter.appendText(")"); // NOI18N } } else { // formatter.appendText(element.getFqn()); formatter.appendText(element.getName()); } if (element.getClz() != null) { formatter.appendText(" "); formatter.appendText(NbBundle.getMessage(PythonDeclarationFinder.class, "In")); formatter.appendText(" "); formatter.appendText(element.getClz()); formatter.appendHtml(" "); // NOI18N } String filename = null; String url = element.getFilenameUrl(); if (url == null) { // Deleted file? // Just leave out the file name } else if (url.indexOf("pythonstubs") != -1) { // NOI18N filename = NbBundle.getMessage(PythonDeclarationFinder.class, "PythonLib"); // // if (url.indexOf("/stub_") == -1) { // // Not a stub file, such as ftools.py // // TODO - don't hardcode for version // String stub = "pythonstubs/2.5/"; // int stubStart = url.indexOf(stub); // if (stubStart != -1) { // filename = filename+": " + url.substring(stubStart); // } // } } else { FileObject fo = element.getFileObject(); if (fo != null) { filename = fo.getNameExt(); } else { // Perhaps a file that isn't present here, such as something in site_ruby int lastIndex = url.lastIndexOf('/'); if (lastIndex != -1) { String s = url.substring(0, lastIndex); int almostLastIndex = s.lastIndexOf('/'); if (almostLastIndex != -1 && ((url.length() - almostLastIndex) < 40)) { filename = url.substring(almostLastIndex + 1); if (filename.indexOf(':') != -1) { // Don't include prefix like cluster:, file:, etc. filename = url.substring(lastIndex + 1); } } else { filename = url.substring(lastIndex + 1); } } } // // TODO - make this work with 1.9 etc. // //final String GEM_LOC = "lib/ruby/gems/1.8/gems/"; // Pattern p = Pattern.compile("lib/ruby/gems/\\d+\\.\\d+/gems/"); // Matcher m = p.matcher(url); // //int gemIndex = url.indexOf(GEM_LOC); // //if (gemIndex != -1) { // if (m.find()) { // //int gemIndex = m.start(); // //gemIndex += GEM_LOC.length(); // int gemIndex = m.end(); // int gemEnd = url.indexOf('/', gemIndex); // if (gemEnd != -1) { // //int libIndex = url.indexOf("lib/", gemEnd); // //if (libIndex != -1) { // // filename = url.substring(libIndex+4); // //} // filename = url.substring(gemIndex, gemEnd) + ": " + // filename; // } // } } if (filename != null) { formatter.appendText(" "); formatter.appendText(NbBundle.getMessage(PythonDeclarationFinder.class, "In")); formatter.appendText(" "); formatter.appendText(filename); } if (documented) { formatter.appendText(" "); formatter.appendText(NbBundle.getMessage(PythonDeclarationFinder.class, "Documented")); } else if (nodoc) { formatter.appendText(" "); formatter.appendText(NbBundle.getMessage(PythonDeclarationFinder.class, "NoDoced")); } if (isPreferred) { formatter.emphasis(false); } else if (nodoc) { formatter.deprecated(false); } cachedDisplayItem = formatter.getText(); } return cachedDisplayItem; }
private IndexedElement findBestMatchHelper( PythonParserResult info, String name, Set<IndexedElement> elements, BaseDocument doc, int astOffset, int lexOffset, AstPath path, PythonTree callNode, PythonIndex index) { Set<IndexedElement> candidates = new HashSet<IndexedElement>(); if (elements.size() == 0) { return null; } else if (elements.size() == 1) { return elements.iterator().next(); } // 1. Prefer matches in the current file String searchUrl = info.getSnapshot().getSource().getFileObject().toURL().toExternalForm(); candidates = new HashSet<IndexedElement>(); for (IndexedElement element : elements) { String url = element.getFilenameUrl(); if (url.equals(searchUrl)) { candidates.add(element); } } if (candidates.size() == 1) { return candidates.iterator().next(); } else if (!candidates.isEmpty()) { elements = candidates; } // 2. See which of the class references are defined in files directly // included by this file. Set<String> included = new HashSet<String>(); candidates = new HashSet<IndexedElement>(); SymbolTable table = PythonAstUtils.getParseResult(info).getSymbolTable(); List<Import> imports = table.getImports(); for (Import imp : imports) { List<alias> names = imp.getInternalNames(); if (names != null) { for (alias at : names) { included.add(at.getInternalName()); } } } List<ImportFrom> importsFrom = table.getImportsFrom(); for (ImportFrom imp : importsFrom) { included.add(imp.getInternalModule()); } if (included.size() > 0) { for (IndexedElement element : elements) { String mod = element.getModule(); if (included.contains(mod)) { candidates.add(element); } } if (candidates.size() == 1) { return candidates.iterator().next(); } else if (!candidates.isEmpty()) { elements = candidates; } } // 4. Prefer builtins candidates = new HashSet<IndexedElement>(); for (IndexedElement element : elements) { String url = element.getFilenameUrl(); if (url != null && url.indexOf("pythonstubs") != -1) { // NOI18N candidates.add(element); } } if (candidates.size() == 1) { return candidates.iterator().next(); } else if (!candidates.isEmpty()) { elements = candidates; } // 5. Prefer documented classes candidates = new HashSet<IndexedElement>(); for (IndexedElement element : elements) { if (element.isDocumented()) { candidates.add(element); } } if (candidates.size() == 1) { return candidates.iterator().next(); } else if (!candidates.isEmpty()) { elements = candidates; } // TODO - use some heuristics here! return elements.iterator().next(); }