Java Code Examples for org.eclipse.jdt.core.dom.ASTParser#setFocalPosition()

The following examples show how to use org.eclipse.jdt.core.dom.ASTParser#setFocalPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaHistoryActionImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
static CompilationUnit parsePartialCompilationUnit(ICompilationUnit unit) {

		if (unit == null) {
			throw new IllegalArgumentException();
		}
		try {
			ASTParser c= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
			c.setSource(unit);
			c.setFocalPosition(0);
			c.setResolveBindings(false);
			c.setWorkingCopyOwner(null);
			ASTNode result= c.createAST(null);
			return (CompilationUnit) result;
		} catch (IllegalStateException e) {
			// convert ASTParser's complaints into old form
			throw new IllegalArgumentException();
		}
	}
 
Example 2
Source File: ClassPathDetector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void visitCompilationUnit(IFile file) {
	ICompilationUnit cu= JavaCore.createCompilationUnitFrom(file);
	if (cu != null) {
		ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
		parser.setSource(cu);
		parser.setFocalPosition(0);
		CompilationUnit root= (CompilationUnit)parser.createAST(null);
		PackageDeclaration packDecl= root.getPackage();
		
		IPath packPath= file.getParent().getFullPath();
		String cuName= file.getName();
		if (packDecl == null) {
			addToMap(fSourceFolders, packPath, new Path(cuName));
		} else {
			IPath relPath= new Path(packDecl.getName().getFullyQualifiedName().replace('.', '/'));
			IPath folderPath= getFolderPath(packPath, relPath);
			if (folderPath != null) {
				addToMap(fSourceFolders, folderPath, relPath.append(cuName));
			}
		}
	}
}
 
Example 3
Source File: SarlClassPathDetector.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Visit the given Java compilation unit.
 *
 * @param jfile the compilation unit.
 */
protected void visitJavaCompilationUnit(IFile jfile) {
	final ICompilationUnit cu = JavaCore.createCompilationUnitFrom(jfile);
	if (cu != null) {
		final ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
		parser.setSource(cu);
		parser.setFocalPosition(0);
		final CompilationUnit root = (CompilationUnit) parser.createAST(null);
		final PackageDeclaration packDecl = root.getPackage();

		final IPath packPath = jfile.getParent().getFullPath();
		final String cuName = jfile.getName();
		if (packDecl == null) {
			addToMap(this.sourceFolders, packPath, new Path(cuName));
		} else {
			final IPath relPath = new Path(packDecl.getName().getFullyQualifiedName().replace('.', '/'));
			final IPath folderPath = getFolderPath(packPath, relPath);
			if (folderPath != null) {
				addToMap(this.sourceFolders, folderPath, relPath.append(cuName));
			}
		}
	}
}
 
Example 4
Source File: NewTypeWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private CompilationUnit createASTForImports(ICompilationUnit cu) {
	ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
	parser.setSource(cu);
	parser.setResolveBindings(true);
	parser.setFocalPosition(0);
	return (CompilationUnit) parser.createAST(null);
}
 
Example 5
Source File: JavaStructureCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private IStructureComparator createStructureComparator(final Object input, char[] buffer, IDocument doc, ISharedDocumentAdapter adapter, IProgressMonitor monitor) {
	String contents;
	Map<String, String> compilerOptions= null;

	if (input instanceof IResourceProvider) {
		IResource resource= ((IResourceProvider) input).getResource();
		if (resource != null) {
			IJavaElement element= JavaCore.create(resource);
			if (element != null) {
				IJavaProject javaProject= element.getJavaProject();
				if (javaProject != null)
					compilerOptions= javaProject.getOptions(true);
			}
		}
	}
	if (compilerOptions == null)
		compilerOptions= fDefaultCompilerOptions;

	if (doc != null) {
		boolean isEditable= false;
		if (input instanceof IEditableContent)
			isEditable= ((IEditableContent) input).isEditable();

		// we hook into the root node to intercept all node changes
		JavaNode root= new RootJavaNode(doc, isEditable, input, adapter);

		if (buffer == null) {
			contents= doc.get();
			int n= contents.length();
			buffer= new char[n];
			contents.getChars(0, n, buffer, 0);
		}

		ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
		if (compilerOptions != null)
			parser.setCompilerOptions(compilerOptions);
		parser.setSource(buffer);
		parser.setFocalPosition(0);
		CompilationUnit cu= (CompilationUnit) parser.createAST(monitor);
		cu.accept(new JavaParseTreeBuilder(root, buffer, true));

		return root;
	}
	return null;
}