Java Code Examples for org.eclipse.jdt.core.ICompilationUnit#makeConsistent()

The following examples show how to use org.eclipse.jdt.core.ICompilationUnit#makeConsistent() . 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: SyntaxServerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCompletionOnQualifiedName() throws Exception{
	URI fileURI = openFile("maven/salut4", "src/main/java/java/Completion.java");
	ICompilationUnit cu = JDTUtils.resolveCompilationUnit(fileURI);
	assertNotNull(cu);
	cu.getBuffer().setContents("package java;\n\n" +
		"public class Completion {\n" +
		"	void foo() {\n" +
		"		String str = new String(\"hello\");\n" +
		"		str.starts" +
		"	}\n" +
		"}\n");
	cu.makeConsistent(null);

	int[] loc = findLocation(cu, "str.starts");
	String fileUri = ResourceUtils.fixURI(fileURI);
	TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri);
	CompletionParams params = new CompletionParams(identifier, new Position(loc[0], loc[1]));
	CompletionList list = server.completion(params).join().getRight();
	assertNotNull(list);
	assertFalse("No proposals were found", list.getItems().isEmpty());

	List<CompletionItem> items = list.getItems();
	for (CompletionItem item : items) {
		assertTrue(StringUtils.isNotBlank(item.getLabel()));
		assertTrue(item.getLabel().startsWith("starts"));
		assertEquals(CompletionItemKind.Method, item.getKind());
		assertTrue(StringUtils.isNotBlank(item.getSortText()));
	}
}
 
Example 2
Source File: AbstractCompilationUnitBasedTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected ICompilationUnit getWorkingCopy(String path, String source) throws JavaModelException {
	ICompilationUnit workingCopy = getCompilationUnit(path);
	workingCopy.getWorkingCopy(wcOwner, monitor);
	workingCopy.getBuffer().setContents(source);
	workingCopy.makeConsistent(monitor);
	return workingCopy;
}
 
Example 3
Source File: DeleteElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void deleteElement(IJavaElement elementToRemove, ICompilationUnit cu) throws JavaModelException {
	// ensure cu is consistent (noop if already consistent)
	cu.makeConsistent(this.progressMonitor);
	this.parser.setSource(cu);
	CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
	ASTNode node = ((JavaElement) elementToRemove).findNode(astCU);
	if (node == null)
		Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$
	AST ast = astCU.getAST();
	ASTRewrite rewriter = ASTRewrite.create(ast);
	rewriter.remove(node, null);
		TextEdit edits = rewriter.rewriteAST();
		applyTextEdit(cu, edits);
}
 
Example 4
Source File: CreateElementInCUOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected CompilationUnit parse(ICompilationUnit cu) throws JavaModelException {
	// ensure cu is consistent (noop if already consistent)
	cu.makeConsistent(this.progressMonitor);
	// create an AST for the compilation unit
	ASTParser parser = ASTParser.newParser(AST.JLS8);
	parser.setSource(cu);
	return (CompilationUnit) parser.createAST(this.progressMonitor);
}
 
Example 5
Source File: BaseDocumentLifeCycleHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private IStatus performValidation(IProgressMonitor monitor) throws JavaModelException {
	long start = System.currentTimeMillis();

	List<ICompilationUnit> cusToReconcile;
	synchronized (toReconcile) {
		if (toReconcile.isEmpty()) {
			return Status.OK_STATUS;
		}
		cusToReconcile = new ArrayList<>(toReconcile.size());
		cusToReconcile.addAll(toReconcile);
		toReconcile.clear();
	}
	if (monitor.isCanceled()) {
		return Status.CANCEL_STATUS;
	}
	// first reconcile all units with content changes
	SubMonitor progress = SubMonitor.convert(monitor, cusToReconcile.size() + 1);
	for (ICompilationUnit cu : cusToReconcile) {
		if (monitor.isCanceled()) {
			return Status.CANCEL_STATUS;
		}
		cu.makeConsistent(progress);
		//cu.reconcile(ICompilationUnit.NO_AST, false, null, progress.newChild(1));
	}
	JavaLanguageServerPlugin.logInfo("Reconciled " + toReconcile.size() + ". Took " + (System.currentTimeMillis() - start) + " ms");
	if (monitor.isCanceled()) {
		return Status.CANCEL_STATUS;
	}
	if (publishDiagnosticsJob != null) {
		publishDiagnosticsJob.cancel();
		try {
			publishDiagnosticsJob.join();
		} catch (InterruptedException e) {
			// ignore
		}
		if (monitor.isCanceled()) {
			return Status.CANCEL_STATUS;
		}
		publishDiagnosticsJob.schedule(400);
	} else {
		return publishDiagnostics(new NullProgressMonitor());
	}
	return Status.OK_STATUS;
}
 
Example 6
Source File: SyntaxServerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testCompletionOnSingleName() throws Exception{
	URI fileURI = openFile("maven/salut4", "src/main/java/java/Completion.java");
	ICompilationUnit cu = JDTUtils.resolveCompilationUnit(fileURI);
	assertNotNull(cu);
	cu.getBuffer().setContents("package java;\n\n" +
		"public class Completion {\n" +
		"	void foo() {\n" +
		"		Objec\n" +
		"	}\n" +
		"}\n");
	cu.makeConsistent(null);

	int[] loc = findLocation(cu, "Objec");
	String fileUri = ResourceUtils.fixURI(fileURI);
	TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri);
	CompletionParams params = new CompletionParams(identifier, new Position(loc[0], loc[1]));
	CompletionList list = server.completion(params).join().getRight();
	assertNotNull(list);
	assertFalse("No proposals were found", list.getItems().isEmpty());

	List<CompletionItem> items = list.getItems();
	for (CompletionItem item : items) {
		assertTrue(StringUtils.isNotBlank(item.getLabel()));
		assertNotNull(item.getKind());
		assertTrue(StringUtils.isNotBlank(item.getSortText()));
		//text edits are set during calls to "completion"
		assertNotNull(item.getTextEdit());
		assertTrue(StringUtils.isNotBlank(item.getInsertText()));
		assertNotNull(item.getFilterText());
		assertFalse(item.getFilterText().contains(" "));
		assertTrue(item.getLabel().startsWith(item.getInsertText()));
		assertTrue(item.getFilterText().startsWith("Objec"));
		//Check contains data used for completionItem resolution
		@SuppressWarnings("unchecked")
		Map<String,String> data = (Map<String, String>) item.getData();
		assertNotNull(data);
		assertTrue(StringUtils.isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_URI)));
		assertTrue(StringUtils.isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID)));
		assertTrue(StringUtils.isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_REQUEST_ID)));
	}
}
 
Example 7
Source File: SyntaxServerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testResolveCompletion() throws Exception {
	URI fileURI = openFile("maven/salut4", "src/main/java/java/Completion.java");
	ICompilationUnit cu = JDTUtils.resolveCompilationUnit(fileURI);
	assertNotNull(cu);
	cu.getBuffer().setContents("package java;\n\n" +
		"public class Completion {\n" +
		"	public static void main(String[] args) {\n" +
		"		fo\n" +
		"		System.out.println(\"Hello World!\");\n" +
		"	}\n\n" +
		"	/**\n" +
		"	* This method has Javadoc\n" +
		"	*/\n" +
		"	public static void foo(String bar) {\n" +
		"	}\n" +
		"	/**\n" +
		"	* Another Javadoc\n" +
		"	*/\n" +
		"	public static void foo() {\n" +
		"	}\n" +
		"}\n");
	cu.makeConsistent(null);

	int[] loc = findLocation(cu, "\t\tfo");
	String fileUri = ResourceUtils.fixURI(fileURI);
	TextDocumentIdentifier identifier = new TextDocumentIdentifier(fileUri);
	CompletionParams params = new CompletionParams(identifier, new Position(loc[0], loc[1]));
	CompletionList list = server.completion(params).join().getRight();
	assertNotNull(list);
	CompletionItem ci = list.getItems().stream().filter(item -> item.getLabel().startsWith("foo(String bar) : void")).findFirst().orElse(null);
	assertNotNull(ci);
	CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
	assertNotNull(resolvedItem);
	assertNotNull(resolvedItem.getDocumentation());
	assertNotNull(resolvedItem.getDocumentation().getLeft());
	String javadoc = resolvedItem.getDocumentation().getLeft();
	assertEquals(javadoc, " This method has Javadoc ");
	ci = list.getItems().stream().filter(item -> item.getLabel().startsWith("foo() : void")).findFirst().orElse(null);
	assertNotNull(ci);
	resolvedItem = server.resolveCompletionItem(ci).join();
	assertNotNull(resolvedItem);
	assertNotNull(resolvedItem.getDocumentation());
	assertNotNull(resolvedItem.getDocumentation().getLeft());
	javadoc = resolvedItem.getDocumentation().getLeft();
	assertEquals(javadoc, " Another Javadoc ");
}