Java Code Examples for org.eclipse.jdt.ls.core.internal.JDTUtils#getFileURI()

The following examples show how to use org.eclipse.jdt.ls.core.internal.JDTUtils#getFileURI() . 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: MoveHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static PackageNode createPackageNode(IPackageFragment fragment) {
	if (fragment == null) {
		return null;
	}

	String projectName = fragment.getJavaProject().getProject().getName();
	String uri = null;
	if (fragment.getResource() != null) {
		uri = JDTUtils.getFileURI(fragment.getResource());
	}

	if (fragment.isDefaultPackage()) {
		return new PackageNode(DEFAULT_PACKAGE_DISPLAYNAME, uri, fragment.getPath().toPortableString(), projectName, true);
	}

	return new PackageNode(fragment.getElementName(), uri, fragment.getPath().toPortableString(), projectName, false);
}
 
Example 2
Source File: SemanticHighlightingService.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<Position> install(ICompilationUnit unit) throws JavaModelException, BadPositionCategoryException {
	if (enabled.get()) {
		List<HighlightedPositionCore> positions = calculateHighlightedPositions(unit, false);
		String uri = JDTUtils.getFileURI(unit.getResource());
		this.cache.put(uri, positions);
		if (!positions.isEmpty()) {
			IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
			List<SemanticHighlightingInformation> infos = toInfos(document, positions);
			VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier(uri, 1);
			notifyClient(textDocument, infos);
		}
		return ImmutableList.copyOf(positions);
	}
	return emptyList();
}
 
Example 3
Source File: SemanticHighlightingService.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<HighlightedPositionCore> calculateHighlightedPositions(ICompilationUnit unit, boolean cache) throws JavaModelException, BadPositionCategoryException {
	if (enabled.get()) {
		IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
		ASTNode ast = getASTNode(unit);
		List<HighlightedPositionCore> positions = calculateHighlightedPositions(document, ast);
		if (cache) {
			String uri = JDTUtils.getFileURI(unit.getResource());
			this.cache.put(uri, positions);
		}
		return ImmutableList.copyOf(positions);
	}
	return emptyList();
}
 
Example 4
Source File: WorkspaceDiagnosticsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void publishMarkers(IProject project, IMarker[] markers) throws CoreException {
	Range range = new Range(new Position(0, 0), new Position(0, 0));

	List<IMarker> projectMarkers = new ArrayList<>(markers.length);

	String uri = JDTUtils.getFileURI(project);
	IFile pom = project.getFile("pom.xml");
	List<IMarker> pomMarkers = new ArrayList<>();
	for (IMarker marker : markers) {
		if (!marker.exists() || CheckMissingNaturesListener.MARKER_TYPE.equals(marker.getType())) {
			continue;
		}
		if (IMavenConstants.MARKER_CONFIGURATION_ID.equals(marker.getType())) {
			pomMarkers.add(marker);
		} else {
			projectMarkers.add(marker);
		}
	}
	List<Diagnostic> diagnostics = toDiagnosticArray(range, projectMarkers, isDiagnosticTagSupported);
	String clientUri = ResourceUtils.toClientUri(uri);
	connection.publishDiagnostics(new PublishDiagnosticsParams(clientUri, diagnostics));
	if (pom.exists()) {
		IDocument document = JsonRpcHelpers.toDocument(pom);
		diagnostics = toDiagnosticsArray(document, pom.findMarkers(null, true, IResource.DEPTH_ZERO), isDiagnosticTagSupported);
		List<Diagnostic> diagnosicts2 = toDiagnosticArray(range, pomMarkers, isDiagnosticTagSupported);
		diagnostics.addAll(diagnosicts2);
		String pomSuffix = clientUri.endsWith("/") ? "pom.xml" : "/pom.xml";
		connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(clientUri + pomSuffix), diagnostics));
	}
}
 
Example 5
Source File: WorkspaceDiagnosticsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void cleanUpDiagnostics(IResource resource, boolean addTrailingSlash) {
	String uri = JDTUtils.getFileURI(resource);
	if (uri != null) {
		if (addTrailingSlash && !uri.endsWith("/")) {
			uri = uri + "/";
		}
		this.connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), Collections.emptyList()));
	}
}
 
Example 6
Source File: WorkspaceEventHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDiscardStaleWorkingCopies() throws Exception {
	IJavaProject javaProject = newEmptyProject();
	IFolder src = javaProject.getProject().getFolder("src");
	IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(src);
	IPackageFragment mypack = srcRoot.createPackageFragment("mypack", true, null);
	// @formatter:off
	String contents = "package mypack;\n" +
					"public class Foo {" +
					"}\n";
	// @formatter:on
	ICompilationUnit unit = mypack.createCompilationUnit("Foo.java", contents, true, null);
	openDocument(unit, contents, 1);
	assertTrue(unit.isWorkingCopy());

	String oldUri = JDTUtils.getFileURI(srcRoot.getResource());
	String parentUri = JDTUtils.getFileURI(src);
	String newUri = oldUri.replace("mypack", "mynewpack");
	File oldPack = mypack.getResource().getLocation().toFile();
	File newPack = new File(oldPack.getParent(), "mynewpack");
	Files.move(oldPack, newPack);
	assertTrue(unit.isWorkingCopy());
	DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams(Arrays.asList(
		new FileEvent(newUri, FileChangeType.Created),
		new FileEvent(parentUri, FileChangeType.Changed),
		new FileEvent(oldUri, FileChangeType.Deleted)
	));
	new WorkspaceEventsHandler(projectsManager, javaClient, lifeCycleHandler).didChangeWatchedFiles(params);
	assertFalse(unit.isWorkingCopy());
}
 
Example 7
Source File: WorkspaceEventHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDeleteProjectFolder() throws Exception {
	importProjects("maven/multimodule3");

	IProject module2 = ProjectUtils.getProject("module2");
	assertTrue(module2 != null && module2.exists());

	String projectUri = JDTUtils.getFileURI(module2);
	FileUtils.deleteDirectory(module2.getLocation().toFile());
	assertTrue(module2.exists());

	clientRequests.clear();
	DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams(Arrays.asList(
		new FileEvent(projectUri, FileChangeType.Deleted)
	));
	new WorkspaceEventsHandler(projectsManager, javaClient, lifeCycleHandler).didChangeWatchedFiles(params);
	waitForBackgroundJobs();
	assertFalse(module2.exists());

	List<PublishDiagnosticsParams> diags = getClientRequests("publishDiagnostics");
	assertEquals(9L, diags.size());
	assertEndsWith(diags.get(0).getUri(), "/module2");
	assertEndsWith(diags.get(1).getUri(), "/multimodule3");
	assertEndsWith(diags.get(2).getUri(), "/multimodule3/pom.xml");
	assertEndsWith(diags.get(3).getUri(), "/module2/pom.xml");
	assertEquals(0L, diags.get(3).getDiagnostics().size());
	assertEndsWith(diags.get(4).getUri(), "/module2");
	assertEquals(0L, diags.get(4).getDiagnostics().size());
	assertEndsWith(diags.get(5).getUri(), "/App.java");
	assertEquals(0L, diags.get(5).getDiagnostics().size());
	assertEndsWith(diags.get(6).getUri(), "/AppTest.java");
	assertEquals(0L, diags.get(6).getDiagnostics().size());
	assertEndsWith(diags.get(7).getUri(), "/multimodule3");
	assertEndsWith(diags.get(8).getUri(), "/multimodule3/pom.xml");
}
 
Example 8
Source File: FileEventHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testRenamePackage() throws JavaModelException, BadLocationException {
	when(clientPreferences.isResourceOperationSupported()).thenReturn(true);

	IPackageFragment pack1 = sourceFolder.createPackageFragment("parent.pack1", false, null);
	IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.pack2", false, null);

	StringBuilder codeA = new StringBuilder();
	codeA.append("package parent.pack1;\n");
	codeA.append("import parent.pack2.B;\n");
	codeA.append("public class A {\n");
	codeA.append("	public void foo() {\n");
	codeA.append("		B b = new B();\n");
	codeA.append("		b.foo();\n");
	codeA.append("	}\n");
	codeA.append("}\n");

	StringBuilder codeB = new StringBuilder();
	codeB.append("package parent.pack2;\n");
	codeB.append("public class B {\n");
	codeB.append("	public B() {}\n");
	codeB.append("	public void foo() {}\n");
	codeB.append("}\n");

	ICompilationUnit cuA = pack1.createCompilationUnit("A.java", codeA.toString(), false, null);
	ICompilationUnit cuB = pack2.createCompilationUnit("B.java", codeB.toString(), false, null);

	String pack2Uri = JDTUtils.getFileURI(pack2.getResource());
	String newPack2Uri = pack2Uri.replace("pack2", "newpack2");
	WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new FileRenameParams(Arrays.asList(new FileRenameEvent(pack2Uri, newPack2Uri))), new NullProgressMonitor());
	assertNotNull(edit);
	List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
	assertEquals(2, documentChanges.size());

	assertTrue(documentChanges.get(0).isLeft());
	assertEquals(documentChanges.get(0).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
	assertEquals(TextEditUtil.apply(codeA.toString(), documentChanges.get(0).getLeft().getEdits()),
			"package parent.pack1;\n" +
			"import parent.newpack2.B;\n" +
			"public class A {\n" +
			"	public void foo() {\n" +
			"		B b = new B();\n" +
			"		b.foo();\n" +
			"	}\n" +
			"}\n"
			);

	assertTrue(documentChanges.get(1).isLeft());
	assertEquals(documentChanges.get(1).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
	assertEquals(TextEditUtil.apply(codeB.toString(), documentChanges.get(1).getLeft().getEdits()),
			"package parent.newpack2;\n" +
			"public class B {\n" +
			"	public B() {}\n" +
			"	public void foo() {}\n" +
			"}\n"
			);
}
 
Example 9
Source File: FileEventHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testRenameSubPackage() throws JavaModelException, BadLocationException {
	when(clientPreferences.isResourceOperationSupported()).thenReturn(true);

	IPackageFragment parentPack = sourceFolder.createPackageFragment("parent", false, null);
	IPackageFragment pack1 = sourceFolder.createPackageFragment("parent.pack1", false, null);
	IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.pack2", false, null);

	StringBuilder codeA = new StringBuilder();
	codeA.append("package parent.pack1;\n");
	codeA.append("import parent.pack2.B;\n");
	codeA.append("public class A {\n");
	codeA.append("	public void foo() {\n");
	codeA.append("		B b = new B();\n");
	codeA.append("		b.foo();\n");
	codeA.append("	}\n");
	codeA.append("}\n");

	StringBuilder codeB = new StringBuilder();
	codeB.append("package parent.pack2;\n");
	codeB.append("public class B {\n");
	codeB.append("	public B() {}\n");
	codeB.append("	public void foo() {}\n");
	codeB.append("}\n");

	ICompilationUnit cuA = pack1.createCompilationUnit("A.java", codeA.toString(), false, null);
	ICompilationUnit cuB = pack2.createCompilationUnit("B.java", codeB.toString(), false, null);

	String parentPackUri = JDTUtils.getFileURI(parentPack.getResource());
	String newParentPackUri = parentPackUri.replace("parent", "newparent");
	WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new FileRenameParams(Arrays.asList(new FileRenameEvent(parentPackUri, newParentPackUri))), new NullProgressMonitor());
	assertNotNull(edit);
	List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
	assertEquals(3, documentChanges.size());

	assertTrue(documentChanges.get(0).isLeft());
	assertEquals(documentChanges.get(0).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
	assertTrue(documentChanges.get(1).isLeft());
	assertEquals(documentChanges.get(1).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
	List<TextEdit> edits = new ArrayList<>();
	edits.addAll(documentChanges.get(0).getLeft().getEdits());
	edits.addAll(documentChanges.get(1).getLeft().getEdits());
	assertEquals(TextEditUtil.apply(codeA.toString(), edits),
			"package newparent.pack1;\n" +
			"import newparent.pack2.B;\n" +
			"public class A {\n" +
			"	public void foo() {\n" +
			"		B b = new B();\n" +
			"		b.foo();\n" +
			"	}\n" +
			"}\n"
			);

	assertTrue(documentChanges.get(2).isLeft());
	assertEquals(documentChanges.get(2).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
	assertEquals(TextEditUtil.apply(codeB.toString(), documentChanges.get(2).getLeft().getEdits()),
			"package newparent.pack2;\n" +
			"public class B {\n" +
			"	public B() {}\n" +
			"	public void foo() {}\n" +
			"}\n"
			);
}
 
Example 10
Source File: MoveHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testMoveFile() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
	//@formatter:off
	ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" +
			"import jdtls.test2.B;\r\n" +
			"\r\n" +
			"public class A {\r\n" +
			"	private B b = new B();\r\n" +
			"}", true, null);
	//@formatter:on

	IPackageFragment pack2 = sourceFolder.createPackageFragment("jdtls.test2", false, null);
	//@formatter:off
	ICompilationUnit unitB = pack2.createCompilationUnit("B.java", "package jdtls.test2;\r\n" +
			"\r\n" +
			"public class B {\r\n" +
			"}", true, null);
	//@formatter:on

	IPackageFragment pack3 = sourceFolder.createPackageFragment("jdtls.test3", false, null);
	String packageUri = JDTUtils.getFileURI(pack3.getResource());
	RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveResource", new String[] { JDTUtils.toURI(unitB) }, packageUri, true), new NullProgressMonitor());
	assertNotNull(refactorEdit);
	assertNotNull(refactorEdit.edit);
	List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
	assertEquals(4, changes.size());

	//@formatter:off
	String expected = "package jdtls.test1;\r\n" +
			"import jdtls.test3.B;\r\n" +
			"\r\n" +
			"public class A {\r\n" +
			"	private B b = new B();\r\n" +
			"}";
	//@formatter:on
	TextDocumentEdit textEdit = changes.get(0).getLeft();
	assertNotNull(textEdit);
	assertEquals(expected, TextEditUtil.apply(unitA.getSource(), textEdit.getEdits()));

	//@formatter:off
	expected = "package jdtls.test3;\r\n" +
			"\r\n" +
			"public class B {\r\n" +
			"}";
	//@formatter:on
	textEdit = changes.get(1).getLeft();
	assertNotNull(textEdit);
	List<TextEdit> edits = new ArrayList<>(textEdit.getEdits());
	textEdit = changes.get(2).getLeft();
	assertNotNull(textEdit);
	edits.addAll(textEdit.getEdits());
	assertEquals(expected, TextEditUtil.apply(unitB.getSource(), edits));

	RenameFile renameFile = (RenameFile) changes.get(3).getRight();
	assertNotNull(renameFile);
	assertEquals(JDTUtils.toURI(unitB), renameFile.getOldUri());
	assertEquals(ResourceUtils.fixURI(unitB.getResource().getRawLocationURI()).replace("test2", "test3"), renameFile.getNewUri());
}
 
Example 11
Source File: MoveHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testMoveMultiFiles() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
	//@formatter:off
	ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" +
			"\r\n" +
			"public class A {\r\n" +
			"	private B b = new B();\r\n" +
			"}", true, null);
	//@formatter:on

	//@formatter:off
	ICompilationUnit unitB = pack1.createCompilationUnit("B.java", "package jdtls.test1;\r\n" +
			"\r\n" +
			"public class B {\r\n" +
			"}", true, null);
	//@formatter:on

	IPackageFragment pack2 = sourceFolder.createPackageFragment("jdtls.test2", false, null);
	String packageUri = JDTUtils.getFileURI(pack2.getResource());
	RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveResource", new String[] { JDTUtils.toURI(unitA), JDTUtils.toURI(unitB) }, packageUri, true), new NullProgressMonitor());
	assertNotNull(refactorEdit);
	assertNotNull(refactorEdit.edit);
	List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
	assertEquals(6, changes.size());

	//@formatter:off
	String expected = "package jdtls.test2;\r\n" +
			"\r\n" +
			"public class A {\r\n" +
			"	private B b = new B();\r\n" +
			"}";
	//@formatter:on
	TextDocumentEdit textEdit = changes.get(0).getLeft();
	assertNotNull(textEdit);
	List<TextEdit> edits = new ArrayList<>(textEdit.getEdits());
	textEdit = changes.get(4).getLeft();
	assertNotNull(textEdit);
	edits.addAll(textEdit.getEdits());
	assertEquals(expected, TextEditUtil.apply(unitA.getSource(), edits));

	//@formatter:off
	expected = "package jdtls.test2;\r\n" +
			"\r\n" +
			"public class B {\r\n" +
			"}";
	//@formatter:on
	textEdit = changes.get(1).getLeft();
	assertNotNull(textEdit);
	edits = new ArrayList<>(textEdit.getEdits());
	textEdit = changes.get(2).getLeft();
	assertNotNull(textEdit);
	edits.addAll(textEdit.getEdits());
	assertEquals(expected, TextEditUtil.apply(unitB.getSource(), edits));

	RenameFile renameFileB = (RenameFile) changes.get(3).getRight();
	assertNotNull(renameFileB);
	assertEquals(JDTUtils.toURI(unitB), renameFileB.getOldUri());
	assertEquals(ResourceUtils.fixURI(unitB.getResource().getRawLocationURI()).replace("test1", "test2"), renameFileB.getNewUri());

	RenameFile renameFileA = (RenameFile) changes.get(5).getRight();
	assertNotNull(renameFileA);
	assertEquals(JDTUtils.toURI(unitA), renameFileA.getOldUri());
	assertEquals(ResourceUtils.fixURI(unitA.getResource().getRawLocationURI()).replace("test1", "test2"), renameFileA.getNewUri());
}