@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(); } }
private TextEdit change(final Position startPos, final Position endPos, final String newText) { TextEdit _textEdit = new TextEdit(); final Procedure1<TextEdit> _function = (TextEdit it) -> { if ((startPos != null)) { Range _range = new Range(); final Procedure1<Range> _function_1 = (Range it_1) -> { it_1.setStart(startPos); it_1.setEnd(endPos); }; Range _doubleArrow = ObjectExtensions.<Range>operator_doubleArrow(_range, _function_1); it.setRange(_doubleArrow); } it.setNewText(newText); }; return ObjectExtensions.<TextEdit>operator_doubleArrow(_textEdit, _function); }
@Test public void testRangeFormattingService() { final Procedure1<RangeFormattingConfiguration> _function = (RangeFormattingConfiguration it) -> { StringConcatenation _builder = new StringConcatenation(); _builder.append("type Foo{int bar} type Bar{Foo foo}"); it.setModel(_builder.toString()); Range _range = new Range(); final Procedure1<Range> _function_1 = (Range it_1) -> { Position _position = new Position(0, 0); it_1.setStart(_position); Position _position_1 = new Position(0, 17); it_1.setEnd(_position_1); }; Range _doubleArrow = ObjectExtensions.<Range>operator_doubleArrow(_range, _function_1); it.setRange(_doubleArrow); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("type Foo{"); _builder_1.newLine(); _builder_1.append("\t"); _builder_1.append("int bar"); _builder_1.newLine(); _builder_1.append("} type Bar{Foo foo}"); it.setExpectedText(_builder_1.toString()); }; this.testRangeFormatting(_function); }
private static Optional<Location> findPath(TreePath path, Trees trees) { CompilationUnitTree compilationUnit = path.getCompilationUnit(); long start = trees.getSourcePositions().getStartPosition(compilationUnit, path.getLeaf()); long end = trees.getSourcePositions().getEndPosition(compilationUnit, path.getLeaf()); if (start == Diagnostic.NOPOS) return Optional.empty(); if (end == Diagnostic.NOPOS) end = start; int startLine = (int) compilationUnit.getLineMap().getLineNumber(start); int startColumn = (int) compilationUnit.getLineMap().getColumnNumber(start); int endLine = (int) compilationUnit.getLineMap().getLineNumber(end); int endColumn = (int) compilationUnit.getLineMap().getColumnNumber(end); return Optional.of( new Location( compilationUnit.getSourceFile().toUri().toString(), new Range( new Position(startLine - 1, startColumn - 1), new Position(endLine - 1, endColumn - 1)))); }
static Optional<Diagnostic> convert(javax.tools.Diagnostic<? extends JavaFileObject> error) { if (error.getStartPosition() != javax.tools.Diagnostic.NOPOS) { Range range = position(error); Diagnostic diagnostic = new Diagnostic(); DiagnosticSeverity severity = severity(error.getKind()); diagnostic.setSeverity(severity); diagnostic.setRange(range); diagnostic.setCode(error.getCode()); diagnostic.setMessage(error.getMessage(null)); return Optional.of(diagnostic); } else { LOG.warning("Skipped " + error.getMessage(Locale.getDefault())); return Optional.empty(); } }
protected TextEdit toTextEdit(final Document document, final String formattedText, final int startOffset, final int length) { TextEdit _textEdit = new TextEdit(); final Procedure1<TextEdit> _function = (TextEdit it) -> { it.setNewText(formattedText); Range _range = new Range(); final Procedure1<Range> _function_1 = (Range it_1) -> { it_1.setStart(document.getPosition(startOffset)); it_1.setEnd(document.getPosition((startOffset + length))); }; Range _doubleArrow = ObjectExtensions.<Range>operator_doubleArrow(_range, _function_1); it.setRange(_doubleArrow); }; return ObjectExtensions.<TextEdit>operator_doubleArrow(_textEdit, _function); }
@SuppressWarnings("restriction") private static Range convertRange(IProblem problem) { Position start = new Position(); Position end = new Position(); start.setLine(problem.getSourceLineNumber()-1);// VSCode is 0-based end.setLine(problem.getSourceLineNumber()-1); if(problem instanceof DefaultProblem){ DefaultProblem dProblem = (DefaultProblem)problem; start.setCharacter(dProblem.getSourceColumnNumber() - 1); int offset = 0; if (dProblem.getSourceStart() != -1 && dProblem.getSourceEnd() != -1) { offset = dProblem.getSourceEnd() - dProblem.getSourceStart() + 1; } end.setCharacter(dProblem.getSourceColumnNumber() - 1 + offset); } return new Range(start, end); }
@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 testCompletion_import_package() throws JavaModelException{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "import java.sq \n" + "public class Foo {\n"+ " void foo() {\n"+ " }\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "java.sq"); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); assertEquals(1, list.getItems().size()); CompletionItem item = list.getItems().get(0); // Check completion item assertNull(item.getInsertText()); assertEquals("java.sql",item.getLabel()); assertEquals(CompletionItemKind.Module, item.getKind() ); assertEquals("999999215", item.getSortText()); assertNull(item.getTextEdit()); CompletionItem resolvedItem = server.resolveCompletionItem(item).join(); assertNotNull(resolvedItem); TextEdit te = item.getTextEdit(); assertNotNull(te); assertEquals("java.sql.*;",te.getNewText()); assertNotNull(te.getRange()); Range range = te.getRange(); assertEquals(0, range.getStart().getLine()); assertEquals(7, range.getStart().getCharacter()); assertEquals(0, range.getEnd().getLine()); //Not checking the range end character }
@Test public void testWorkspaceSearch() { String query = "Abstract"; List<SymbolInformation> results = handler.search(query, monitor); assertNotNull(results); assertEquals("Found " + results.size() + " results", 33, results.size()); Range defaultRange = JDTUtils.newRange(); for (SymbolInformation symbol : results) { assertNotNull("Kind is missing", symbol.getKind()); assertNotNull("ContainerName is missing", symbol.getContainerName()); assertTrue(symbol.getName().startsWith(query)); Location location = symbol.getLocation(); assertEquals(defaultRange, location.getRange()); //No class in the workspace project starts with Abstract, so everything comes from the JDK assertTrue("Unexpected uri "+ location.getUri(), location.getUri().startsWith("jdt://")); } }
@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); }
private void changeDocument(ICompilationUnit cu, String content, int version, Range range, int length) throws JavaModelException { DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams(); VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier(); textDocument.setUri(JDTUtils.toURI(cu)); textDocument.setVersion(version); changeParms.setTextDocument(textDocument); TextDocumentContentChangeEvent event = new TextDocumentContentChangeEvent(); if (range != null) { event.setRange(range); event.setRangeLength(length); } event.setText(content); List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>(); contentChanges.add(event); changeParms.setContentChanges(contentChanges); lifeCycleHandler.didChange(changeParms); }
@Test public void testGetCodeLensSymbols() throws Exception { String payload = createCodeLensSymbolsRequest("src/java/Foo.java"); CodeLensParams codeLensParams = getParams(payload); String uri = codeLensParams.getTextDocument().getUri(); assertFalse(uri.isEmpty()); //when List<CodeLens> result = handler.getCodeLensSymbols(uri, monitor); //then assertEquals("Found " + result, 3, result.size()); CodeLens cl = result.get(0); Range r = cl.getRange(); //CodeLens on main method assertRange(7, 20, 24, r); cl = result.get(1); r = cl.getRange(); // CodeLens on foo method assertRange(14, 13, 16, r); cl = result.get(2); r = cl.getRange(); //CodeLens on Foo type assertRange(5, 13, 16, r); }
@SuppressWarnings("unchecked") @Test public void testResolveImplementationsCodeLense() { String source = "src/java/IFoo.java"; String payload = createCodeLensImplementationsRequest(source, 5, 17, 21); CodeLens lens = getParams(payload); Range range = lens.getRange(); assertRange(5, 17, 21, range); CodeLens result = handler.resolve(lens, monitor); assertNotNull(result); //Check if command found Command command = result.getCommand(); assertNotNull(command); assertEquals("2 implementations", command.getTitle()); assertEquals("java.show.implementations", command.getCommand()); //Check codelens args List<Object> args = command.getArguments(); assertEquals(3, args.size()); //Check we point to the Bar class String sourceUri = args.get(0).toString(); assertTrue(sourceUri.endsWith("IFoo.java")); //CodeLens position Map<String, Object> map = (Map<String, Object>) args.get(1); assertEquals(5.0, map.get("line")); assertEquals(17.0, map.get("character")); //Reference location List<Location> locations = (List<Location>) args.get(2); assertEquals(2, locations.size()); Location loc = locations.get(0); assertTrue(loc.getUri().endsWith("src/java/Foo2.java")); assertRange(5, 13, 17, loc.getRange()); }
@Test public void withoutNull() { final List<? extends Range> input = this.sort(CollectionLiterals.<Range>newArrayList(this.newRange(1, 2, 1, 2), this.newRange(1, 1, 2, 1), this.newRange(1, 1, 1, 2), this.newRange(1, 1, 1, 1), this.newRange(2, 2, 2, 3))); Assert.assertEquals(1, input.get(0).getStart().getLine()); Assert.assertEquals(1, input.get(0).getStart().getCharacter()); Assert.assertEquals(1, input.get(0).getEnd().getLine()); Assert.assertEquals(1, input.get(0).getEnd().getCharacter()); Assert.assertEquals(1, input.get(1).getStart().getLine()); Assert.assertEquals(1, input.get(1).getStart().getCharacter()); Assert.assertEquals(1, input.get(1).getEnd().getLine()); Assert.assertEquals(2, input.get(1).getEnd().getCharacter()); Assert.assertEquals(1, input.get(2).getStart().getLine()); Assert.assertEquals(1, input.get(2).getStart().getCharacter()); Assert.assertEquals(2, input.get(2).getEnd().getLine()); Assert.assertEquals(1, input.get(2).getEnd().getCharacter()); Assert.assertEquals(1, input.get(3).getStart().getLine()); Assert.assertEquals(2, input.get(3).getStart().getCharacter()); Assert.assertEquals(1, input.get(3).getEnd().getLine()); Assert.assertEquals(2, input.get(3).getEnd().getCharacter()); Assert.assertEquals(2, input.get(4).getStart().getLine()); Assert.assertEquals(2, input.get(4).getStart().getCharacter()); Assert.assertEquals(2, input.get(4).getEnd().getLine()); Assert.assertEquals(3, input.get(4).getEnd().getCharacter()); }
private Optional<TextEdit> insertInAlphabeticalOrder(String packageName, String className) { String insertLine = String.format("import %s.%s;\n", packageName, className); return source.getImports() .stream() .filter(i -> qualifiedName(i).compareTo(packageName + "." + className) > 0) .map(this::startPosition) .findFirst() .map(at -> new TextEdit(new Range(at, at), insertLine)); }
protected String _toExpectation(final DocumentHighlight it) { String _xblockexpression = null; { StringConcatenation _builder = new StringConcatenation(); { Range _range = it.getRange(); boolean _tripleEquals = (_range == null); if (_tripleEquals) { _builder.append("[NaN, NaN]:[NaN, NaN]"); } else { String _expectation = this.toExpectation(it.getRange()); _builder.append(_expectation); } } final String rangeString = _builder.toString(); StringConcatenation _builder_1 = new StringConcatenation(); { DocumentHighlightKind _kind = it.getKind(); boolean _tripleEquals_1 = (_kind == null); if (_tripleEquals_1) { _builder_1.append("NaN"); } else { String _expectation_1 = this.toExpectation(it.getKind()); _builder_1.append(_expectation_1); } } _builder_1.append(" "); _builder_1.append(rangeString); _xblockexpression = _builder_1.toString(); } return _xblockexpression; }
protected String _toExpectation(final Range it) { StringConcatenation _builder = new StringConcatenation(); _builder.append("["); String _expectation = this.toExpectation(it.getStart()); _builder.append(_expectation); _builder.append(" .. "); String _expectation_1 = this.toExpectation(it.getEnd()); _builder.append(_expectation_1); _builder.append("]"); return _builder.toString(); }
public static void checkSends(final Map<String, SomStructures> structuralProbes, final SomStructures newProbe, final List<Diagnostic> diagnostics) { Collection<SomStructures> probes; synchronized (structuralProbes) { probes = new ArrayList<>(structuralProbes.values()); } List<Call> calls = newProbe.getCalls(); for (Call c : calls) { if (newProbe.defines(c.selector)) { continue; } boolean defined = false; for (SomStructures p : probes) { if (p.defines(c.selector)) { defined = true; break; } } if (!defined) { Range r = new Range(pos(c.sections[0].getStartLine(), c.sections[0].getStartColumn()), pos(c.sections[c.sections.length - 1].getEndLine(), c.sections[c.sections.length - 1].getEndColumn() + 1)); diagnostics.add(new Diagnostic(r, "No " + c.selector.getString() + " defined. Might cause run time error.", DiagnosticSeverity.Warning, LINT_NAME)); } } }
public static Range toRange(final SourceSection ss) { Range range = new Range(); range.setStart(pos(ss.getStartLine(), ss.getStartColumn())); range.setEnd(pos(ss.getEndLine(), ss.getEndColumn() + 1)); return range; }
public static Range toRangeMax(final SourceCoordinate coord) { Range range = new Range(); range.setStart(pos(coord.startLine, coord.startColumn)); range.setEnd(pos(coord.startLine, Integer.MAX_VALUE)); return range; }
protected Range getRange(final HoverContext it) { boolean _contains = it.getRegion().contains(it.getOffset()); boolean _not = (!_contains); if (_not) { return null; } return this._documentExtensions.newRange(it.getResource(), it.getRegion()); }
/** * Gets the end offset for the diagnostic. * * @param unit * @param range * @return starting offset or negative value if can not be determined */ public static int getEndOffset(ICompilationUnit unit, Range range){ try { return JsonRpcHelpers.toOffset(unit.getBuffer(), range.getEnd().getLine(), range.getEnd().getCharacter()); } catch (JavaModelException e) { return -1; } }
/** * Gets the start offset for the diagnostic. * * @param unit * @param range * @return starting offset or negative value if can not be determined */ public static int getStartOffset(ICompilationUnit unit, Range range){ try { return JsonRpcHelpers.toOffset(unit.getBuffer(), range.getStart().getLine(), range.getStart().getCharacter()); } catch (JavaModelException e) { return -1; } }
private CodeLens getCodeLens(String type, IJavaElement element, ITypeRoot typeRoot) throws JavaModelException { CodeLens lens = new CodeLens(); ISourceRange r = ((ISourceReference) element).getNameRange(); final Range range = JDTUtils.toRange(typeRoot, r.getOffset(), r.getLength()); lens.setRange(range); String uri = ResourceUtils.toClientUri(JDTUtils.toUri(typeRoot)); lens.setData(Arrays.asList(uri, range.getStart(), type)); return lens; }
private IRegion getRegion(Range range, IDocument document) { try { int offset = document.getLineOffset(range.getStart().getLine()) + range.getStart().getCharacter(); int endOffset = document.getLineOffset(range.getEnd().getLine()) + range.getEnd().getCharacter(); int length = endOffset - offset; return new Region(offset, length); } catch (BadLocationException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } return null; }
/** * @param marker * @return */ private static Range convertRange(IDocument document, IMarker marker) { int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1; int cStart = 0; int cEnd = 0; try { //Buildship doesn't provide markers for gradle files, Maven does if (marker.isSubtypeOf(IMavenConstants.MARKER_ID)) { cStart = marker.getAttribute(IMavenConstants.MARKER_COLUMN_START, -1); cEnd = marker.getAttribute(IMavenConstants.MARKER_COLUMN_END, -1); } else { int lineOffset = 0; try { lineOffset = document.getLineOffset(line); } catch (BadLocationException unlikelyException) { JavaLanguageServerPlugin.logException(unlikelyException.getMessage(), unlikelyException); return new Range(new Position(line, 0), new Position(line, 0)); } cEnd = marker.getAttribute(IMarker.CHAR_END, -1) - lineOffset; cStart = marker.getAttribute(IMarker.CHAR_START, -1) - lineOffset; } } catch (CoreException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } cStart = Math.max(0, cStart); cEnd = Math.max(0, cEnd); return new Range(new Position(line, cStart), new Position(line, cEnd)); }
@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()); }
private Diagnostic getDiagnostic(String code, Range range){ Diagnostic $ = new Diagnostic(); $.setCode(code); $.setRange(range); $.setSeverity(DiagnosticSeverity.Error); $.setMessage("Test Diagnostic"); return $; }
@Test public void testCompletion_constructor() throws Exception{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ " Object o = new O\n"+ " }\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "new O"); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); assertFalse("No proposals were found",list.getItems().isEmpty()); List<CompletionItem> items = new ArrayList<>(list.getItems()); Comparator<CompletionItem> comparator = (CompletionItem a, CompletionItem b) -> a.getSortText().compareTo(b.getSortText()); Collections.sort(items, comparator); CompletionItem ctor = items.get(0); assertEquals("Object()", ctor.getLabel()); assertEquals("Object", ctor.getInsertText()); CompletionItem resolvedItem = server.resolveCompletionItem(ctor).join(); assertNotNull(resolvedItem); TextEdit te = resolvedItem.getTextEdit(); assertNotNull(te); assertEquals("Object()",te.getNewText()); assertNotNull(te.getRange()); Range range = te.getRange(); assertEquals(2, range.getStart().getLine()); assertEquals(17, range.getStart().getCharacter()); assertEquals(2, range.getEnd().getLine()); assertEquals(18, range.getEnd().getCharacter()); }
@Test public void testCompletion_package() throws JavaModelException{ ClientPreferences mockCapabilies = Mockito.mock(ClientPreferences.class); Mockito.when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); ICompilationUnit unit = getWorkingCopy( "src/org/sample/Baz.java", "package o"+ "public class Baz {\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "package o"); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); List<CompletionItem> items = new ArrayList<>(list.getItems()); assertTrue(items.size() > 1); items.sort((i1, i2) -> i1.getSortText().compareTo(i2.getSortText())); CompletionItem item = items.get(0); // current package should appear 1st assertEquals("org.sample",item.getLabel()); CompletionItem resolvedItem = server.resolveCompletionItem(item).join(); assertNotNull(resolvedItem); TextEdit te = item.getTextEdit(); assertNotNull(te); assertEquals("org.sample", te.getNewText()); assertNotNull(te.getRange()); Range range = te.getRange(); assertEquals(0, range.getStart().getLine()); assertEquals(8, range.getStart().getCharacter()); assertEquals(0, range.getEnd().getLine()); assertEquals(15, range.getEnd().getCharacter()); }
@SuppressWarnings("unchecked") @Test public void testResolveCodeLense() { String source = "src/java/Foo.java"; String payload = createCodeLensRequest(source, 5, 13, 16); CodeLens lens = getParams(payload); Range range = lens.getRange(); assertRange(5, 13, 16, range); CodeLens result = handler.resolve(lens, monitor); assertNotNull(result); //Check if command found Command command = result.getCommand(); assertNotNull(command); assertEquals("1 reference", command.getTitle()); assertEquals("java.show.references",command.getCommand()); //Check codelens args List<Object> args = command.getArguments(); assertEquals(3,args.size()); //Check we point to the Bar class String sourceUri = args.get(0).toString(); assertTrue(sourceUri.endsWith(source)); //CodeLens position Map<String, Object> map = (Map<String, Object>)args.get(1); assertEquals(5.0, map.get("line")); assertEquals(13.0, map.get("character")); //Reference location List<Location> locations = (List<Location>)args.get(2); assertEquals(1, locations.size()); Location loc = locations.get(0); assertTrue(loc.getUri().endsWith("src/java/Bar.java")); assertRange(5, 25, 28, loc.getRange()); }
protected void _handleReplacements(final IEmfResourceChange change) { try { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { final URI uri = change.getResource().getURI(); change.getResource().save(outputStream, null); byte[] _byteArray = outputStream.toByteArray(); String _charset = this.getCharset(change.getResource()); final String newContent = new String(_byteArray, _charset); final Function2<Document, XtextResource, List<TextEdit>> _function = (Document document, XtextResource resource) -> { List<TextEdit> _xblockexpression = null; { Position _position = document.getPosition(0); Position _position_1 = document.getPosition(document.getContents().length()); final Range range = new Range(_position, _position_1); final TextEdit textEdit = new TextEdit(range, newContent); _xblockexpression = this.addTextEdit(uri, textEdit); } return _xblockexpression; }; this.workspaceManager.<List<TextEdit>>doRead(uri, _function); } finally { outputStream.close(); } } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
protected Hover hover(final HoverContext context) { if ((context == null)) { return IHoverService.EMPTY_HOVER; } final List<Either<String, MarkedString>> contents = this.getContents(context); if ((contents == null)) { return IHoverService.EMPTY_HOVER; } final Range range = this.getRange(context); if ((range == null)) { return IHoverService.EMPTY_HOVER; } return new Hover(contents, range); }
@Test public void withNull() { final List<? extends Range> input = this.sort(CollectionLiterals.<Range>newArrayList(this.newRange(2, 2, 2, 3), null, this.newRange(1, 1, 1, 1))); Assert.assertEquals(1, input.get(0).getStart().getLine()); Assert.assertEquals(1, input.get(0).getStart().getCharacter()); Assert.assertEquals(1, input.get(0).getEnd().getLine()); Assert.assertEquals(1, input.get(0).getEnd().getCharacter()); Assert.assertEquals(2, input.get(1).getStart().getLine()); Assert.assertEquals(2, input.get(1).getStart().getCharacter()); Assert.assertEquals(2, input.get(1).getEnd().getLine()); Assert.assertEquals(3, input.get(1).getEnd().getCharacter()); Assert.assertNull(IterableExtensions.last(input)); }
public Range newRange(final Resource resource, final ITextRegion region) { if ((region == null)) { return null; } int _offset = region.getOffset(); int _offset_1 = region.getOffset(); int _length = region.getLength(); int _plus = (_offset_1 + _length); return this.newRange(resource, _offset, _plus); }
@Override public List<? extends ColoringInformation> getColoring(final XtextResource resource, final Document document) { if ((resource == null)) { return CollectionLiterals.<ColoringInformation>emptyList(); } final ImmutableList.Builder<ColoringInformation> builder = ImmutableList.<ColoringInformation>builder(); final Procedure1<Object> _function = (Object it) -> { List<INode> _xifexpression = null; if ((it instanceof Property)) { _xifexpression = NodeModelUtils.findNodesForFeature(((EObject)it), TestLanguagePackage.Literals.MEMBER__NAME); } else { List<INode> _xifexpression_1 = null; if ((it instanceof Operation)) { _xifexpression_1 = NodeModelUtils.findNodesForFeature(((EObject)it), TestLanguagePackage.Literals.MEMBER__NAME); } else { _xifexpression_1 = CollectionLiterals.<INode>emptyList(); } _xifexpression = _xifexpression_1; } final List<INode> nodes = _xifexpression; final Consumer<INode> _function_1 = (INode it_1) -> { final int start = it_1.getOffset(); int _offset = it_1.getOffset(); int _length = it_1.getLength(); final int end = (_offset + _length); Position _position = document.getPosition(start); Position _position_1 = document.getPosition(end); final Range range = new Range(_position, _position_1); ColoringInformation _coloringInformation = new ColoringInformation(range, ColoringServiceImpl.STYLE_IDS); builder.add(_coloringInformation); }; nodes.forEach(_function_1); }; IteratorExtensions.<Object>forEach(EcoreUtil.<Object>getAllContents(resource, true), _function); return builder.build(); }
private Optional<TextEdit> insertAfterImports(String packageName, String className) { String insertLine = String.format("\nimport %s.%s;", packageName, className); return endOfImports().map(at -> new TextEdit(new Range(at, at), insertLine)); }
private Optional<TextEdit> insertAfterPackage(String packageName, String className) { String insertLine = String.format("\n\nimport %s.%s;", packageName, className); return endOfPackage().map(at -> new TextEdit(new Range(at, at), insertLine)); }