📄 abstractastmanager.java
字号:
/**
* @see org.python.pydev.core.ICodeCompletionASTManager#getCompletionsForToken(java.io.File, org.eclipse.jface.text.IDocument, org.python.pydev.editor.codecompletion.revisited.CompletionState)
*/
public IToken[] getCompletionsForToken(File file, IDocument doc, ICompletionState state) throws CompletionRecursionException {
IModule module = createModule(file, doc, state, this);
return getCompletionsForModule(module, state, true, true);
}
/**
* @see org.python.pydev.editor.codecompletion.revisited.ICodeCompletionASTManage#getCompletionsForToken(org.eclipse.jface.text.IDocument, org.python.pydev.editor.codecompletion.revisited.CompletionState)
*/
public IToken[] getCompletionsForToken(IDocument doc, ICompletionState state) {
IToken[] completionsForModule;
try {
Tuple<SimpleNode, Throwable> obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, true, state.getNature(), state.getLine()));
SimpleNode n = obj.o1;
IModule module = AbstractModule.createModule(n);
completionsForModule = getCompletionsForModule(module, state, true, true);
} catch (CompletionRecursionException e) {
completionsForModule = new IToken[]{ new ConcreteToken(e.getMessage(), e.getMessage(), "","", IToken.TYPE_UNKNOWN)};
}
return completionsForModule;
}
/**
* By default does not look for relative import
*/
public IModule getModule(String name, IPythonNature nature, boolean dontSearchInit) {
return modulesManager.getModule(name, nature, dontSearchInit, false);
}
/**
* This method returns the module that corresponds to the path passed as a parameter.
*
* @param name the name of the module we're looking for
* @param lookingForRelative determines whether we're looking for a relative module (in which case we should
* not check in other places... only in the module)
* @return the module represented by this name
*/
public IModule getModule(String name, IPythonNature nature, boolean dontSearchInit, boolean lookingForRelative) {
if(lookingForRelative){
return modulesManager.getRelativeModule(name, nature);
}else{
return modulesManager.getModule(name, nature, dontSearchInit);
}
}
/**
* Identifies the token passed and if it maps to a builtin not 'easily recognizable', as
* a string or list, we return it.
*
* @param state
* @return
*/
protected IToken[] getBuiltinsCompletions(ICompletionState state){
ICompletionState state2 = state.getCopy();
String act = state.getActivationToken();
//check for the builtin types.
state2.setActivationToken (NodeUtils.getBuiltinType(act));
if(state2.getActivationToken() != null){
IModule m = getBuiltinMod(state.getNature());
return m.getGlobalTokens(state2, this);
}
if(act.equals("__builtins__") || act.startsWith("__builtins__.")){
act = act.substring(12);
if(act.startsWith(".")){
act = act.substring(1);
}
IModule m = getBuiltinMod(state.getNature());
ICompletionState state3 = state.getCopy();
state3.setActivationToken(act);
return m.getGlobalTokens(state3, this);
}
return null;
}
/**
* @throws CompletionRecursionException
* @see org.python.pydev.editor.codecompletion.revisited.ICodeCompletionASTManage#getCompletionsForModule(org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule, org.python.pydev.editor.codecompletion.revisited.CompletionState)
*/
public IToken[] getCompletionsForModule(IModule module, ICompletionState state) throws CompletionRecursionException {
return getCompletionsForModule(module, state, true);
}
/**
* @throws CompletionRecursionException
* @see org.python.pydev.editor.codecompletion.revisited.ICodeCompletionASTManage#getCompletionsForModule(org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule, org.python.pydev.editor.codecompletion.revisited.CompletionState, boolean)
*/
public IToken[] getCompletionsForModule(IModule module, ICompletionState state, boolean searchSameLevelMods) throws CompletionRecursionException {
return getCompletionsForModule(module, state, true, false);
}
/**
* @see org.python.pydev.editor.codecompletion.revisited.ICodeCompletionASTManage#getCompletionsForModule(org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule, org.python.pydev.editor.codecompletion.revisited.CompletionState, boolean, boolean)
*/
public IToken[] getCompletionsForModule(IModule module, ICompletionState state, boolean searchSameLevelMods, boolean lookForArgumentCompletion) throws CompletionRecursionException{
if(PyCodeCompletion.DEBUG_CODE_COMPLETION){
Log.toLogFile(this, "getCompletionsForModule");
}
ArrayList<IToken> importedModules = new ArrayList<IToken>();
ILocalScope localScope = null;
int line = state.getLine();
int col = state.getCol();
if(state.getLocalImportsGotten() == false){
//in the first analyzed module, we have to get the local imports too.
state.setLocalImportsGotten (true);
if(module != null && line >= 0){
localScope = module.getLocalScope(line, col);
if(localScope != null){
importedModules.addAll(localScope.getLocalImportedModules(line+1, col+1, module.getName()));
}
}
}
IToken[] builtinsCompletions = getBuiltinsCompletions(state);
if(builtinsCompletions != null){
return builtinsCompletions;
}
String act = state.getActivationToken();
int parI = act.indexOf('(');
if(parI != -1){
act = act.substring(0, parI);
state.setActivationToken(act);
state.setLookingFor(ICompletionState.LOOKING_FOR_INSTANCED_VARIABLE);
}
if (module != null) {
//get the tokens (global, imported and wild imported)
IToken[] globalTokens = module.getGlobalTokens();
List<IToken> tokenImportedModules = Arrays.asList(module.getTokenImportedModules());
importedModules.addAll(tokenImportedModules);
state.setTokenImportedModules(importedModules);
IToken[] wildImportedModules = module.getWildImportedModules();
//now, lets check if this is actually a module that is an __init__ (if so, we have to get all
//the other .py files as modules that are in the same level as the __init__)
Set<IToken> initial = new HashSet<IToken>();
if(searchSameLevelMods){
String modName = module.getName();
if(modName != null && modName.endsWith(".__init__")){
HashSet<IToken> gotten = new HashSet<IToken>();
getAbsoluteImportTokens(FullRepIterable.getParentModule(modName), gotten, IToken.TYPE_IMPORT, true);
for (IToken token : gotten) {
if(token.getRepresentation().equals("__init__") == false){
initial.add(token);
}
}
}
}
if (state.getActivationToken().length() == 0) {
List<IToken> completions = getGlobalCompletions(globalTokens, importedModules.toArray(EMPTY_ITOKEN_ARRAY), wildImportedModules, state, module);
//now find the locals for the module
if (line >= 0){
IToken[] localTokens = module.getLocalTokens(line, col, localScope);
for (int i = 0; i < localTokens.length; i++) {
completions.add(localTokens[i]);
}
}
completions.addAll(initial); //just addd all that are in the same level if it was an __init__
return completions.toArray(EMPTY_ITOKEN_ARRAY);
}else{ //ok, we have a token, find it and get its completions.
//first check if the token is a module... if it is, get the completions for that module.
IToken[] tokens = findTokensOnImportedMods(importedModules.toArray(EMPTY_ITOKEN_ARRAY), state, module);
if(tokens != null && tokens.length > 0){
return tokens;
}
//if it is an __init__, modules on the same level are treated as local tokens
if(searchSameLevelMods){
tokens = searchOnSameLevelMods(initial, state);
if(tokens != null && tokens.length > 0){
return tokens;
}
}
//wild imports: recursively go and get those completions and see if any matches it.
for (int i = 0; i < wildImportedModules.length; i++) {
IToken name = wildImportedModules[i];
IModule mod = getModule(name.getAsRelativeImport(module.getName()), state.getNature(), false); //relative (for wild imports this is ok... only a module can be used in wild imports)
if (mod == null) {
mod = getModule(name.getOriginalRep(), state.getNature(), false); //absolute
}
if (mod != null) {
state.checkFindModuleCompletionsMemory(mod, state.getActivationToken());
IToken[] completionsForModule = getCompletionsForModule(mod, state);
if(completionsForModule.length > 0)
return completionsForModule;
} else {
//"Module not found:" + name.getRepresentation()
}
}
//it was not a module (would have returned already), so, try to get the completions for a global token defined.
tokens = module.getGlobalTokens(state, this);
if (tokens.length > 0){
return tokens;
}
//If it was still not found, go to builtins.
IModule builtinsMod = getBuiltinMod(state.getNature());
if(builtinsMod != null && builtinsMod != module){
tokens = getCompletionsForModule( builtinsMod, state);
if (tokens.length > 0){
if (tokens[0].getRepresentation().equals("ERROR:") == false){
return tokens;
}
}
}
if(lookForArgumentCompletion && localScope != null){
//now, if we have to look for arguments and search things in the local scope, let's also
//check for assert (isinstance...) in this scope with the given variable.
{
List<String> lookForClass = localScope.getPossibleClassesForActivationToken(state.getActivationToken());
if(lookForClass.size() > 0){
HashSet<IToken> hashSet = new HashSet<IToken>();
getCompletionsForClassInLocalScope(module, state, searchSameLevelMods,
lookForArgumentCompletion, lookForClass, hashSet);
if(hashSet.size() > 0){
return hashSet.toArray(EMPTY_ITOKEN_ARRAY);
}
}
}
//ok, didn't find in assert isinstance... keep going
//if there was no assert for the class, get from extensions / local scope interface
tokens = getArgsCompletion(state, localScope);
if(tokens != null && tokens.length > 0){
return tokens;
}
}
//nothing worked so far, so, let's look for an assignment...
return getAssignAnalysis().getAssignCompletions(this, module, state).toArray(EMPTY_ITOKEN_ARRAY);
}
}else{
System.err.println("Module passed in is null!!");
}
return EMPTY_ITOKEN_ARRAY;
}
/**
* @see ICodeCompletionASTManager#getCompletionsForClassInLocalScope(IModule, ICompletionState, boolean, boolean, List, HashSet)
*/
public void getCompletionsForClassInLocalScope(IModule module, ICompletionState state, boolean searchSameLevelMods,
boolean lookForArgumentCompletion, List<String> lookForClass, HashSet<IToken> hashSet) throws CompletionRecursionException {
IToken[] tokens;
//if found here, it's an instanced variable (force it and restore if we didn't find it here...)
ICompletionState stateCopy = state.getCopy();
int prevLookingFor = stateCopy.getLookingFor();
//force looking for instance
stateCopy.setLookingFor(ICompletionState.LOOKING_FOR_INSTANCED_VARIABLE, true);
for(String classFound: lookForClass){
stateCopy.setLocalImportsGotten(false);
stateCopy.setActivationToken(classFound);
//same thing as the initial request, but with the class we could find...
tokens = getCompletionsForModule(module, stateCopy, searchSameLevelMods, lookForArgumentCompletion);
if(tokens != null){
for(IToken tok:tokens){
hashSet.add(tok);
}
}
}
if(hashSet.size() == 0){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -