org.eclipse.lsp4j.PrepareRenameResult Java Examples

The following examples show how to use org.eclipse.lsp4j.PrepareRenameResult. 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: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameJavadoc() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

	String[] codes = {
			"package test1;\n",
			"public class E {\n",
			"	/**\n",
			"	 *@param i int\n",
			"	 */\n",
			"   public int foo(int i|*) {\n",
			"		E e = new E();\n",
			"		e.foo();\n",
			"   }\n",
			"}\n"
	};
	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);

	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "i2");
	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #2
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameParameter() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

	String[] codes = {
		"package test1;\n",
		"public class E {\n",
		"   public int foo(String str) {\n",
		"  		str|*.length();\n",
		"   }\n",
		"   public int bar(String str) {\n",
		"   	str.length();\n",
		"   }\n",
		"}\n"
	};
	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);

	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "newname");

	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #3
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameLocalVariable() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

	String[] codes = {
		"package test1;\n",
		"public class E {\n",
		"   public int bar() {\n",
		"		String str = new String();\n",
		"   	str.length();\n",
		"   }\n",
		"   public int foo() {\n",
		"		String str = new String();\n",
		"   	str|*.length()\n",
		"   }\n",
		"}\n"
	};
	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);

	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "newname");
	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #4
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameField() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

	String[] codes = {
			"package test1;\n",
			"public class E {\n",
			"	private int myValue = 2;\n",
			"   public void bar() {\n",
			"		myValue|* = 3;\n",
			"   }\n",
			"}\n"
	};
	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);

	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "newname");
	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #5
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameMethod() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

	String[] codes = {
		"package test1;\n",
		"public class E {\n",
		"   public int bar() {\n",
		"   }\n",
		"   public int foo() {\n",
		"		this.bar|*();\n",
		"   }\n",
		"}\n"
	};
	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);

	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "newname");
	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #6
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameTypeWithResourceChanges() throws JavaModelException, BadLocationException {
	when(clientPreferences.isResourceOperationSupported()).thenReturn(true);

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

	String[] codes = { "package test1;\n",
			           "public class E|* {\n",
			           "   public E() {\n",
			           "   }\n",
			           "   public int bar() {\n", "   }\n",
			           "   public int foo() {\n",
			           "		this.bar();\n",
			           "   }\n",
			           "}\n" };
	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);

	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "Newname");
	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #7
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameTypeParameter() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

	String[] codes= {
			"package test1;\n",
			"public class A<T|*> {\n",
			"	private T t;\n",
			"	public T get() { return t; }\n",
			"}\n"
	};

	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("A.java", builder.toString(), false, null);


	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "TT");
	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #8
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameTypeParameterInMethod() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

	String[] codes = {
			"package test1;\n",
			"public class B<T> {\n",
			"	private T t;\n",
			"	public <U|* extends Number> inspect(U u) { return u; }\n",
			"}\n"
	};

	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("B.java", builder.toString(), false, null);

	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "UU");
	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #9
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRenameLambdaParameter() throws JavaModelException, BadLocationException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

	// @formatter:off
	String[] codes =
		{
			"package test1;\n",
			"import java.util.function.Function;\n",
			"public class Test {\n",
			"    Function<Integer, String> f = i|* -> \"\" + i;\n",
			"}\n"
		};
	// @formatter:on
	StringBuilder builder = new StringBuilder();
	Position pos = mergeCode(builder, codes);
	ICompilationUnit cu = pack1.createCompilationUnit("Test.java", builder.toString(), false, null);

	Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "j");
	assertNotNull(result.getLeft());
	assertTrue(result.getLeft().getStart().getLine() > 0);
}
 
Example #10
Source File: XLanguageServerImpl.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Prepare the rename operation. Executed in a read request.
 */
protected Either<Range, PrepareRenameResult> prepareRename(OpenFileContext ofc, TextDocumentPositionParams params,
		CancelIndicator cancelIndicator) {
	URI uri = ofc.getURI();
	IRenameService2 renameService = getService(uri, IRenameService2.class);
	if (renameService == null) {
		throw new UnsupportedOperationException();
	}
	IRenameService2.PrepareRenameOptions options = new IRenameService2.PrepareRenameOptions();
	options.setLanguageServerAccess(access);
	options.setParams(params);
	options.setCancelIndicator(cancelIndicator);
	return renameService.prepareRename(options);
}
 
Example #11
Source File: LanguageServerImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Prepare the rename operation. Executed in a read request.
 * @since 2.20
 */
protected Either<Range, PrepareRenameResult> prepareRename(PrepareRenameParams params,
		CancelIndicator cancelIndicator) {
	URI uri = getURI(params);
	IRenameService2 renameService = getService(uri, IRenameService2.class);
	if (renameService == null) {
		throw new UnsupportedOperationException();
	}
	IRenameService2.PrepareRenameOptions options = new IRenameService2.PrepareRenameOptions();
	options.setLanguageServerAccess(access);
	options.setParams(params);
	options.setCancelIndicator(cancelIndicator);
	return renameService.prepareRename(options);
}
 
Example #12
Source File: RenamePositionTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void renameAndFail(final String model, final Position position, final String messageFragment) {
  final String modelFile = this.writeFile("MyType.testlang", model);
  this.initialize();
  try {
    final TextDocumentIdentifier identifier = new TextDocumentIdentifier(modelFile);
    PrepareRenameParams _prepareRenameParams = new PrepareRenameParams(identifier, position);
    final Either<Range, PrepareRenameResult> prepareRenameResult = this.languageServer.prepareRename(_prepareRenameParams).get();
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("expected null result got ");
    _builder.append(prepareRenameResult);
    _builder.append(" instead");
    Assert.assertNull(_builder.toString(), prepareRenameResult);
    TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(modelFile);
    final RenameParams renameParams = new RenameParams(_textDocumentIdentifier, position, "Tescht");
    this.languageServer.rename(renameParams).get();
    Assert.fail("Rename should have failed");
  } catch (final Throwable _t) {
    if (_t instanceof Exception) {
      final Exception exc = (Exception)_t;
      final Throwable rootCause = Throwables.getRootCause(exc);
      Assert.assertTrue((rootCause instanceof ResponseErrorException));
      final ResponseError error = ((ResponseErrorException) rootCause).getResponseError();
      Assert.assertTrue(error.getData().toString().contains(messageFragment));
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
}
 
Example #13
Source File: JDTLanguageServer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Either<Range, PrepareRenameResult>> prepareRename(PrepareRenameParams params) {
	logInfo(">> document/prepareRename");
	PrepareRenameHandler handler = new PrepareRenameHandler();
	return computeAsync((monitor) -> {
		waitForLifecycleJobs(monitor);
		return handler.prepareRename(params, monitor);
	});
}
 
Example #14
Source File: PrepareRenameHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public Either<Range, PrepareRenameResult> prepareRename(TextDocumentPositionParams params, IProgressMonitor monitor) {

		final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
		if (unit != null) {
			try {
				OccurrencesFinder finder = new OccurrencesFinder();
				CompilationUnit ast = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor);

				if (ast != null) {
					int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), params.getPosition().getLine(), params.getPosition().getCharacter());
					String error = finder.initialize(ast, offset, 0);
					if (error == null) {
						OccurrenceLocation[] occurrences = finder.getOccurrences();
						if (occurrences != null) {
							for (OccurrenceLocation loc : occurrences) {
								if (monitor.isCanceled()) {
									return Either.forLeft(new Range());
								}
								if (loc.getOffset() <= offset && loc.getOffset() + loc.getLength() >= offset) {
									InnovationContext context = new InnovationContext(unit, loc.getOffset(), loc.getLength());
									context.setASTRoot(ast);
									ASTNode node = context.getCoveredNode();
									// Rename package is not fully supported yet.
									if (!isBinaryOrPackage(node)) {
										return Either.forLeft(JDTUtils.toRange(unit, loc.getOffset(), loc.getLength()));
									}
								}
							}
						}
					}
				}

			} catch (CoreException e) {
				JavaLanguageServerPlugin.logException("Problem computing occurrences for" + unit.getElementName() + " in prepareRename", e);
			}
		}
		throw new ResponseErrorException(new ResponseError(ResponseErrorCode.InvalidRequest, "Renaming this element is not supported.", null));
	}
 
Example #15
Source File: XLanguageServerImpl.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @since 2.18
 */
@Override
public CompletableFuture<Either<Range, PrepareRenameResult>> prepareRename(TextDocumentPositionParams params) {
	URI uri = getURI(params);
	return openFilesManager.runInOpenFileContext(uri, "prepareRename", (ofc, ci) -> {
		return prepareRename(ofc, params, ci);
	});
}
 
Example #16
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 #17
Source File: RenameService2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected Either<Range, PrepareRenameResult> doPrepareRename(Resource resource, Document document,
		PrepareRenameParams params, CancelIndicator cancelIndicator) {
	String uri = params.getTextDocument().getUri();
	if (resource instanceof XtextResource) {
		ICompositeNode rootNode = null;
		XtextResource xtextResource = (XtextResource) resource;
		if (xtextResource != null) {
			IParseResult parseResult = xtextResource.getParseResult();
			if (parseResult != null) {
				rootNode = parseResult.getRootNode();
			}
		}
		if (rootNode == null) {
			RenameService2.LOG.trace("Could not retrieve root node for resource. URI: " + uri);
			return null;
		}
		Position caretPosition = params.getPosition();
		try {
			int caretOffset = document.getOffSet(caretPosition);
			EObject element = null;
			int candidateOffset = caretOffset;
			do {
				element = getElementWithIdentifierAt(xtextResource, candidateOffset);
				if (element != null && !element.eIsProxy()) {
					ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, candidateOffset);
					if (leaf != null && isIdentifier(leaf)) {
						String convertedNameValue = getConvertedValue(leaf.getGrammarElement(), leaf);
						String elementName = getElementName(element);
						if (!Strings.isEmpty(convertedNameValue) && !Strings.isEmpty(elementName)
								&& Objects.equal(convertedNameValue, elementName)) {
							Position start = document.getPosition(leaf.getOffset());
							Position end = document.getPosition(leaf.getEndOffset());
							return Either.forLeft(new Range(start, end));
						}
					}
				}
				candidateOffset = (candidateOffset - 1);
			} while (((candidateOffset >= 0) && ((candidateOffset + 1) >= caretOffset)));
		} catch (IndexOutOfBoundsException e) {
			RenameService2.LOG.trace("Invalid document " + toPositionFragment(caretPosition, uri));
			return null;
		}
		RenameService2.LOG.trace("No element found at " + toPositionFragment(caretPosition, uri));
	} else {
		RenameService2.LOG.trace("Loaded resource is not an XtextResource. URI: " + resource.getURI());
	}
	return null;
}
 
Example #18
Source File: RenameService2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * If this method returns {@code false}, it is sure, that the rename operation will fail. There is no guarantee that
 * it will succeed even if it returns {@code true}.
 */
protected boolean mayPerformRename(Either<Range, PrepareRenameResult> prepareRenameResult,
		RenameParams renameParams) {
	return prepareRenameResult != null && prepareRenameResult.getLeft() != null
			&& Ranges.containsPosition(prepareRenameResult.getLeft(), renameParams.getPosition());
}
 
Example #19
Source File: LanguageServerImpl.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.18
 */
@Override
public CompletableFuture<Either<Range, PrepareRenameResult>> prepareRename(PrepareRenameParams params) {
	return requestManager.runRead(cancelIndicator -> prepareRename(params, cancelIndicator));
}
 
Example #20
Source File: IRenameService2.java    From xtext-core with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Returns a {@link Range range} describing the range of the string to rename and optionally a placeholder text of
 * the string content to be renamed.
 * 
 * <p>
 * If {@code null} is returned then it is deemed that invoking {@link #rename rename} with the same text document
 * position will not result in a valid {@link WorkspaceEdit workspace edit}.
 * 
 * <p>
 * The default implementation only checks whether there is an identifier under the give text document position or
 * not.
 * 
 * <p>
 * This method should be used to set up and to test the validity of a rename operation at a given location.</br>
 * See <a href=
 * "https://microsoft.github.io/language-server-protocol/specification#textDocument_prepareRename">{@code textDocument/prepareRename}</a>
 * for more details.
 */
Either<Range, PrepareRenameResult> prepareRename(IRenameService2.PrepareRenameOptions options);
 
Example #21
Source File: TextDocumentService.java    From lsp4j with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * The prepare rename request is sent from the client to the server to setup and test the validity of a rename
 * operation at a given location.
 * 
 * Since version 3.12.0
 */
@JsonRequest
@ResponseJsonAdapter(PrepareRenameResponseAdapter.class)
default CompletableFuture<Either<Range, PrepareRenameResult>> prepareRename(PrepareRenameParams params) {
	throw new UnsupportedOperationException();
}