Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove deprecated StringUtils from commons #1205

Merged
merged 1 commit into from
Oct 5, 2023
Merged
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 @@ -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;
Expand Down Expand Up @@ -159,6 +159,7 @@ public static URI toUri(Document document) {

/**
* Returns the @{@link Document} associated to the given @{@link URI}, or <code>null</code> if there's no match.
*
* @param documentUri the uri of the Document to return
* @return the @{@link Document} associated to <code>documentUri</code>, or <code>null</code>
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,127 +40,127 @@
*/
public class LSPLazyCodeActionIntentionAction implements IntentionAction {

private final LSPLazyCodeActions lazyCodeActions;

private final int index;
private Either<Command, CodeAction> 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<Boolean, CodeActionOptions> 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<Command, CodeAction> 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<Boolean, CodeActionOptions> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading