Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode: Various code completion enhancements. #5963

Merged
merged 6 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ protected void resolve(CompilationController controller) throws IOException {
case ENUM_CONSTANT:
case FIELD:
case METHOD:
case LOCAL_VARIABLE:
case PARAMETER:
case EXCEPTION_PARAMETER:
case RESOURCE_VARIABLE:
case BINDING_VARIABLE:
documentation = (T)factory.create(controller, el, cancel);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -160,7 +161,10 @@ public static Supplier<String> getDocumentation(Document doc, int offset, Elemen
JavaDocumentationTask<Future<String>> task = JavaDocumentationTask.create(offset, handle, new JavaDocumentationTask.DocumentationFactory<Future<String>>() {
@Override
public Future<String> create(CompilationInfo compilationInfo, Element element, Callable<Boolean> cancel) {
return ElementJavadoc.create(compilationInfo, element, cancel).getTextAsync();
ElementJavadoc doc = ElementJavadoc.create(compilationInfo, element, cancel);
return ((CompletableFuture<String>) doc.getTextAsync()).thenApplyAsync(content -> {
return Utilities.resolveLinks(content, doc);
});
}
}, () -> false);
ParserManager.parse(Collections.singletonList(Source.create(doc)), new UserTask() {
Expand Down Expand Up @@ -890,8 +894,10 @@ public Completion createLambdaItem(CompilationInfo info, TypeElement elem, Decla
}
cnt++;
String paramTypeName = Utilities.getTypeName(info, tm, false, desc.isVarArgs() && !tIt.hasNext()).toString();
String paramName = it.next().getSimpleName().toString();
label.append(paramTypeName).append(' ').append(paramName);
VariableElement var = it.next();
List<String> varNames = Utilities.varNamesSuggestions(tm, var.getKind(), Collections.emptySet(), null, null, info.getTypes(), info.getElements(), Collections.emptyList(), CodeStyle.getDefault(info.getFileObject()));
String paramName = varNames.isEmpty() ? var.getSimpleName().toString() : varNames.get(0);
label.append(paramName);
insertText.append("${").append(cnt).append(":").append(paramName).append("}");
sortText.append(paramTypeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public CompletableFuture<String> getHoverContent(Document doc, int offset) {
JavaDocumentationTask<CompletableFuture<String>> task = JavaDocumentationTask.create(offset, null, new JavaDocumentationTask.DocumentationFactory<CompletableFuture<String>>() {
@Override
public CompletableFuture<String> create(CompilationInfo compilationInfo, Element element, Callable<Boolean> cancel) {
return (CompletableFuture<String>) ElementJavadoc.create(compilationInfo, element, cancel).getTextAsync();
ElementJavadoc doc = ElementJavadoc.create(compilationInfo, element, cancel);
return ((CompletableFuture<String>) doc.getTextAsync()).thenApplyAsync(content -> {
return Utilities.resolveLinks(content, doc);
});
}
}, () -> false);
ParserManager.parse(Collections.singletonList(Source.create(doc)), new UserTask() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@
import org.netbeans.api.java.source.support.ErrorAwareTreeScanner;

import java.awt.Color;
import java.net.URL;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.lang.model.SourceVersion;
Expand All @@ -62,7 +63,6 @@

import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.editor.settings.SimpleValueNames;
import org.netbeans.api.java.lexer.JavaTokenId;
import org.netbeans.api.java.source.CodeStyle;
import org.netbeans.api.java.source.CodeStyleUtils;
Expand All @@ -72,6 +72,7 @@
import org.netbeans.api.java.source.TreeUtilities;
import org.netbeans.api.java.source.TypeUtilities.TypeNameOptions;
import org.netbeans.api.java.source.support.ReferencesCount;
import org.netbeans.api.java.source.ui.ElementJavadoc;
import org.netbeans.api.lexer.InputAttributes;
import org.netbeans.api.lexer.Language;
import org.netbeans.api.lexer.LanguagePath;
Expand All @@ -93,6 +94,7 @@
public final class Utilities {

private static final String ERROR = "<error>"; //NOI18N
private static final Pattern LINK_PATTERN = Pattern.compile("<a href='(\\*\\d+)'>(.*?)<\\/a>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

private static boolean guessMethodArguments = CodeCompletionPanel.GUESS_METHOD_ARGUMENTS_DEFAULT;
private static boolean autoPopupOnJavaIdentifierPart = CodeCompletionPanel.JAVA_AUTO_POPUP_ON_IDENTIFIER_PART_DEFAULT;
Expand Down Expand Up @@ -572,7 +574,11 @@ private static List<String> varNamesForType(TypeMirror type, Types types, Elemen
if ("Iterator".equals(tn)) //NOI18N
al.add("it"); //NOI18N
while((tn = nextName(tn)).length() > 0) {
al.add(tn);
if (SourceVersion.isKeyword(tn)) {
al.add('a' + tn.substring(0, 1).toUpperCase() + tn.substring(1));
} else {
al.add(tn);
}
sb.append(tn.charAt(0));
}
if (sb.length() > 0) {
Expand All @@ -592,7 +598,11 @@ private static List<String> varNamesForType(TypeMirror type, Types types, Elemen
if ("Iterator".equals(tn)) //NOI18N
al.add("it"); //NOI18N
while((tn = nextName(tn)).length() > 0) {
al.add(tn);
if (SourceVersion.isKeyword(tn)) {
al.add('a' + tn.substring(0, 1).toUpperCase() + tn.substring(1));
} else {
al.add(tn);
}
sb.append(tn.charAt(0));
}
if (iterable != null && types.isSubtype(type, iterable)) {
Expand Down Expand Up @@ -1066,6 +1076,24 @@ private static List<ExecutableElement> resolveMethod(CompilationInfo info, List<
return found;
}

static String resolveLinks(String content, ElementJavadoc doc) {
Matcher matcher = LINK_PATTERN.matcher(content);
String updatedContent = matcher.replaceAll(result -> {
if (result.groupCount() == 2) {
try {
ElementJavadoc link = doc.resolveLink(result.group(1));
URL url = link != null ? link.getURL() : null;
if (url != null) {
return "<a href='" + url.toString() + "'>" + result.group(2) + "</a>";
}
} catch (Exception ex) {}
return result.group(2);
}
return result.group();
});
return updatedContent;
}

private Utilities() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ public void testRecords2() throws Exception {
}, true, false);

toolTip = toolTip.replace(source.toURI().toString(), "FILE");
assertEquals("<html><body><base href=\"FILE\"></base><font size='+0'><b><a href='*0'>test.&#x200B;Test.&#x200B;R</a></b></font>", toolTip);
assertEquals("<html><body><base href=\"FILE\"></base><font size='+0'><b><a href='*0'>test.&#x200B;Test.&#x200B;R</a></b></font><pre>int <b>ff</b></pre>", toolTip);
}

public void testRecords3() throws Exception {
Expand Down Expand Up @@ -1457,7 +1457,7 @@ public void testRecords3() throws Exception {
}, true, false);

toolTip = toolTip.replace(source.toURI().toString(), "FILE");
assertEquals("<html><body><base href=\"FILE\"></base><font size='+0'><b><a href='*0'>test.&#x200B;Test</a></b></font>", toolTip);
assertEquals("<html><body><base href=\"FILE\"></base><font size='+0'><b><a href='*0'>test.&#x200B;Test</a></b></font><pre>public record <b>RR</b></pre>", toolTip);
}

public void testRecords4() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public final class J2SEPlatformDefaultJavadocImpl implements J2SEPlatformDefault
if (now.isAfter(jdk9)) { // time traveler -> only java 8 doc for you
int jdk = 9;
for (LocalDate t = jdk9; t.isBefore(now); t = t.plusMonths(6)) {
OFFICIAL_JAVADOC.put(String.valueOf(jdk), "https://docs.oracle.com/javase/" + jdk + "/docs/api/"); // NOI18N
OFFICIAL_JAVADOC.put(String.valueOf(jdk), "https://docs.oracle.com/en/java/javase/" + jdk + "/docs/api/"); // NOI18N
jdk++;
}
OFFICIAL_JAVADOC.put(String.valueOf(jdk), "https://download.java.net/java/early_access/jdk" + jdk + "/docs/api/"); // NOI18N Early access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ private FileObject fromURI(String uri, boolean tryHard) {
}
missingFileDiscovered(uri);
} catch (MalformedURLException ex) {
if (!uri.startsWith("untitled:")) {
if (!uri.startsWith("untitled:") && !uri.startsWith("jdt:")) {
LOG.log(Level.WARNING, "Invalid file URL: " + uri, ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public void testMain() throws Exception {
assertEquals(Arrays.asList("Method:test() : void"), actualItems);
assertEquals(null, actualCompletionItem.get(0).getDocumentation());
CompletionItem resolvedItem = server.getTextDocumentService().resolveCompletionItem(actualCompletionItem.get(0)).get();
assertEquals("**[Test](*0)**\n" +
assertEquals("**Test**\n" +
"\n" +
"```\n" +
"public void test()\n" +
Expand Down Expand Up @@ -1386,9 +1386,7 @@ public void logMessage(MessageParams arg0) {
MarkupContent content = hover.getContents().getRight();
assertNotNull(content);
assertEquals(content.getKind(), "markdown");
assertEquals(content.getValue(), "**[](*0)**\n" +
"\n" +
"```\n" +
assertEquals(content.getValue(), "```\n" +
"public class Test\n" +
"extends Object\n" +
"```\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public JavadocForBinaryQuery.Result findJavadoc(@NonNull final URL binaryRoot) {
candidates.add(jp);
}
}
for (ClassPath.Entry entry : jp.getSourceFolders().entries()) {
if (binaryRoot.equals(entry.getURL())) {
candidates.add(jp);
}
}
}
return candidates.isEmpty() ? null : new R(candidates);
}
Expand Down
Loading