📄 abstractastmanager.java
字号:
//force looking for what was set before...
stateCopy.setLookingFor(prevLookingFor, true);
}
}
/**
* Get the completions based on the arguments received
*
* @param state this is the state used for the completion
* @param localScope this is the scope we're currently on (may be null)
*/
@SuppressWarnings("unchecked")
private IToken[] getArgsCompletion(ICompletionState state, ILocalScope localScope) {
IToken[] args = localScope.getLocalTokens(-1,-1,true); //only to get the args
String activationToken = state.getActivationToken();
String firstPart = FullRepIterable.getFirstPart(activationToken);
for (IToken token : args) {
if(token.getRepresentation().equals(firstPart)){
Collection<IToken> interfaceForLocal = localScope.getInterfaceForLocal(state.getActivationToken());
Collection argsCompletionFromParticipants = getArgsCompletionFromParticipants(state, localScope, interfaceForLocal);
for (IToken t : interfaceForLocal) {
if(!t.getRepresentation().equals(state.getQualifier())){
argsCompletionFromParticipants.add(t);
}
}
return (IToken[]) argsCompletionFromParticipants.toArray(EMPTY_ITOKEN_ARRAY);
}
}
return null;
}
@SuppressWarnings("unchecked")
private Collection getArgsCompletionFromParticipants(ICompletionState state, ILocalScope localScope, Collection<IToken> interfaceForLocal) {
ArrayList ret = new ArrayList();
List participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_COMPLETION);
for (Iterator iter = participants.iterator(); iter.hasNext();) {
IPyDevCompletionParticipant participant = (IPyDevCompletionParticipant) iter.next();
ret.addAll(participant.getArgsCompletion(state, localScope, interfaceForLocal));
}
return ret;
}
/**
* Attempt to search on modules on the same level as this one (this will only happen if we are in an __init__
* module (otherwise, the initial set will be empty)
*
* @param initial this is the set of tokens generated from modules in the same level
* @param state the current state of the completion
*
* @return a list of tokens found.
* @throws CompletionRecursionException
*/
protected IToken[] searchOnSameLevelMods(Set<IToken> initial, ICompletionState state) throws CompletionRecursionException {
for (IToken token : initial) {
//ok, maybe it was from the set that is in the same level as this one (this will only happen if we are on an __init__ module)
String rep = token.getRepresentation();
if(state.getActivationToken().startsWith(rep)){
String absoluteImport = token.getAsAbsoluteImport();
IModule sameLevelMod = getModule(absoluteImport, state.getNature(), true);
if(sameLevelMod == null){
return null;
}
String qualifier = state.getActivationToken().substring(rep.length());
ICompletionState copy = state.getCopy();
copy.setBuiltinsGotten (true); //we don't want builtins...
if(state.getActivationToken().equals(rep)){
copy.setActivationToken ("");
return getCompletionsForModule(sameLevelMod, copy);
} else if(qualifier.startsWith(".")){
copy.setActivationToken (qualifier.substring(1));
return getCompletionsForModule(sameLevelMod, copy);
}
}
}
return null;
}
/**
* @see ICodeCompletionASTManager#getGlobalCompletions
*/
public List<IToken> getGlobalCompletions(IToken[] globalTokens, IToken[] importedModules, IToken[] wildImportedModules, ICompletionState state, IModule current) {
if(PyCodeCompletion.DEBUG_CODE_COMPLETION){
Log.toLogFile(this, "getGlobalCompletions");
}
List<IToken> completions = new ArrayList<IToken>();
//in completion with nothing, just go for what is imported and global tokens.
for (int i = 0; i < globalTokens.length; i++) {
completions.add(globalTokens[i]);
}
//now go for the token imports
for (int i = 0; i < importedModules.length; i++) {
completions.add(importedModules[i]);
}
//wild imports: recursively go and get those completions.
for (int i = 0; i < wildImportedModules.length; i++) {
IToken name = wildImportedModules[i];
getCompletionsForWildImport(state, current, completions, name);
}
if(!state.getBuiltinsGotten()){
state.setBuiltinsGotten (true) ;
if(PyCodeCompletion.DEBUG_CODE_COMPLETION){
Log.toLogFile(this, "getBuiltinCompletions");
}
//last thing: get completions from module __builtin__
getBuiltinCompletions(state, completions);
if(PyCodeCompletion.DEBUG_CODE_COMPLETION){
Log.toLogFile(this, "END getBuiltinCompletions");
}
}
return completions;
}
/**
* @return the builtin completions
*/
public List<IToken> getBuiltinCompletions(ICompletionState state, List<IToken> completions) {
IPythonNature nature = state.getNature();
IToken[] builtinCompletions = getBuiltinComps(nature);
if(builtinCompletions != null){
for (int i = 0; i < builtinCompletions.length; i++) {
completions.add(builtinCompletions[i]);
}
}
return completions;
}
/**
* @return the tokens in the builtins
*/
protected IToken[] getBuiltinComps(IPythonNature nature) {
IToken[] builtinCompletions = nature.getBuiltinCompletions();
if(builtinCompletions == null || builtinCompletions.length == 0){
IModule builtMod = getBuiltinMod(nature);
if(builtMod != null){
builtinCompletions = builtMod.getGlobalTokens();
nature.setBuiltinCompletions(builtinCompletions);
}
}
return builtinCompletions;
}
/**
* TODO: WHEN CLEARING CACHE, CLEAR THE BUILTIN REF TOO
* @return the module that represents the builtins
*/
protected IModule getBuiltinMod(IPythonNature nature) {
IModule mod = nature.getBuiltinMod();
if(mod == null){
mod = getModule("__builtin__", nature, false);
nature.setBuiltinMod(mod);
}
return mod;
}
/**
* Resolves a token defined with 'from module import something' statement
* to a proper type, as defined in module.
* @param imported the token to resolve.
* @return the resolved token or the original token in case no additional information could be obtained.
* @throws CompletionRecursionException
*/
public IToken resolveImport(ICompletionState state, IToken imported) throws CompletionRecursionException {
String curModName = imported.getParentPackage();
Tuple3<IModule, String, IToken> modTok = findOnImportedMods(new IToken[]{imported}, state.getCopyWithActTok(imported.getRepresentation()), curModName);
if(modTok != null && modTok.o1 != null){
if(modTok.o2.length() == 0){
return imported; //it's a module actually, so, no problems...
} else{
try{
state.checkResolveImportMemory(modTok.o1, modTok.o2);
}catch(CompletionRecursionException e){
return imported;
}
IToken repInModule = getRepInModule(modTok.o1, modTok.o2, state.getNature(), state);
if(repInModule != null){
return repInModule;
}
}
}
return imported;
}
/**
* This is the public interface
* @throws CompletionRecursionException
* @see org.python.pydev.core.ICodeCompletionASTManager#getRepInModule(org.python.pydev.core.IModule, java.lang.String, org.python.pydev.core.IPythonNature)
*/
public IToken getRepInModule(IModule module, String tokName, IPythonNature nature) throws CompletionRecursionException {
return getRepInModule(module, tokName, nature, null);
}
/**
* Get the actual token representing the tokName in the passed module
* @param module the module where we're looking
* @param tokName the name of the token we're looking for
* @param nature the nature we're looking for
* @return the actual token in the module (or null if it was not possible to find it).
* @throws CompletionRecursionException
*/
private IToken getRepInModule(IModule module, String tokName, IPythonNature nature, ICompletionState state) throws CompletionRecursionException {
if(module != null){
if(tokName.startsWith(".")){
tokName = tokName.substring(1);
}
//ok, we are getting some token from the module... let's see if it is really available.
String[] headAndTail = FullRepIterable.headAndTail(tokName);
String actToken = headAndTail[0]; //tail (if os.path, it is os)
String hasToBeFound = headAndTail[1]; //head (it is path)
//if it was os.path:
//initial would be os.path
//foundAs would be os
//actToken would be path
//now, what we will do is try to do a code completion in os and see if path is found
if(state == null){
state = CompletionStateFactory.getEmptyCompletionState(actToken, nature);
}else{
state = state.getCopy();
state.setActivationToken(actToken);
}
IToken[] completionsForModule = getCompletionsForModule(module, state);
for (IToken foundTok : completionsForModule) {
if(foundTok.getRepresentation().equals(hasToBeFound)){
return foundTok;
}
}
}
return null;
}
/* (non-Javadoc)
* @see ICodeCompletionASTManager#getCompletionsForWildImport(ICompletionState, IModule, List, IToken)
*/
@SuppressWarnings("unchecked")
public List<IToken> getCompletionsForWildImport(ICompletionState state, IModule current, List completions, IToken name) {
try {
//this one is an exception... even though we are getting the name as a relative import, we say it
//is not because we want to get the module considering __init__
IModule mod = null;
if(current != null){
//we cannot get the relative path if we don't have a current module
mod = getModule(name.getAsRelativeImport(current.getName()), state.getNature(), false);
}
if (mod == null) {
mod = getModule(name.getOriginalRep(), state.getNature(), false); //absolute import
}
if (mod != null) {
state.checkWildImportInMemory(current, mod);
IToken[] completionsForModule = getCompletionsForModule(mod, state);
for (int j = 0; j < completionsForModule.length; j++) {
completions.add(completionsForModule[j]);
}
} else {
//"Module not found:" + name.getRepresentation()
}
} catch (CompletionRecursionException e) {
//probably found a recursion... let's return the tokens we have so far
}
return completions;
}
public IToken[] findTokensOnImportedMods( IToken[] importedModules, ICompletionState state, IModule current) throws CompletionRecursionException {
Tuple3<IModule, String, IToken> o = findOnImportedMods(importedModules, state, current.getName());
if(o == null)
return null;
IModule mod = o.o1;
String tok = o.o2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -