This is an automated email from the ASF dual-hosted git repository.
dbalek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 96f51a7 Various fixes for LSP codeAction and rename requests. (#2655)
96f51a7 is described below
commit 96f51a783e8794e18b4dea5629d5a26b89ced179
Author: Dusan Balek <[email protected]>
AuthorDate: Sat Jan 9 17:02:18 2021 +0100
Various fixes for LSP codeAction and rename requests. (#2655)
* Request textDocument/codeAction failed when selecting source code for
Introduce var refactor - fixed.
* Implementing an interface results in NullPointerException - fixed.
* GeneratorUtils$DuplicateMemberException: Class member already exists form
Generate Constructor source action - fixed.
* IllegalStateException from Rename Symbol - fixed.
* GeneratorUtils$DuplicateMemberException: Class member already exists
thrown from introduce element hints - fixed.
---
.../modules/java/hints/errors/CreateClassFix.java | 2 +-
.../IntroduceExpressionBasedMethodFix.java | 14 ++++-
.../java/hints/introduce/IntroduceMethodFix.java | 14 ++++-
.../modules/java/lsp/server/LspServerUtils.java | 2 +-
.../lsp/server/protocol/ConstructorGenerator.java | 16 ++++--
.../server/protocol/TextDocumentServiceImpl.java | 63 ++++++++++++----------
java/java.lsp.server/vscode/src/extension.ts | 4 +-
7 files changed, 75 insertions(+), 40 deletions(-)
diff --git
a/java/java.hints/src/org/netbeans/modules/java/hints/errors/CreateClassFix.java
b/java/java.hints/src/org/netbeans/modules/java/hints/errors/CreateClassFix.java
index d83bf63..46469b8 100644
---
a/java/java.hints/src/org/netbeans/modules/java/hints/errors/CreateClassFix.java
+++
b/java/java.hints/src/org/netbeans/modules/java/hints/errors/CreateClassFix.java
@@ -309,7 +309,7 @@ public abstract class CreateClassFix extends CreateFixBase
implements EnhancedFi
source =
make.Class(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), simpleName,
Collections.<TypeParameterTree>emptyList(), null,
Collections.<Tree>emptyList(), Collections.<Tree>emptyList());
break;
case INTERFACE:
- source =
make.Interface(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), simpleName,
Collections.<TypeParameterTree>emptyList(), null,
Collections.<Tree>emptyList());
+ source =
make.Interface(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), simpleName,
Collections.<TypeParameterTree>emptyList(), Collections.<Tree>emptyList(),
Collections.<Tree>emptyList());
break;
case ANNOTATION_TYPE:
source =
make.AnnotationType(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), simpleName,
Collections.<Tree>emptyList());
diff --git
a/java/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceExpressionBasedMethodFix.java
b/java/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceExpressionBasedMethodFix.java
index 5b08e05..d5aa9e4 100644
---
a/java/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceExpressionBasedMethodFix.java
+++
b/java/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceExpressionBasedMethodFix.java
@@ -27,6 +27,7 @@ import com.sun.source.tree.Tree;
import com.sun.source.tree.TypeParameterTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
+import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
@@ -199,7 +200,16 @@ final class IntroduceExpressionBasedMethodFix extends
IntroduceFixBase implement
@Override
public ModificationResult getModificationResult() throws IOException {
- return getModificationResult("method", targets.iterator().next(),
true, EnumSet.of(Modifier.PRIVATE), false, null);
+ ModificationResult result = null;
+ int counter = 0;
+ do {
+ try {
+ result = getModificationResult("method" + (counter != 0 ?
String.valueOf(counter) : ""), targets.iterator().next(), true,
EnumSet.of(Modifier.PRIVATE), false, null);
+ } catch (Exception e) {
+ counter++;
+ }
+ } while (result == null && counter < 10);
+ return result;
}
private ModificationResult getModificationResult(final String name, final
TargetDescription target, final boolean replaceOther, final Set<Modifier>
access, final boolean redoReferences, final MemberSearchResult searchResult)
throws IOException {
@@ -259,7 +269,7 @@ final class IntroduceExpressionBasedMethodFix extends
IntroduceFixBase implement
TreePath firstLeaf = desc.getOccurrenceRoot();
int startOff = (int)
copy.getTrees().getSourcePositions().getStartPosition(copy.getCompilationUnit(),
firstLeaf.getLeaf());
int endOff = (int)
copy.getTrees().getSourcePositions().getEndPosition(copy.getCompilationUnit(),
firstLeaf.getLeaf());
- if (!IntroduceHint.shouldReplaceDuplicate(doc,
startOff, endOff)) {
+ if (!GraphicsEnvironment.isHeadless() &&
!IntroduceHint.shouldReplaceDuplicate(doc, startOff, endOff)) {
continue;
}
//XXX:
diff --git
a/java/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceMethodFix.java
b/java/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceMethodFix.java
index d3284d8..0eefbb2 100644
---
a/java/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceMethodFix.java
+++
b/java/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceMethodFix.java
@@ -35,6 +35,7 @@ import com.sun.source.tree.TypeParameterTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
+import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
@@ -438,7 +439,16 @@ public final class IntroduceMethodFix extends
IntroduceFixBase implements Fix {
@Override
public ModificationResult getModificationResult() throws IOException {
- return js.runModificationTask(new
TaskImpl(EnumSet.of(Modifier.PRIVATE), "method", targets.iterator().next(),
true, null, false));
+ ModificationResult result = null;
+ int counter = 0;
+ do {
+ try {
+ result = js.runModificationTask(new
TaskImpl(EnumSet.of(Modifier.PRIVATE), "method" + (counter != 0 ?
String.valueOf(counter) : ""), targets.iterator().next(), true, null, false));
+ } catch (Exception e) {
+ counter++;
+ }
+ } while (result == null && counter < 10);
+ return result;
}
static class OccurrencePositionComparator implements
Comparator<Occurrence> {
@@ -863,7 +873,7 @@ public final class IntroduceMethodFix extends
IntroduceFixBase implements Fix {
int startOff = (int)
copy.getTrees().getSourcePositions().getStartPosition(copy.getCompilationUnit(),
firstSt);
int endOff = (int)
copy.getTrees().getSourcePositions().getEndPosition(copy.getCompilationUnit(),
lastSt);
- if (usedAfter ||
!IntroduceHint.shouldReplaceDuplicate(doc, startOff, endOff)) {
+ if (usedAfter || !GraphicsEnvironment.isHeadless() &&
!IntroduceHint.shouldReplaceDuplicate(doc, startOff, endOff)) {
continue;
}
List<StatementTree> newStatements = new
LinkedList<StatementTree>();
diff --git
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/LspServerUtils.java
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/LspServerUtils.java
index ab14aed..c98193b 100644
---
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/LspServerUtils.java
+++
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/LspServerUtils.java
@@ -85,7 +85,7 @@ public class LspServerUtils {
public static final void avoidClientMessageThread(Lookup context) {
NbCodeLanguageClient client = LspServerUtils.findLspClient(context);
if (LspServerUtils.isClientResponseThread(client)) {
- throw new IllegalStateException("Can not block LSP server message
loop. Use RequestProcessor to run the calling code, or use notifyLater()");
+ throw new IllegalStateException("Cannot block LSP server message
loop. Use RequestProcessor to run the calling code, or use notifyLater()");
}
}
}
diff --git
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ConstructorGenerator.java
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ConstructorGenerator.java
index d3e6fab..27e3dd8 100644
---
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ConstructorGenerator.java
+++
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ConstructorGenerator.java
@@ -110,9 +110,14 @@ public final class ConstructorGenerator extends
CodeGenerator {
}
List<QuickPickItem> constructors;
if (typeElement.getKind() != ElementKind.ENUM &&
inheritedConstructors.size() == 1) {
- QuickPickItem item = new QuickPickItem(createLabel(info,
inheritedConstructors.get(0)));
- item.setUserData(new ElementData(inheritedConstructors.get(0)));
- constructors = Collections.singletonList(item);
+ if (uninitializedFields.isEmpty() &&
inheritedConstructors.get(0).getParameters().isEmpty()
+ &&
ElementFilter.constructorsIn(typeElement.getEnclosedElements()).stream().filter(ctor
-> ctor.getParameters().isEmpty() &&
!info.getElementUtilities().isSynthetic(ctor)).count() > 0) {
+ constructors = Collections.emptyList();
+ } else {
+ QuickPickItem item = new QuickPickItem(createLabel(info,
inheritedConstructors.get(0)));
+ item.setUserData(new
ElementData(inheritedConstructors.get(0)));
+ constructors = Collections.singletonList(item);
+ }
} else if (inheritedConstructors.size() > 1) {
constructors = new ArrayList<>(inheritedConstructors.size());
for (ExecutableElement constructorElement : inheritedConstructors)
{
@@ -190,6 +195,9 @@ public final class ConstructorGenerator extends
CodeGenerator {
}
}
+ @NbBundle.Messages({
+ "DN_ConstructorAlreadyExists=Given constructor already exists",
+ })
private void generate(NbCodeLanguageClient client, String uri, int offset,
List<QuickPickItem> constructors, List<QuickPickItem> fields) {
try {
FileObject file = Utils.fromUri(uri);
@@ -214,6 +222,8 @@ public final class ConstructorGenerator extends
CodeGenerator {
}
});
client.applyEdit(new ApplyWorkspaceEditParams(new
WorkspaceEdit(Collections.singletonMap(uri, edits))));
+ } catch (GeneratorUtils.DuplicateMemberException dme) {
+ client.showMessage(new MessageParams(MessageType.Info,
Bundle.DN_ConstructorAlreadyExists()));
} catch (IOException | IllegalArgumentException ex) {
client.logMessage(new MessageParams(MessageType.Error,
ex.getLocalizedMessage()));
}
diff --git
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java
index 2a55323..52085f0 100644
---
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java
+++
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java
@@ -121,6 +121,7 @@ import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
+import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageClientAware;
import org.eclipse.lsp4j.services.TextDocumentService;
@@ -165,6 +166,7 @@ import org.netbeans.modules.java.hints.spiimpl.JavaFixImpl;
import org.netbeans.modules.java.hints.spiimpl.hints.HintsInvoker;
import org.netbeans.modules.java.hints.spiimpl.options.HintsSettings;
import org.netbeans.modules.java.lsp.server.Utils;
+import org.netbeans.modules.java.lsp.server.debugging.utils.ErrorUtilities;
import org.netbeans.modules.java.source.ElementHandleAccessor;
import org.netbeans.modules.java.source.ui.ElementOpenAccessor;
import org.netbeans.modules.parsing.api.ParserManager;
@@ -963,18 +965,18 @@ public class TextDocumentServiceImpl implements
TextDocumentService, LanguageCli
p = query[0].checkParameters();
if (cancel.get()) return ;
if (p != null && p.isFatal()) {
- result.completeExceptionally(new
IllegalStateException(p.getMessage()));
+ ErrorUtilities.completeExceptionally(result,
p.getMessage(), ResponseErrorCode.UnknownErrorCode);
return ;
}
p = query[0].preCheck();
if (p != null && p.isFatal()) {
- result.completeExceptionally(new
IllegalStateException(p.getMessage()));
+ ErrorUtilities.completeExceptionally(result,
p.getMessage(), ResponseErrorCode.UnknownErrorCode);
return ;
}
if (cancel.get()) return ;
p = query[0].prepare(refactoring);
if (p != null && p.isFatal()) {
- result.completeExceptionally(new
IllegalStateException(p.getMessage()));
+ ErrorUtilities.completeExceptionally(result,
p.getMessage(), ResponseErrorCode.UnknownErrorCode);
return ;
}
for (RefactoringElement re :
refactoring.getRefactoringElements()) {
@@ -1258,32 +1260,35 @@ public class TextDocumentServiceImpl implements
TextDocumentService, LanguageCli
for (ErrorDescription err : IntroduceHint.computeError(cc,
Utils.getOffset(doc, range.getStart()), Utils.getOffset(doc, range.getEnd()),
new EnumMap<IntroduceKind, Fix>(IntroduceKind.class), new
EnumMap<IntroduceKind, String>(IntroduceKind.class), new AtomicBoolean())) {
for (Fix fix : err.getFixes().getFixes()) {
if (fix instanceof IntroduceFixBase) {
- ModificationResult changes =
((IntroduceFixBase) fix).getModificationResult();
- if (changes != null) {
- List<Either<TextDocumentEdit,
ResourceOperation>> documentChanges = new ArrayList<>();
- Set<? extends FileObject> fos =
changes.getModifiedFileObjects();
- if (fos.size() == 1) {
- FileObject fileObject =
fos.iterator().next();
- List<? extends
ModificationResult.Difference> diffs = changes.getDifferences(fileObject);
- if (diffs != null) {
- List<TextEdit> edits = new
ArrayList<>();
- for (ModificationResult.Difference
diff : diffs) {
- String newText =
diff.getNewText();
- edits.add(new TextEdit(new
Range(Utils.createPosition(fileObject, diff.getStartPosition().getOffset()),
-
Utils.createPosition(fileObject, diff.getEndPosition().getOffset())),
- newText
!= null ? newText : ""));
+ try {
+ ModificationResult changes =
((IntroduceFixBase) fix).getModificationResult();
+ if (changes != null) {
+ List<Either<TextDocumentEdit,
ResourceOperation>> documentChanges = new ArrayList<>();
+ Set<? extends FileObject> fos =
changes.getModifiedFileObjects();
+ if (fos.size() == 1) {
+ FileObject fileObject =
fos.iterator().next();
+ List<? extends
ModificationResult.Difference> diffs = changes.getDifferences(fileObject);
+ if (diffs != null) {
+ List<TextEdit> edits = new
ArrayList<>();
+ for
(ModificationResult.Difference diff : diffs) {
+ String newText =
diff.getNewText();
+ edits.add(new TextEdit(new
Range(Utils.createPosition(fileObject, diff.getStartPosition().getOffset()),
+
Utils.createPosition(fileObject, diff.getEndPosition().getOffset())),
+
newText != null ? newText : ""));
+ }
+
documentChanges.add(Either.forLeft(new TextDocumentEdit(new
VersionedTextDocumentIdentifier(Utils.toUri(fileObject), -1), edits)));
}
-
documentChanges.add(Either.forLeft(new TextDocumentEdit(new
VersionedTextDocumentIdentifier(Utils.toUri(fileObject), -1), edits)));
- }
- CodeAction codeAction = new
CodeAction(fix.getText());
-
codeAction.setKind(CodeActionKind.RefactorExtract);
- codeAction.setEdit(new
WorkspaceEdit(documentChanges));
- int renameOffset = ((IntroduceFixBase)
fix).getNameOffset(changes);
- if (renameOffset >= 0) {
- codeAction.setCommand(new
Command("Rename", "java.rename.element.at",
Collections.singletonList(renameOffset)));
+ CodeAction codeAction = new
CodeAction(fix.getText());
+
codeAction.setKind(CodeActionKind.RefactorExtract);
+ codeAction.setEdit(new
WorkspaceEdit(documentChanges));
+ int renameOffset =
((IntroduceFixBase) fix).getNameOffset(changes);
+ if (renameOffset >= 0) {
+ codeAction.setCommand(new
Command("Rename", "java.rename.element.at",
Collections.singletonList(renameOffset)));
+ }
+
result.add(Either.forRight(codeAction));
}
-
result.add(Either.forRight(codeAction));
}
+ } catch
(GeneratorUtils.DuplicateMemberException dme) {
}
}
}
@@ -1453,18 +1458,18 @@ public class TextDocumentServiceImpl implements
TextDocumentService, LanguageCli
p = refactoring[0].checkParameters();
if (cancel.get()) return ;
if (p != null && p.isFatal()) {
- result.completeExceptionally(new
IllegalStateException(p.getMessage()));
+ ErrorUtilities.completeExceptionally(result,
p.getMessage(), ResponseErrorCode.UnknownErrorCode);
return ;
}
p = refactoring[0].preCheck();
if (p != null && p.isFatal()) {
- result.completeExceptionally(new
IllegalStateException(p.getMessage()));
+ ErrorUtilities.completeExceptionally(result,
p.getMessage(), ResponseErrorCode.UnknownErrorCode);
return ;
}
if (cancel.get()) return ;
p = refactoring[0].prepare(session);
if (p != null && p.isFatal()) {
- result.completeExceptionally(new
IllegalStateException(p.getMessage()));
+ ErrorUtilities.completeExceptionally(result,
p.getMessage(), ResponseErrorCode.UnknownErrorCode);
return ;
}
//TODO: check client capabilities!
diff --git a/java/java.lsp.server/vscode/src/extension.ts
b/java/java.lsp.server/vscode/src/extension.ts
index a6c1f0c..c3a78f3 100644
--- a/java/java.lsp.server/vscode/src/extension.ts
+++ b/java/java.lsp.server/vscode/src/extension.ts
@@ -29,7 +29,7 @@ import {
Message,
MessageType,
LogMessageNotification,
- HandlerResult
+ RevealOutputChannelOn
} from 'vscode-languageclient';
import * as net from 'net';
@@ -395,7 +395,7 @@ function doActivateWithJDK(specifiedJDK: string | null,
context: ExtensionContex
]
},
outputChannel: log,
- revealOutputChannelOn: 3, // error
+ revealOutputChannelOn: RevealOutputChannelOn.Never,
progressOnInitialization: true,
initializationOptions : {
'nbcodeCapabilities' : {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists