@Test public void testCodeAction_exception() throws JavaModelException { URI uri = project.getFile("nopackage/Test.java").getRawLocationURI(); ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri); try { cu.becomeWorkingCopy(new NullProgressMonitor()); CodeActionParams params = new CodeActionParams(); params.setTextDocument(new TextDocumentIdentifier(uri.toString())); final Range range = new Range(); range.setStart(new Position(0, 17)); range.setEnd(new Position(0, 17)); params.setRange(range); CodeActionContext context = new CodeActionContext(); context.setDiagnostics(Collections.emptyList()); params.setContext(context); List<? extends Command> commands = server.codeAction(params).join(); Assert.assertNotNull(commands); Assert.assertEquals(0, commands.size()); } finally { cu.discardWorkingCopy(); } }
@Test public void testProvideDocumentationOnHover() throws Exception { CamelLanguageServer camelLanguageServer = initializeLanguageServer("<from uri=\"ahc:httpUri\" xmlns=\"http://camel.apache.org/schema/spring\"></from>\n"); TextDocumentPositionParams position = new TextDocumentPositionParams(new TextDocumentIdentifier(DUMMY_URI), new Position(0, 13)); CompletableFuture<Hover> hover = camelLanguageServer.getTextDocumentService().hover(position); assertThat(hover.get().getContents().get(0).getLeft()).isEqualTo(AHC_DOCUMENTATION); }
private SignatureHelp doHelp(String file, int row, int column) throws IOException { TextDocumentIdentifier document = new TextDocumentIdentifier(); document.setUri(FindResource.uri(file).toString()); Position position = new Position(); position.setLine(row - 1); position.setCharacter(column - 1); TextDocumentPositionParams p = new TextDocumentPositionParams(); p.setTextDocument(document); p.setPosition(position); try { return server.getTextDocumentService().signatureHelp(p).get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } }
@Test public void testEquals() { Assert.assertEquals(new TextDocumentIdentifier("foo"), new TextDocumentIdentifier("foo")); Assert.assertNotEquals(new TextDocumentIdentifier("foo"), new TextDocumentIdentifier("bar")); CompletionItem e1 = new CompletionItem(); e1.setAdditionalTextEdits(new ArrayList<>()); e1.getAdditionalTextEdits().add(new TextEdit(new Range(new Position(1,1), new Position(1,1)), "foo")); CompletionItem e2 = new CompletionItem(); e2.setAdditionalTextEdits(new ArrayList<>()); e2.getAdditionalTextEdits().add(new TextEdit(new Range(new Position(1,1), new Position(1,1)), "foo")); CompletionItem e3 = new CompletionItem(); e3.setAdditionalTextEdits(new ArrayList<>()); e3.getAdditionalTextEdits().add(new TextEdit(new Range(new Position(1,1), new Position(1,2)), "foo")); Assert.assertEquals(e1, e2); Assert.assertNotEquals(e1, e3); Assert.assertEquals(e1.hashCode(), e2.hashCode()); Assert.assertNotEquals(e1.hashCode(), e3.hashCode()); }
@Test public void testRequest() throws Exception { TextDocumentPositionParams p = new TextDocumentPositionParams(); p.setPosition(new Position(1,1)); p.setTextDocument(new TextDocumentIdentifier("test/foo.txt")); CompletionList result = new CompletionList(); result.setIsIncomplete(true); result.setItems(new ArrayList<>()); CompletionItem item = new CompletionItem(); item.setDetail("test"); item.setDocumentation("doc"); item.setFilterText("filter"); item.setInsertText("insert"); item.setKind(CompletionItemKind.Field); result.getItems().add(item); server.expectedRequests.put("textDocument/completion", new Pair<>(p, result)); CompletableFuture<Either<List<CompletionItem>, CompletionList>> future = clientLauncher.getRemoteProxy().getTextDocumentService().completion(p); Assert.assertEquals(Either.forRight(result).toString(), future.get(TIMEOUT, TimeUnit.MILLISECONDS).toString()); client.joinOnEmpty(); }
@Test public void testWillSaveWaitUntil() throws Exception { URI srcUri = project.getFile("src/java/Foo4.java").getRawLocationURI(); ICompilationUnit cu = JDTUtils.resolveCompilationUnit(srcUri); StringBuilder buf = new StringBuilder(); buf.append("package java;\n"); buf.append("\n"); buf.append("public class Foo4 {\n"); buf.append("}\n"); WillSaveTextDocumentParams params = new WillSaveTextDocumentParams(); TextDocumentIdentifier document = new TextDocumentIdentifier(); document.setUri(srcUri.toString()); params.setTextDocument(document); List<TextEdit> result = handler.willSaveWaitUntil(params, monitor); Document doc = new Document(); doc.set(cu.getSource()); assertEquals(TextEditUtil.apply(doc, result), buf.toString()); }
@Test public void testReference(){ URI uri = project.getFile("src/java/Foo2.java").getRawLocationURI(); String fileURI = ResourceUtils.fixURI(uri); ReferenceParams param = new ReferenceParams(); param.setPosition(new Position(5,16)); param.setContext(new ReferenceContext(true)); param.setTextDocument( new TextDocumentIdentifier(fileURI)); List<Location> references = handler.findReferences(param, monitor); assertNotNull("findReferences should not return null",references); assertEquals(1, references.size()); Location l = references.get(0); String refereeUri = ResourceUtils.fixURI(project.getFile("src/java/Foo3.java").getRawLocationURI()); assertEquals(refereeUri, l.getUri()); }
@Test public void testCodeAction_removeUnusedImport() throws Exception{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "import java.sql.*; \n" + "public class Foo {\n"+ " void foo() {\n"+ " }\n"+ "}\n"); CodeActionParams params = new CodeActionParams(); params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit))); final Range range = getRange(unit, "java.sql"); params.setRange(range); params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnusedImport), range)))); List<? extends Command> commands = server.codeAction(params).join(); Assert.assertNotNull(commands); Assert.assertEquals(2, commands.size()); Command c = commands.get(0); Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand()); }
@Test public void testCodeAction_removeUnterminatedString() throws Exception{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ "String s = \"some str\n" + " }\n"+ "}\n"); CodeActionParams params = new CodeActionParams(); params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit))); final Range range = getRange(unit, "some str"); params.setRange(range); params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnterminatedString), range)))); List<? extends Command> commands = server.codeAction(params).join(); Assert.assertNotNull(commands); Assert.assertEquals(1, commands.size()); Command c = commands.get(0); Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand()); }
@Test public void testHoverThrowable() throws Exception { String uriString = ClassFileUtil.getURI(project, "java.lang.Exception"); IClassFile classFile = JDTUtils.resolveClassFile(uriString); String contents = JavaLanguageServerPlugin.getContentProviderManager().getSource(classFile, monitor); IDocument document = new Document(contents); IRegion region = new FindReplaceDocumentAdapter(document).find(0, "Throwable", true, false, false, false); int offset = region.getOffset(); int line = document.getLineOfOffset(offset); int character = offset - document.getLineOffset(line); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uriString); Position position = new Position(line, character); TextDocumentPositionParams params = new TextDocumentPositionParams(textDocument, position); Hover hover = handler.hover(params, monitor); assertNotNull(hover); assertTrue("Unexpected hover ", !hover.getContents().isEmpty()); }
@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); }
@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); }
protected List<Command> evaluateCodeActions(ICompilationUnit cu) throws JavaModelException { CompilationUnit astRoot = SharedASTProvider.getInstance().getAST(cu, null); IProblem[] problems = astRoot.getProblems(); Range range = getRange(cu, problems); CodeActionParams parms = new CodeActionParams(); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(); textDocument.setUri(JDTUtils.toURI(cu)); parms.setTextDocument(textDocument); parms.setRange(range); CodeActionContext context = new CodeActionContext(); context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(Arrays.asList(problems))); parms.setContext(context); return new CodeActionHandler().getCodeActionCommands(parms, new NullProgressMonitor()); }
protected void doTest(final String fileName, final Position position) { try { TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(fileName); final RenameParams params = new RenameParams(_textDocumentIdentifier, position, "Tescht"); final WorkspaceEdit workspaceEdit = this.languageServer.rename(params).get(); StringConcatenation _builder = new StringConcatenation(); _builder.append("changes :"); _builder.newLine(); _builder.append("\t"); _builder.append("MyType1.testlang : Tescht [[0, 5] .. [0, 9]]"); _builder.newLine(); _builder.append("\t"); _builder.append("Tescht [[1, 4] .. [1, 8]]"); _builder.newLine(); _builder.append("\t"); _builder.append("MyType2.testlang : Tescht [[1, 4] .. [1, 8]]"); _builder.newLine(); _builder.append("documentChanges : "); _builder.newLine(); this.assertEquals(_builder.toString(), this.toExpectation(workspaceEdit)); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
protected void testDocumentSymbol(final Procedure1<? super DocumentSymbolConfiguraiton> configurator) { try { @Extension final DocumentSymbolConfiguraiton configuration = new DocumentSymbolConfiguraiton(); configuration.setFilePath(("MyModel." + this.fileExtension)); configurator.apply(configuration); final String fileUri = this.initializeContext(configuration).getUri(); TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(fileUri); DocumentSymbolParams _documentSymbolParams = new DocumentSymbolParams(_textDocumentIdentifier); final CompletableFuture<List<? extends SymbolInformation>> symbolsFuture = this.languageServer.documentSymbol(_documentSymbolParams); final List<? extends SymbolInformation> symbols = symbolsFuture.get(); Procedure1<? super List<? extends SymbolInformation>> _assertSymbols = configuration.getAssertSymbols(); boolean _tripleNotEquals = (_assertSymbols != null); if (_tripleNotEquals) { configuration.getAssertSymbols().apply(symbols); } else { final String actualSymbols = this.toExpectation(symbols); this.assertEquals(configuration.getExpectedSymbols(), actualSymbols); } } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
protected void testFormatting(final Procedure1<? super FormattingConfiguration> configurator) { try { @Extension final FormattingConfiguration configuration = new FormattingConfiguration(); configuration.setFilePath(("MyModel." + this.fileExtension)); configurator.apply(configuration); final FileInfo fileInfo = this.initializeContext(configuration); DocumentFormattingParams _documentFormattingParams = new DocumentFormattingParams(); final Procedure1<DocumentFormattingParams> _function = (DocumentFormattingParams it) -> { String _uri = fileInfo.getUri(); TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(_uri); it.setTextDocument(_textDocumentIdentifier); }; DocumentFormattingParams _doubleArrow = ObjectExtensions.<DocumentFormattingParams>operator_doubleArrow(_documentFormattingParams, _function); final CompletableFuture<List<? extends TextEdit>> changes = this.languageServer.formatting(_doubleArrow); String _contents = fileInfo.getContents(); final Document result = new Document(1, _contents).applyChanges(ListExtensions.<TextEdit>reverse(CollectionLiterals.<TextEdit>newArrayList(((TextEdit[])Conversions.unwrapArray(changes.get(), TextEdit.class))))); this.assertEquals(configuration.getExpectedText(), result.getContents()); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
protected void testRangeFormatting(final Procedure1<? super RangeFormattingConfiguration> configurator) { try { @Extension final RangeFormattingConfiguration configuration = new RangeFormattingConfiguration(); configuration.setFilePath(("MyModel." + this.fileExtension)); configurator.apply(configuration); final FileInfo fileInfo = this.initializeContext(configuration); DocumentRangeFormattingParams _documentRangeFormattingParams = new DocumentRangeFormattingParams(); final Procedure1<DocumentRangeFormattingParams> _function = (DocumentRangeFormattingParams it) -> { String _uri = fileInfo.getUri(); TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(_uri); it.setTextDocument(_textDocumentIdentifier); it.setRange(configuration.getRange()); }; DocumentRangeFormattingParams _doubleArrow = ObjectExtensions.<DocumentRangeFormattingParams>operator_doubleArrow(_documentRangeFormattingParams, _function); final CompletableFuture<List<? extends TextEdit>> changes = this.languageServer.rangeFormatting(_doubleArrow); String _contents = fileInfo.getContents(); final Document result = new Document(1, _contents).applyChanges(ListExtensions.<TextEdit>reverse(CollectionLiterals.<TextEdit>newArrayList(((TextEdit[])Conversions.unwrapArray(changes.get(), TextEdit.class))))); this.assertEquals(configuration.getExpectedText(), result.getContents()); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
@Test public void testDontProvideDocumentationOnHoverForBadPlaces() throws Exception { CamelLanguageServer camelLanguageServer = initializeLanguageServer("<from uri=\"ahc:httpUri\" xmlns=\"http://camel.apache.org/schema/spring\"></from>\n"); TextDocumentPositionParams position = new TextDocumentPositionParams(new TextDocumentIdentifier(DUMMY_URI), new Position(0, 4)); CompletableFuture<Hover> hover = camelLanguageServer.getTextDocumentService().hover(position); assertThat(hover.get()).isNull(); }
protected CompletableFuture<Either<List<CompletionItem>, CompletionList>> getCompletionFor(CamelLanguageServer camelLanguageServer, Position position) { TextDocumentService textDocumentService = camelLanguageServer.getTextDocumentService(); TextDocumentPositionParams dummyCompletionPositionRequest = new TextDocumentPositionParams(new TextDocumentIdentifier(DUMMY_URI), position); CompletableFuture<Either<List<CompletionItem>, CompletionList>> completions = textDocumentService.completion(dummyCompletionPositionRequest); return completions; }
@Test public void testInvalidCompletion() { RequestMessage message = new RequestMessage(); message.setJsonrpc("2.0"); message.setId("1"); message.setMethod(MessageMethods.DOC_COMPLETION); TextDocumentPositionParams params = new TextDocumentPositionParams(); params.setTextDocument(new TextDocumentIdentifier("file:///tmp/foo")); message.setParams(params); assertIssues(message, "The accessor 'getPosition' must return a non-null value."); }
@Test public void testDocumentLink_01() throws Exception, ExecutionException { LanguageServer languageServer = wrap(LanguageServer.class, new MockLanguageServer() { @Override public CompletableFuture<List<DocumentLink>> documentLink(DocumentLinkParams params) { return CompletableFutures.computeAsync(canceler -> { return new ArrayList<>(); }); } }); CompletableFuture<List<DocumentLink>> future = languageServer.getTextDocumentService().documentLink(new DocumentLinkParams(new TextDocumentIdentifier("test"))); List<DocumentLink> list = future.get(TIMEOUT, TimeUnit.MILLISECONDS); Assert.assertTrue(list.isEmpty()); }
public void fileChanged(String uriString, CHANGE_TYPE changeType) { if (uriString == null) { return; } IResource resource = JDTUtils.findFile(uriString); if (resource == null) { return; } try { if (changeType == CHANGE_TYPE.DELETED) { resource = resource.getParent(); } if (resource != null) { resource.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); } if (isBuildFile(resource)) { FeatureStatus status = preferenceManager.getPreferences().getUpdateBuildConfigurationStatus(); switch (status) { case automatic: updateProject(resource.getProject()); break; case disabled: break; default: if (client != null) { String cmd = "java.projectConfiguration.status"; TextDocumentIdentifier uri = new TextDocumentIdentifier(uriString); ActionableNotification updateProjectConfigurationNotification = new ActionableNotification().withSeverity(MessageType.Info) .withMessage("A build file was modified. Do you want to synchronize the Java classpath/configuration?").withCommands(asList(new Command("Never", cmd, asList(uri, FeatureStatus.disabled)), new Command("Now", cmd, asList(uri, FeatureStatus.interactive)), new Command("Always", cmd, asList(uri, FeatureStatus.automatic)))); client.sendActionableNotification(updateProjectConfigurationNotification); } } } } catch (CoreException e) { JavaLanguageServerPlugin.logException("Problem refreshing workspace", e); } }
public void updateConfiguration(TextDocumentIdentifier param) { IFile file = JDTUtils.findFile(param.getUri()); if (file == null) { return; } projectManager.updateProject(file.getProject()); }
@Override public CompletableFuture<String> classFileContents(TextDocumentIdentifier param) { logInfo(">> java/classFileContents"); ContentProviderManager handler = JavaLanguageServerPlugin.getContentProviderManager(); URI uri = JDTUtils.toURI(param.getUri()); return computeAsync((cc) -> handler.getContent(uri, toMonitor(cc))); }
@Test public void testEmpty(){ ReferenceParams param = new ReferenceParams(); param.setPosition(new Position(1, 1)); param.setContext(new ReferenceContext(false)); param.setTextDocument( new TextDocumentIdentifier("/foo/bar")); List<Location> references = handler.findReferences(param, monitor); assertNotNull(references); assertTrue("references are not empty", references.isEmpty()); }
@Test @Ignore public void testCodeAction_superfluousSemicolon() throws Exception{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ ";" + " }\n"+ "}\n"); CodeActionParams params = new CodeActionParams(); params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit))); final Range range = getRange(unit, ";"); params.setRange(range); params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.SuperfluousSemicolon), range)))); List<? extends Command> commands = server.codeAction(params).join(); Assert.assertNotNull(commands); Assert.assertEquals(1, commands.size()); Command c = commands.get(0); Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand()); Assert.assertNotNull(c.getArguments()); Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit); WorkspaceEdit we = (WorkspaceEdit) c.getArguments().get(0); List<org.eclipse.lsp4j.TextEdit> edits = we.getChanges().get(JDTUtils.toURI(unit)); Assert.assertEquals(1, edits.size()); Assert.assertEquals("", edits.get(0).getNewText()); Assert.assertEquals(range, edits.get(0).getRange()); }
@Test public void testDocumentHighlightHandler() throws Exception { String uri = ClassFileUtil.getURI(project, "org.sample.Highlight"); TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri); TextDocumentPositionParams params = new TextDocumentPositionParams(identifier, new Position(5, 10)); List<? extends DocumentHighlight> highlights = handler.documentHighlight(params, monitor); assertEquals(4, highlights.size()); assertHighlight(highlights.get(0), 5, 9, 15, DocumentHighlightKind.Write); assertHighlight(highlights.get(1), 6, 2, 8, DocumentHighlightKind.Read); assertHighlight(highlights.get(2), 7, 2, 8, DocumentHighlightKind.Write); assertHighlight(highlights.get(3), 8, 2, 8, DocumentHighlightKind.Read); }
@Test public void testGetEmptyDefinition() throws Exception { List<? extends Location> definitions = handler.definition( new TextDocumentPositionParams(new TextDocumentIdentifier("/foo/bar"), new Position(1, 1)), monitor); assertNotNull(definitions); assertEquals(1, definitions.size()); assertNotNull("Location has no Range", definitions.get(0).getRange()); }
private void testClass(String className, int line, int column) throws JavaModelException { String uri = ClassFileUtil.getURI(project, className); TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri); List<? extends Location> definitions = handler .definition(new TextDocumentPositionParams(identifier, new Position(line, column)), monitor); assertNotNull(definitions); assertEquals(1, definitions.size()); assertNotNull(definitions.get(0).getUri()); assertTrue(definitions.get(0).getRange().getStart().getLine() >= 0); }
@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); }
@Test public void testDocumentFormattingWithTabs() throws Exception { 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); }
@Test public void testFormatting_onOffTags() throws Exception { ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", //@formatter:off "package org.sample;\n\n" + " public class Baz {\n"+ "// @formatter:off\n"+ "\tvoid foo(){\n"+ " }\n"+ "// @formatter:on\n"+ "}\n" //@formatter:off ); String uri = JDTUtils.toURI(unit); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri); FormattingOptions options = new FormattingOptions(4, 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"+ "// @formatter:off\n"+ "\tvoid foo(){\n"+ " }\n"+ "// @formatter:on\n"+ "}\n"; //@formatter:on String newText = TextEditUtil.apply(unit, edits); assertEquals(expectedText, newText); }
private void saveDocument(ICompilationUnit cu) throws Exception { DidSaveTextDocumentParams saveParms = new DidSaveTextDocumentParams(); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(); textDocument.setUri(JDTUtils.toURI(cu)); saveParms.setTextDocument(textDocument); saveParms.setText(cu.getSource()); lifeCycleHandler.didSave(saveParms); waitForBackgroundJobs(); }
private void closeDocument(ICompilationUnit cu) { DidCloseTextDocumentParams closeParms = new DidCloseTextDocumentParams(); TextDocumentIdentifier textDocument = new TextDocumentIdentifier(); textDocument.setUri(JDTUtils.toURI(cu)); closeParms.setTextDocument(textDocument); lifeCycleHandler.didClose(closeParms); }
private List<? extends SymbolInformation> getSymbols(String className) throws JavaModelException, UnsupportedEncodingException, InterruptedException, ExecutionException { String uri = ClassFileUtil.getURI(project, className); TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri); DocumentSymbolParams params = new DocumentSymbolParams(); params.setTextDocument(identifier); List<? extends SymbolInformation> symbols = handler.documentSymbol(params, monitor); assertTrue(symbols.size() > 0); return symbols; }
@Test public void testRenameSelfRef() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("package foo"); _builder.newLine(); _builder.newLine(); _builder.append("element Foo {"); _builder.newLine(); _builder.append(" "); _builder.append("ref Foo"); _builder.newLine(); _builder.append("}"); _builder.newLine(); final String model = _builder.toString(); final String file = this.writeFile("foo/Foo.fileawaretestlanguage", model); this.initialize(); TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(file); Position _position = new Position(2, 9); final RenameParams params = new RenameParams(_textDocumentIdentifier, _position, "Bar"); final WorkspaceEdit workspaceEdit = this.languageServer.rename(params).get(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("changes :"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("Foo.fileawaretestlanguage : Bar [[2, 8] .. [2, 11]]"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("Bar [[3, 5] .. [3, 8]]"); _builder_1.newLine(); _builder_1.append("documentChanges : "); _builder_1.newLine(); this.assertEquals(_builder_1.toString(), this.toExpectation(workspaceEdit)); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
protected void close(final String fileUri) { DidCloseTextDocumentParams _didCloseTextDocumentParams = new DidCloseTextDocumentParams(); final Procedure1<DidCloseTextDocumentParams> _function = (DidCloseTextDocumentParams it) -> { TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(fileUri); it.setTextDocument(_textDocumentIdentifier); }; DidCloseTextDocumentParams _doubleArrow = ObjectExtensions.<DidCloseTextDocumentParams>operator_doubleArrow(_didCloseTextDocumentParams, _function); this.languageServer.didClose(_doubleArrow); }
protected void testDefinition(final Procedure1<? super DefinitionTestConfiguration> configurator) { try { @Extension final DefinitionTestConfiguration configuration = new DefinitionTestConfiguration(); configuration.setFilePath(("MyModel." + this.fileExtension)); configurator.apply(configuration); final String fileUri = this.initializeContext(configuration).getUri(); TextDocumentPositionParams _textDocumentPositionParams = new TextDocumentPositionParams(); final Procedure1<TextDocumentPositionParams> _function = (TextDocumentPositionParams it) -> { TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(fileUri); it.setTextDocument(_textDocumentIdentifier); int _line = configuration.getLine(); int _column = configuration.getColumn(); Position _position = new Position(_line, _column); it.setPosition(_position); }; TextDocumentPositionParams _doubleArrow = ObjectExtensions.<TextDocumentPositionParams>operator_doubleArrow(_textDocumentPositionParams, _function); final CompletableFuture<List<? extends Location>> definitionsFuture = this.languageServer.definition(_doubleArrow); final List<? extends Location> definitions = definitionsFuture.get(); Procedure1<? super List<? extends Location>> _assertDefinitions = configuration.getAssertDefinitions(); boolean _tripleNotEquals = (_assertDefinitions != null); if (_tripleNotEquals) { configuration.getAssertDefinitions().apply(definitions); } else { final String actualDefinitions = this.toExpectation(definitions); this.assertEquals(configuration.getExpectedDefinitions(), actualDefinitions); } } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
@Test public void testDocumentLink_02() throws Exception, ExecutionException { LanguageServer languageServer = wrap(LanguageServer.class, new MockLanguageServer() { @Override public CompletableFuture<List<DocumentLink>> documentLink(DocumentLinkParams params) { return CompletableFutures.computeAsync(canceler -> { return null; }); } }); CompletableFuture<List<DocumentLink>> future = languageServer.getTextDocumentService().documentLink(new DocumentLinkParams(new TextDocumentIdentifier("test"))); List<DocumentLink> list = future.get(TIMEOUT, TimeUnit.MILLISECONDS); Assert.assertNull(list); }
@Override public CompletableFuture<ICodeLens[]> provideCodeLenses(ICodeLensContext context, IProgressMonitor monitor) { ITextEditor textEditor = ((IEditorCodeLensContext) context).getTextEditor(); LSPDocumentInfo info = null; Collection<LSPDocumentInfo> infos = LanguageServiceAccessor.getLSPDocumentInfosFor( LSPEclipseUtils.getDocument((ITextEditor) textEditor), capabilities -> capabilities.getCodeLensProvider() != null); if (!infos.isEmpty()) { info = infos.iterator().next(); } else { info = null; } if (info != null) { CodeLensParams param = new CodeLensParams(new TextDocumentIdentifier(info.getFileUri().toString())); final CompletableFuture<List<? extends CodeLens>> codeLens = info.getLanguageClient() .getTextDocumentService().codeLens(param); return codeLens.thenApply(lens -> { List<ICodeLens> lenses = new ArrayList<>(); for (CodeLens cl : lens) { lenses.add(new LSPCodeLens(cl)); } return lenses.toArray(new ICodeLens[lenses.size()]); }); // try { // // // // List<ICodeLens> lenses = new ArrayList<>(); // List<? extends CodeLens> lens = codeLens.get(5000, TimeUnit.MILLISECONDS); // for (CodeLens cl : lens) { // lenses.add(new LSPCodeLens(cl)); // } // return lenses.toArray(new ICodeLens[lenses.size()]); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } } return null; }