| /******************************************************************************* |
| * Copyright (c) 2005, 2016 IBM Corporation and others. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.dltk.python.internal.core.codeassist; |
| |
| import java.util.ArrayList; |
| import java.util.Iterator; |
| import java.util.List; |
| |
| import org.eclipse.dltk.ast.ASTNode; |
| import org.eclipse.dltk.ast.declarations.ModuleDeclaration; |
| import org.eclipse.dltk.ast.parser.ISourceParser; |
| import org.eclipse.dltk.codeassist.IAssistParser; |
| import org.eclipse.dltk.compiler.env.IModuleSource; |
| import org.eclipse.dltk.core.DLTKLanguageManager; |
| import org.eclipse.dltk.python.core.PythonNature; |
| |
| public abstract class PythonAssistParser implements IAssistParser { |
| protected ISourceParser parser; |
| |
| protected ModuleDeclaration module; |
| |
| protected ASTNode assistNodeParent = null; |
| |
| protected PythonAssistParser() { |
| parser = DLTKLanguageManager.getSourceParser(PythonNature.NATURE_ID); |
| } |
| |
| @Override |
| public ASTNode getAssistNodeParent() { |
| return assistNodeParent; |
| } |
| |
| protected void findElementsTo(List statements, ASTNode node, List elements) { |
| if (statements == null) { |
| return; |
| } |
| Iterator i = statements.iterator(); |
| while (i.hasNext()) { |
| ASTNode n = (ASTNode) i.next(); |
| if (n.equals(node)) { |
| elements.add(n); |
| return; |
| } |
| if (n.sourceStart() <= node.sourceStart() |
| && node.sourceEnd() <= n.sourceEnd()) { |
| elements.add(n); |
| findElementsTo(PythonASTUtil.getStatements(n), node, elements); |
| return; |
| } |
| } |
| |
| } |
| |
| protected List findLevelsTo(ASTNode astNodeParent) { |
| List elements = new ArrayList(); |
| if (this.module != null || astNodeParent instanceof ModuleDeclaration) { |
| if (this.module == null) { |
| this.module = (ModuleDeclaration) astNodeParent; |
| } |
| elements.add(this.module); |
| findElementsTo(PythonASTUtil.getStatements(this.module), |
| astNodeParent, elements); |
| } |
| return elements; |
| } |
| |
| @Override |
| public void setSource(ModuleDeclaration unit) { |
| this.module = unit; |
| } |
| |
| @Override |
| public ModuleDeclaration parse(IModuleSource sourceUnit) { |
| ModuleDeclaration module = (ModuleDeclaration) this.parser.parse(sourceUnit, null); |
| module.rebuild(); |
| |
| PythonASTUtil.extendStatements(module, sourceUnit.getSourceContents()); |
| |
| return module; |
| } |
| } |