moduleadapter.java
来自「Python Development Environment (Python I」· Java 代码 · 共 621 行 · 第 1/2 页
JAVA
621 行
return bases;
}
public IClassDefAdapter resolveClass(String name) {
for (IClassDefAdapter classAdapter : getClasses()) {
if (classAdapter.getName().compareTo(name) == 0) {
return classAdapter;
} else if (name.contains(".")) {
String fileName = file.getName().substring(0, file.getName().indexOf("."));
if (name.startsWith(fileName + ".")) {
name = name.substring(name.indexOf(".") + 1);
}
if (name.endsWith("." + classAdapter.getName())) {
return classAdapter;
}
IClassDefAdapter current = classAdapter;
String fullNestedName = classAdapter.getName();
while (nodeHelper.isClassDef(current.getParent().getASTNode())) {
current = (IClassDefAdapter) current.getParent();
fullNestedName = current.getName() + "." + fullNestedName;
}
if (fullNestedName.compareTo(name) == 0)
return classAdapter;
}
}
return null;
}
/**
* Will resolve module and real identifier if an alias is used. The returned import may be a relative one...
*
* @param aliasName
* Identifier/Token (e.g. foo.classname)
* @return Array consisting of module and real identifier
*/
public List<FQIdentifier> resolveFullyQualified(String aliasName) {
List<FQIdentifier> qualifiedIdentifiers = new ArrayList<FQIdentifier>();
String FQPrefix = "";
String aliasIdentifier = "";
int longestMatch = 0;
for (String module : getRegularImportedModules().keySet()) {
if (aliasName.startsWith(module)) {
if (module.length() > longestMatch) {
FQPrefix = getRegularImportedModules().get(module);
longestMatch = module.length();
}
}
}
if (longestMatch > 0) {
if (aliasName.length() > longestMatch)
aliasIdentifier = aliasName.substring(longestMatch + 1);
qualifiedIdentifiers.add(new FQIdentifier(FQPrefix, aliasIdentifier, aliasIdentifier));
return qualifiedIdentifiers;
}
for (FQIdentifier identifier : getAliasToIdentifier()) {
if (aliasName.startsWith(identifier.getAlias())) {
String attribute = aliasName.substring(identifier.getAlias().length());
FQIdentifier id = new FQIdentifier(identifier.getModule(), identifier.getRealName() + attribute, identifier.getAlias()
+ attribute);
qualifiedIdentifiers.add(id);
return qualifiedIdentifiers;
}
}
for (String moduleAlias : getRegularImportedModules().keySet()) {
qualifiedIdentifiers.add(new FQIdentifier(getRegularImportedModules().get(moduleAlias), aliasName, aliasName));
}
return qualifiedIdentifiers;
}
/**
* This method fills the bases list (out) with asts for the methods that can be overriden.
*
* Still, compiled modules will not have an actual ast, but a list of tokens (that should be used
* to know what should be overriden), so, this method should actually be changed so that
* it works with tokens (that are resolved when a completion is requested), so, if we request a completion
* for each base class, all the tokens from it will be returned, what's missing in this approach is that currently
* the tokens returned don't have an associated context, so, after getting them, it may be hard to actually
* tell the whole class structure above it (but this can be considered secondary for now).
*/
private Set<IClassDefAdapter> resolveImportedClass(Set<String> importedBase) {
Set<IClassDefAdapter> bases = new HashSet<IClassDefAdapter>();
if (moduleManager == null)
return bases;
for (String baseName : importedBase) {
ICompletionState state = new CompletionState(-1,-1,baseName,nature,"");
IToken[] ret = null;
try {
ret = nature.getAstManager().getCompletionsForToken(file, doc, state);
} catch (CompletionRecursionException e) {
throw new RuntimeException(e);
}
Map<String, List<IToken>> map = new HashMap<String, List<IToken>>();
Set<ClassDef> classDefAsts = new HashSet<ClassDef>();
for (IToken tok: ret) {
if(tok instanceof SourceToken){
SourceToken token = (SourceToken) tok;
SimpleNode ast = token.getAst();
if(ast instanceof ClassDef || ast instanceof FunctionDef){
if(ast.parent instanceof ClassDef){
classDefAsts.add((ClassDef) ast.parent);
}
}
}else if(tok instanceof CompiledToken){
CompiledToken token = (CompiledToken) tok;
List<IToken> toks = map.get(token.getParentPackage());
if(toks == null){
toks = new ArrayList<IToken>();
map.put(token.getParentPackage(), toks);
}
toks.add(token);
}else{
throw new RuntimeException("Unexpected token:"+tok.getClass());
}
}
for(Map.Entry<String, List<IToken>> entry:map.entrySet()){
bases.add(new ClassDefAdapterFromTokens(entry.getKey(), entry.getValue(), getEndLineDelimiter()));
}
for(ClassDef classDef:classDefAsts){
bases.add(new ClassDefAdapterFromClassDef(classDef, getEndLineDelimiter()));
}
}
return bases;
}
public void setStrategy(IASTNodeAdapter adapter, int strategy) {
switch (strategy) {
case IOffsetStrategy.AFTERINIT:
this.offsetStrategy = new InitOffset(adapter, this.doc);
break;
case IOffsetStrategy.BEGIN:
this.offsetStrategy = new BeginOffset(adapter, this.doc);
break;
case IOffsetStrategy.END:
this.offsetStrategy = new EndOffset(adapter, this.doc);
break;
default:
this.offsetStrategy = new BeginOffset(adapter, this.doc);
}
}
public AbstractScopeNode<?> getScopeAdapter(ITextSelection selection) {
AbstractScopeNode<?> bestScopeNode = null;
bestScopeNode = getScopeFunction(selection);
if (bestScopeNode == null) {
bestScopeNode = (AbstractScopeNode<?>) getScopeClass(selection);
}
if (bestScopeNode == null) {
bestScopeNode = this;
}
return bestScopeNode;
}
private AbstractScopeNode<?> getScopeFunction(ITextSelection selection) {
AbstractScopeNode<?> scopeAdapter = null;
Iterator<FunctionDefAdapter> iter = getFunctions().iterator();
while (iter.hasNext()) {
FunctionDefAdapter functionScope = iter.next();
if (isSelectionInAdapter(selection, functionScope))
scopeAdapter = functionScope;
if (functionScope.getNodeFirstLine() > selection.getEndLine())
break;
}
return scopeAdapter;
}
@Override
public int getNodeBodyIndent() {
return 0;
}
@Override
public int getNodeIndent() {
return 0;
}
public List<SimpleAdapter> getWithinSelection(ITextSelection selection, List<SimpleAdapter> variables) {
List<SimpleAdapter> withinOffsetAdapters = new ArrayList<SimpleAdapter>();
for (SimpleAdapter adapter : variables) {
if (isAdapterInSelection(selection, adapter)) {
withinOffsetAdapters.add(adapter);
}
}
return withinOffsetAdapters;
}
public ITextSelection extendSelection(ITextSelection selection, SimpleNode nodeStart, SimpleNode nodeEnd) {
if (this.doc != null) {
try {
int startOffset = getStartOffset(nodeStart);
int endOffset = getStartOffset(nodeEnd) - 1;
if (startOffset > selection.getOffset()) {
startOffset = selection.getOffset();
}
if (endOffset < selection.getOffset() + selection.getLength()) {
endOffset = selection.getOffset() + selection.getLength();
}
selection = new TextSelection(doc, startOffset, endOffset - startOffset);
} catch (BadLocationException e) {
}
}
return normalizeSelection(selection);
}
public ITextSelection normalizeSelection(ITextSelection userSelection) {
String txt = userSelection.getText();
while (txt != null && (txt.startsWith(" ") || txt.startsWith("\n") || txt.startsWith("\r"))) {
userSelection = new TextSelection(this.doc, userSelection.getOffset() + 1, userSelection.getLength() - 1);
txt = userSelection.getText();
}
while (txt != null && (txt.endsWith(" ") || txt.endsWith("\n") || txt.endsWith("\r"))) {
userSelection = new TextSelection(this.doc, userSelection.getOffset(), userSelection.getLength() - 1);
txt = userSelection.getText();
}
return userSelection;
}
public ITextSelection extendSelectionToEnd(ITextSelection selection, SimpleNode node) {
if (this.doc != null) {
SimpleAdapter adapter = new SimpleAdapter(this, this, node, getEndLineDelimiter());
int lastLine = adapter.getNodeLastLine() - 1;
try {
int adapterEndOffset = doc.getLineOffset(lastLine);
adapterEndOffset += doc.getLineLength(lastLine);
selection = new TextSelection(doc, selection.getOffset(), adapterEndOffset - selection.getOffset());
} catch (BadLocationException e) {
}
}
return selection;
}
public ITextSelection extendSelection(ITextSelection selection, SimpleNode node) {
if (this.doc != null && (node instanceof Str)) {
SimpleAdapter adapter = new SimpleAdapter(this, this, node, getEndLineDelimiter());
try {
int startOffset = getStartOffset(adapter);
if (startOffset > selection.getOffset()) {
startOffset = selection.getOffset();
}
int endOffset = startOffset + adapter.getName().length() + 2;
if (endOffset < selection.getOffset() + selection.getLength()) {
endOffset = selection.getOffset() + selection.getLength();
}
selection = new TextSelection(doc, startOffset, endOffset - startOffset);
} catch (BadLocationException e) {
}
}
return selection;
}
@Override
public int getNodeFirstLine() {
int i = 0;
while (i < getASTNode().body.length) {
SimpleNode node = this.getASTNode().body[i];
if (!nodeHelper.isImport(node))
return node.beginLine;
i += 1;
}
return 1;
}
public boolean isImport(String name) {
for (String module : getRegularImportedModules().keySet()) {
if (module.compareTo(name) == 0)
return true;
}
for (FQIdentifier fq : getAliasToIdentifier()) {
if (fq.getAlias().compareTo(name) == 0)
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?