Skip to content
Open
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 @@ -104,7 +104,6 @@ public static JavaFixAllImports getDefault() {
/** Creates a new instance of JavaFixAllImports */
private JavaFixAllImports() {
}

public void fixAllImports(final FileObject fo, final JTextComponent target) {
final AtomicBoolean cancel = new AtomicBoolean();
final JavaSource javaSource = JavaSource.forFileObject(fo);
Expand Down Expand Up @@ -202,7 +201,7 @@ List<TreePathHandle> getImports() {
}
}

private static void performFixImports(WorkingCopy wc, ImportData data, CandidateDescription[] selections, boolean removeUnusedImports) throws IOException {
public static void performFixImports(WorkingCopy wc, ImportData data, CandidateDescription[] selections, boolean removeUnusedImports) throws IOException {
//do imports:
Set<Element> toImport = new HashSet<Element>();
Map<Name, Element> useFQNsFor = new HashMap<Name, Element>();
Expand Down Expand Up @@ -263,7 +262,7 @@ private static void performFixImports(WorkingCopy wc, ImportData data, Candidate
}
}

private static ImportData computeImports(CompilationInfo info) {
public static ImportData computeImports(CompilationInfo info) {
ComputeImports imps = new ComputeImports(info);
Pair<Map<String, List<Element>>, Map<String, List<Element>>> candidates = imps.computeCandidates();

Expand Down Expand Up @@ -351,7 +350,7 @@ private static ImportData computeImports(CompilationInfo info) {
return data;
}

static final class ImportData {
public static final class ImportData {
public final String[] simpleNames;
public final CandidateDescription[][] variants;
public final CandidateDescription[] defaults;
Expand Down Expand Up @@ -440,7 +439,7 @@ public void actionPerformed(ActionEvent e) {
d.dispose();
}

static final class CandidateDescription {
public static final class CandidateDescription {
public final String displayName;
public final Icon icon;
public final ElementHandle<Element> toImport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* @author Dusan Balek
*/
public abstract class CodeActionsProvider {

public static final String FIX_IMPORTS_KIND = "source.fixImports";
public static final String CODE_GENERATOR_KIND = "source.generate";
public static final String CODE_ACTIONS_PROVIDER_CLASS = "providerClass";
public static final String DATA = "data";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.java.lsp.server.protocol;

import com.google.gson.JsonPrimitive;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import net.java.html.json.Function;
import net.java.html.json.Model;
import net.java.html.json.ModelOperation;
import net.java.html.json.Property;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.netbeans.api.htmlui.HTMLDialog;
import org.netbeans.api.java.source.CompilationController;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.modules.java.editor.imports.JavaFixAllImports;
import org.netbeans.modules.java.editor.imports.JavaFixAllImports.CandidateDescription;
import org.netbeans.modules.java.editor.imports.JavaFixAllImports.ImportData;
import org.netbeans.modules.java.lsp.server.Utils;
import org.netbeans.modules.parsing.api.ResultIterator;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
*
* @author shimadan
*/
@ServiceProvider(service = CodeActionsProvider.class, position = 91)
public class FixImportsCodeAction extends CodeActionsProvider {
@Override
@NbBundle.Messages({
"DN_FixImports=Fix Imports...",})
public List<CodeAction> getCodeActions(NbCodeLanguageClient client, ResultIterator resultIterator, CodeActionParams params) throws Exception {
List<String> only = params.getContext().getOnly();
if (only == null || !only.contains(CodeActionKind.Source)) {
return Collections.emptyList();
}
CompilationController info = resultIterator.getParserResult() != null ? CompilationController.get(resultIterator.getParserResult()) : null;
if (info == null) {
return Collections.emptyList();
}
String uri = Utils.toUri(info.getFileObject());
return Collections.singletonList(createCodeAction(client, Bundle.DN_FixImports(), FIX_IMPORTS_KIND, uri, null));
}

@Override
public CompletableFuture<CodeAction> resolve(NbCodeLanguageClient client, CodeAction codeAction, Object data) {
CompletableFuture<CodeAction> future = new CompletableFuture<>();
try {
String uri = ((JsonPrimitive) data).getAsString();
FileObject file = Utils.fromUri(uri);
JavaSource js = JavaSource.forFileObject(file);
if (js == null) {
throw new IOException("Cannot get JavaSource for: " + uri);
}
final AtomicReference<ImportData> missingImports = new AtomicReference<ImportData>();
js.runUserActionTask(cc -> {
cc.toPhase(JavaSource.Phase.RESOLVED);
missingImports.set(JavaFixAllImports.computeImports(cc));
}, true);
future = showFixImportsDialog(missingImports.get()).thenApply(selections -> {
List<TextEdit> edits;
try {
edits = TextDocumentServiceImpl.modify2TextEdits(js, wc -> {
wc.toPhase(JavaSource.Phase.RESOLVED);
JavaFixAllImports.performFixImports(wc, missingImports.get(), selections, false);

});
if (!edits.isEmpty()) {
codeAction.setEdit(new WorkspaceEdit(Collections.singletonMap(uri, edits)));
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);

}

return codeAction;
});
} catch (IOException | IllegalArgumentException ex) {
future.completeExceptionally(ex);
}
return future;
}

private CompletableFuture<CandidateDescription[]> showFixImportsDialog(ImportData Impdata) {
CompletableFuture<CandidateDescription[]> selections = new CompletableFuture<>();
Pages.showFixImportsDialog(Impdata, selections);
return selections;
}

@HTMLDialog(url = "ui/FixImports.html", resources = "FixImports.css")
static final HTMLDialog.OnSubmit showFixImportsDialog(ImportData missingImports, CompletableFuture<CandidateDescription[]> selectedCandidates) {
FixImportsUI model = new FixImportsUI();
ImportDataUI[] imports = IntStream.range(0, missingImports.simpleNames.length)
.mapToObj(i -> new ImportDataUI(
missingImports.simpleNames[i],
missingImports.defaults[i].displayName,
Stream.of(missingImports.variants[i]).map((candidate)->candidate.displayName).toArray(String[]::new)
))
.toArray(ImportDataUI[]::new);
model.withImports(imports).assignData(missingImports, selectedCandidates);
model.applyBindings();
return (id) -> {
if ("accept".equals(id)) {
model.completeSelectedCandidates();
}else{
model.cancel();
}
return true;
};
}

@Model(className = "FixImportsUI", targetId = "", instance = true, builder = "with",
properties = {
@Property(name = "imports", type = ImportDataUI.class, array = true)
})
static final class FixImportsControl {

private CompletableFuture<CandidateDescription[]> selectedCandidates;
private ImportData missingImports;

@ModelOperation
void assignData(FixImportsUI ui, ImportData missingImports, CompletableFuture<CandidateDescription[]> selectedCandidates) {
this.selectedCandidates = selectedCandidates;
this.missingImports = missingImports;
}

@ModelOperation
@Function
void completeSelectedCandidates(FixImportsUI ui) {
List<ImportDataUI> imports = ui.getImports();
CandidateDescription[] choosen = IntStream.range(0, imports.size())
.mapToObj(i -> Stream.of(missingImports.variants[i])
.filter((variant) -> variant.displayName.equals(imports.get(i).getSelectedCandidateFQN()))
.findFirst()
.get()
)
.toArray(CandidateDescription[]::new);
selectedCandidates.complete(choosen);
}
@ModelOperation
@Function
void cancel(){
selectedCandidates.cancel(true);
}
}

@Model(className = "ImportDataUI", instance = true, properties = {
@Property(name = "simpleName", type = String.class),
@Property(name = "selectedCandidateFQN", type = String.class),
@Property(name = "candidatesFQN", type = String.class, array = true)

})
static final class ImportDataControl {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.lsp4j.WorkspaceEdit;
import org.netbeans.api.java.source.CompilationController;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.modules.java.editor.imports.JavaFixAllImports;
import org.netbeans.modules.java.hints.OrganizeImports;
import org.netbeans.modules.java.lsp.server.Utils;
import org.netbeans.modules.parsing.api.ResultIterator;
Expand Down Expand Up @@ -73,7 +74,7 @@ public CompletableFuture<CodeAction> resolve(NbCodeLanguageClient client, CodeAc
}
List<TextEdit> edits = TextDocumentServiceImpl.modify2TextEdits(js, wc -> {
wc.toPhase(JavaSource.Phase.RESOLVED);
OrganizeImports.doOrganizeImports(wc, null, false);
OrganizeImports.doOrganizeImports(wc, null, false);
});
if (!edits.isEmpty()) {
codeAction.setEdit(new WorkspaceEdit(Collections.singletonMap(uri, edits)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!--

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

-->
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src http: 'unsafe-inline' 'unsafe-eval'">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="FixImports.css">
<title>Fix Imports...</title>
</head>

<body>
<div class="section">
<label>Missing Imports:</label>
<div class="flex">
<label class="flex-grow params-caption">Name:</label>
<label class="flex-grow params-caption">Candidates:</label>
<span class="filler"></span>
</div>
</div>
<div class="flex-vscrollable section2" data-bind="foreach: imports">
<div class="flex row">
<div class="flex-equal">
<label data-bind="text: simpleName" class="flex-grow"></label>
</div>
<div class="flex-equal hdivider">
<select data-bind="options: candidatesFQN, value: selectedCandidateFQN"></select>
</div>
</div>
</div>
<div>
<button id="accept" hidden class="regular-button vscode-font align-right">Add Imports</button>
<button id="reject" hidden class="regular-button vscode-font">Cancel</button>
</div>
</body>
</html>
Loading