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

The following examples show how to use org.eclipse.jdt.ls.core.internal.JDTUtils#toURI() . 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: HoverHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testInvalidJavadoc() throws Exception {
	importProjects("maven/aspose");
	IProject project = null;
	ICompilationUnit unit = null;
	try {
		project = ResourcesPlugin.getWorkspace().getRoot().getProject("aspose");
		IJavaProject javaProject = JavaCore.create(project);
		IType type = javaProject.findType("org.sample.TestJavadoc");
		unit = type.getCompilationUnit();
		unit.becomeWorkingCopy(null);
		String uri = JDTUtils.toURI(unit);
		TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
		TextDocumentPositionParams position = new TextDocumentPositionParams(textDocument, new Position(8, 24));
		Hover hover = handler.hover(position, monitor);
		assertNotNull(hover);
		assertNotNull(hover.getContents());
		assertEquals(1, hover.getContents().getLeft().size());
		assertEquals("com.aspose.words.Document.Document(String fileName) throws Exception", hover.getContents().getLeft().get(0).getRight().getValue());
	} finally {
		if (unit != null) {
			unit.discardWorkingCopy();
		}
	}
}
 
Example 2
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testJavaFormatEnable() throws Exception {
	String text =
	//@formatter:off
			"package org.sample   ;\n\n" +
			"      public class Baz {  String name;}\n";
		//@formatter:on"
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", text);
	preferenceManager.getPreferences().setJavaFormatEnabled(false);
	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
	DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
	List<? extends TextEdit> edits = server.formatting(params).get();
	assertNotNull(edits);
	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(text, newText);
}
 
Example 3
Source File: MoveHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testGetPackageDestinations() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
	//@formatter:off
	ICompilationUnit unit = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" +
			"\r\n" +
			"public class A {\r\n" +
			"}", true, null);
	//@formatter:on
	MoveParams params = new MoveParams("moveResource", new String[] { JDTUtils.toURI(unit) });
	MoveDestinationsResponse response = MoveHandler.getMoveDestinations(params);
	assertNotNull(response);
	assertNotNull(response.destinations);
	assertEquals(3, response.destinations.length);
	assertTrue(((PackageNode) response.destinations[0]).isDefaultPackage);
	assertEquals("jdtls", ((PackageNode) response.destinations[1]).displayName);
	assertEquals("jdtls.test1", ((PackageNode) response.destinations[2]).displayName);
	assertTrue(((PackageNode) response.destinations[2]).isParentOfSelectedFile);
}
 
Example 4
Source File: FindLinksHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testNoSuperMethod() throws JavaModelException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	//@formatter:off
	ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package test1;\n" +
			"\n" +
			"public class A {\n" +
			"	public void run() {\n" +
			"	}\n" +
			"}", true, null);
	//@formatter:on

	String uri = JDTUtils.toURI(unitA);
	List<? extends Location> response = FindLinksHandler.findLinks("superImplementation", new TextDocumentPositionParams(new TextDocumentIdentifier(uri), new Position(3, 14)), new NullProgressMonitor());
	assertTrue(response == null || response.isEmpty());
}
 
Example 5
Source File: DiagnosticsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public DiagnosticsHandler(JavaClientConnection conn, ICompilationUnit cu) {
	super(conn, cu);
	this.cu = cu;
	this.uri = JDTUtils.toURI(cu);
	this.isDefaultProject = JDTUtils.isDefaultProject(cu);
	this.nonProjectFile = isDefaultProject || !JDTUtils.isOnClassPath(cu);
}
 
Example 6
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test // typing } should format the previous block
public void testFormattingOnTypeCloseBlock() throws Exception {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
	//@formatter:off
		  "package org.sample;\n"
		+ "\n"
		+ "    public      class     Baz {  \n"
		+ "String          name       ;\n"
		+ "}  "//typed } here
	//@formatter:on
	);

	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces

	DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(4, 0), "}");
	params.setTextDocument(textDocument);
	params.setOptions(options);

	preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
	List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
	assertNotNull(edits);

	//@formatter:off
	String expectedText =
		  "package org.sample;\n"
		+ "\n"
		+ "public class Baz {\n"
		+ "    String name;\n"
		+ "}";
	//@formatter:on

	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(expectedText, newText);
}
 
Example 7
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test // typing new_line after closing a block should format the that block
public void testFormattingOnTypeReturnAfterClosedBlock() throws Exception {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
	//@formatter:off
		  "package org.sample;\n"
		+ "\n"
		+ "    public      class     Baz {  \n"
		+ "String          name       ;\n"
		+ "}  \n"//typed \n here
	//@formatter:on
	);

	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces

	DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(4, 3), "\n");
	params.setTextDocument(textDocument);
	params.setOptions(options);

	preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
	List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
	assertNotNull(edits);

	//@formatter:off
	String expectedText =
		  "package org.sample;\n"
		+ "\n"
		+ "public class Baz {\n"
		+ "    String name;\n"
		+ "}\n";
	//@formatter:on

	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(expectedText, newText);
}
 
Example 8
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDocumentFormattingWithTabs() throws Exception {
	javaProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE);

	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
	//@formatter:off
		"package org.sample;\n\n" +
		"public class Baz {\n"+
		"    void foo(){\n"+
		"}\n"+
		"}\n"
	//@formatter:on
	);

	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	FormattingOptions options = new FormattingOptions(2, false);// ident == tab
	DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
	List<? extends TextEdit> edits = server.formatting(params).get();
	assertNotNull(edits);

	//@formatter:off
	String expectedText =
		"package org.sample;\n"+
		"\n"+
		"public class Baz {\n"+
		"\tvoid foo() {\n"+
		"\t}\n"+
		"}\n";
	//@formatter:on
	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(expectedText, newText);
}
 
Example 9
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test // typing new_line after opening a block should only format the current line
public void testFormattingOnTypeReturnAfterOpeningBlock() throws Exception {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
	//@formatter:off
		  "package org.sample;\n"
		+ "\n"
		+ "    public      class     Baz {  \n"//typed \n here
		+ "String          name       ;\n"
		+ "}  \n"
	//@formatter:on
	);

	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces

	DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(2, 33), "\n");
	params.setTextDocument(textDocument);
	params.setOptions(options);

	preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
	List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
	assertNotNull(edits);

	//@formatter:off
	String expectedText =
		  "package org.sample;\n"
		+ "\n"
		+ "public class Baz {\n"
		+ "String          name       ;\n"
		+ "}  \n";
	//@formatter:on

	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(expectedText, newText);
}
 
Example 10
Source File: EditUtils.java    From vscode-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static WorkspaceEdit convertToWorkspaceEdit(ICompilationUnit unit, TextEdit edit) {
    final WorkspaceEdit workspaceEdit = new WorkspaceEdit();
    final TextEditConverter converter = new TextEditConverter(unit, edit);
    final String uri = JDTUtils.toURI(unit);
    workspaceEdit.getChanges().put(uri, converter.convert());
    return workspaceEdit;
}
 
Example 11
Source File: FindLinksHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindSuperMethod() throws JavaModelException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	//@formatter:off
	ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package test1;\n" +
			"\n" +
			"public class A {\n" +
			"	public void run() {\n" +
			"	}\n" +
			"}", true, null);
	//@formatter:on

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

	String uri = JDTUtils.toURI(unitB);
	List<? extends Location> response = FindLinksHandler.findLinks("superImplementation", new TextDocumentPositionParams(new TextDocumentIdentifier(uri), new Position(3, 14)), new NullProgressMonitor());
	assertTrue(response != null && response.size() == 1);
	LinkLocation location = (LinkLocation) response.get(0);
	assertEquals("test1.A.run", location.displayName);
	assertEquals("method", location.kind);
	assertEquals(JDTUtils.toURI(unitA), location.getUri());
	Range range = location.getRange();
	assertEquals(3, range.getStart().getLine());
	assertEquals(13, range.getStart().getCharacter());
	assertEquals(3, range.getEnd().getLine());
	assertEquals(16, range.getEnd().getCharacter());
}
 
Example 12
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testRangeFormatting() throws Exception {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
	//@formatter:off
		"package org.sample;\n" +
		"      public class Baz {\n"+
		"\tvoid foo(){\n" +
		"    }\n"+
		"	}\n"
	//@formatter:on
	);

	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);

	Range range = new Range(new Position(2, 0), new Position(3, 5));// range around foo()
	DocumentRangeFormattingParams params = new DocumentRangeFormattingParams(range);
	params.setTextDocument(textDocument);
	params.setOptions(new FormattingOptions(3, true));// ident == 3 spaces

	List<? extends TextEdit> edits = server.rangeFormatting(params).get();
	//@formatter:off
	String expectedText =
		"package org.sample;\n" +
		"      public class Baz {\n"+
		"         void foo() {\n" +
		"         }\n"+
		"	}\n";
	//@formatter:on
	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(expectedText, newText);
}
 
Example 13
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test // typing new_line after inserting a new line should format the previous block if previous non-whitespace char is }
public void testFormattingOnTypeReturnAfterEmptyLine() throws Exception {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
	//@formatter:off
		  "package org.sample;\n"
		+ "\n"
		+ "    public      class     Baz {  \n"
		+ "String          name       ;\n"
		+ "}  \n"
		+ "   \n"//typed \n here
	//@formatter:on
	);

	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces

	DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(5, 3), "\n");
	params.setTextDocument(textDocument);
	params.setOptions(options);

	preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
	List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
	assertNotNull(edits);

	//@formatter:off
	String expectedText =
		  "package org.sample;\n"
		+ "\n"
		+ "public class Baz {\n"
		+ "    String name;\n"
		+ "}\n"
		+ "   \n";
	//@formatter:on

	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(expectedText, newText);
}
 
Example 14
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test // typing new_line after an empty block on a single line should format that block
public void testFormattingOnTypeReturnAfterEmptyBlock() throws Exception {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
	//@formatter:off
		  "package org.sample;\n"
		+ "\n"
		+ "    public      class     Baz {}  \n"//typed \n here
	//@formatter:on
	);

	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces

	DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(new Position(2, 34), "\n");
	params.setTextDocument(textDocument);
	params.setOptions(options);

	preferenceManager.getPreferences().setJavaFormatOnTypeEnabled(true);
	List<? extends TextEdit> edits = server.onTypeFormatting(params).get();
	assertNotNull(edits);

	//@formatter:off
	String expectedText =
		  "package org.sample;\n"
		+ "\n"
		+ "public class Baz {\n"
		+ "}\n";
	//@formatter:on

	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(expectedText, newText);
}
 
Example 15
Source File: SourceAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static WorkspaceEdit convertToWorkspaceEdit(ICompilationUnit cu, TextEdit edit) {
	if (cu == null || edit == null) {
		return null;
	}

	WorkspaceEdit workspaceEdit = new WorkspaceEdit();
	TextEditConverter converter = new TextEditConverter(cu, edit);
	String uri = JDTUtils.toURI(cu);
	workspaceEdit.getChanges().put(uri, converter.convert());
	return workspaceEdit;
}
 
Example 16
Source File: FormatterHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDocumentFormatting() throws Exception {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
	//@formatter:off
		"package org.sample   ;\n\n" +
		"      public class Baz {  String name;}\n"
	//@formatter:on
	);

	String uri = JDTUtils.toURI(unit);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	FormattingOptions options = new FormattingOptions(4, true);// ident == 4 spaces
	DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
	List<? extends TextEdit> edits = server.formatting(params).get();
	assertNotNull(edits);

	//@formatter:off
	String expectedText =
		"package org.sample;\n"
		+ "\n"
		+ "public class Baz {\n"
		+ "    String name;\n"
		+ "}\n";
	//@formatter:on

	String newText = TextEditUtil.apply(unit, edits);
	assertEquals(expectedText, newText);
}
 
Example 17
Source File: MoveHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testGetInstanceMethodDestinations() throws Exception {
	when(preferenceManager.getClientPreferences().isMoveRefactoringSupported()).thenReturn(true);

	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	//@formatter:off
	pack1.createCompilationUnit("Second.java", "package test1;\n"
			+ "\n"
			+ "public class Second {\n"
			+ "    public void foo() {\n"
			+ "    }\n"
			+ "}",
			false, null);
	//@formatter:on

	//@formatter:off
	pack1.createCompilationUnit("Third.java", "package test1;\n"
			+ "\n"
			+ "public class Third {\n"
			+ "    public void bar() {\n"
			+ "    }\n"
			+ "}",
			false, null);
	//@formatter:on

	//@formatter:off
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n"
			+ "\n"
			+ "public class E {\n"
			+ "    Second s;\n"
			+ "    String name;\n"
			+ "    int id;\n"
			+ "    public void print(Third t) {\n"
			+ "        s.foo();\n"
			+ "        t.bar();\n"
			+ "    }\n"
			+ "}",
			false, null);
	//@formatter:on

	CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "s.foo");
	MoveParams moveParams = new MoveParams("moveInstanceMethod", new String[] { JDTUtils.toURI(cu) }, params);
	MoveDestinationsResponse response = MoveHandler.getMoveDestinations(moveParams);
	assertNotNull(response);
	assertNull(response.errorMessage);
	assertNotNull(response.destinations);
	assertEquals(2, response.destinations.length);
	assertEquals("t", ((LspVariableBinding) response.destinations[0]).name);
	assertFalse(((LspVariableBinding) response.destinations[0]).isField);
	assertEquals("Third", ((LspVariableBinding) response.destinations[0]).type);
	assertEquals("s", ((LspVariableBinding) response.destinations[1]).name);
	assertTrue(((LspVariableBinding) response.destinations[1]).isField);
	assertEquals("Second", ((LspVariableBinding) response.destinations[1]).type);
}
 
Example 18
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private Either<Range, PrepareRenameResult> prepareRename(ICompilationUnit cu, Position pos, String newName) {
	TextDocumentIdentifier identifier = new TextDocumentIdentifier(JDTUtils.toURI(cu));

	TextDocumentPositionParams params = new TextDocumentPositionParams(identifier, pos);
	return handler.prepareRename(params, monitor);
}
 
Example 19
Source File: FileEventHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testDidRenameFiles_fileNameRenamed() throws JavaModelException, BadLocationException {
	when(clientPreferences.isResourceOperationSupported()).thenReturn(true);

	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	StringBuilder builderA = new StringBuilder();
	builderA.append("package test1;\n");
	builderA.append("public class A {\n");
	builderA.append("	public void foo() {\n");
	builderA.append("	}\n");
	builderA.append("}\n");
	ICompilationUnit cuA = pack1.createCompilationUnit("ANew.java", builderA.toString(), false, null);
	
	StringBuilder builderB = new StringBuilder();
	builderB.append("package test1;\n");
	builderB.append("public class B {\n");
	builderB.append("	public void foo() {\n");
	builderB.append("		A a = new A();\n");
	builderB.append("		a.foo();\n");
	builderB.append("	}\n");
	builderB.append("}\n");
	ICompilationUnit cuB = pack1.createCompilationUnit("B.java", builderB.toString(), false, null);

	String uriA = JDTUtils.toURI(cuA);
	String oldUriA = uriA.replace("ANew", "A");
	WorkspaceEdit edit = FileEventHandler.handleRenameFiles(new FileRenameParams(Arrays.asList(new FileRenameEvent(oldUriA, uriA))), new NullProgressMonitor());
	assertNotNull(edit);
	assertEquals(2, edit.getDocumentChanges().size());

	assertTrue(edit.getDocumentChanges().get(0).isLeft());
	assertEquals(edit.getDocumentChanges().get(0).getLeft().getTextDocument().getUri(), uriA);
	assertEquals(TextEditUtil.apply(builderA.toString(), edit.getDocumentChanges().get(0).getLeft().getEdits()),
			"package test1;\n" +
			"public class ANew {\n" +
			"	public void foo() {\n" +
			"	}\n" +
			"}\n"
			);

	assertTrue(edit.getDocumentChanges().get(1).isLeft());
	assertEquals(edit.getDocumentChanges().get(1).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
	assertEquals(TextEditUtil.apply(builderB.toString(), edit.getDocumentChanges().get(1).getLeft().getEdits()),
			"package test1;\n" +
			"public class B {\n" +
			"	public void foo() {\n" +
			"		ANew a = new ANew();\n" +
			"		a.foo();\n" +
			"	}\n" +
			"}\n"
			);
}
 
Example 20
Source File: SyntaxLanguageServer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<String> classFileContents(TextDocumentIdentifier param) {
	logInfo(">> java/classFileContents");
	URI uri = JDTUtils.toURI(param.getUri());
	return computeAsync((monitor) -> contentProviderManager.getContent(uri, monitor));
}