use of org.python.pydev.core.IToken in project Pydev by fabioz.
the class CtxParticipant method getCompletionsForMethodParameter.
/**
* IPyDevCompletionParticipant
* @throws CompletionRecursionException
*/
@Override
public TokensList getCompletionsForMethodParameter(ICompletionState state, ILocalScope localScope, TokensList interfaceForLocal) throws CompletionRecursionException {
ArrayList<IToken> ret = new ArrayList<IToken>();
String qual = state.getQualifier();
String activationToken = state.getActivationToken();
FastStack<ISimpleNode> scopeStack = localScope.getScopeStack();
if (!scopeStack.empty()) {
Object peek = scopeStack.peek();
if (peek instanceof FunctionDef) {
FunctionDef testFuncDef = (FunctionDef) peek;
String representationString = NodeUtils.getRepresentationString(testFuncDef);
if (representationString != null && representationString.startsWith("test")) {
ICodeCompletionASTManager astManager = state.getNature().getAstManager();
if (astManager != null) {
ItemPointer itemPointer = findItemPointerFromPyTestFixture(state.getNature(), state, activationToken);
if (itemPointer != null) {
TokensList completionsFromItemPointer = getCompletionsFromItemPointer(state, astManager, itemPointer);
if (completionsFromItemPointer != null && completionsFromItemPointer.notEmpty()) {
return completionsFromItemPointer;
}
}
}
}
}
}
if (qual.length() >= PyCodeCompletionPreferences.getCharsForContextInsensitiveGlobalTokensCompletion()) {
// at least n characters
// if we have a parameter, do code-completion with all available tokens, since we don't know what's the type which
// may actually be received
boolean useSubstringMatchInCodeCompletion = PyCodeCompletionPreferences.getUseSubstringMatchInCodeCompletion();
List<IInfo> tokensStartingWith;
if (useSubstringMatchInCodeCompletion) {
IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(useSubstringMatchInCodeCompletion, qual);
try {
tokensStartingWith = AdditionalProjectInterpreterInfo.getTokensStartingWith("", state.getNature(), AbstractAdditionalTokensInfo.INNER);
} catch (MisconfigurationException e) {
Log.log(e);
return new TokensList(ret);
}
for (IInfo info : tokensStartingWith) {
if (nameFilter.acceptName(info.getName())) {
ret.add(new SourceToken(null, info.getName(), null, null, info.getDeclaringModuleName(), info.getType(), info.getNature()));
}
}
} else {
try {
tokensStartingWith = AdditionalProjectInterpreterInfo.getTokensStartingWith(qual, state.getNature(), AbstractAdditionalTokensInfo.INNER);
} catch (MisconfigurationException e) {
Log.log(e);
return new TokensList(ret);
}
for (IInfo info : tokensStartingWith) {
ret.add(new SourceToken(null, info.getName(), null, null, info.getDeclaringModuleName(), info.getType(), info.getNature()));
}
}
}
return new TokensList(ret);
}
use of org.python.pydev.core.IToken in project Pydev by fabioz.
the class ImportsCompletionParticipant method getImportedNames.
private HashSet<String> getImportedNames(ICompletionState state) {
TokensList tokenImportedModules = state.getTokenImportedModules();
HashSet<String> importedNames = new HashSet<String>();
if (tokenImportedModules != null) {
for (IterTokenEntry entry : tokenImportedModules) {
IToken token = entry.getToken();
importedNames.add(token.getRepresentation());
}
}
return importedNames;
}
use of org.python.pydev.core.IToken in project Pydev by fabioz.
the class AbstractASTManager method internalGenerateGetCompletionsForModule.
/**
* This method should only be accessed from the public getCompletionsForModule (which caches the result).
*/
private TokensList internalGenerateGetCompletionsForModule(IModule module, ICompletionState state, boolean searchSameLevelMods, boolean lookForArgumentCompletion) throws CompletionRecursionException {
if (DebugSettings.DEBUG_CODE_COMPLETION) {
log("internalGenerateGetCompletionsForModule", module, state);
}
state.checkMaxTimeForCompletion();
TokensList importedModules = new TokensList();
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()));
}
}
}
TokensList builtinsCompletions = getBuiltinsCompletions(state);
if (builtinsCompletions != null) {
return builtinsCompletions;
}
String act = state.getActivationToken();
int parI = act.indexOf('(');
if (parI != -1) {
state.setFullActivationToken(act);
act = ParsingUtils.removeCalls(act);
state.setActivationToken(act);
state.setLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE);
}
if (module != null) {
// get the tokens (global, imported and wild imported)
TokensList globalTokens = module.getGlobalTokens();
TokensList tokenImportedModules = module.getTokenImportedModules();
importedModules.addAll(tokenImportedModules);
state.setTokenImportedModules(importedModules);
TokensList 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) {
// or only folders -- not classes -- in java).
if (module.isPackage()) {
HashSet<IToken> gotten = new HashSet<IToken>();
// the module also decides how to get its submodules
getAbsoluteImportTokens(module.getPackageFolderName(), gotten, IToken.TYPE_IMPORT, true, null, false);
for (IToken token : gotten) {
if (token.getRepresentation().equals("__init__") == false) {
initial.add(token);
}
}
}
}
if (state.getActivationToken().length() == 0) {
TokensList completions = getGlobalCompletions(globalTokens, importedModules, wildImportedModules, state, module);
// now find the locals for the module
if (line >= 0) {
TokensList localTokens = module.getLocalTokens(line, col, localScope);
completions.addAll(localTokens);
}
// just add all that are in the same level if it was an __init__
completions.addAll(new TokensList(initial.toArray(EMPTY_ITOKEN_ARRAY)));
return completions;
} 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.
TokensList tokens = findTokensOnImportedMods(importedModules, state, module);
if (tokens != null && tokens.size() > 0) {
return decorateWithLocal(tokens, localScope, state);
}
// 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.size() > 0) {
return decorateWithLocal(tokens, localScope, state);
}
}
// wild imports: recursively go and get those completions and see if any matches it.
for (IterTokenEntry entry : wildImportedModules) {
IToken name = entry.getToken();
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)
state);
if (mod == null) {
// absolute
mod = getModule(name.getOriginalRep(), state.getNature(), false, state);
}
if (mod != null) {
state.checkFindModuleCompletionsMemory(mod, state.getActivationToken());
TokensList completionsForModule = getCompletionsForModule(mod, state);
if (completionsForModule.size() > 0) {
return decorateWithLocal(completionsForModule, localScope, state);
}
} 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.size() > 0) {
return decorateWithLocal(tokens, localScope, state);
}
// If it was still not found, go to builtins.
IModule builtinsMod = getBuiltinMod(state.getNature(), state);
if (builtinsMod != null && builtinsMod != module) {
tokens = getCompletionsForModule(builtinsMod, state);
if (tokens.notEmpty()) {
if (tokens.getFirst().getRepresentation().equals("ERROR:") == false) {
return decorateWithLocal(tokens, localScope, state);
}
}
}
// Let's check if we have to unpack it...
try (NoExceptionCloseable x = state.pushLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE)) {
if (state.getActivationToken().endsWith(".__getitem__")) {
String activationToken = state.getActivationToken();
String compoundActivationToken = activationToken.substring(0, activationToken.length() - 12);
TokensList ret = getCompletionsUnpackingObject(module, state.getCopyWithActTok(compoundActivationToken), localScope, new UnpackInfo());
if (ret != null && ret.size() > 0) {
return ret;
}
} else {
if (localScope != null) {
ISimpleNode foundAtASTNode = localScope.getFoundAtASTNode();
if (foundAtASTNode instanceof For) {
For for1 = (For) foundAtASTNode;
// case where we may have to unpack some iteration
// e.g.: for a, b in x.items():
TokensList ret = getCompletionsUnpackingForLoop(module, state, localScope, for1);
if (ret != null && ret.size() > 0) {
return ret;
}
// Note: we don't bail out here because it's possible that the user has
// added the type on the context (because on a for unpacking either we find it
// when checking the for loop unpack or the user has to explicitly give
// us a hint).
} else if (foundAtASTNode instanceof With) {
With with = (With) foundAtASTNode;
TokensList ret = getCompletionsUnpackingWith(module, state, localScope, with);
if (ret != null && ret.size() > 0) {
return ret;
}
}
}
}
}
if (lookForArgumentCompletion && localScope != null) {
TokensList ret = getCompletionsFromTokenInLocalScope(module, state, searchSameLevelMods, lookForArgumentCompletion, localScope);
if (ret != null && ret.size() > 0) {
return ret;
}
}
// nothing worked so far, so, let's look for an assignment...
return getAssignCompletions(module, state, lookForArgumentCompletion, localScope);
}
} else {
Log.log("Module passed in is null!!");
}
return new TokensList();
}
use of org.python.pydev.core.IToken in project Pydev by fabioz.
the class AbstractASTManager method getGlobalCompletions.
/**
* @see ICodeCompletionASTManager#getGlobalCompletions
*/
@Override
public TokensList getGlobalCompletions(TokensList globalTokens, TokensList importedModules, TokensList wildImportedModules, ICompletionState state, IModule current) {
if (DebugSettings.DEBUG_CODE_COMPLETION) {
log("getGlobalCompletions", current, state);
}
TokensList completions = new TokensList();
// in completion with nothing, just go for what is imported and global tokens.
completions.addAll(globalTokens);
// now go for the token imports
completions.addAll(importedModules);
if (!state.getBuiltinsGotten()) {
state.setBuiltinsGotten(true);
if (DebugSettings.DEBUG_CODE_COMPLETION) {
org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "getBuiltinCompletions");
}
// last thing: get completions from module __builtin__
getBuiltinCompletions(state, completions);
if (DebugSettings.DEBUG_CODE_COMPLETION) {
org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "END getBuiltinCompletions");
}
}
// filtered out if they are gotten from a wild import and not from the module itself.
for (IterTokenEntry entry : wildImportedModules) {
IToken name = entry.getToken();
// for wild imports, we must get the global completions with __all__ filtered
getCompletionsForWildImport(state, current, completions, name);
}
return completions;
}
use of org.python.pydev.core.IToken in project Pydev by fabioz.
the class AbstractASTManager method findOnImportedMods.
/**
* This function tries to find some activation token defined in some imported module.
* @return tuple with: the module and the token that should be used from it.
*
* @param this is the activation token we have. It may be a single token or some dotted name.
*
* If it is a dotted name, such as testcase.TestCase, we need to match against some import
* represented as testcase or testcase.TestCase.
*
* If a testcase.TestCase matches against some import named testcase, the import is returned and
* the TestCase is put as the module
*
* 0: mod
* 1: tok (string)
* 2: actual tok
* @throws CompletionRecursionException
*/
@Override
public Tuple3<IModule, String, IToken> findOnImportedMods(TokensList importedModules, ICompletionState state, String currentModuleName, IModule current) throws CompletionRecursionException {
FullRepIterable iterable = new FullRepIterable(state.getActivationToken(), true);
for (String tok : iterable) {
for (IterTokenEntry entry : importedModules) {
IToken importedModule = entry.getToken();
// this is its 'real' representation (alias) on the file (if it is from xxx import a as yyy, it is yyy)
final String modRep = importedModule.getRepresentation();
if (modRep.equals(tok)) {
String act = state.getActivationToken();
Tuple<IModule, String> r;
try {
r = findOnImportedMods(importedModule, tok, state, act, currentModuleName, current);
if (r != null) {
return new Tuple3<IModule, String, IToken>(r.o1, r.o2, importedModule);
}
// Note, if r==null, even though the name matched, keep on going (to handle cases of
// try..except ImportError, as we cannot be sure of which version will actually match).
} catch (MisconfigurationException e) {
Log.log(e);
}
}
}
}
return null;
}
Aggregations