@Override public void run() { try { StopWatch stp2 = new StopWatch(); stp2.start(); JSONObject json = new JSONObject(); job.setStatus("running"); if (job.progress() == 100.0) { finalizeJob(job); return; } Vector<String> ids = new Vector<String>(); Vector<String> original_names = new Vector<String>(); String data = job.getNextDataBatch(); if (data == null || data.equals("")) return; String[] lines = data.split("\n"); if (job.containsId()) { for (int i = 0; i < lines.length; i++) { if (lines[i].trim().equals("")) continue; ids.add(NameUtil.getNameId(lines[i])); } } for (int i = 0; i < lines.length; i++) { original_names.add(NameUtil.processName(lines[i], job.containsId())); } String names = NameUtil.CleanNames(lines, job); if (names.equals("")) return; if (job.getType() == TnrsJob.NAME_MATCH_JOB) { TaxamatchInterface taxa_match = new TaxamatchInterface(tnrsBaseUrl); String result = taxa_match.queryTaxamatch(names, job); json = (JSONObject) JSONSerializer.toJSON(result); } else if (job.getType() == TnrsJob.PARSING_JOB) { json = gni_interface.parseNames(names); } if (job.outstandingNames() == 0) { JobHelper.persistJobInfo(baseFolder, job); } saveResults(job, json, ids, original_names, ""); job.setStatus("idle"); stp2.stop(); log.info("overall :" + stp2.toString()); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); job.setStatus("failed"); ex.printStackTrace(); } }
private String[] getSuggestionsByName( String name, VariableKind variableKind, boolean isArray, boolean correctKeywords) { boolean upperCaseStyle = variableKind == VariableKind.STATIC_FINAL_FIELD; boolean preferLongerNames = getSettings().PREFER_LONGER_NAMES; String prefix = getPrefixByVariableKind(variableKind); String suffix = getSuffixByVariableKind(variableKind); List<String> answer = new ArrayList<String>(); for (String suggestion : NameUtil.getSuggestionsByName( name, prefix, suffix, upperCaseStyle, preferLongerNames, isArray)) { answer.add(correctKeywords ? changeIfNotIdentifier(suggestion) : suggestion); } return ArrayUtil.toStringArray(answer); }
private int indexOfWordStart(@NotNull String name, int patternIndex, int startFrom) { final char p = myPattern[patternIndex]; if (startFrom >= name.length() || myHasHumps && isLowerCase[patternIndex] && !(patternIndex > 0 && isWordSeparator[patternIndex - 1])) { return -1; } int nextWordStart = startFrom; while (true) { nextWordStart = NameUtil.nextWord(name, nextWordStart); if (nextWordStart >= name.length()) { return -1; } if (charEquals(p, patternIndex, name.charAt(nextWordStart), true)) { return nextWordStart; } } }
/** * After a wildcard (* or space), search for the first non-wildcard pattern character in the name * starting from nameIndex and try to {@link #matchFragment(String, int, int, * com.intellij.psi.codeStyle.MinusculeMatcher.MatchingState)} for it. */ @Nullable private FList<TextRange> matchWildcards( @NotNull String name, int patternIndex, int nameIndex, MatchingState matchingState) { if (nameIndex < 0) { return null; } if (!isWildcard(patternIndex)) { if (patternIndex == myPattern.length) { return FList.emptyList(); } return matchFragment(name, patternIndex, nameIndex, matchingState); } do { patternIndex++; } while (isWildcard(patternIndex)); if (patternIndex == myPattern.length) { boolean space = isPatternChar(patternIndex - 1, ' '); // the trailing space should match if the pattern ends with the last word part, or only its // first hump character if (space && nameIndex != name.length() && (patternIndex < 2 || !NameUtil.isWordStart(myPattern[patternIndex - 2]))) { int spaceIndex = name.indexOf(' ', nameIndex); if (spaceIndex >= 0) { return FList.<TextRange>emptyList().prepend(TextRange.from(spaceIndex, 1)); } return null; } return FList.emptyList(); } FList<TextRange> ranges = matchFragment(name, patternIndex, nameIndex, matchingState); if (ranges != null) { return ranges; } return matchSkippingWords(name, patternIndex, nameIndex, true, matchingState); }
@Override public String propertyNameToVariableName(String propertyName, VariableKind variableKind) { if (variableKind == VariableKind.STATIC_FINAL_FIELD) { String[] words = NameUtil.nameToWords(propertyName); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < words.length; i++) { String word = words[i]; if (i > 0) { buffer.append("_"); } buffer.append(StringUtil.toUpperCase(word)); } return buffer.toString(); } String prefix = getPrefixByVariableKind(variableKind); String name = propertyName; if (!name.isEmpty() && !prefix.isEmpty() && !StringUtil.endsWithChar(prefix, '_')) { name = Character.toUpperCase(name.charAt(0)) + name.substring(1); } name = prefix + name + getSuffixByVariableKind(variableKind); name = changeIfNotIdentifier(name); return name; }
private NamesByExprInfo suggestVariableNameByExpressionPlace( PsiExpression expr, final VariableKind variableKind, boolean correctKeywords) { if (expr.getParent() instanceof PsiExpressionList) { PsiExpressionList list = (PsiExpressionList) expr.getParent(); PsiElement listParent = list.getParent(); PsiSubstitutor subst = PsiSubstitutor.EMPTY; PsiMethod method = null; if (listParent instanceof PsiMethodCallExpression) { final JavaResolveResult resolveResult = ((PsiMethodCallExpression) listParent).getMethodExpression().advancedResolve(false); method = (PsiMethod) resolveResult.getElement(); subst = resolveResult.getSubstitutor(); } else { if (listParent instanceof PsiAnonymousClass) { listParent = listParent.getParent(); } if (listParent instanceof PsiNewExpression) { method = ((PsiNewExpression) listParent).resolveConstructor(); } } if (method != null) { final PsiElement navElement = method.getNavigationElement(); if (navElement instanceof PsiMethod) { method = (PsiMethod) navElement; } PsiExpression[] expressions = list.getExpressions(); int index = -1; for (int i = 0; i < expressions.length; i++) { if (expressions[i] == expr) { index = i; break; } } PsiParameter[] parameters = method.getParameterList().getParameters(); if (index < parameters.length) { String name = parameters[index].getName(); if (name != null && TypeConversionUtil.areTypesAssignmentCompatible( subst.substitute(parameters[index].getType()), expr)) { name = variableNameToPropertyName(name, VariableKind.PARAMETER); String[] names = getSuggestionsByName(name, variableKind, false, correctKeywords); if (expressions.length == 1) { final String methodName = method.getName(); String[] words = NameUtil.nameToWords(methodName); if (words.length > 0) { final String firstWord = words[0]; if (SET_PREFIX.equals(firstWord)) { final String propertyName = methodName.substring(firstWord.length()); final String[] setterNames = getSuggestionsByName(propertyName, variableKind, false, correctKeywords); names = ArrayUtil.mergeArrays(names, setterNames); } } } return new NamesByExprInfo(name, names); } } } } else if (expr.getParent() instanceof PsiAssignmentExpression && variableKind == VariableKind.PARAMETER) { final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) expr.getParent(); if (expr == assignmentExpression.getRExpression()) { final PsiExpression leftExpression = assignmentExpression.getLExpression(); if (leftExpression instanceof PsiReferenceExpression && ((PsiReferenceExpression) leftExpression).getQualifier() == null) { String name = leftExpression.getText(); if (name != null) { final PsiElement resolve = ((PsiReferenceExpression) leftExpression).resolve(); if (resolve instanceof PsiVariable) { name = variableNameToPropertyName(name, getVariableKind((PsiVariable) resolve)); } String[] names = getSuggestionsByName(name, variableKind, false, correctKeywords); return new NamesByExprInfo(name, names); } } } } return new NamesByExprInfo(null, ArrayUtil.EMPTY_STRING_ARRAY); }
private NamesByExprInfo suggestVariableNameByExpressionOnly( PsiExpression expr, final VariableKind variableKind, boolean correctKeywords, boolean useAllMethodNames) { if (expr instanceof PsiMethodCallExpression) { PsiReferenceExpression methodExpr = ((PsiMethodCallExpression) expr).getMethodExpression(); String methodName = methodExpr.getReferenceName(); if (methodName != null) { if ("of".equals(methodName) || "ofNullable".equals(methodName)) { if (isJavaUtilMethodCall((PsiMethodCallExpression) expr)) { PsiExpression[] expressions = ((PsiMethodCallExpression) expr).getArgumentList().getExpressions(); if (expressions.length > 0) { return suggestVariableNameByExpressionOnly( expressions[0], variableKind, correctKeywords, useAllMethodNames); } } } if ("map".equals(methodName) || "flatMap".equals(methodName) || "filter".equals(methodName)) { if (isJavaUtilMethodCall((PsiMethodCallExpression) expr)) { return new NamesByExprInfo(null); } } String[] words = NameUtil.nameToWords(methodName); if (words.length > 0) { final String firstWord = words[0]; if (GET_PREFIX.equals(firstWord) || IS_PREFIX.equals(firstWord) || FIND_PREFIX.equals(firstWord) || CREATE_PREFIX.equals(firstWord)) { if (words.length > 1) { final String propertyName = methodName.substring(firstWord.length()); String[] names = getSuggestionsByName(propertyName, variableKind, false, correctKeywords); final PsiExpression qualifierExpression = methodExpr.getQualifierExpression(); if (qualifierExpression instanceof PsiReferenceExpression && ((PsiReferenceExpression) qualifierExpression).resolve() instanceof PsiVariable) { names = ArrayUtil.append( names, StringUtil.sanitizeJavaIdentifier( changeIfNotIdentifier( qualifierExpression.getText() + StringUtil.capitalize(propertyName)))); } return new NamesByExprInfo(propertyName, names); } } else if (words.length == 1 || useAllMethodNames) { return new NamesByExprInfo( methodName, getSuggestionsByName(methodName, variableKind, false, correctKeywords)); } } } } else if (expr instanceof PsiReferenceExpression) { String propertyName = ((PsiReferenceExpression) expr).getReferenceName(); PsiElement refElement = ((PsiReferenceExpression) expr).resolve(); if (refElement instanceof PsiVariable) { VariableKind refVariableKind = getVariableKind((PsiVariable) refElement); propertyName = variableNameToPropertyName(propertyName, refVariableKind); } if (refElement != null && propertyName != null) { String[] names = getSuggestionsByName(propertyName, variableKind, false, correctKeywords); return new NamesByExprInfo(propertyName, names); } } else if (expr instanceof PsiArrayAccessExpression) { PsiExpression arrayExpr = ((PsiArrayAccessExpression) expr).getArrayExpression(); if (arrayExpr instanceof PsiReferenceExpression) { String arrayName = ((PsiReferenceExpression) arrayExpr).getReferenceName(); PsiElement refElement = ((PsiReferenceExpression) arrayExpr).resolve(); if (refElement instanceof PsiVariable) { VariableKind refVariableKind = getVariableKind((PsiVariable) refElement); arrayName = variableNameToPropertyName(arrayName, refVariableKind); } if (arrayName != null) { String name = StringUtil.unpluralize(arrayName); if (name != null) { String[] names = getSuggestionsByName(name, variableKind, false, correctKeywords); return new NamesByExprInfo(name, names); } } } } else if (expr instanceof PsiLiteralExpression && variableKind == VariableKind.STATIC_FINAL_FIELD) { final PsiLiteralExpression literalExpression = (PsiLiteralExpression) expr; final Object value = literalExpression.getValue(); if (value instanceof String) { final String stringValue = (String) value; String[] names = getSuggestionsByValue(stringValue); if (names.length > 0) { return new NamesByExprInfo(null, constantValueToConstantName(names)); } } } else if (expr instanceof PsiParenthesizedExpression) { return suggestVariableNameByExpressionOnly( ((PsiParenthesizedExpression) expr).getExpression(), variableKind, correctKeywords, useAllMethodNames); } else if (expr instanceof PsiTypeCastExpression) { return suggestVariableNameByExpressionOnly( ((PsiTypeCastExpression) expr).getOperand(), variableKind, correctKeywords, useAllMethodNames); } else if (expr instanceof PsiLiteralExpression) { final String text = StringUtil.stripQuotesAroundValue(expr.getText()); if (isIdentifier(text)) { return new NamesByExprInfo( text, getSuggestionsByName(text, variableKind, false, correctKeywords)); } } return new NamesByExprInfo(null, ArrayUtil.EMPTY_STRING_ARRAY); }
public int matchingDegree(@NotNull String name) { FList<TextRange> iterable = matchingFragments(name); if (iterable == null) return Integer.MIN_VALUE; if (iterable.isEmpty()) return 0; final TextRange first = iterable.getHead(); boolean startMatch = first.getStartOffset() == 0; int matchingCase = 0; int p = -1; int integral = 0; // -sum of matching-char-count * hump-index over all matched humps; favors longer // fragments matching earlier words int humpIndex = 1; int nextHumpStart = 0; for (TextRange range : iterable) { for (int i = range.getStartOffset(); i < range.getEndOffset(); i++) { boolean isHumpStart = false; while (nextHumpStart <= i) { if (nextHumpStart == i) { isHumpStart = true; } nextHumpStart = NameUtil.nextWord(name, nextHumpStart); if (first != range) { humpIndex++; } } integral -= humpIndex; char c = name.charAt(i); p = StringUtil.indexOf(myPattern, c, p + 1, myPattern.length, false); if (p < 0) { break; } if (c == myPattern[p]) { if (isUpperCase[p]) matchingCase += 50; // strongly prefer user's uppercase matching uppercase: they made an effort to // press Shift else if (i == 0 && startMatch) matchingCase += 15; // the very first letter case distinguishes classes in Java etc else if (isHumpStart) matchingCase += 1; // if a lowercase matches lowercase hump start, that also means something } else if (isHumpStart) { // disfavor hump starts where pattern letter case doesn't match name case matchingCase -= 20; } } } int startIndex = first.getStartOffset(); boolean afterSeparator = StringUtil.indexOfAny(name, HARD_SEPARATORS, 0, startIndex) >= 0; boolean wordStart = startIndex == 0 || isWordStart(name, startIndex) && !isWordStart(name, startIndex - 1); boolean finalMatch = iterable.get(iterable.size() - 1).getEndOffset() == name.length(); return (wordStart ? 1000 : 0) + integral * 10 + matchingCase * (startMatch ? 10 : 1) + // in start matches, case is more important; in middle matches - fragment length // (integral) (afterSeparator ? 0 : 2) + (finalMatch ? 1 : 0); }