diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java index 9e2ab6278..08fb20996 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java @@ -23,8 +23,8 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.impl.light.LightRecordField; +import com.redhat.devtools.intellij.lsp4ij.internal.StringUtils; import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.StringUtils; import org.eclipse.lsp4j.*; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.jetbrains.annotations.NotNull; @@ -159,6 +159,7 @@ public static URI toUri(Document document) { /** * Returns the @{@link Document} associated to the given @{@link URI}, or null if there's no match. + * * @param documentUri the uri of the Document to return * @return the @{@link Document} associated to documentUri, or null */ @@ -239,9 +240,8 @@ public static Range toRange(TextRange range, Document document) { /** * Returns the IJ {@link TextRange} from the given LSP range and null otherwise. * - * @param range the LSP range to conert. + * @param range the LSP range to conert. * @param document the document. - * * @return the IJ {@link TextRange} from the given LSP range and null otherwise. */ public static @Nullable TextRange toTextRange(Range range, Document document) { @@ -356,10 +356,8 @@ public static void applyWorkspaceEdit(WorkspaceEdit edit, String label) { * Create the file with the given file Uri. * * @param fileUri the file Uri. - * - * @throws IOException - * * @return the created virtual file and null otherwise. + * @throws IOException */ public static @Nullable VirtualFile createFile(String fileUri) throws IOException { URI targetURI = URI.create(fileUri); @@ -370,10 +368,8 @@ public static void applyWorkspaceEdit(WorkspaceEdit edit, String label) { * Create the file with the given file Uri. * * @param fileUri the file Uri. - * - * @throws IOException - * * @return the created virtual file and null otherwise. + * @throws IOException */ public static @Nullable VirtualFile createFile(URI fileUri) throws IOException { File newFile = new File(fileUri); diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/StringUtils.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/StringUtils.java new file mode 100644 index 000000000..d6084baae --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/StringUtils.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.lsp4ij.internal; + +public class StringUtils { + + /** + * Copied from https://github.com/apache/commons-lang/blob/24744a40b2c094945e542b71cc1fbf59caa0d70b/src/main/java/org/apache/commons/lang3/StringUtils.java#L3596C5-L3598C6 + * + * @param cs + * @return + */ + public static boolean isEmpty(final CharSequence cs) { + return cs == null || cs.length() == 0; + } + + /** + * Copied code from https://github.com/apache/commons-lang/blob/24744a40b2c094945e542b71cc1fbf59caa0d70b/src/main/java/org/apache/commons/lang3/StringUtils.java#L3714C5-L3716C6 + * @param cs + * @return + */ + public static boolean isNotBlank(final CharSequence cs) { + return !isBlank(cs); + } + + /** + * Copied code from https://github.com/apache/commons-lang/blob/24744a40b2c094945e542b71cc1fbf59caa0d70b/src/main/java/org/apache/commons/lang3/StringUtils.java#L3564 + * @param cs + * @return + */ + public static boolean isBlank(final CharSequence cs) { + final int strLen = length(cs); + if (strLen == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + return true; + } + + /** + * Copied code from https://github.com/apache/commons-lang/blob/24744a40b2c094945e542b71cc1fbf59caa0d70b/src/main/java/org/apache/commons/lang3/StringUtils.java#L5281 + * @param cs + * @return + */ + public static int length(final CharSequence cs) { + return cs == null ? 0 : cs.length(); + } +} \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/codeactions/LSPLazyCodeActionIntentionAction.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/codeactions/LSPLazyCodeActionIntentionAction.java index 2c9a81afc..2a5ea6c5a 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/codeactions/LSPLazyCodeActionIntentionAction.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/codeactions/LSPLazyCodeActionIntentionAction.java @@ -25,7 +25,7 @@ import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils; import com.redhat.devtools.intellij.lsp4ij.LanguageServerWrapper; import com.redhat.devtools.intellij.lsp4ij.commands.CommandExecutor; -import org.apache.commons.lang.StringUtils; +import com.redhat.devtools.intellij.lsp4ij.internal.StringUtils; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionOptions; import org.eclipse.lsp4j.Command; @@ -40,127 +40,127 @@ */ public class LSPLazyCodeActionIntentionAction implements IntentionAction { - private final LSPLazyCodeActions lazyCodeActions; - - private final int index; - private Either action; - private CodeAction codeAction; - - private String title; - private Command command; - private String familyName; - - public LSPLazyCodeActionIntentionAction(LSPLazyCodeActions lazyCodeActions, int index) { - this.lazyCodeActions = lazyCodeActions; - this.index = index; - } - - @Override - public @IntentionName @NotNull String getText() { - loadCodeActionIfNeeded(); - return title; - } - - @Override - public @NotNull @IntentionFamilyName String getFamilyName() { - loadCodeActionIfNeeded(); - return familyName; - } - - @Override - public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { - loadCodeActionIfNeeded(); - return isValidCodeAction(); - } - - @Override - public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { - String serverId = getLanguageServerWrapper().serverDefinition.id; - if (codeAction != null) { - if (codeAction.getEdit() == null && codeAction.getCommand() == null && isCodeActionResolveSupported()) { - // Unresolved code action "edit" property. Resolve it. - getLanguageServerWrapper().getInitializedServer() - .thenApply(ls -> - ls.getTextDocumentService().resolveCodeAction(codeAction) - .thenAccept(resolved -> { - ApplicationManager.getApplication().invokeLater(() -> { - DocumentUtil.writeInRunUndoTransparentAction(() -> { - apply(resolved != null ? resolved : codeAction, project, file, serverId); - }); - }); - }) - ); - } else { - apply(codeAction, project, file, serverId); - } - } else if (command != null) { - executeCommand(command, project, file, serverId); - } else { - // Should never get here - } - } - - private void apply(CodeAction codeaction, @NotNull Project project, PsiFile file, String serverId ) { - if (codeaction != null) { - if (codeaction.getEdit() != null) { - LSPIJUtils.applyWorkspaceEdit(codeaction.getEdit(), codeaction.getTitle()); - } - if (codeaction.getCommand() != null) { - executeCommand(codeaction.getCommand(), project, file, serverId); - } - } - } - - private void executeCommand(Command command, @NotNull Project project, PsiFile file, String serverId) { - CommandExecutor.executeCommand(project, command, LSPIJUtils.toUri(file), serverId); - } - - private LanguageServerWrapper getLanguageServerWrapper() { - return lazyCodeActions.getLanguageServerWrapper(); - } - - private boolean isCodeActionResolveSupported() { - ServerCapabilities capabilities = getLanguageServerWrapper().getServerCapabilities(); - if (capabilities != null) { - Either caProvider = capabilities.getCodeActionProvider(); - if (caProvider.isLeft()) { - // It is wrong, but we need to parse the registerCapability - return caProvider.getLeft(); - } else if (caProvider.isRight()) { - CodeActionOptions options = caProvider.getRight(); - return options.getResolveProvider().booleanValue(); - } - } - return false; - } - - @Override - public boolean startInWriteAction() { - return true; - } - - private void loadCodeActionIfNeeded() { - if (action != null) { - // The LSP code action has been already loaded. - return; - } - // Try to get the LSP code action from the given indes - this.action = lazyCodeActions.getCodeActionAt(index); - if (isValidCodeAction()) { - if (action.isRight()) { - codeAction = action.getRight(); - title = action.getRight().getTitle(); - familyName = StringUtils.isNotBlank(codeAction.getKind()) ? codeAction.getKind() : "LSP QuickFix"; - } else { - command = action.getLeft(); - title = action.getRight().getTitle(); - familyName = "LSP Command"; - } - } - } - - private boolean isValidCodeAction() { - return action != null && !NO_CODEACTION_AT_INDEX.equals(action); - } + private final LSPLazyCodeActions lazyCodeActions; + + private final int index; + private Either action; + private CodeAction codeAction; + + private String title; + private Command command; + private String familyName; + + public LSPLazyCodeActionIntentionAction(LSPLazyCodeActions lazyCodeActions, int index) { + this.lazyCodeActions = lazyCodeActions; + this.index = index; + } + + @Override + public @IntentionName @NotNull String getText() { + loadCodeActionIfNeeded(); + return title; + } + + @Override + public @NotNull @IntentionFamilyName String getFamilyName() { + loadCodeActionIfNeeded(); + return familyName; + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + loadCodeActionIfNeeded(); + return isValidCodeAction(); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + String serverId = getLanguageServerWrapper().serverDefinition.id; + if (codeAction != null) { + if (codeAction.getEdit() == null && codeAction.getCommand() == null && isCodeActionResolveSupported()) { + // Unresolved code action "edit" property. Resolve it. + getLanguageServerWrapper().getInitializedServer() + .thenApply(ls -> + ls.getTextDocumentService().resolveCodeAction(codeAction) + .thenAccept(resolved -> { + ApplicationManager.getApplication().invokeLater(() -> { + DocumentUtil.writeInRunUndoTransparentAction(() -> { + apply(resolved != null ? resolved : codeAction, project, file, serverId); + }); + }); + }) + ); + } else { + apply(codeAction, project, file, serverId); + } + } else if (command != null) { + executeCommand(command, project, file, serverId); + } else { + // Should never get here + } + } + + private void apply(CodeAction codeaction, @NotNull Project project, PsiFile file, String serverId) { + if (codeaction != null) { + if (codeaction.getEdit() != null) { + LSPIJUtils.applyWorkspaceEdit(codeaction.getEdit(), codeaction.getTitle()); + } + if (codeaction.getCommand() != null) { + executeCommand(codeaction.getCommand(), project, file, serverId); + } + } + } + + private void executeCommand(Command command, @NotNull Project project, PsiFile file, String serverId) { + CommandExecutor.executeCommand(project, command, LSPIJUtils.toUri(file), serverId); + } + + private LanguageServerWrapper getLanguageServerWrapper() { + return lazyCodeActions.getLanguageServerWrapper(); + } + + private boolean isCodeActionResolveSupported() { + ServerCapabilities capabilities = getLanguageServerWrapper().getServerCapabilities(); + if (capabilities != null) { + Either caProvider = capabilities.getCodeActionProvider(); + if (caProvider.isLeft()) { + // It is wrong, but we need to parse the registerCapability + return caProvider.getLeft(); + } else if (caProvider.isRight()) { + CodeActionOptions options = caProvider.getRight(); + return options.getResolveProvider().booleanValue(); + } + } + return false; + } + + @Override + public boolean startInWriteAction() { + return true; + } + + private void loadCodeActionIfNeeded() { + if (action != null) { + // The LSP code action has been already loaded. + return; + } + // Try to get the LSP code action from the given indes + this.action = lazyCodeActions.getCodeActionAt(index); + if (isValidCodeAction()) { + if (action.isRight()) { + codeAction = action.getRight(); + title = action.getRight().getTitle(); + familyName = StringUtils.isNotBlank(codeAction.getKind()) ? codeAction.getKind() : "LSP QuickFix"; + } else { + command = action.getLeft(); + title = action.getRight().getTitle(); + familyName = "LSP Command"; + } + } + } + + private boolean isValidCodeAction() { + return action != null && !NO_CODEACTION_AT_INDEX.equals(action); + } } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/CompletionPrefix.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/CompletionPrefix.java index 0673ed300..50016260f 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/CompletionPrefix.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/CompletionPrefix.java @@ -15,7 +15,7 @@ import com.intellij.openapi.editor.Document; import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils; -import org.apache.commons.lang.StringUtils; +import com.redhat.devtools.intellij.lsp4ij.internal.StringUtils; import org.eclipse.lsp4j.CompletionItem; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/LSPCompletionContributor.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/LSPCompletionContributor.java index 7052c627c..5f92fd671 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/LSPCompletionContributor.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/LSPCompletionContributor.java @@ -28,10 +28,9 @@ import com.redhat.devtools.intellij.lsp4ij.LanguageServerItem; import com.redhat.devtools.intellij.lsp4ij.LanguageServiceAccessor; import com.redhat.devtools.intellij.lsp4ij.internal.CancellationSupport; -import org.apache.commons.lang.StringUtils; +import com.redhat.devtools.intellij.lsp4ij.internal.StringUtils; import org.eclipse.lsp4j.*; import org.eclipse.lsp4j.jsonrpc.messages.Either; -import org.eclipse.lsp4j.services.LanguageServer; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/LSPCompletionProposal.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/LSPCompletionProposal.java index 6a8a61efb..fc9ea5361 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/LSPCompletionProposal.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/completion/LSPCompletionProposal.java @@ -28,8 +28,8 @@ import com.redhat.devtools.intellij.lsp4ij.LanguageServerItem; import com.redhat.devtools.intellij.lsp4ij.LanguageServiceAccessor; import com.redhat.devtools.intellij.lsp4ij.commands.CommandExecutor; +import com.redhat.devtools.intellij.lsp4ij.internal.StringUtils; import com.redhat.devtools.intellij.lsp4ij.operations.completion.snippet.LspSnippetIndentOptions; -import org.apache.commons.lang.StringUtils; import org.eclipse.lsp4j.*; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.jetbrains.annotations.NotNull; @@ -276,6 +276,7 @@ protected void apply(Document document, char trigger, int stateMask, int offset) /** * Execute custom command of the completion item. + * * @param command * @param documentUri */ diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/projectWizard/QuarkusExtensionsStep.java b/src/main/java/com/redhat/devtools/intellij/quarkus/projectWizard/QuarkusExtensionsStep.java index e5489ec98..609d3a109 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/projectWizard/QuarkusExtensionsStep.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/projectWizard/QuarkusExtensionsStep.java @@ -20,8 +20,8 @@ import com.intellij.ui.components.JBList; import com.intellij.ui.components.JBScrollPane; import com.intellij.util.ui.JBUI; +import com.redhat.devtools.intellij.lsp4ij.internal.StringUtils; import com.redhat.devtools.intellij.quarkus.QuarkusConstants; -import org.apache.commons.lang.StringUtils; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory;