📄 pycodecompletion.java
字号:
Log.remLogLevel();
Log.toLogFile(this, "Finished completion. Returned:"+ret.size()+" completions.\r\n");
}
return ret;
}
/**
* @return whether we're currently in a valid context for a code-completion request for this engine.
*/
private boolean isValidCompletionContext(CompletionRequest request) {
//this engine does not work 'correctly' in the default scope on:
//- class definitions - after 'class' and before '('
//- method definitions - after 'def' and before '('
PySelection ps = request.getPySelection();
if(ps.isInDeclarationLine() != PySelection.DECLARATION_NONE){
return false;
}
return true;
}
/**
* Does a code-completion that will retrieve the globals in the module
*/
private void doGlobalsCompletion(CompletionRequest request, ICodeCompletionASTManager astManager, List<Object> tokensList, ICompletionState state) throws CompletionRecursionException {
state.setActivationToken(request.activationToken);
if(DEBUG_CODE_COMPLETION){
Log.toLogFile(this,"astManager.getCompletionsForToken");
Log.addLogLevel();
}
IToken[] comps = astManager.getCompletionsForToken(request.editorFile, request.doc, state);
if(DEBUG_CODE_COMPLETION){
Log.remLogLevel();
Log.toLogFile(this,"END astManager.getCompletionsForToken");
}
tokensList.addAll(Arrays.asList(comps));
tokensList.addAll(getGlobalsFromParticipants(request, state));
}
/**
* Does a code-completion that will retrieve the all matches for some token in the module
*/
private void doTokenCompletion(CompletionRequest request, ICodeCompletionASTManager astManager, List<Object> tokensList, String trimmed, ICompletionState state) throws CompletionRecursionException {
if (request.activationToken.endsWith(".")) {
request.activationToken = request.activationToken.substring(0, request.activationToken.length() - 1);
}
char[] toks = new char[]{'.', ' '};
List<Object> completions = new ArrayList<Object>();
if (trimmed.equals("self") || FullRepIterable.getFirstPart(trimmed, toks).equals("self")) {
state.setLookingFor(ICompletionState.LOOKING_FOR_INSTANCED_VARIABLE);
getSelfOrClsCompletions(request, tokensList, state, false);
}else if (trimmed.equals("cls") || FullRepIterable.getFirstPart(trimmed, toks).equals("cls")) {
state.setLookingFor(ICompletionState.LOOKING_FOR_CLASSMETHOD_VARIABLE);
getSelfOrClsCompletions(request, tokensList, state, false);
} else {
state.setActivationToken(request.activationToken);
//Ok, looking for a token in globals.
IToken[] comps = astManager.getCompletionsForToken(request.editorFile, request.doc, state);
tokensList.addAll(Arrays.asList(comps));
}
tokensList.addAll(completions);
}
/**
* Does a code-completion that will check for imports
*/
private boolean doImportCompletion(CompletionRequest request, ICodeCompletionASTManager astManager, List<Object> tokensList, ImportInfo importsTipper) throws CompletionRecursionException {
boolean importsTip;
//get the project and make the code completion!!
//so, we want to do a code completion for imports...
//let's see what we have...
importsTip = true;
importsTipper.importsTipperStr = importsTipper.importsTipperStr.trim();
IToken[] imports = astManager.getCompletionsForImport(importsTipper, request);
tokensList.addAll(Arrays.asList(imports));
return importsTip;
}
/**
* Checks if the python nature is valid
*/
private void checkPythonNature(IPythonNature pythonNature) {
if (pythonNature == null) {
throw new RuntimeException("Unable to get python nature.");
}
}
/**
* Pre-initializes the shell (NOT in a thread, as we may need it shortly, so, no use in putting it into a thread)
*/
private void lazyStartShell(CompletionRequest request) {
try {
if(DEBUG_CODE_COMPLETION){
Log.toLogFile(this,"AbstractShell.getServerShell");
}
if (CompiledModule.COMPILED_MODULES_ENABLED) {
AbstractShell.getServerShell(request.nature, AbstractShell.COMPLETION_SHELL); //just start it
}
if(DEBUG_CODE_COMPLETION){
Log.toLogFile(this,"END AbstractShell.getServerShell");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @return completions added from contributors
*/
@SuppressWarnings("unchecked")
private Collection<Object> getGlobalsFromParticipants(CompletionRequest request, ICompletionState state) {
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.getGlobalCompletions(request, state));
}
return ret;
}
/**
* @param request this is the request for the completion
* @param theList OUT - returned completions are added here. (IToken instances)
* @param getOnlySupers whether we should only get things from super classes (in this case, we won't get things from the current class)
* @return the same tokens added in theList
*/
public static void getSelfOrClsCompletions(CompletionRequest request, List theList, ICompletionState state, boolean getOnlySupers) {
SimpleNode s = PyParser.reparseDocument(new PyParser.ParserInfo(request.doc, true, request.nature, state.getLine())).o1;
getSelfOrClsCompletions(request, theList, state, getOnlySupers, s);
}
public static void getSelfOrClsCompletions(CompletionRequest request, List theList, ICompletionState state, boolean getOnlySupers, SimpleNode s) {
if(s != null){
FindScopeVisitor visitor = new FindScopeVisitor(state.getLine(), 0);
try {
s.accept(visitor);
getSelfOrClsCompletions(visitor.scope, request, theList, state, getOnlySupers);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
/**
* Get self completions when you already have a scope
*/
@SuppressWarnings("unchecked")
public static void getSelfOrClsCompletions(ILocalScope scope, CompletionRequest request, List theList, ICompletionState state, boolean getOnlySupers) throws BadLocationException {
for(Iterator<SimpleNode> it = scope.iterator(); it.hasNext();){
SimpleNode node = it.next();
if(node instanceof ClassDef){
ClassDef d = (ClassDef) node;
if(getOnlySupers){
List gottenComps = new ArrayList();
for (int i = 0; i < d.bases.length; i++) {
if(d.bases[i] instanceof Name){
Name n = (Name) d.bases[i];
state.setActivationToken(n.id);
IToken[] completions;
try {
completions = request.nature.getAstManager().getCompletionsForToken(request.editorFile, request.doc, state);
gottenComps.addAll(Arrays.asList(completions));
} catch (CompletionRecursionException e) {
//ok...
}
}
}
theList.addAll(gottenComps);
}else{
//ok, get the completions for the class, only thing we have to take care now is that we may
//not have only 'self' for completion, but somthing lile self.foo.
//so, let's analyze our activation token to see what should we do.
String trimmed = request.activationToken.replace('.', ' ').trim();
String[] actTokStrs = trimmed.split(" ");
if(actTokStrs.length == 0 || (!actTokStrs[0].equals("self")&& !actTokStrs[0].equals("cls")) ){
throw new AssertionError("We need to have at least one token (self or cls) for doing completions in the class.");
}
if(actTokStrs.length == 1){
//ok, it's just really self, let's get on to get the completions
state.setActivationToken(NodeUtils.getNameFromNameTok((NameTok) d.name));
try {
theList.addAll(Arrays.asList(request.nature.getAstManager().getCompletionsForToken(request.editorFile, request.doc, state)));
} catch (CompletionRecursionException e) {
//ok
}
}else{
//it's not only self, so, first we have to get the definition of the token
//the first one is self, so, just discard it, and go on, token by token to know what is the last
//one we are completing (e.g.: self.foo.bar)
int line = request.doc.getLineOfOffset(request.documentOffset);
IRegion region = request.doc.getLineInformationOfOffset(request.documentOffset);
int col = request.documentOffset - region.getOffset();
IModule module = AbstractModule.createModuleFromDoc("", null, request.doc, request.nature, line);
ASTManager astMan = ((ASTManager)request.nature.getAstManager());
theList.addAll(new AssignAnalysis().getAssignCompletions(astMan, module, new CompletionState(line, col, request.activationToken, request.nature, request.qualifier)));
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -