import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
private void createGetPropProposals(String code, Node inspectedNode, Map> proposals, ArrayList internalVars, ArrayList externalVars) {
Node firstChild = inspectedNode.getFirstChild();
Optional firstChildType = Optional.ofNullable(firstChild.getJSType());
if (firstChildType.isPresent()) {
JSType t = fixNullType(firstChildType.get());
if (t instanceof ObjectType) {
ObjectType ot = ObjectType.cast(t);
ArrayList internalProperties = new ArrayList<>();
ArrayList externalProperties = new ArrayList<>();
Comparator sortProps = (s1,s2)->{
Optional propertyType1 = Optional.ofNullable(ot.getPropertyType(s1));
Optional propertyType2 = Optional.ofNullable(ot.getPropertyType(s2));
if (propertyType1.isPresent()) {
if (!propertyType2.isPresent())
return -1;
return compareJsType(propertyType1.get(),propertyType2.get());
}
if (propertyType2.isPresent())
return 1;
return 0;
};
ot.getPropertyNames().stream().filter(n -> !n.startsWith("__"))
.forEach(n -> {
if (isIntern(ot.getPropertyDefSite(n))) {
internalProperties.add(n);
} else {
externalProperties.add(n);
}
});
internalProperties.sort(sortProps);
externalProperties.sort(sortProps);
UnaryOperator addFunctionBrackets = s -> {
Optional propertyType = Optional.ofNullable(ot.getPropertyType(s));
boolean isFunction = propertyType.map(pt -> pt instanceof FunctionType).orElse(false);
return isFunction ? s + "()" : s;
};
internalProperties.replaceAll(addFunctionBrackets);
externalProperties.replaceAll(addFunctionBrackets);
List ret = new ArrayList<>();
int sourcePosition = firstChild.getSourceOffset();
if (sourcePosition > -1) {
sourcePosition += firstChild.getLength();
while (sourcePosition < code.length() &&
(Character.isWhitespace(code.charAt(sourcePosition)))) {
++sourcePosition;
}
if (sourcePosition < code.length() && '.' == code.charAt(sourcePosition))
++sourcePosition;
}
int sp = sourcePosition;
if (sp > -1) {
if (ot instanceof FunctionType) {
internalVars.stream().filter(v -> v.getNameNode().getSourceOffset() < sp).map(v -> new Proposal(v.getName())).forEach(ret::add);
internalProperties.stream().map(s -> new Proposal(s)).forEach(ret::add);
externalVars.stream().map(v -> new Proposal(v.getName())).forEach(ret::add);
externalProperties.stream().map(s -> new Proposal(s)).forEach(ret::add);
} else {
internalProperties.stream().map(s -> new Proposal(s)).forEach(ret::add);
externalProperties.stream().map(s -> new Proposal(s)).forEach(ret::add);
}
proposals.put(sourcePosition, ret);
}
}
}
}