Search in sources :

Example 96 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier in project xtext-core by eclipse.

the class RenameTest3 method testRenameQuotedRef.

@Test
public void testRenameQuotedRef() throws Exception {
    String model = "type ^type {\n" + "}\n" + "\n" + "type Bar extends ^type {\n" + "}\n" + "";
    String file = writeFile("foo/Foo.renametl", model);
    initialize();
    TextDocumentIdentifier identifier = new TextDocumentIdentifier(file);
    Position position = new Position(3, 19);
    Range range = languageServer.prepareRename(new PrepareRenameParams(identifier, position)).get().getLeft();
    assertEquals("^type", new Document(0, model).getSubstring(range));
    RenameParams params = new RenameParams(identifier, position, "Foo");
    WorkspaceEdit workspaceEdit = languageServer.rename(params).get();
    String expectation = "changes :\n" + "documentChanges : \n" + "    Foo.renametl <1> : Foo [[0, 5] .. [0, 10]]\n" + "    Foo [[3, 17] .. [3, 22]]\n" + "";
    assertEquals(expectation.toString(), toExpectation(workspaceEdit));
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) PrepareRenameParams(org.eclipse.lsp4j.PrepareRenameParams) RenameParams(org.eclipse.lsp4j.RenameParams) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Range(org.eclipse.lsp4j.Range) PrepareRenameParams(org.eclipse.lsp4j.PrepareRenameParams) Document(org.eclipse.xtext.ide.server.Document) Test(org.junit.Test) AbstractLanguageServerTest(org.eclipse.xtext.testing.AbstractLanguageServerTest)

Example 97 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier in project xtext-core by eclipse.

the class AbstractIdeQuickfixTest method quickfixesAreOffered.

private void quickfixesAreOffered(EObject target, String issueCode, String originalText, QuickfixExpectation... expected) {
    List<QuickfixExpectation> expectedSorted = IterableExtensions.sortBy(Arrays.asList(expected), it -> it.label);
    ICompositeNode elementNode = NodeModelUtils.getNode(target);
    LineAndColumn elementStartPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getOffset());
    LineAndColumn elementEndPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getEndOffset());
    Position startPos = new Position(elementStartPosition.getLine() - 1, elementStartPosition.getColumn() - 1);
    Position endPos = new Position(elementEndPosition.getLine() - 1, elementEndPosition.getColumn() - 1);
    Diagnostic issue = new Diagnostic();
    issue.setCode(issueCode);
    issue.setMessage("error");
    issue.setSeverity(DiagnosticSeverity.Error);
    issue.setSource("source");
    issue.setRange(new Range(startPos, endPos));
    ICodeActionService2.Options options = new ICodeActionService2.Options();
    options.setCancelIndicator(CancelIndicator.NullImpl);
    options.setDocument(new Document(Integer.valueOf(0), originalText));
    options.setResource((XtextResource) target.eResource());
    options.setLanguageServerAccess(new ILanguageServerAccess() {

        @Override
        public void addBuildListener(ILanguageServerAccess.IBuildListener listener) {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T extends Object> CompletableFuture<T> doRead(String uri, Function<ILanguageServerAccess.Context, T> function) {
            ILanguageServerAccess.Context ctx = new ILanguageServerAccess.Context(options.getResource(), options.getDocument(), true, CancelIndicator.NullImpl);
            return CompletableFuture.completedFuture(function.apply(ctx));
        }

        @Override
        public <T extends Object> CompletableFuture<T> doReadIndex(Function<? super ILanguageServerAccess.IndexContext, ? extends T> function) {
            return null;
        }

        @Override
        public InitializeParams getInitializeParams() {
            return null;
        }

        @Override
        public InitializeResult getInitializeResult() {
            return null;
        }

        @Override
        public LanguageClient getLanguageClient() {
            return null;
        }

        @Override
        public ResourceSet newLiveScopeResourceSet(URI uri) {
            // re-using the existing ResourceSet because it contains the URI protocol mapping for "inmemory" resources.
            ResourceSet resourceSet = options.getResource().getResourceSet();
            return resourceSet;
        }
    });
    CodeActionParams codeActionParams = new CodeActionParams();
    codeActionParams.setRange(new Range(startPos, endPos));
    codeActionParams.setTextDocument(new TextDocumentIdentifier(target.eResource().getURI().toString()));
    CodeActionContext codeActionContext = new CodeActionContext();
    codeActionContext.setDiagnostics(Collections.singletonList(issue));
    codeActionParams.setContext(codeActionContext);
    options.setCodeActionParams(codeActionParams);
    List<DiagnosticResolution> actualIssueResolutions = IterableExtensions.sortBy(quickFixProvider.getResolutions(options, issue), DiagnosticResolution::getLabel);
    assertEquals("The number of quickfixes does not match!", expectedSorted.size(), actualIssueResolutions.size());
    for (int i = 0; i < actualIssueResolutions.size(); i++) {
        DiagnosticResolution actualIssueResolution = actualIssueResolutions.get(i);
        QuickfixExpectation expectedIssueResolution = expectedSorted.get(i);
        assertEquals(expectedIssueResolution.label, actualIssueResolution.getLabel());
        assertEquals(expectedIssueResolution.description, actualIssueResolution.getLabel());
        assertIssueResolutionResult(toUnixLineSeparator(expectedIssueResolution.getExpectedResult()), actualIssueResolution, originalText, options.getDocument());
    }
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) DiagnosticResolution(org.eclipse.xtext.ide.editor.quickfix.DiagnosticResolution) Diagnostic(org.eclipse.lsp4j.Diagnostic) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) CompletableFuture(java.util.concurrent.CompletableFuture) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ILanguageServerAccess(org.eclipse.xtext.ide.server.ILanguageServerAccess) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Position(org.eclipse.lsp4j.Position) ICodeActionService2(org.eclipse.xtext.ide.server.codeActions.ICodeActionService2) InitializeParams(org.eclipse.lsp4j.InitializeParams) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LineAndColumn(org.eclipse.xtext.util.LineAndColumn) Range(org.eclipse.lsp4j.Range) InitializeResult(org.eclipse.lsp4j.InitializeResult) CodeActionContext(org.eclipse.lsp4j.CodeActionContext)

Example 98 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier in project xtext-core by eclipse.

the class PrepareRenameTest method testRenameFqn_missing_file_null.

@Test
public void testRenameFqn_missing_file_null() throws Exception {
    String uri = uriExtensions.toUriString(new File("missing." + fileExtension).toURI().normalize());
    initializeWithPrepareSupport();
    RenameParams params = new RenameParams(new TextDocumentIdentifier(uri), new Position(2, 5), "Does not matter");
    Assert.assertNull(languageServer.rename(params).get());
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) PrepareRenameParams(org.eclipse.lsp4j.PrepareRenameParams) RenameParams(org.eclipse.lsp4j.RenameParams) File(java.io.File) Test(org.junit.Test)

Example 99 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier in project xtext-core by eclipse.

the class PrepareRenameTest method testRenameFqn_before_ok.

@Test
public void testRenameFqn_before_ok() throws Exception {
    String model = "package foo.bar {\n" + "  type A {\n" + "    foo.bar.MyType bar\n" + "  }\n" + "  type MyType { }\n" + "}\n";
    String uri = writeFile("my-type-valid.testlang", model);
    initializeWithPrepareSupport();
    RenameParams params = new RenameParams(new TextDocumentIdentifier(uri), new Position(2, 13), "YourType");
    WorkspaceEdit workspaceEdit = languageServer.rename(params).get();
    String expectation = "changes :\n" + "	my-type-valid.testlang : foo.bar.YourType [[2, 4] .. [2, 18]]\n" + "	YourType [[4, 7] .. [4, 13]]\n" + "documentChanges : \n";
    assertEquals(expectation.toString(), toExpectation(workspaceEdit));
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) PrepareRenameParams(org.eclipse.lsp4j.PrepareRenameParams) RenameParams(org.eclipse.lsp4j.RenameParams) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Test(org.junit.Test)

Example 100 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier in project xtext-core by eclipse.

the class PrepareRenameTest method testPrepareRenameFqn_missing_file_exception.

@Test
public void testPrepareRenameFqn_missing_file_exception() throws Exception {
    String uri = uriExtensions.toUriString(new File("missing." + fileExtension).toURI().normalize());
    initialize();
    PrepareRenameParams params = new PrepareRenameParams(new TextDocumentIdentifier(uri), new Position(2, 5));
    try {
        Assert.assertNull(languageServer.prepareRename(params).get());
        Assert.fail("Expected an error.");
    } catch (Exception e) {
        Assert.assertTrue(Throwables.getRootCause(e) instanceof FileNotFoundException);
    }
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) FileNotFoundException(java.io.FileNotFoundException) PrepareRenameParams(org.eclipse.lsp4j.PrepareRenameParams) File(java.io.File) ResponseErrorException(org.eclipse.lsp4j.jsonrpc.ResponseErrorException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Aggregations

TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)172 Test (org.junit.Test)113 Position (org.eclipse.lsp4j.Position)102 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)56 Range (org.eclipse.lsp4j.Range)47 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)37 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)35 URI (java.net.URI)34 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)33 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)32 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)32 List (java.util.List)26 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)25 Location (org.eclipse.lsp4j.Location)23 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)22 Command (org.eclipse.lsp4j.Command)22 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)20 FormattingOptions (org.eclipse.lsp4j.FormattingOptions)19 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)18 CodeAction (org.eclipse.lsp4j.CodeAction)18