JaroslavTulach commented on a change in pull request #3349:
URL: https://github.com/apache/netbeans/pull/3349#discussion_r762816805



##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/refactoring/ChangeMethodParametersRefactoring.java
##########
@@ -0,0 +1,363 @@
+/*
+ * 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.refactoring;
+
+import com.google.gson.Gson;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.TreePath;
+import com.sun.source.util.Trees;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.BiConsumer;
+import java.util.logging.Level;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.TypeKind;
+import net.java.html.json.ComputedProperty;
+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.ApplyWorkspaceEditParams;
+import org.eclipse.lsp4j.CodeAction;
+import org.eclipse.lsp4j.CodeActionKind;
+import org.eclipse.lsp4j.CodeActionParams;
+import org.eclipse.lsp4j.MessageParams;
+import org.eclipse.lsp4j.MessageType;
+import org.netbeans.api.htmlui.HTMLDialog;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.ElementHandle;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.SourceUtils;
+import org.netbeans.api.java.source.TreePathHandle;
+import org.netbeans.modules.editor.java.Utilities;
+import org.netbeans.modules.java.lsp.server.Utils;
+import org.netbeans.modules.java.lsp.server.protocol.CodeActionsProvider;
+import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
+import org.netbeans.modules.java.lsp.server.protocol.QuickPickItem;
+import org.netbeans.modules.parsing.api.ResultIterator;
+import org.netbeans.modules.refactoring.java.api.ChangeParametersRefactoring;
+import org.netbeans.modules.refactoring.java.api.JavaRefactoringUtils;
+import org.openide.filesystems.FileObject;
+import org.openide.util.Exceptions;
+import org.openide.util.NbBundle;
+import org.openide.util.RequestProcessor;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+@ServiceProvider(service = CodeActionsProvider.class, position = 200)
+public final class ChangeMethodParametersRefactoring extends CodeRefactoring {
+
+    private static final String CHANGE_METHOD_PARAMS_REFACTORING_KIND = 
"refactor.change.method.params";
+    private static final String CHANGE_METHOD_PARAMS_REFACTORING_COMMAND =  
"java.refactor.change.method.params";
+
+    private final Set<String> commands = 
Collections.singleton(CHANGE_METHOD_PARAMS_REFACTORING_COMMAND);
+    private final Gson gson = new Gson();
+
+    @Override
+    @NbBundle.Messages({
+        "DN_ChangeMethodParams=Change Method Parameters...",
+    })
+    public List<CodeAction> getCodeActions(ResultIterator resultIterator, 
CodeActionParams params) throws Exception {
+        List<String> only = params.getContext().getOnly();
+        if (only == null || !only.contains(CodeActionKind.Refactor)) {
+            return Collections.emptyList();
+        }
+        CompilationController info = 
CompilationController.get(resultIterator.getParserResult());
+        if (info == null || 
!JavaRefactoringUtils.isRefactorable(info.getFileObject())) {
+            return Collections.emptyList();
+        }
+        info.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
+        int offset = getOffset(info, params.getRange().getStart());
+        Trees trees = info.getTrees();
+        TreePath path = info.getTreeUtilities().pathFor(offset);
+        Tree.Kind kind = null;
+        while (path != null && (kind = path.getLeaf().getKind()) != 
Tree.Kind.METHOD && kind != Tree.Kind.METHOD_INVOCATION && kind != 
Tree.Kind.NEW_CLASS && kind != Tree.Kind.MEMBER_REFERENCE) {
+            path = path.getParentPath();
+        }
+        Element element = null;
+        FileObject elementSource = null;
+        if (kind == Tree.Kind.METHOD || kind == Tree.Kind.METHOD_INVOCATION || 
kind == Tree.Kind.NEW_CLASS || kind == Tree.Kind.MEMBER_REFERENCE) {
+            element = trees.getElement(path);
+            if (element == null || element.asType().getKind() == 
TypeKind.ERROR) {
+                return Collections.emptyList();
+            }
+            elementSource = SourceUtils.getFile(ElementHandle.create(element), 
info.getClasspathInfo());
+        }
+        if (elementSource == null) {
+            return Collections.emptyList();
+        }
+        QuickPickItem elementItem = new QuickPickItem(createLabel(info, 
element, true));
+        elementItem.setUserData(new ElementData(element));
+        return 
Collections.singletonList(createCodeAction(Bundle.DN_ChangeMethodParams(), 
CHANGE_METHOD_PARAMS_REFACTORING_KIND, null, 
CHANGE_METHOD_PARAMS_REFACTORING_COMMAND, Utils.toUri(elementSource), 
elementItem));
+    }
+
+    @Override
+    public Set<String> getCommands() {
+        return commands;
+    }
+
+    @Override
+    @NbBundle.Messages({
+        "DN_ChangeMethodSignature=Change method signature",
+    })
+    public CompletableFuture<Object> processCommand(NbCodeLanguageClient 
client, String command, List<Object> arguments) {
+        try {
+            if (arguments.size() > 1) {
+                String uri = gson.fromJson(gson.toJson(arguments.get(0)), 
String.class);
+                QuickPickItem sourceItem = 
gson.fromJson(gson.toJson(arguments.get(1)), QuickPickItem.class);
+                ElementHandle handle = 
gson.fromJson(gson.toJson(sourceItem.getUserData()), 
ElementData.class).toHandle();
+                FileObject file = Utils.fromUri(uri);
+                JavaSource js = JavaSource.forFileObject(file);
+                if (js != null) {
+                    return CompletableFuture.supplyAsync(() -> {
+                        try {
+                            js.runUserActionTask(ci -> {
+                                ci.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
+                                ExecutableElement method = (ExecutableElement) 
handle.resolve(ci);
+                                if (method != null) {
+                                    Pages.showChangeMethodParametersUI(ci, 
client, file, handle, method);
+                                }
+                            }, true);
+                            return null;
+                        } catch (IOException ex) {
+                            throw new IllegalStateException(ex);
+                        }
+                    }, RequestProcessor.getDefault());
+                }
+            } else {
+                throw new IllegalArgumentException(String.format("Illegal 
number of arguments received for command: %s", command));
+            }
+        } catch (Exception ex) {
+            client.showMessage(new MessageParams(MessageType.Error, 
ex.getLocalizedMessage()));
+        }
+        return CompletableFuture.completedFuture(true);
+    }
+
+    private static String defaultValue(String type) {
+        switch(type) {
+            case "boolean":
+                return "false";
+            case "byte":
+            case "char":
+            case "double":
+            case "float":
+            case "int":
+            case "long":
+            case "short":
+                return "0";
+        }
+        return "null";
+    }
+
+    @HTMLDialog(url = "ui/ChangeMethodParameters.html")
+    static HTMLDialog.OnSubmit showChangeMethodParametersUI(
+        CompilationController ci,
+        NbCodeLanguageClient client,
+        FileObject file,
+        ElementHandle handle,
+        ExecutableElement method
+    ) {
+        ParameterUI[] params = new ParameterUI[method.getParameters().size()];
+        for (int i = 0; i < method.getParameters().size(); i++) {
+            VariableElement param = method.getParameters().get(i);
+            ChangeParametersRefactoring.ParameterInfo info = new 
ChangeParametersRefactoring.ParameterInfo(i, param.getSimpleName().toString(), 
Utilities.getTypeName(ci, param.asType(), true).toString(), null);
+            params[i] = new ParameterUI(info.getType(), info.getName());
+            params[i].assignInfo(info);
+        }
+        Modifier mod;
+        if 
(method.getModifiers().contains(javax.lang.model.element.Modifier.PUBLIC)) {
+            mod = Modifier.PUBLIC;
+        } else if 
(method.getModifiers().contains(javax.lang.model.element.Modifier.PROTECTED)) {
+            mod = Modifier.PROTECTED;
+        } else if 
(method.getModifiers().contains(javax.lang.model.element.Modifier.PRIVATE)) {
+            mod = Modifier.PRIVATE;
+        } else {
+            mod = Modifier.PACKAGE_PRIVATE;
+        }
+        ChangeMethodParameterUI model = new ChangeMethodParameterUI();
+        model.withName(method.getSimpleName().toString())
+                .withReturnType(Utilities.getTypeName(ci, 
method.getReturnType(), true).toString())
+                .withSelectedModifier(mod)
+                .withParameters(params)
+                .assignData(client, file, TreePathHandle.from(handle, 
ClasspathInfo.create(file)));
+        model.applyBindings();
+        return (id) -> {
+            if ("accept".equals(id)) {

Review comment:
       Here you can validate the content of the dialog, @dbalek.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
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

Reply via email to