Skip to content

Commit

Permalink
Opt-in to use ErrorProviderBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
jtulach committed Aug 12, 2024
1 parent 536ddb9 commit dc5d4e0
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
14 changes: 14 additions & 0 deletions ide/api.lsp/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@
<!-- ACTUAL CHANGES BEGIN HERE: -->

<changes>
<change id="ErrorProvider-hintsLayerNameFor">
<api name="LSP_API"/>
<summary>ErrorProvider.hintsLayerNameFor()</summary>
<version major="1" minor="29"/>
<date day="12" month="8" year="2024"/>
<author login="jtulach"/>
<compatibility binary="compatible" source="compatible" addition="yes" deletion="no" />
<description>
Control when <code>ErrorProvider</code> results should be used in
the NetBeans IDE by overriding <code>hintsLayerNameFor</code>
and returning non-<code>null</code> value.
</description>
<class package="org.netbeans.spi.lsp" name="ErrorProvider"/>
</change>
<change id="CodeActionProvider.getSupportedCodeActionKinds">
<api name="LSP_API"/>
<summary>Adding CodeActionProvider.getSupportedCodeActionKinds method</summary>
Expand Down
2 changes: 1 addition & 1 deletion ide/api.lsp/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Manifest-Version: 1.0
OpenIDE-Module: org.netbeans.api.lsp/1
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/lsp/Bundle.properties
OpenIDE-Module-Specification-Version: 1.28
OpenIDE-Module-Specification-Version: 1.29

This comment has been minimized.

Copy link
@neilcsmith-net

neilcsmith-net Aug 12, 2024

Member

Changing spec versions is not allowed on delivery.

AutoUpdate-Show-In-Client: false
23 changes: 23 additions & 0 deletions ide/api.lsp/src/org/netbeans/spi/lsp/ErrorProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ public interface ErrorProvider {
*/
public List<? extends Diagnostic> computeErrors(Context context);

/** The default hints layer name. Since version 1.29 of this module it
* is possible to display <b>hints</b> in NetBeans IDE (not only in
* NetBeans VSCode extension). However one has to <b>opt-in</b> to do so.
* Override this method to return non-{@code null} values:
* <pre>
* @Override
* public String hintsLayerNameFor(Kind kind) {
* return switch (kind) {
* case ERRORS -> "lsp:errors";
* case HINTS -> "lsp:hints";
* };
* }
* </pre>
*
* @param kind errors or hints
* @return non-{@code null} value if you want these errors or hints be visible
* in the NetBeans IDE
* @since 1.29
*/
public default String hintsLayerNameFor(Kind kind) {
return null;
}

/**
* The context for the error provider.
*/
Expand Down
2 changes: 1 addition & 1 deletion ide/lsp.client/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.28</specification-version>
<specification-version>1.29</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ final void waitFinished() {
@Override
public final void run() {
for (ErrorProvider p : errorProviders) {
computeHints(ErrorProvider.Kind.ERRORS, p, "lsp:errors");
computeHints(ErrorProvider.Kind.HINTS, p, "lsp:hints");
computeHints(ErrorProvider.Kind.ERRORS, p);
computeHints(ErrorProvider.Kind.HINTS, p);
}
}

private void computeHints(final ErrorProvider.Kind type, ErrorProvider p, final String prefix) {
private void computeHints(final ErrorProvider.Kind type, ErrorProvider p) {
final String prefix = p.hintsLayerNameFor(type);
if (prefix == null) {
return;
}
List<ErrorDescription> arr = new ArrayList<>();
ErrorProvider.Context errorCtx = new ErrorProvider.Context(file, type);
List<? extends Diagnostic> errors = p.computeErrors(errorCtx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
Expand Down Expand Up @@ -49,16 +48,11 @@
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.netbeans.api.editor.EditorRegistry;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.lsp.Diagnostic;
import org.netbeans.editor.BaseDocumentEvent;
import org.netbeans.lib.editor.util.swing.DocumentUtilities;
import org.netbeans.modules.editor.*;
import org.netbeans.modules.lsp.client.LSPBindings;
import org.netbeans.modules.lsp.client.Utils;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.HintsController;
import org.netbeans.spi.editor.hints.Severity;
import org.netbeans.spi.lsp.ErrorProvider;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
Expand Down Expand Up @@ -333,7 +327,14 @@ private void registerBackgroundTasks(JTextComponent c) {

if (server == null) {
Lookup lkp = MimeLookup.getLookup(file.getMIMEType());
Collection<? extends ErrorProvider> errorProviders = lkp.lookupAll(ErrorProvider.class);
ArrayList<ErrorProvider> errorProviders = new ArrayList<>();
for (ErrorProvider ep : lkp.lookupAll(ErrorProvider.class)) {
boolean errors = ep.hintsLayerNameFor(ErrorProvider.Kind.ERRORS) != null;
boolean hints = ep.hintsLayerNameFor(ErrorProvider.Kind.HINTS) != null;
if (errors || hints) {
errorProviders.add(ep);
}
}
if (!errorProviders.isEmpty()) {
ErrorProviderBridge b = new ErrorProviderBridge(doc, file, errorProviders, WORKER);
b.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ private static final class MockErrorProvider implements ErrorProvider {
this.c = new WeakReference<>(c);
}

@Override
public String hintsLayerNameFor(Kind kind) {
switch (kind) {
case ERRORS:
return "lsp:errors";
case HINTS:
return "lsp:hints";
default:
throw new IllegalStateException();
}
}

@Override
public List<? extends Diagnostic> computeErrors(Context context) {
List<Diagnostic> arr = new ArrayList<>();
Expand Down

0 comments on commit dc5d4e0

Please sign in to comment.