@Override public CompletableFuture<ICodeLens> resolveCodeLens(ICodeLensContext context, ICodeLens codeLens, IProgressMonitor monitor) { ITextEditor textEditor = ((IEditorCodeLensContext) context).getTextEditor(); LSPDocumentInfo info = null; Collection<LSPDocumentInfo> infos = LanguageServiceAccessor.getLSPDocumentInfosFor( LSPEclipseUtils.getDocument((ITextEditor) textEditor), capabilities -> capabilities.getCodeLensProvider() != null && capabilities.getCodeLensProvider().isResolveProvider()); if (!infos.isEmpty()) { info = infos.iterator().next(); } else { info = null; } if (info != null) { LSPCodeLens lscl = ((LSPCodeLens) codeLens); CodeLens unresolved = lscl.getCl(); return info.getLanguageClient().getTextDocumentService().resolveCodeLens(unresolved).thenApply(resolved -> { lscl.update(resolved); return lscl; }); } return null; }
@Override public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) { LOGGER.info("codeLens: " + params.getTextDocument()); return CompletableFuture.completedFuture(Collections.emptyList()); }
private static CodeLens addTestMethod(final MixinDefinition def, final String documentUri, final SInvokable i) { CodeLens lens = new CodeLens(); Command cmd = new Command(); cmd.setCommand(COMMAND); cmd.setTitle("Run test"); Range r = SomAdapter.toRange(i.getSourceSection()); cmd.setArguments(Lists.newArrayList(documentUri, def.getName().getString() + "." + i.getSignature().getString(), r.getStart().getLine(), r.getStart().getCharacter(), r.getEnd().getLine(), r.getEnd().getCharacter())); lens.setCommand(cmd); lens.setRange(r); return lens; }
public void getCodeLenses(final List<CodeLens> codeLenses, final String documentUri) { String path; try { path = docUriToNormalizedPath(documentUri); } catch (URISyntaxException e) { return; } SomStructures probe; synchronized (path) { probe = structuralProbes.get(path); } if (probe != null) { for (MixinDefinition c : probe.getClasses()) { SomMinitest.checkForTests(c, codeLenses, documentUri); } } }
@Override public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) { logInfo(">> document/codeLens"); CodeLensHandler handler = new CodeLensHandler(preferenceManager); return computeAsync((cc) -> { IProgressMonitor monitor = toMonitor(cc); try { Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor); } catch (OperationCanceledException ignorable) { // No need to pollute logs when query is cancelled } catch (InterruptedException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } return handler.getCodeLensSymbols(params.getTextDocument().getUri(), monitor); }); }
@Override public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) { logInfo(">> codeLens/resolve"); CodeLensHandler handler = new CodeLensHandler(preferenceManager); return computeAsync((cc) -> { IProgressMonitor monitor = toMonitor(cc); try { Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor); } catch (OperationCanceledException ignorable) { // No need to pollute logs when query is cancelled } catch (InterruptedException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } return handler.resolve(unresolved, monitor); }); }
@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); }
@Test public void testDisableCodeLensSymbols() throws Exception { Preferences noCodeLenses = Preferences.createFrom(Collections.singletonMap(Preferences.REFERENCES_CODE_LENS_ENABLED_KEY, "false")); Mockito.reset(preferenceManager); when(preferenceManager.getPreferences()).thenReturn(noCodeLenses); handler = new CodeLensHandler(preferenceManager); String payload = createCodeLensSymbolsRequest("src/java/IFoo.java"); CodeLensParams codeLensParams = getParams(payload); String uri = codeLensParams.getTextDocument().getUri(); assertFalse(uri.isEmpty()); //when List<CodeLens> result = handler.getCodeLensSymbols(uri, monitor); //then assertEquals(0, result.size()); }
@Test public void testEnableImplementationsCodeLensSymbols() throws Exception { Preferences implementationsCodeLenses = Preferences.createFrom(Collections.singletonMap(Preferences.IMPLEMENTATIONS_CODE_LENS_ENABLED_KEY, "true")); Mockito.reset(preferenceManager); when(preferenceManager.getPreferences()).thenReturn(implementationsCodeLenses); handler = new CodeLensHandler(preferenceManager); String payload = createCodeLensSymbolsRequest("src/java/IFoo.java"); CodeLensParams codeLensParams = getParams(payload); String uri = codeLensParams.getTextDocument().getUri(); assertFalse(uri.isEmpty()); //when List<CodeLens> result = handler.getCodeLensSymbols(uri, monitor); //then assertEquals(2, result.size()); CodeLens lens = result.get(1); @SuppressWarnings("unchecked") List<Object> data = (List<Object>) lens.getData(); String type = (String) data.get(2); assertEquals(type, "implementations"); }
@Test public void testDisableImplementationsCodeLensSymbols() throws Exception { Preferences noImplementationsCodeLenses = Preferences.createFrom(Collections.singletonMap(Preferences.IMPLEMENTATIONS_CODE_LENS_ENABLED_KEY, "false")); Mockito.reset(preferenceManager); when(preferenceManager.getPreferences()).thenReturn(noImplementationsCodeLenses); Preferences noReferencesCodeLenses = Preferences.createFrom(Collections.singletonMap(Preferences.REFERENCES_CODE_LENS_ENABLED_KEY, "false")); Mockito.reset(preferenceManager); when(preferenceManager.getPreferences()).thenReturn(noReferencesCodeLenses); handler = new CodeLensHandler(preferenceManager); String payload = createCodeLensSymbolsRequest("src/java/IFoo.java"); CodeLensParams codeLensParams = getParams(payload); String uri = codeLensParams.getTextDocument().getUri(); assertFalse(uri.isEmpty()); //when List<CodeLens> result = handler.getCodeLensSymbols(uri, monitor); //then assertEquals(0, result.size()); }
@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 testCodeLens() { final Procedure1<AbstractLanguageServerTest.TestCodeLensConfiguration> _function = (AbstractLanguageServerTest.TestCodeLensConfiguration it) -> { StringConcatenation _builder = new StringConcatenation(); _builder.append("type Foo {}"); _builder.newLine(); _builder.append("type Bar {"); _builder.newLine(); _builder.append("\t"); _builder.append("Foo foo"); _builder.newLine(); _builder.append("}"); _builder.newLine(); it.setModel(_builder.toString()); final Procedure1<List<? extends CodeLens>> _function_1 = (List<? extends CodeLens> it_1) -> { this.assertEquals("Do Awesome Stuff(RESOLVED)", IterableExtensions.head(it_1).getCommand().getTitle()); Object _data = IterableExtensions.head(it_1).getData(); Assert.assertEquals(1, ((Position) _data).getLine()); }; it.setAssertCodeLenses(_function_1); }; this.testCodeLens(_function); }
private URI uninstallURI(final CodeLens lens) { URI result = null; Object _data = lens.getData(); if ((_data instanceof String)) { result = URI.createURI(lens.getData().toString()); lens.setData(null); } else { Object _data_1 = lens.getData(); if ((_data_1 instanceof List<?>)) { Object _data_2 = lens.getData(); final List<?> l = ((List<?>) _data_2); result = URI.createURI(IterableExtensions.head(l).toString()); lens.setData(l.get(1)); } } return result; }
@Override public CompletableFuture<List<? extends CodeLens>> codeLens(final CodeLensParams params) { final Function1<CancelIndicator, List<? extends CodeLens>> _function = (CancelIndicator cancelIndicator) -> { final URI uri = this._uriExtensions.toUri(params.getTextDocument().getUri()); final IResourceServiceProvider resourceServiceProvider = this.languagesRegistry.getResourceServiceProvider(uri); ICodeLensService _get = null; if (resourceServiceProvider!=null) { _get=resourceServiceProvider.<ICodeLensService>get(ICodeLensService.class); } final ICodeLensService codeLensService = _get; if ((codeLensService == null)) { return CollectionLiterals.<CodeLens>emptyList(); } final Function2<Document, XtextResource, List<? extends CodeLens>> _function_1 = (Document document, XtextResource resource) -> { final List<? extends CodeLens> result = codeLensService.computeCodeLenses(document, resource, params, cancelIndicator); this.installURI(result, uri.toString()); return result; }; return this.workspaceManager.<List<? extends CodeLens>>doRead(uri, _function_1); }; return this.requestManager.<List<? extends CodeLens>>runRead(_function); }
@Override public CompletableFuture<CodeLens> resolveCodeLens(final CodeLens unresolved) { final URI uri = this.uninstallURI(unresolved); if ((uri == null)) { return CompletableFuture.<CodeLens>completedFuture(unresolved); } final Function1<CancelIndicator, CodeLens> _function = (CancelIndicator cancelIndicator) -> { final IResourceServiceProvider resourceServiceProvider = this.languagesRegistry.getResourceServiceProvider(uri); ICodeLensResolver _get = null; if (resourceServiceProvider!=null) { _get=resourceServiceProvider.<ICodeLensResolver>get(ICodeLensResolver.class); } final ICodeLensResolver resolver = _get; if ((resolver == null)) { return unresolved; } final Function2<Document, XtextResource, CodeLens> _function_1 = (Document document, XtextResource resource) -> { final CodeLens result = resolver.resolveCodeLens(document, resource, unresolved, cancelIndicator); return result; }; return this.workspaceManager.<CodeLens>doRead(uri, _function_1); }; return this.requestManager.<CodeLens>runRead(_function); }
public static void checkForTests(final MixinDefinition def, final List<CodeLens> codeLenses, final String documentUri) { for (SSymbol s : def.getFactoryMethods().keySet()) { if (s == TEST_CONTEXT) { CodeLens lens = createTestLense(def, documentUri); codeLenses.add(lens); addTestMethods(def, codeLenses, documentUri); return; } } }
private static CodeLens createTestLense(final MixinDefinition def, final String documentUri) { CodeLens lens = new CodeLens(); Command cmd = new Command(); cmd.setCommand(COMMAND); cmd.setTitle("Run tests"); Range r = SomAdapter.toRange(def.getNameSourceSection()); cmd.setArguments(Lists.newArrayList(documentUri, def.getName().getString(), r.getStart().getLine(), r.getStart().getCharacter(), r.getEnd().getLine(), r.getEnd().getCharacter())); lens.setCommand(cmd); lens.setRange(r); return lens; }
private static void addTestMethods(final MixinDefinition def, final List<CodeLens> codeLenses, final String documentUri) { for (Dispatchable d : def.getInstanceDispatchables().values()) { if (d instanceof SInvokable) { SInvokable i = (SInvokable) d; if (i.getSignature().getString().startsWith(TEST_PREFIX)) { CodeLens lens = addTestMethod(def, documentUri, i); codeLenses.add(lens); } } } }
public List<CodeLens> getCodeLensSymbols(String uri, IProgressMonitor monitor) { if (!preferenceManager.getPreferences().isCodeLensEnabled()) { return Collections.emptyList(); } final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); IClassFile classFile = null; if (unit == null) { classFile = JDTUtils.resolveClassFile(uri); if (classFile == null) { return Collections.emptyList(); } } else { if (!unit.getResource().exists() || monitor.isCanceled()) { return Collections.emptyList(); } } try { ITypeRoot typeRoot = unit != null ? unit : classFile; IJavaElement[] elements = typeRoot.getChildren(); ArrayList<CodeLens> lenses = new ArrayList<>(elements.length); collectCodeLenses(typeRoot, elements, lenses, monitor); if (monitor.isCanceled()) { lenses.clear(); } return lenses; } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem getting code lenses for" + unit.getElementName(), e); } return Collections.emptyList(); }
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; }
@Test public void testGetCodeLensSymbolsForClass() throws Exception { Preferences implementationsCodeLenses = Preferences.createFrom(Collections.singletonMap(Preferences.IMPLEMENTATIONS_CODE_LENS_ENABLED_KEY, "true")); Mockito.reset(preferenceManager); when(preferenceManager.getPreferences()).thenReturn(implementationsCodeLenses); handler = new CodeLensHandler(preferenceManager); String uriString = ClassFileUtil.getURI(project, "java.lang.Runnable"); String payload = createCodeLensSymbolRequest(new URI(uriString)); CodeLensParams codeLensParams = getParams(payload); String uri = codeLensParams.getTextDocument().getUri(); assertFalse(uri.isEmpty()); List<CodeLens> lenses = handler.getCodeLensSymbols(uri, monitor); assertEquals("Found " + lenses, 3, lenses.size()); }
@Test public void testGetCodeLenseBoundaries() { List<CodeLens> result = handler.getCodeLensSymbols(null, monitor); assertNotNull(result); assertEquals(0, result.size()); String payload = createCodeLensSymbolsRequest("src/java/Missing.java"); CodeLensParams codeLensParams = getParams(payload); String uri = codeLensParams.getTextDocument().getUri(); result = handler.getCodeLensSymbols(uri, monitor); assertEquals(0, result.size()); }
@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()); }
@Test public void testResolveCodeLenseBoundaries() { CodeLens result = handler.resolve(null, monitor); assertNull(result); String payload = createCodeLensRequest("src/java/Missing.java", 5, 13, 16); CodeLens lens = getParams(payload); result = handler.resolve(lens, monitor); assertSame(lens, result); assertNotNull(result.getCommand()); }
@Test public void testIgnoreLombokCodeLensSymbols() throws Exception { String payload = createCodeLensSymbolsRequest("src/java/Bar.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, 4, result.size()); //CodeLens on constructor CodeLens cl = result.get(0); assertRange(7, 11, 14, cl.getRange()); //CodeLens on somethingFromJPAModelGen cl = result.get(1); assertRange(16, 16, 40, cl.getRange()); // CodeLens on foo cl = result.get(2); assertRange(22, 16, 19, cl.getRange()); //CodeLens on Bar type cl = result.get(3); assertRange(5, 13, 16, cl.getRange()); }
@Override public CodeLens resolveCodeLens(final Document document, final XtextResource resource, final CodeLens codeLens, final CancelIndicator indicator) { Command _command = codeLens.getCommand(); String _title = codeLens.getCommand().getTitle(); String _plus = (_title + "(RESOLVED)"); _command.setTitle(_plus); return codeLens; }
private void installURI(final List<? extends CodeLens> codeLenses, final String uri) { for (final CodeLens lens : codeLenses) { Object _data = lens.getData(); boolean _tripleNotEquals = (_data != null); if (_tripleNotEquals) { lens.setData(CollectionLiterals.<Object>newArrayList(uri, lens.getData())); } else { lens.setData(uri); } } }
@Test public void testInvalidCodeLens() { ResponseMessage message = new ResponseMessage(); message.setId("1"); CodeLens codeLens = new CodeLens(new Range(new Position(3, 32), new Position(3, 35)), null, null); // forbidden self reference! codeLens.setData(codeLens); message.setResult(codeLens); assertIssues(message, "An element of the message has a direct or indirect reference to itself."); }
@Test public void testNoAnnotation() { final Function1<Annotation, Boolean> _function = (Annotation it) -> { Class<? extends Annotation> _annotationType = it.annotationType(); return Boolean.valueOf(Objects.equal(_annotationType, JsonRpcData.class)); }; Assert.assertFalse(IterableExtensions.<Annotation>exists(((Iterable<Annotation>)Conversions.doWrapArray(CodeLens.class.getAnnotations())), _function)); }
@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; }
@Override public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) { LOGGER.info("resolveCodeLens: " + unresolved.getCommand().getCommand()); return CompletableFuture.completedFuture(null); }
@Override public CompletableFuture<List<? extends CodeLens>> codeLens(final CodeLensParams params) { List<CodeLens> result = new ArrayList<>(); som.getCodeLenses(result, params.getTextDocument().getUri()); return CompletableFuture.completedFuture(result); }
@Override public CompletableFuture<CodeLens> resolveCodeLens(final CodeLens unresolved) { // TODO Auto-generated method stub return null; }
@Pure public Procedure1<? super List<? extends CodeLens>> getAssertCodeLenses() { return this.assertCodeLenses; }
public void setAssertCodeLenses(final Procedure1<? super List<? extends CodeLens>> assertCodeLenses) { this.assertCodeLenses = assertCodeLenses; }
protected String _toExpectation(final CodeLens it) { String _title = it.getCommand().getTitle(); String _plus = (_title + " "); String _expectation = this.toExpectation(it.getRange()); return (_plus + _expectation); }
@Override public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) { throw new UnsupportedOperationException(); }
@Override public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) { throw new UnsupportedOperationException(); }
@Override public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) { return null; }
@Override public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) { return null; }